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: JaxpSchemaFactory.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 java.io.IOException;
041    import java.net.URL;
042    import java.util.ArrayList;
043    import java.util.List;
044    import javax.xml.transform.Source;
045    import javax.xml.transform.stream.StreamSource;
046    import javax.xml.validation.Schema;
047    import javax.xml.validation.SchemaFactory;
048    import org.jomc.sdk.model.SchemaType;
049    import org.jomc.sdk.model.SchemasType;
050    import org.xml.sax.SAXException;
051    import static org.jomc.sdk.model.modlet.SdkModelProvider.XML_SCHEMA_JAVA_CLASSPATH_ID_ATTRIBUTE;
052    
053    // SECTION-START[Documentation]
054    // <editor-fold defaultstate="collapsed" desc=" Generated Documentation ">
055    /**
056     * XML Schema Set JAXP 'Schema' factory implementation.
057     * <p><b>Specifications</b><ul>
058     * <li>{@code 'javax.xml.validation.Schema'} {@code (javax.xml.validation.Schema)} {@code Multiton}</li>
059     * </ul></p>
060     * <p><b>Properties</b><ul>
061     * <li>"{@link #getSchemaFeatures schemaFeatures}"
062     * <blockquote>Property of type {@code java.util.Map<String,Boolean>}.
063     * </blockquote></li>
064     * <li>"{@link #getSchemaProperties schemaProperties}"
065     * <blockquote>Property of type {@code java.util.Map<String,Object>}.
066     * </blockquote></li>
067     * <li>"{@link #getSchemas schemas}"
068     * <blockquote>Property of type {@code org.jomc.sdk.model.SchemasType}.
069     * <p>List of XML schemas ('schemas' element from XML namespace 'http://jomc.org/sdk/model).</p>
070     * </blockquote></li>
071     * </ul></p>
072     * <p><b>Dependencies</b><ul>
073     * <li>"{@link #getErrorHandler errorHandler}"<blockquote>
074     * Dependency on {@code 'org.xml.sax.ErrorHandler'} {@code (org.xml.sax.ErrorHandler)} bound to an instance.</blockquote></li>
075     * <li>"{@link #getResourceResolver resourceResolver}"<blockquote>
076     * Dependency on {@code 'org.w3c.dom.ls.LSResourceResolver'} {@code (org.w3c.dom.ls.LSResourceResolver)} bound to an instance.</blockquote></li>
077     * </ul></p>
078     *
079     * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a> 1.0
080     * @version $Id: JaxpSchemaFactory.java 2234 2010-06-29 00:03:38Z schulte2005 $
081     */
082    // </editor-fold>
083    // SECTION-END
084    // SECTION-START[Annotations]
085    // <editor-fold defaultstate="collapsed" desc=" Generated Annotations ">
086    @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
087    // </editor-fold>
088    // SECTION-END
089    public final class JaxpSchemaFactory
090    {
091        // SECTION-START[Schema]
092        // SECTION-END
093        // SECTION-START[JaxpSchemaFactory]
094    
095        public Schema getObject() throws IOException, SAXException
096        {
097            Schema schema = null;
098            final SchemaFactory schemaFactory = SchemaFactory.newInstance( this.getSchemas().getLanguageId() );
099            final SchemasType schemas = this.getSchemas();
100            final List<Source> sources = new ArrayList<Source>( schemas.getSchema().size() );
101    
102            for ( SchemaType s : schemas.getSchema() )
103            {
104                final StreamSource source = new StreamSource();
105                source.setPublicId( s.getPublicId() );
106                source.setSystemId( s.getSystemId() );
107    
108                if ( s.getOtherAttributes().containsKey( XML_SCHEMA_JAVA_CLASSPATH_ID_ATTRIBUTE ) )
109                {
110                    String absoluteLocation = s.getOtherAttributes().get( XML_SCHEMA_JAVA_CLASSPATH_ID_ATTRIBUTE );
111                    if ( !absoluteLocation.startsWith( "/" ) )
112                    {
113                        absoluteLocation = '/' + absoluteLocation;
114                    }
115    
116                    final URL resource = this.getClass().getResource( absoluteLocation );
117                    if ( resource != null )
118                    {
119                        source.setSystemId( resource.toExternalForm() );
120                        source.setInputStream( resource.openStream() );
121                    }
122                }
123    
124                sources.add( source );
125            }
126    
127            if ( !sources.isEmpty() )
128            {
129                schemaFactory.setErrorHandler( this.getErrorHandler() );
130                schemaFactory.setResourceResolver( this.getResourceResolver() );
131    
132                for ( Map.Entry<String, Boolean> e : this.getSchemaFeatures().entrySet() )
133                {
134                    schemaFactory.setFeature( e.getKey(), e.getValue() );
135                }
136    
137                for ( Map.Entry<String, Object> e : this.getSchemaProperties().entrySet() )
138                {
139                    schemaFactory.setProperty( e.getKey(), e.getValue() );
140                }
141    
142                schema = schemaFactory.newSchema( sources.toArray( new Source[ sources.size() ] ) );
143            }
144    
145            return schema;
146        }
147    
148        // SECTION-END
149        // SECTION-START[Constructors]
150        // <editor-fold defaultstate="collapsed" desc=" Generated Constructors ">
151    
152        /** Creates a new {@code JaxpSchemaFactory} instance. */
153        @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
154        public JaxpSchemaFactory()
155        {
156            // SECTION-START[Default Constructor]
157            super();
158            // SECTION-END
159        }
160        // </editor-fold>
161        // SECTION-END
162        // SECTION-START[Dependencies]
163        // <editor-fold defaultstate="collapsed" desc=" Generated Dependencies ">
164    
165        /**
166         * Gets the {@code errorHandler} dependency.
167         * <p>This method returns the {@code 'JOMC SDK Model Default'} object of the {@code 'org.xml.sax.ErrorHandler'} {@code (org.xml.sax.ErrorHandler)} specification.</p>
168         * <p>That specification does not apply to any scope. A new object is returned whenever requested and bound to this instance.</p>
169         * @return The {@code errorHandler} dependency.
170         * {@code null} if no object is available.
171         * @throws org.jomc.ObjectManagementException if getting the dependency instance fails.
172         */
173        @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
174        private org.xml.sax.ErrorHandler getErrorHandler()
175        {
176            return (org.xml.sax.ErrorHandler) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getDependency( this, "errorHandler" );
177        }
178    
179        /**
180         * Gets the {@code resourceResolver} dependency.
181         * <p>This method returns the {@code 'JOMC SDK Model Default'} object of the {@code 'org.w3c.dom.ls.LSResourceResolver'} {@code (org.w3c.dom.ls.LSResourceResolver)} specification.</p>
182         * <p>That specification does not apply to any scope. A new object is returned whenever requested and bound to this instance.</p>
183         * @return The {@code resourceResolver} dependency.
184         * {@code null} if no object is available.
185         * @throws org.jomc.ObjectManagementException if getting the dependency instance fails.
186         */
187        @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
188        private org.w3c.dom.ls.LSResourceResolver getResourceResolver()
189        {
190            return (org.w3c.dom.ls.LSResourceResolver) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getDependency( this, "resourceResolver" );
191        }
192        // </editor-fold>
193        // SECTION-END
194        // SECTION-START[Properties]
195        // <editor-fold defaultstate="collapsed" desc=" Generated Properties ">
196    
197        /**
198         * Gets the value of the {@code schemaFeatures} property.
199         * @return The value of the {@code schemaFeatures} property.
200         * @throws org.jomc.ObjectManagementException if getting the property instance fails.
201         */
202        @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
203        private java.util.Map<String,Boolean> getSchemaFeatures()
204        {
205            final java.util.Map<String,Boolean> _p = (java.util.Map<String,Boolean>) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "schemaFeatures" );
206            assert _p != null : "'schemaFeatures' property not found.";
207            return _p;
208        }
209    
210        /**
211         * Gets the value of the {@code schemaProperties} property.
212         * @return The value of the {@code schemaProperties} property.
213         * @throws org.jomc.ObjectManagementException if getting the property instance fails.
214         */
215        @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
216        private java.util.Map<String,Object> getSchemaProperties()
217        {
218            final java.util.Map<String,Object> _p = (java.util.Map<String,Object>) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "schemaProperties" );
219            assert _p != null : "'schemaProperties' property not found.";
220            return _p;
221        }
222    
223        /**
224         * Gets the value of the {@code schemas} property.
225         * @return List of XML schemas ('schemas' element from XML namespace 'http://jomc.org/sdk/model).
226         * @throws org.jomc.ObjectManagementException if getting the property instance fails.
227         */
228        @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
229        private org.jomc.sdk.model.SchemasType getSchemas()
230        {
231            final org.jomc.sdk.model.SchemasType _p = (org.jomc.sdk.model.SchemasType) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "schemas" );
232            assert _p != null : "'schemas' property not found.";
233            return _p;
234        }
235        // </editor-fold>
236        // SECTION-END
237        // SECTION-START[Messages]
238        // SECTION-END
239    }