001    // SECTION-START[License Header]
002    // <editor-fold defaultstate="collapsed" desc=" Generated License ">
003    /*
004     *   Copyright (c) 2010 The JOMC Project
005     *   Copyright (c) 2005 Christian Schulte <schulte2005@users.sourceforge.net>
006     *   All rights reserved.
007     *
008     *   Redistribution and use in source and binary forms, with or without
009     *   modification, are permitted provided that the following conditions
010     *   are met:
011     *
012     *     o Redistributions of source code must retain the above copyright
013     *       notice, this list of conditions and the following disclaimer.
014     *
015     *     o Redistributions in binary form must reproduce the above copyright
016     *       notice, this list of conditions and the following disclaimer in
017     *       the documentation and/or other materials provided with the
018     *       distribution.
019     *
020     *   THIS SOFTWARE IS PROVIDED BY THE JOMC PROJECT AND CONTRIBUTORS "AS IS"
021     *   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
022     *   THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
023     *   PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE JOMC PROJECT OR
024     *   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
025     *   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
026     *   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
027     *   OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
028     *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
029     *   OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
030     *   ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
031     *
032     *   $Id: JaxbUnmarshallerFactory.java 2234 2010-06-29 00:03:38Z schulte2005 $
033     *
034     */
035    // </editor-fold>
036    // SECTION-END
037    package org.jomc.sdk.model.support;
038    
039    import java.util.Map;
040    import javax.xml.bind.JAXBContext;
041    import javax.xml.bind.JAXBException;
042    import javax.xml.bind.Unmarshaller;
043    import org.jomc.sdk.model.SchemaType;
044    import static org.jomc.sdk.model.modlet.SdkModelProvider.XML_SCHEMA_JAVA_CONTEXT_ID_ATTRIBUTE;
045    import org.xml.sax.SAXException;
046    
047    // SECTION-START[Documentation]
048    // <editor-fold defaultstate="collapsed" desc=" Generated Documentation ">
049    /**
050     * XML Schema Set JAXB 'Unmarshaller' factory implementation.
051     * <p><b>Specifications</b><ul>
052     * <li>{@code 'javax.xml.bind.Unmarshaller'} {@code (javax.xml.bind.Unmarshaller)} {@code Multiton}</li>
053     * </ul></p>
054     * <p><b>Properties</b><ul>
055     * <li>"{@link #getSchemas schemas}"
056     * <blockquote>Property of type {@code org.jomc.sdk.model.SchemasType}.
057     * <p>List of XML schemas ('schemas' element from XML namespace 'http://jomc.org/sdk/model).</p>
058     * </blockquote></li>
059     * <li>"{@link #getUnmarshallerProperties unmarshallerProperties}"
060     * <blockquote>Property of type {@code java.util.Map<String,Object>}.
061     * </blockquote></li>
062     * </ul></p>
063     * <p><b>Dependencies</b><ul>
064     * <li>"{@link #getAttachmentUnmarshaller attachmentUnmarshaller}"<blockquote>
065     * Dependency on {@code 'javax.xml.bind.attachment.AttachmentUnmarshaller'} {@code (javax.xml.bind.attachment.AttachmentUnmarshaller)} bound to an instance.</blockquote></li>
066     * <li>"{@link #getSchema schema}"<blockquote>
067     * Dependency on {@code 'javax.xml.validation.Schema'} {@code (javax.xml.validation.Schema)} bound to an instance.</blockquote></li>
068     * <li>"{@link #getValidationEventHandler validationEventHandler}"<blockquote>
069     * Dependency on {@code 'javax.xml.bind.ValidationEventHandler'} {@code (javax.xml.bind.ValidationEventHandler)} bound to an instance.</blockquote></li>
070     * </ul></p>
071     *
072     * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a> 1.0
073     * @version $Id: JaxbUnmarshallerFactory.java 2234 2010-06-29 00:03:38Z schulte2005 $
074     */
075    // </editor-fold>
076    // SECTION-END
077    // SECTION-START[Annotations]
078    // <editor-fold defaultstate="collapsed" desc=" Generated Annotations ">
079    @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
080    // </editor-fold>
081    // SECTION-END
082    public final class JaxbUnmarshallerFactory
083    {
084        // SECTION-START[Unmarshaller]
085        // SECTION-END
086        // SECTION-START[JaxbUnmarshallerFactory]
087    
088        public Unmarshaller getObject() throws JAXBException, SAXException
089        {
090            Unmarshaller unmarshaller = null;
091            final StringBuilder packageNames = new StringBuilder();
092    
093            for ( SchemaType s : this.getSchemas().getSchema() )
094            {
095                if ( s.getOtherAttributes().containsKey( XML_SCHEMA_JAVA_CONTEXT_ID_ATTRIBUTE ) )
096                {
097                    packageNames.append( ':' ).append( s.getOtherAttributes().get( XML_SCHEMA_JAVA_CONTEXT_ID_ATTRIBUTE ) );
098                }
099            }
100    
101            if ( packageNames.length() > 0 )
102            {
103                unmarshaller = JAXBContext.newInstance( packageNames.substring( 1 ) ).createUnmarshaller();
104    
105                for ( Map.Entry<String, Object> e : this.getUnmarshallerProperties().entrySet() )
106                {
107                    unmarshaller.setProperty( e.getKey(), e.getValue() );
108                }
109    
110                unmarshaller.setAttachmentUnmarshaller( this.getAttachmentUnmarshaller() );
111                unmarshaller.setEventHandler( this.getValidationEventHandler() );
112                unmarshaller.setSchema( this.getSchema() );
113            }
114    
115            return unmarshaller;
116        }
117    
118        // SECTION-END
119        // SECTION-START[Constructors]
120        // <editor-fold defaultstate="collapsed" desc=" Generated Constructors ">
121    
122        /** Creates a new {@code JaxbUnmarshallerFactory} instance. */
123        @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
124        public JaxbUnmarshallerFactory()
125        {
126            // SECTION-START[Default Constructor]
127            super();
128            // SECTION-END
129        }
130        // </editor-fold>
131        // SECTION-END
132        // SECTION-START[Dependencies]
133        // <editor-fold defaultstate="collapsed" desc=" Generated Dependencies ">
134    
135        /**
136         * Gets the {@code attachmentUnmarshaller} dependency.
137         * <p>This method returns the {@code 'JOMC SDK Model Default'} object of the {@code 'javax.xml.bind.attachment.AttachmentUnmarshaller'} {@code (javax.xml.bind.attachment.AttachmentUnmarshaller)} specification.</p>
138         * <p>That specification does not apply to any scope. A new object is returned whenever requested and bound to this instance.</p>
139         * @return The {@code attachmentUnmarshaller} dependency.
140         * {@code null} if no object is available.
141         * @throws org.jomc.ObjectManagementException if getting the dependency instance fails.
142         */
143        @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
144        private javax.xml.bind.attachment.AttachmentUnmarshaller getAttachmentUnmarshaller()
145        {
146            return (javax.xml.bind.attachment.AttachmentUnmarshaller) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getDependency( this, "attachmentUnmarshaller" );
147        }
148    
149        /**
150         * Gets the {@code schema} dependency.
151         * <p>This method returns the {@code 'JOMC SDK Model Default'} object of the {@code 'javax.xml.validation.Schema'} {@code (javax.xml.validation.Schema)} specification.</p>
152         * <p>That specification does not apply to any scope. A new object is returned whenever requested and bound to this instance.</p>
153         * @return The {@code schema} dependency.
154         * {@code null} if no object is available.
155         * @throws org.jomc.ObjectManagementException if getting the dependency instance fails.
156         */
157        @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
158        private javax.xml.validation.Schema getSchema()
159        {
160            return (javax.xml.validation.Schema) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getDependency( this, "schema" );
161        }
162    
163        /**
164         * Gets the {@code validationEventHandler} dependency.
165         * <p>This method returns the {@code 'JOMC SDK Model Default'} object of the {@code 'javax.xml.bind.ValidationEventHandler'} {@code (javax.xml.bind.ValidationEventHandler)} specification.</p>
166         * <p>That specification does not apply to any scope. A new object is returned whenever requested and bound to this instance.</p>
167         * @return The {@code validationEventHandler} dependency.
168         * {@code null} if no object is available.
169         * @throws org.jomc.ObjectManagementException if getting the dependency instance fails.
170         */
171        @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
172        private javax.xml.bind.ValidationEventHandler getValidationEventHandler()
173        {
174            return (javax.xml.bind.ValidationEventHandler) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getDependency( this, "validationEventHandler" );
175        }
176        // </editor-fold>
177        // SECTION-END
178        // SECTION-START[Properties]
179        // <editor-fold defaultstate="collapsed" desc=" Generated Properties ">
180    
181        /**
182         * Gets the value of the {@code schemas} property.
183         * @return List of XML schemas ('schemas' element from XML namespace 'http://jomc.org/sdk/model).
184         * @throws org.jomc.ObjectManagementException if getting the property instance fails.
185         */
186        @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
187        private org.jomc.sdk.model.SchemasType getSchemas()
188        {
189            final org.jomc.sdk.model.SchemasType _p = (org.jomc.sdk.model.SchemasType) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "schemas" );
190            assert _p != null : "'schemas' property not found.";
191            return _p;
192        }
193    
194        /**
195         * Gets the value of the {@code unmarshallerProperties} property.
196         * @return The value of the {@code unmarshallerProperties} property.
197         * @throws org.jomc.ObjectManagementException if getting the property instance fails.
198         */
199        @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
200        private java.util.Map<String,Object> getUnmarshallerProperties()
201        {
202            final java.util.Map<String,Object> _p = (java.util.Map<String,Object>) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "unmarshallerProperties" );
203            assert _p != null : "'unmarshallerProperties' property not found.";
204            return _p;
205        }
206        // </editor-fold>
207        // SECTION-END
208        // SECTION-START[Messages]
209        // SECTION-END
210    }