View Javadoc
1 /* 2 * Copyright (C) The Spice Group. All rights reserved. 3 * 4 * This software is published under the terms of the Spice 5 * Software License version 1.1, a copy of which has been included 6 * with this distribution in the LICENSE.txt file. 7 */ 8 package org.codehaus.spice.configkit; 9 10 import java.io.IOException; 11 import java.io.InputStream; 12 import java.util.ArrayList; 13 import java.util.List; 14 import javax.xml.parsers.ParserConfigurationException; 15 import javax.xml.parsers.SAXParser; 16 import javax.xml.parsers.SAXParserFactory; 17 import org.iso_relax.verifier.Schema; 18 import org.iso_relax.verifier.Verifier; 19 import org.iso_relax.verifier.VerifierConfigurationException; 20 import org.iso_relax.verifier.VerifierFilter; 21 import org.w3c.dom.Node; 22 import org.xml.sax.ContentHandler; 23 import org.xml.sax.EntityResolver; 24 import org.xml.sax.ErrorHandler; 25 import org.xml.sax.InputSource; 26 import org.xml.sax.SAXException; 27 import org.xml.sax.XMLReader; 28 29 /*** 30 * The ConfigValidator is responsible for validating an XML configuration source 31 * according to a specified Schema. The schema is defined when the validator is 32 * created. Note that the user should get a reference to the validator via the 33 * ConfigValidatorFactory. 34 * 35 * @author Peter Donald 36 * @version $Revision: 1.1 $ $Date: 2003/12/03 03:19:28 $ 37 */ 38 public class ConfigValidator 39 { 40 /*** The schema that used when validating configuration. */ 41 private Schema m_schema; 42 43 /*** The entity resolver used by validator if any. */ 44 private EntityResolver m_resolver; 45 46 /*** 47 * Create a validator for specified schema. 48 * 49 * @param schema the schema 50 */ 51 ConfigValidator( final Schema schema, 52 final EntityResolver resolver ) 53 { 54 if( null == schema ) 55 { 56 throw new NullPointerException( "schema" ); 57 } 58 m_schema = schema; 59 m_resolver = resolver; 60 } 61 62 /*** 63 * Validate input stream according to shcema. 64 * 65 * @param inputStream the inputStream to validate 66 * @param contentHandler the handler to send to after validation 67 * @param errorHandler that will be passed any errors. May be null. 68 * @throws ValidateException if unable to validate the input 69 */ 70 public void validate( final InputStream inputStream, 71 final ContentHandler contentHandler, 72 final ErrorHandler errorHandler ) 73 throws ValidateException 74 { 75 if( null == inputStream ) 76 { 77 throw new NullPointerException( "inputStream" ); 78 } 79 validate( new InputSource( inputStream ), 80 contentHandler, 81 errorHandler ); 82 } 83 84 /*** 85 * Validate input stream according to shcema. 86 * 87 * @param inputStream the inputStream to validate 88 * @param errorHandler that will be passed any errors. May be null. 89 * @throws ValidateException if unable to validate the input 90 */ 91 public void validate( final InputStream inputStream, 92 final ErrorHandler errorHandler ) 93 throws ValidateException 94 { 95 if( null == inputStream ) 96 { 97 throw new NullPointerException( "inputStream" ); 98 } 99 validate( new InputSource( inputStream ), errorHandler ); 100 } 101 102 /*** 103 * Validate InputStream according to shcema. 104 * 105 * @param inputStream the inputStream to validate 106 * @return the ValidationResults 107 */ 108 public ValidationResult validate( final InputStream inputStream ) 109 { 110 if( null == inputStream ) 111 { 112 throw new NullPointerException( "inputStream" ); 113 } 114 return validate( new InputSource( inputStream ) ); 115 } 116 117 /*** 118 * Validate InputStream according to shcema. 119 * 120 * @param inputStream the inputStream to validate 121 * @param contentHandler the handler to send to after validation 122 * @return the ValidationResults 123 */ 124 public ValidationResult validate( final InputStream inputStream, 125 final ContentHandler contentHandler ) 126 { 127 if( null == inputStream ) 128 { 129 throw new NullPointerException( "inputStream" ); 130 } 131 return validate( new InputSource( inputStream ), contentHandler ); 132 } 133 134 /*** 135 * Validate InputSource according to shcema. 136 * 137 * @param source the InputSource to validate 138 * @param errorHandler that will be passed any errors. May be null. 139 * @throws ValidateException if unable to validate the input 140 */ 141 public void validate( final InputSource source, 142 final ErrorHandler errorHandler ) 143 throws ValidateException 144 { 145 if( null == source ) 146 { 147 throw new NullPointerException( "source" ); 148 } 149 doValidate( source, errorHandler ); 150 } 151 152 /*** 153 * Validate InputSource according to shcema. 154 * 155 * @param source the InputSource to validate 156 * @param contentHandler the handler to send to after validation 157 * @param errorHandler that will be passed any errors. May be null. 158 * @throws ValidateException if unable to validate the input 159 */ 160 public void validate( final InputSource source, 161 final ContentHandler contentHandler, 162 final ErrorHandler errorHandler ) 163 throws ValidateException 164 { 165 if( null == source ) 166 { 167 throw new NullPointerException( "source" ); 168 } 169 doValidate( source, contentHandler, errorHandler ); 170 } 171 172 /*** 173 * Validate InputSource according to shcema. 174 * 175 * @param source the source to validate 176 * @return the ValidationResults 177 */ 178 public ValidationResult validate( final InputSource source ) 179 { 180 if( null == source ) 181 { 182 throw new NullPointerException( "source" ); 183 } 184 return doValidate( source ); 185 } 186 187 /*** 188 * Validate InputSource according to shcema. 189 * 190 * @param source the source to validate 191 * @param contentHandler the handler to send to after validation 192 * @return the ValidationResults 193 */ 194 public ValidationResult validate( final InputSource source, 195 final ContentHandler contentHandler ) 196 { 197 if( null == source ) 198 { 199 throw new NullPointerException( "source" ); 200 } 201 return doValidate( source, contentHandler ); 202 } 203 204 /*** 205 * Validate Node according to shcema. 206 * 207 * @param node the node to validate 208 * @param errorHandler that will be passed any errors. May be null. 209 * @throws ValidateException if unable to validate the input 210 */ 211 public void validate( final Node node, final ErrorHandler errorHandler ) 212 throws ValidateException 213 { 214 if( null == node ) 215 { 216 throw new NullPointerException( "node" ); 217 } 218 doValidate( node, errorHandler ); 219 } 220 221 /*** 222 * Validate Node according to shcema. 223 * 224 * @param node the node to validate 225 * @return the ValidationResults 226 */ 227 public ValidationResult validate( final Node node ) 228 { 229 if( null == node ) 230 { 231 throw new NullPointerException( "node" ); 232 } 233 return doValidate( node ); 234 } 235 236 /*** 237 * Perform the validation and collect the results. 238 * 239 * @param data either a DOM Node or an InputSource to validate 240 * @return the ValidationResults 241 */ 242 private ValidationResult doValidate( final Object data ) 243 { 244 final List issueSet = new ArrayList(); 245 ValidateException exception = null; 246 try 247 { 248 doValidate( data, new IssueCollector( issueSet ) ); 249 } 250 catch( final ValidateException ve ) 251 { 252 exception = ve; 253 } 254 255 final ValidationIssue[] issues = (ValidationIssue[])issueSet. 256 toArray( new ValidationIssue[ issueSet.size() ] ); 257 return new ValidationResult( exception, issues ); 258 } 259 260 /*** 261 * Actually perform the validation. 262 * 263 * @param data either a DOM Node or an InputSource to validate 264 * @param errorHandler the error handler that receives error messages 265 * @throws ValidateException if unable to validate the input 266 */ 267 private void doValidate( final Object data, 268 final ErrorHandler errorHandler ) 269 throws ValidateException 270 { 271 try 272 { 273 final Verifier verifier = m_schema.newVerifier(); 274 if( null != errorHandler ) 275 { 276 verifier.setErrorHandler( errorHandler ); 277 } 278 if( null != m_resolver ) 279 { 280 verifier.setEntityResolver( m_resolver ); 281 } 282 boolean valid; 283 if( data instanceof InputSource ) 284 { 285 valid = verifier.verify( (InputSource)data ); 286 } 287 else 288 { 289 valid = verifier.verify( (Node)data ); 290 } 291 292 if( !valid ) 293 { 294 final String message = "Unable to validate input according to schema"; 295 throw new ValidateException( message ); 296 } 297 } 298 catch( final VerifierConfigurationException vce ) 299 { 300 final String message = "Unable to locate verifier for schema"; 301 throw new ValidateException( message, vce ); 302 } 303 catch( final SAXException se ) 304 { 305 final String message = "Malformed input XML."; 306 throw new ValidateException( message, se ); 307 } 308 catch( final IOException ioe ) 309 { 310 final String message = "Error reading input data XML."; 311 throw new ValidateException( message, ioe ); 312 } 313 } 314 315 /*** 316 * Perform the validation via filtering and collect the results. 317 * 318 * @param input an InputSource to validate 319 * @param contentHandler the handler to send to after validation 320 * @return the ValidationResults 321 */ 322 private ValidationResult doValidate( final InputSource input, 323 final ContentHandler contentHandler ) 324 { 325 final List issueSet = new ArrayList(); 326 ValidateException exception = null; 327 try 328 { 329 doValidate( input, 330 contentHandler, 331 new IssueCollector( issueSet ) ); 332 } 333 catch( final ValidateException ve ) 334 { 335 exception = ve; 336 } 337 338 final ValidationIssue[] issues = (ValidationIssue[])issueSet. 339 toArray( new ValidationIssue[ issueSet.size() ] ); 340 return new ValidationResult( exception, issues ); 341 } 342 343 /*** 344 * Actually perform the validation via filtering. 345 * 346 * @param input an InputSource to validate 347 * @param contentHandler the handler to send to after validation 348 * @param errorHandler the error handler that receives error messages 349 * @throws ValidateException if unable to validate the input 350 */ 351 private void doValidate( final InputSource input, 352 final ContentHandler contentHandler, 353 final ErrorHandler errorHandler ) 354 throws ValidateException 355 { 356 if( null == contentHandler ) 357 { 358 throw new NullPointerException( "contentHandler" ); 359 } 360 final SAXParserFactory saxParserFactory = SAXParserFactory.newInstance(); 361 saxParserFactory.setNamespaceAware( true ); 362 363 try 364 { 365 final SAXParser saxParser = saxParserFactory.newSAXParser(); 366 final XMLReader reader = saxParser.getXMLReader(); 367 368 final Verifier verifier = m_schema.newVerifier(); 369 if( null != errorHandler ) 370 { 371 verifier.setErrorHandler( errorHandler ); 372 } 373 if( null != m_resolver ) 374 { 375 verifier.setEntityResolver( m_resolver ); 376 } 377 final VerifierFilter filter = verifier.getVerifierFilter(); 378 filter.setParent( reader ); 379 filter.setContentHandler( contentHandler ); 380 if( null != errorHandler ) 381 { 382 filter.setErrorHandler( errorHandler ); 383 } 384 if( null != m_resolver ) 385 { 386 filter.setEntityResolver( m_resolver ); 387 } 388 filter.parse( input ); 389 390 if( !filter.isValid() ) 391 { 392 final String message = "Unable to validate input according to schema"; 393 throw new ValidateException( message ); 394 } 395 } 396 catch( final VerifierConfigurationException vce ) 397 { 398 final String message = "Unable to locate verifier for schema"; 399 throw new ValidateException( message, vce ); 400 } 401 catch( final SAXException se ) 402 { 403 final String message = "Malformed input XML."; 404 throw new ValidateException( message, se ); 405 } 406 catch( final IOException ioe ) 407 { 408 final String message = "Error reading input input XML."; 409 throw new ValidateException( message, ioe ); 410 } 411 catch( final ParserConfigurationException pce ) 412 { 413 final String message = "Unable to get parser."; 414 throw new ValidateException( message, pce ); 415 } 416 } 417 }

This page was automatically generated by Maven