001    /*****************************************************************************
002     * Copyright (C) NanoContainer Organization. All rights reserved.            *
003     * ------------------------------------------------------------------------- *
004     * The software in this package is published under the terms of the BSD      *
005     * style license a copy of which has been included with this distribution in *
006     * the LICENSE.txt file.                                                     *
007     *                                                                           *
008     *****************************************************************************/
009    package org.nanocontainer.script.xml;
010    
011    import org.picocontainer.PicoContainer;
012    import org.w3c.dom.Element;
013    
014    import com.thoughtworks.xstream.XStream;
015    import com.thoughtworks.xstream.io.xml.DomDriver;
016    import com.thoughtworks.xstream.io.xml.DomReader;
017    
018    /**
019     * Implementation of XMLComponentInstanceFactory that uses 
020     * XStream to unmarshal DOM elements.
021     *
022     * @author Paul Hammant
023     * @author Marcos Tarruella
024     * @author Mauro Talevi
025     */
026    public class XStreamComponentInstanceFactory implements XMLComponentInstanceFactory {
027            /** The XStream used to unmarshal the DOM element */
028            private XStream xstream;
029    
030            /**
031             * Creates an XStreamComponentInstanceFactory with the default instance
032             * of XStream
033             */
034            public XStreamComponentInstanceFactory(){
035                    this(new XStream(new DomDriver()));
036            }
037            
038            /**
039             * Creates an XStreamComponentInstanceFactory for a given instance
040             * of XStream
041             * @param xstream the XStream instance
042             */
043            public XStreamComponentInstanceFactory(XStream xstream){
044                    this.xstream = xstream;
045            }
046            
047        /**
048         * {@inheritDoc}
049         *
050         * @see XMLComponentInstanceFactory#makeInstance(org.picocontainer.PicoContainer,org.w3c.dom.Element,ClassLoader)
051         */
052        public Object makeInstance(PicoContainer pico, Element element, ClassLoader classLoader) throws ClassNotFoundException {
053            return xstream.unmarshal(new DomReader(element));
054        }
055    }