View Javadoc

1   // SECTION-START[License Header]
2   // <editor-fold defaultstate="collapsed" desc=" Generated License ">
3   /*
4    *   Copyright (c) 2010 The JOMC Project
5    *   Copyright (c) 2005 Christian Schulte <schulte2005@users.sourceforge.net>
6    *   All rights reserved.
7    *
8    *   Redistribution and use in source and binary forms, with or without
9    *   modification, are permitted provided that the following conditions
10   *   are met:
11   *
12   *     o Redistributions of source code must retain the above copyright
13   *       notice, this list of conditions and the following disclaimer.
14   *
15   *     o Redistributions in binary form must reproduce the above copyright
16   *       notice, this list of conditions and the following disclaimer in
17   *       the documentation and/or other materials provided with the
18   *       distribution.
19   *
20   *   THIS SOFTWARE IS PROVIDED BY THE JOMC PROJECT AND CONTRIBUTORS "AS IS"
21   *   AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22   *   THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23   *   PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE JOMC PROJECT OR
24   *   CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25   *   EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26   *   PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27   *   OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28   *   WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29   *   OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
30   *   ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31   *
32   *   $Id: JaxpEntityResolverFactory.java 2234 2010-06-29 00:03:38Z schulte2005 $
33   *
34   */
35  // </editor-fold>
36  // SECTION-END
37  package org.jomc.sdk.model.support;
38  
39  import java.net.URL;
40  import org.jomc.sdk.model.SchemaType;
41  import org.jomc.sdk.model.SchemasType;
42  import org.xml.sax.InputSource;
43  import org.xml.sax.SAXException;
44  import org.xml.sax.helpers.DefaultHandler;
45  import static org.jomc.sdk.model.modlet.SdkModelProvider.XML_SCHEMA_JAVA_CLASSPATH_ID_ATTRIBUTE;
46  
47  // SECTION-START[Documentation]
48  // <editor-fold defaultstate="collapsed" desc=" Generated Documentation ">
49  /**
50   * XML Schema Set JAXP 'EntityResolver' factory implementation.
51   * <p><b>Specifications</b><ul>
52   * <li>{@code 'org.xml.sax.EntityResolver'} {@code (org.xml.sax.EntityResolver)} {@code Multiton}</li>
53   * </ul></p>
54   * <p><b>Properties</b><ul>
55   * <li>"{@link #getSchemas schemas}"
56   * <blockquote>Property of type {@code org.jomc.sdk.model.SchemasType}.
57   * <p>List of XML schemas ('schemas' element from XML namespace 'http://jomc.org/sdk/model).</p>
58   * </blockquote></li>
59   * </ul></p>
60   *
61   * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a> 1.0
62   * @version $Id: JaxpEntityResolverFactory.java 2234 2010-06-29 00:03:38Z schulte2005 $
63   */
64  // </editor-fold>
65  // SECTION-END
66  // SECTION-START[Annotations]
67  // <editor-fold defaultstate="collapsed" desc=" Generated Annotations ">
68  @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
69  // </editor-fold>
70  // SECTION-END
71  public final class JaxpEntityResolverFactory extends DefaultHandler
72  {
73      // SECTION-START[EntityResolver]
74  
75      @Override
76      public InputSource resolveEntity( final String publicId, final String systemId ) throws SAXException
77      {
78          final SchemasType schemas = this.getSchemas();
79          SchemaType schema = null;
80          InputSource source = null;
81  
82          if ( publicId != null )
83          {
84              schema = schemas.getSchemaByPublicId( publicId );
85          }
86          if ( schema == null )
87          {
88              schema = schemas.getSchemaBySystemId( systemId );
89          }
90          if ( schema != null )
91          {
92              source = new InputSource();
93              source.setPublicId( schema.getPublicId() == null ? publicId : schema.getPublicId() );
94              source.setSystemId( schema.getSystemId() == null ? systemId : schema.getSystemId() );
95  
96              if ( schema.getOtherAttributes().containsKey( XML_SCHEMA_JAVA_CLASSPATH_ID_ATTRIBUTE ) )
97              {
98                  String absoluteLocation = schema.getOtherAttributes().get( XML_SCHEMA_JAVA_CLASSPATH_ID_ATTRIBUTE );
99  
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 }