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: JaxpValidatorFactory.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.SchemaFactory;
47 import javax.xml.validation.Validator;
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 'Validator' factory implementation.
57 * <p><b>Specifications</b><ul>
58 * <li>{@code 'javax.xml.validation.Validator'} {@code (javax.xml.validation.Validator)} {@code Multiton}</li>
59 * </ul></p>
60 * <p><b>Properties</b><ul>
61 * <li>"{@link #getSchemas schemas}"
62 * <blockquote>Property of type {@code org.jomc.sdk.model.SchemasType}.
63 * <p>List of XML schemas ('schemas' element from XML namespace 'http://jomc.org/sdk/model).</p>
64 * </blockquote></li>
65 * <li>"{@link #getValidatorFeatures validatorFeatures}"
66 * <blockquote>Property of type {@code java.util.Map<String,Boolean>}.
67 * </blockquote></li>
68 * <li>"{@link #getValidatorProperties validatorProperties}"
69 * <blockquote>Property of type {@code java.util.Map<String,Object>}.
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: JaxpValidatorFactory.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 JaxpValidatorFactory
90 {
91 // SECTION-START[Validator]
92 // SECTION-END
93 // SECTION-START[JaxpValidatorFactory]
94
95 public Validator getObject() throws IOException, SAXException
96 {
97 Validator validator = 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 validator = schemaFactory.newSchema( sources.toArray( new Source[ sources.size() ] ) ).newValidator();
130 validator.setErrorHandler( this.getErrorHandler() );
131 validator.setResourceResolver( this.getResourceResolver() );
132
133 for ( Map.Entry<String, Boolean> e : this.getValidatorFeatures().entrySet() )
134 {
135 validator.setFeature( e.getKey(), e.getValue() );
136 }
137
138 for ( Map.Entry<String, Object> e : this.getValidatorProperties().entrySet() )
139 {
140 validator.setProperty( e.getKey(), e.getValue() );
141 }
142 }
143
144 return validator;
145 }
146
147 // SECTION-END
148 // SECTION-START[Constructors]
149 // <editor-fold defaultstate="collapsed" desc=" Generated Constructors ">
150
151 /** Creates a new {@code JaxpValidatorFactory} instance. */
152 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
153 public JaxpValidatorFactory()
154 {
155 // SECTION-START[Default Constructor]
156 super();
157 // SECTION-END
158 }
159 // </editor-fold>
160 // SECTION-END
161 // SECTION-START[Dependencies]
162 // <editor-fold defaultstate="collapsed" desc=" Generated Dependencies ">
163
164 /**
165 * Gets the {@code errorHandler} dependency.
166 * <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>
167 * <p>That specification does not apply to any scope. A new object is returned whenever requested and bound to this instance.</p>
168 * @return The {@code errorHandler} dependency.
169 * {@code null} if no object is available.
170 * @throws org.jomc.ObjectManagementException if getting the dependency instance fails.
171 */
172 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
173 private org.xml.sax.ErrorHandler getErrorHandler()
174 {
175 return (org.xml.sax.ErrorHandler) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getDependency( this, "errorHandler" );
176 }
177
178 /**
179 * Gets the {@code resourceResolver} dependency.
180 * <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>
181 * <p>That specification does not apply to any scope. A new object is returned whenever requested and bound to this instance.</p>
182 * @return The {@code resourceResolver} dependency.
183 * {@code null} if no object is available.
184 * @throws org.jomc.ObjectManagementException if getting the dependency instance fails.
185 */
186 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
187 private org.w3c.dom.ls.LSResourceResolver getResourceResolver()
188 {
189 return (org.w3c.dom.ls.LSResourceResolver) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getDependency( this, "resourceResolver" );
190 }
191 // </editor-fold>
192 // SECTION-END
193 // SECTION-START[Properties]
194 // <editor-fold defaultstate="collapsed" desc=" Generated Properties ">
195
196 /**
197 * Gets the value of the {@code schemas} property.
198 * @return List of XML schemas ('schemas' element from XML namespace 'http://jomc.org/sdk/model).
199 * @throws org.jomc.ObjectManagementException if getting the property instance fails.
200 */
201 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
202 private org.jomc.sdk.model.SchemasType getSchemas()
203 {
204 final org.jomc.sdk.model.SchemasType _p = (org.jomc.sdk.model.SchemasType) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "schemas" );
205 assert _p != null : "'schemas' property not found.";
206 return _p;
207 }
208
209 /**
210 * Gets the value of the {@code validatorFeatures} property.
211 * @return The value of the {@code validatorFeatures} property.
212 * @throws org.jomc.ObjectManagementException if getting the property instance fails.
213 */
214 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
215 private java.util.Map<String,Boolean> getValidatorFeatures()
216 {
217 final java.util.Map<String,Boolean> _p = (java.util.Map<String,Boolean>) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "validatorFeatures" );
218 assert _p != null : "'validatorFeatures' property not found.";
219 return _p;
220 }
221
222 /**
223 * Gets the value of the {@code validatorProperties} property.
224 * @return The value of the {@code validatorProperties} property.
225 * @throws org.jomc.ObjectManagementException if getting the property instance fails.
226 */
227 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" )
228 private java.util.Map<String,Object> getValidatorProperties()
229 {
230 final java.util.Map<String,Object> _p = (java.util.Map<String,Object>) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "validatorProperties" );
231 assert _p != null : "'validatorProperties' property not found.";
232 return _p;
233 }
234 // </editor-fold>
235 // SECTION-END
236 // SECTION-START[Messages]
237 // SECTION-END
238 }