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: JaxpResourceResolverFactory.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 org.w3c.dom.ls.LSResourceResolver;
040    import java.io.IOException;
041    import java.io.InputStream;
042    import java.io.Reader;
043    import java.net.URL;
044    import org.jomc.sdk.model.SchemaType;
045    import org.jomc.sdk.model.SchemasType;
046    import org.w3c.dom.ls.LSInput;
047    import static org.jomc.sdk.model.modlet.SdkModelProvider.XML_SCHEMA_JAVA_CLASSPATH_ID_ATTRIBUTE;
048    
049    // SECTION-START[Documentation]
050    // <editor-fold defaultstate="collapsed" desc=" Generated Documentation ">
051    /**
052     * XML Schema Set JAXP 'LSResourceResolver' factory implementation.
053     * <p><b>Specifications</b><ul>
054     * <li>{@code 'org.w3c.dom.ls.LSResourceResolver'} {@code (org.w3c.dom.ls.LSResourceResolver)} {@code Multiton}</li>
055     * </ul></p>
056     * <p><b>Properties</b><ul>
057     * <li>"{@link #getSchemas schemas}"
058     * <blockquote>Property of type {@code org.jomc.sdk.model.SchemasType}.
059     * <p>List of XML schemas ('schemas' element from XML namespace 'http://jomc.org/sdk/model).</p>
060     * </blockquote></li>
061     * </ul></p>
062     *
063     * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a> 1.0
064     * @version $Id: JaxpResourceResolverFactory.java 2234 2010-06-29 00:03:38Z schulte2005 $
065     */
066    // </editor-fold>
067    // SECTION-END
068    // SECTION-START[Annotations]
069    // <editor-fold defaultstate="collapsed" desc=" Generated Annotations ">
070    @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
071    // </editor-fold>
072    // SECTION-END
073    public final class JaxpResourceResolverFactory implements LSResourceResolver
074    {
075        // SECTION-START[LSResourceResolver]
076    
077        public LSInput resolveResource( final String type, final String namespaceURI, final String publicId,
078                                        final String systemId, final String baseURI )
079        {
080            LSInput input = null;
081            final SchemasType schemas = this.getSchemas();
082    
083            if ( schemas.getLanguageId().equals( type ) )
084            {
085                SchemaType schema = null;
086    
087                if ( namespaceURI != null )
088                {
089                    schema = schemas.getSchemaByPublicId( namespaceURI );
090                }
091                if ( schema == null && publicId != null )
092                {
093                    schema = schemas.getSchemaByPublicId( publicId );
094                }
095                if ( schema == null && systemId != null )
096                {
097                    schema = schemas.getSchemaBySystemId( systemId );
098                }
099    
100                if ( schema != null )
101                {
102                    input = new SchemaTypeInput( schema );
103                }
104            }
105    
106            return input;
107        }
108    
109        // SECTION-END
110        // SECTION-START[JaxpResourceResolverFactory]
111        // SECTION-END
112        // SECTION-START[Constructors]
113        // <editor-fold defaultstate="collapsed" desc=" Generated Constructors ">
114    
115        /** Creates a new {@code JaxpResourceResolverFactory} instance. */
116        @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
117        public JaxpResourceResolverFactory()
118        {
119            // SECTION-START[Default Constructor]
120            super();
121            // SECTION-END
122        }
123        // </editor-fold>
124        // SECTION-END
125        // SECTION-START[Dependencies]
126        // SECTION-END
127        // SECTION-START[Properties]
128        // <editor-fold defaultstate="collapsed" desc=" Generated Properties ">
129    
130        /**
131         * Gets the value of the {@code schemas} property.
132         * @return List of XML schemas ('schemas' element from XML namespace 'http://jomc.org/sdk/model).
133         * @throws org.jomc.ObjectManagementException if getting the property instance fails.
134         */
135        @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
136        private org.jomc.sdk.model.SchemasType getSchemas()
137        {
138            final org.jomc.sdk.model.SchemasType _p = (org.jomc.sdk.model.SchemasType) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "schemas" );
139            assert _p != null : "'schemas' property not found.";
140            return _p;
141        }
142        // </editor-fold>
143        // SECTION-END
144        // SECTION-START[Messages]
145        // SECTION-END
146    }
147    
148    /**
149     * {@code LSInput} implementation backed by {@code SchemaType} instance.
150     *
151     * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a> 1.0
152     * @version $Id: JaxpResourceResolverFactory.java 2234 2010-06-29 00:03:38Z schulte2005 $
153     */
154    class SchemaTypeInput implements LSInput
155    {
156    
157        /** The schema backing the input. */
158        private final SchemaType schemaType;
159    
160        /**
161         * Creates a new {@code SchemaTypeInput} instance taking a {@code SchemaType} instance.
162         *
163         * @param schemaType The schema backing the input.
164         */
165        SchemaTypeInput( final SchemaType schemaType )
166        {
167            this.schemaType = schemaType;
168        }
169    
170        public Reader getCharacterStream()
171        {
172            return null;
173        }
174    
175        public void setCharacterStream( final Reader characterStream )
176        {
177        }
178    
179        public InputStream getByteStream()
180        {
181            try
182            {
183                final URL javaResource = this.getJavaResource();
184                if ( javaResource != null )
185                {
186                    return javaResource.openStream();
187                }
188    
189                return null;
190            }
191            catch ( final IOException e )
192            {
193                return null;
194            }
195        }
196    
197        public void setByteStream( final InputStream byteStream )
198        {
199        }
200    
201        public String getStringData()
202        {
203            return null;
204        }
205    
206        public void setStringData( final String stringData )
207        {
208        }
209    
210        public String getSystemId()
211        {
212            final URL javaResource = this.getJavaResource();
213            return javaResource != null ? javaResource.toExternalForm() : this.schemaType.getSystemId();
214        }
215    
216        public void setSystemId( final String systemId )
217        {
218        }
219    
220        public String getPublicId()
221        {
222            return this.schemaType.getPublicId();
223        }
224    
225        public void setPublicId( final String publicId )
226        {
227        }
228    
229        public String getBaseURI()
230        {
231            return null;
232        }
233    
234        public void setBaseURI( final String baseURI )
235        {
236        }
237    
238        public String getEncoding()
239        {
240            return null;
241        }
242    
243        public void setEncoding( final String encoding )
244        {
245        }
246    
247        public boolean getCertifiedText()
248        {
249            return false;
250        }
251    
252        public void setCertifiedText( final boolean certifiedText )
253        {
254        }
255    
256        private URL getJavaResource()
257        {
258            if ( this.schemaType.getOtherAttributes().containsKey( XML_SCHEMA_JAVA_CLASSPATH_ID_ATTRIBUTE ) )
259            {
260                String absoluteLocation =
261                    this.schemaType.getOtherAttributes().get( XML_SCHEMA_JAVA_CLASSPATH_ID_ATTRIBUTE );
262    
263                if ( !absoluteLocation.startsWith( "/" ) )
264                {
265                    absoluteLocation = '/' + absoluteLocation;
266                }
267    
268                return this.getClass().getResource( absoluteLocation );
269            }
270    
271            return null;
272        }
273    
274    }