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: JaxpSchemaFactory.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.util.Map;
40 import java.io.IOException;
41 import java.net.URL;
42 import java.util.ArrayList;
43 import java.util.List;
44 import javax.xml.transform.Source;
45 import javax.xml.transform.stream.StreamSource;
46 import javax.xml.validation.Schema;
47 import javax.xml.validation.SchemaFactory;
48 import org.jomc.sdk.model.SchemaType;
49 import org.jomc.sdk.model.SchemasType;
50 import org.xml.sax.SAXException;
51 import static org.jomc.sdk.model.modlet.SdkModelProvider.XML_SCHEMA_JAVA_CLASSPATH_ID_ATTRIBUTE;
52
53 // SECTION-START[Documentation]
54 // <editor-fold defaultstate="collapsed" desc=" Generated Documentation ">
55 /**
56 * XML Schema Set JAXP 'Schema' factory implementation.
57 * <p><b>Specifications</b><ul>
58 * <li>{@code 'javax.xml.validation.Schema'} {@code (javax.xml.validation.Schema)} {@code Multiton}</li>
59 * </ul></p>
60 * <p><b>Properties</b><ul>
61 * <li>"{@link #getSchemaFeatures schemaFeatures}"
62 * <blockquote>Property of type {@code java.util.Map<String,Boolean>}.
63 * </blockquote></li>
64 * <li>"{@link #getSchemaProperties schemaProperties}"
65 * <blockquote>Property of type {@code java.util.Map<String,Object>}.
66 * </blockquote></li>
67 * <li>"{@link #getSchemas schemas}"
68 * <blockquote>Property of type {@code org.jomc.sdk.model.SchemasType}.
69 * <p>List of XML schemas ('schemas' element from XML namespace 'http://jomc.org/sdk/model).</p>
70 * </blockquote></li>
71 * </ul></p>
72 * <p><b>Dependencies</b><ul>
73 * <li>"{@link #getErrorHandler errorHandler}"<blockquote>
74 * Dependency on {@code 'org.xml.sax.ErrorHandler'} {@code (org.xml.sax.ErrorHandler)} bound to an instance.</blockquote></li>
75 * <li>"{@link #getResourceResolver resourceResolver}"<blockquote>
76 * Dependency on {@code 'org.w3c.dom.ls.LSResourceResolver'} {@code (org.w3c.dom.ls.LSResourceResolver)} bound to an instance.</blockquote></li>
77 * </ul></p>
78 *
79 * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a> 1.0
80 * @version $Id: JaxpSchemaFactory.java 2234 2010-06-29 00:03:38Z schulte2005 $
81 */
82 // </editor-fold>
83 // SECTION-END
84 // SECTION-START[Annotations]
85 // <editor-fold defaultstate="collapsed" desc=" Generated Annotations ">
86 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
87 // </editor-fold>
88 // SECTION-END
89 public final class JaxpSchemaFactory
90 {
91 // SECTION-START[Schema]
92 // SECTION-END
93 // SECTION-START[JaxpSchemaFactory]
94
95 public Schema getObject() throws IOException, SAXException
96 {
97 Schema schema = null;
98 final SchemaFactory schemaFactory = SchemaFactory.newInstance( this.getSchemas().getLanguageId() );
99 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 }