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