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: JaxpValidatorHandlerFactory.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 javax.xml.validation.ValidatorHandler; 41 import java.io.IOException; 42 import java.net.URL; 43 import java.util.ArrayList; 44 import java.util.List; 45 import javax.xml.transform.Source; 46 import javax.xml.transform.stream.StreamSource; 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 'ValidatorHandler' factory implementation. 57 * <p><b>Specifications</b><ul> 58 * <li>{@code 'javax.xml.validation.ValidatorHandler'} {@code (javax.xml.validation.ValidatorHandler)} {@code Multiton}</li> 59 * <li>{@code 'org.xml.sax.ContentHandler'} {@code (org.xml.sax.ContentHandler)} {@code Multiton}</li> 60 * </ul></p> 61 * <p><b>Properties</b><ul> 62 * <li>"{@link #getSchemas schemas}" 63 * <blockquote>Property of type {@code org.jomc.sdk.model.SchemasType}. 64 * <p>List of XML schemas ('schemas' element from XML namespace 'http://jomc.org/sdk/model).</p> 65 * </blockquote></li> 66 * <li>"{@link #getValidatorHandlerFeatures validatorHandlerFeatures}" 67 * <blockquote>Property of type {@code java.util.Map<String,Boolean>}. 68 * </blockquote></li> 69 * <li>"{@link #getValidatorHandlerProperties validatorHandlerProperties}" 70 * <blockquote>Property of type {@code java.util.Map<String,Object>}. 71 * </blockquote></li> 72 * </ul></p> 73 * <p><b>Dependencies</b><ul> 74 * <li>"{@link #getErrorHandler errorHandler}"<blockquote> 75 * Dependency on {@code 'org.xml.sax.ErrorHandler'} {@code (org.xml.sax.ErrorHandler)} bound to an instance.</blockquote></li> 76 * <li>"{@link #getResourceResolver resourceResolver}"<blockquote> 77 * Dependency on {@code 'org.w3c.dom.ls.LSResourceResolver'} {@code (org.w3c.dom.ls.LSResourceResolver)} bound to an instance.</blockquote></li> 78 * </ul></p> 79 * 80 * @author <a href="mailto:schulte2005@users.sourceforge.net">Christian Schulte</a> 1.0 81 * @version $Id: JaxpValidatorHandlerFactory.java 2234 2010-06-29 00:03:38Z schulte2005 $ 82 */ 83 // </editor-fold> 84 // SECTION-END 85 // SECTION-START[Annotations] 86 // <editor-fold defaultstate="collapsed" desc=" Generated Annotations "> 87 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" ) 88 // </editor-fold> 89 // SECTION-END 90 public final class JaxpValidatorHandlerFactory 91 { 92 // SECTION-START[ValidatorHandler] 93 // SECTION-END 94 // SECTION-START[JaxpValidatorHandlerFactory] 95 96 public ValidatorHandler getObject() throws IOException, SAXException 97 { 98 ValidatorHandler validatorHandler = null; 99 final SchemaFactory schemaFactory = SchemaFactory.newInstance( this.getSchemas().getLanguageId() ); 100 final SchemasType schemas = this.getSchemas(); 101 final List<Source> sources = new ArrayList<Source>( schemas.getSchema().size() ); 102 103 for ( SchemaType s : schemas.getSchema() ) 104 { 105 final StreamSource source = new StreamSource(); 106 source.setPublicId( s.getPublicId() ); 107 source.setSystemId( s.getSystemId() ); 108 109 if ( s.getOtherAttributes().containsKey( XML_SCHEMA_JAVA_CLASSPATH_ID_ATTRIBUTE ) ) 110 { 111 String absoluteLocation = s.getOtherAttributes().get( XML_SCHEMA_JAVA_CLASSPATH_ID_ATTRIBUTE ); 112 if ( !absoluteLocation.startsWith( "/" ) ) 113 { 114 absoluteLocation = '/' + absoluteLocation; 115 } 116 117 final URL resource = this.getClass().getResource( absoluteLocation ); 118 if ( resource != null ) 119 { 120 source.setSystemId( resource.toExternalForm() ); 121 source.setInputStream( resource.openStream() ); 122 } 123 } 124 125 sources.add( source ); 126 } 127 128 if ( !sources.isEmpty() ) 129 { 130 validatorHandler = 131 schemaFactory.newSchema( sources.toArray( new Source[ sources.size() ] ) ).newValidatorHandler(); 132 133 validatorHandler.setErrorHandler( this.getErrorHandler() ); 134 validatorHandler.setResourceResolver( this.getResourceResolver() ); 135 136 for ( Map.Entry<String, Boolean> e : this.getValidatorHandlerFeatures().entrySet() ) 137 { 138 validatorHandler.setFeature( e.getKey(), e.getValue() ); 139 } 140 141 for ( Map.Entry<String, Object> e : this.getValidatorHandlerProperties().entrySet() ) 142 { 143 validatorHandler.setProperty( e.getKey(), e.getValue() ); 144 } 145 } 146 147 return validatorHandler; 148 } 149 150 // SECTION-END 151 // SECTION-START[Constructors] 152 // <editor-fold defaultstate="collapsed" desc=" Generated Constructors "> 153 154 /** Creates a new {@code JaxpValidatorHandlerFactory} instance. */ 155 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" ) 156 public JaxpValidatorHandlerFactory() 157 { 158 // SECTION-START[Default Constructor] 159 super(); 160 // SECTION-END 161 } 162 // </editor-fold> 163 // SECTION-END 164 // SECTION-START[Dependencies] 165 // <editor-fold defaultstate="collapsed" desc=" Generated Dependencies "> 166 167 /** 168 * Gets the {@code errorHandler} dependency. 169 * <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> 170 * <p>That specification does not apply to any scope. A new object is returned whenever requested and bound to this instance.</p> 171 * @return The {@code errorHandler} dependency. 172 * {@code null} if no object is available. 173 * @throws org.jomc.ObjectManagementException if getting the dependency instance fails. 174 */ 175 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" ) 176 private org.xml.sax.ErrorHandler getErrorHandler() 177 { 178 return (org.xml.sax.ErrorHandler) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getDependency( this, "errorHandler" ); 179 } 180 181 /** 182 * Gets the {@code resourceResolver} dependency. 183 * <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> 184 * <p>That specification does not apply to any scope. A new object is returned whenever requested and bound to this instance.</p> 185 * @return The {@code resourceResolver} dependency. 186 * {@code null} if no object is available. 187 * @throws org.jomc.ObjectManagementException if getting the dependency instance fails. 188 */ 189 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" ) 190 private org.w3c.dom.ls.LSResourceResolver getResourceResolver() 191 { 192 return (org.w3c.dom.ls.LSResourceResolver) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getDependency( this, "resourceResolver" ); 193 } 194 // </editor-fold> 195 // SECTION-END 196 // SECTION-START[Properties] 197 // <editor-fold defaultstate="collapsed" desc=" Generated Properties "> 198 199 /** 200 * Gets the value of the {@code schemas} property. 201 * @return List of XML schemas ('schemas' element from XML namespace 'http://jomc.org/sdk/model). 202 * @throws org.jomc.ObjectManagementException if getting the property instance fails. 203 */ 204 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" ) 205 private org.jomc.sdk.model.SchemasType getSchemas() 206 { 207 final org.jomc.sdk.model.SchemasType _p = (org.jomc.sdk.model.SchemasType) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "schemas" ); 208 assert _p != null : "'schemas' property not found."; 209 return _p; 210 } 211 212 /** 213 * Gets the value of the {@code validatorHandlerFeatures} property. 214 * @return The value of the {@code validatorHandlerFeatures} property. 215 * @throws org.jomc.ObjectManagementException if getting the property instance fails. 216 */ 217 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" ) 218 private java.util.Map<String,Boolean> getValidatorHandlerFeatures() 219 { 220 final java.util.Map<String,Boolean> _p = (java.util.Map<String,Boolean>) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "validatorHandlerFeatures" ); 221 assert _p != null : "'validatorHandlerFeatures' property not found."; 222 return _p; 223 } 224 225 /** 226 * Gets the value of the {@code validatorHandlerProperties} property. 227 * @return The value of the {@code validatorHandlerProperties} property. 228 * @throws org.jomc.ObjectManagementException if getting the property instance fails. 229 */ 230 @javax.annotation.Generated( value = "org.jomc.tools.SourceFileProcessor 1.0", comments = "See http://jomc.sourceforge.net/jomc/1.0/jomc-tools" ) 231 private java.util.Map<String,Object> getValidatorHandlerProperties() 232 { 233 final java.util.Map<String,Object> _p = (java.util.Map<String,Object>) org.jomc.ObjectManagerFactory.getObjectManager( this.getClass().getClassLoader() ).getProperty( this, "validatorHandlerProperties" ); 234 assert _p != null : "'validatorHandlerProperties' property not found."; 235 return _p; 236 } 237 // </editor-fold> 238 // SECTION-END 239 // SECTION-START[Messages] 240 // SECTION-END 241 // SECTION-START[ContentHandler] 242 // SECTION-END 243 }