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: JaxpEntityResolverFactory.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.net.URL;
040    import org.jomc.sdk.model.SchemaType;
041    import org.jomc.sdk.model.SchemasType;
042    import org.xml.sax.InputSource;
043    import org.xml.sax.SAXException;
044    import org.xml.sax.helpers.DefaultHandler;
045    import static org.jomc.sdk.model.modlet.SdkModelProvider.XML_SCHEMA_JAVA_CLASSPATH_ID_ATTRIBUTE;
046    
047    // SECTION-START[Documentation]
048    // <editor-fold defaultstate="collapsed" desc=" Generated Documentation ">
049    /**
050     * XML Schema Set JAXP 'EntityResolver' factory implementation.
051     * <p><b>Specifications</b><ul>
052     * <li>{@code 'org.xml.sax.EntityResolver'} {@code (org.xml.sax.EntityResolver)} {@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     * </ul></p>
060     *
061     * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a> 1.0
062     * @version $Id: JaxpEntityResolverFactory.java 2234 2010-06-29 00:03:38Z schulte2005 $
063     */
064    // </editor-fold>
065    // SECTION-END
066    // SECTION-START[Annotations]
067    // <editor-fold defaultstate="collapsed" desc=" Generated Annotations ">
068    @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
069    // </editor-fold>
070    // SECTION-END
071    public final class JaxpEntityResolverFactory extends DefaultHandler
072    {
073        // SECTION-START[EntityResolver]
074    
075        @Override
076        public InputSource resolveEntity( final String publicId, final String systemId ) throws SAXException
077        {
078            final SchemasType schemas = this.getSchemas();
079            SchemaType schema = null;
080            InputSource source = null;
081    
082            if ( publicId != null )
083            {
084                schema = schemas.getSchemaByPublicId( publicId );
085            }
086            if ( schema == null )
087            {
088                schema = schemas.getSchemaBySystemId( systemId );
089            }
090            if ( schema != null )
091            {
092                source = new InputSource();
093                source.setPublicId( schema.getPublicId() == null ? publicId : schema.getPublicId() );
094                source.setSystemId( schema.getSystemId() == null ? systemId : schema.getSystemId() );
095    
096                if ( schema.getOtherAttributes().containsKey( XML_SCHEMA_JAVA_CLASSPATH_ID_ATTRIBUTE ) )
097                {
098                    String absoluteLocation = schema.getOtherAttributes().get( XML_SCHEMA_JAVA_CLASSPATH_ID_ATTRIBUTE );
099    
100                    if ( !absoluteLocation.startsWith( "/" ) )
101                    {
102                        absoluteLocation = '/' + absoluteLocation;
103                    }
104    
105                    final URL resource = this.getClass().getResource( absoluteLocation );
106                    if ( resource != null )
107                    {
108                        source.setSystemId( resource.toExternalForm() );
109                    }
110                }
111            }
112    
113            return source;
114        }
115    
116        // SECTION-END
117        // SECTION-START[JaxpEntityResolverFactory]
118        // SECTION-END
119        // SECTION-START[Constructors]
120        // <editor-fold defaultstate="collapsed" desc=" Generated Constructors ">
121    
122        /** Creates a new {@code JaxpEntityResolverFactory} 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 JaxpEntityResolverFactory()
125        {
126            // SECTION-START[Default Constructor]
127            super();
128            // SECTION-END
129        }
130        // </editor-fold>
131        // SECTION-END
132        // SECTION-START[Dependencies]
133        // SECTION-END
134        // SECTION-START[Properties]
135        // <editor-fold defaultstate="collapsed" desc=" Generated Properties ">
136    
137        /**
138         * Gets the value of the {@code schemas} property.
139         * @return List of XML schemas ('schemas' element from XML namespace 'http://jomc.org/sdk/model).
140         * @throws org.jomc.ObjectManagementException if getting the property instance fails.
141         */
142        @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
143        private org.jomc.sdk.model.SchemasType getSchemas()
144        {
145            final org.jomc.sdk.model.SchemasType _p = (org.jomc.sdk.model.SchemasType) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "schemas" );
146            assert _p != null : "'schemas' property not found.";
147            return _p;
148        }
149        // </editor-fold>
150        // SECTION-END
151        // SECTION-START[Messages]
152        // SECTION-END
153    }