1 56 57 package org.jdom.contrib.schema; 58 59 import java.io.File ; 60 import java.io.InputStream ; 61 import java.io.FileInputStream ; 62 import java.io.Reader ; 63 import java.io.IOException ; 64 import java.util.List ; 65 import java.util.ArrayList ; 66 import java.util.LinkedList ; 67 68 import org.xml.sax.InputSource ; 69 import org.xml.sax.Locator ; 70 import org.xml.sax.XMLReader ; 71 import org.xml.sax.SAXException ; 72 import org.xml.sax.SAXParseException ; 73 import org.xml.sax.helpers.XMLFilterImpl ; 74 75 import org.iso_relax.verifier.Verifier; 76 import org.iso_relax.verifier.VerifierFactory; 77 import org.iso_relax.verifier.VerifierConfigurationException; 78 79 import org.jdom.Document; 80 import org.jdom.Element; 81 import org.jdom.JDOMException; 82 import org.jdom.output.SAXOutputter; 83 import org.jdom.output.JDOMLocator; 84 85 122 public class Schema { 123 124 127 public final static Type W3C_XML_SCHEMA = 128 new Type("W3C XML Schema", "http://www.w3.org/2001/XMLSchema"); 129 132 public final static Type RELAX_NG = 133 new Type("RELAX NG", "http://relaxng.org/ns/structure/0.9"); 134 137 public final static Type RELAX_CORE = 138 new Type("RELAX Core", "http://www.xml.gr.jp/xmlns/relaxCore"); 139 142 public final static Type RELAX_NAMESPACE = 143 new Type("RELAX Namespace", 144 "http://www.xml.gr.jp/xmlns/relaxNamespace"); 145 148 public final static Type TREX = 149 new Type("TREX", "http://www.thaiopensource.com/trex"); 150 151 154 private final String uri; 155 156 159 private final Type type; 160 161 164 private final org.iso_relax.verifier.Schema compiledSchema; 165 166 178 private Schema(InputSource source, Type type) 179 throws JDOMException, IOException { 180 181 if ((source == null) || (type == null)) { 182 throw new IllegalArgumentException ("source/type/compiledSchema"); 183 } 184 this.uri = source.getSystemId(); 185 this.type = type; 186 187 try { 188 VerifierFactory vf = VerifierFactory.newInstance(type.getLanguage()); 189 190 this.compiledSchema = vf.compileSchema(source); 191 } 192 catch (IOException e) { 193 throw e; 194 } 195 catch (Exception e) { 196 throw new JDOMException("Failed to parse schema \"" + this.uri + 197 "\": " + e.getMessage(), e); 198 } 199 } 200 201 207 public String getURI() { 208 return this.uri; 209 } 210 211 216 public Type getType() { 217 return this.type; 218 } 219 220 229 private Verifier newVerifier() throws JDOMException { 230 try { 231 return this.compiledSchema.newVerifier(); 232 } 233 catch (VerifierConfigurationException e) { 234 throw new JDOMException( 235 "Failed to allocate schema verifier: " + e.getMessage(), e); 236 } 237 } 238 239 251 public List validate(Document doc) throws JDOMException { 252 ValidationErrorHandler errorHandler = new ValidationErrorHandler(); 253 try { 254 Verifier verifier = this.newVerifier(); 255 verifier.setErrorHandler(errorHandler); 256 257 errorHandler.setContentHandler(verifier.getVerifierHandler()); 258 new SAXOutputter(errorHandler).output(doc); 259 } 260 catch (SAXException e) { 261 } 262 263 return errorHandler.getErrors(); 265 } 266 267 279 public List validate(Element element) throws JDOMException { 280 ValidationErrorHandler errorHandler = new ValidationErrorHandler(); 281 try { 282 Verifier verifier = this.newVerifier(); 283 verifier.setErrorHandler(errorHandler); 284 285 List nodes = new ArrayList (); 286 nodes.add(element); 287 288 errorHandler.setContentHandler(verifier.getVerifierHandler()); 289 new SAXOutputter(errorHandler).output(nodes); 290 } 291 catch (SAXException e) { 292 } 293 294 return errorHandler.getErrors(); 296 } 297 298 313 public static Schema parse(String uri, Type type) 314 throws JDOMException, IOException { 315 return parse(new InputSource (uri), type); 316 } 317 318 336 public static Schema parse(InputStream byteStream, Type type, String uri) 337 throws JDOMException, IOException { 338 InputSource source = new InputSource (byteStream); 339 source.setSystemId(uri); 340 341 return parse(source, type); 342 } 343 344 362 public static Schema parse(Reader reader, Type type, String uri) 363 throws JDOMException, IOException { 364 InputSource source = new InputSource (reader); 365 source.setSystemId(uri); 366 367 return parse(source, type); 368 } 369 370 385 public static Schema parse(File file, Type type) 386 throws JDOMException, IOException { 387 InputSource source = new InputSource (new FileInputStream (file)); 388 source.setSystemId(file.getAbsolutePath()); 389 390 return parse(source, type); 391 } 392 393 409 public static Schema parse(InputSource source, Type type) 410 throws JDOMException, IOException { 411 return new Schema(source, type); 412 } 413 414 415 420 private static final class ValidationErrorHandler extends XMLFilterImpl { 421 422 private List errors = new LinkedList (); 423 424 private JDOMLocator locator = null; 425 426 430 public ValidationErrorHandler() { 431 super(); 432 } 433 434 440 public ValidationErrorHandler(XMLReader parent) { 441 super(parent); 442 } 443 444 451 public List getErrors() { 452 return (this.errors.size() == 0) ? null : this.errors; 453 } 454 455 461 private Object getCurrentNode() { 462 return (this.locator != null) ? this.locator.getNode() : null; 463 } 464 465 472 public void setDocumentLocator(Locator locator) { 473 if (locator instanceof JDOMLocator) { 474 this.locator = (JDOMLocator) locator; 475 } 476 } 477 478 488 public void fatalError(SAXParseException e) throws SAXException { 489 this.errors.add(new ValidationError(ValidationError.FATAL, 490 e.getMessage(), this.getCurrentNode())); 491 throw e; 492 } 493 494 504 public void error(SAXParseException e) throws SAXException { 505 this.errors.add(new ValidationError(ValidationError.ERROR, 506 e.getMessage(), this.getCurrentNode())); 507 } 508 509 519 public void warning(SAXParseException e) throws SAXException { 520 this.errors.add(new ValidationError(ValidationError.WARNING, 521 e.getMessage(), this.getCurrentNode())); 522 } 523 } 524 525 526 530 public static final class Type { 531 532 private final String name; 533 534 private final String language; 535 536 543 protected Type(String name, String language) { 544 this.name = name; 545 this.language = language; 546 } 547 548 553 public String getName() { 554 return this.name; 555 } 556 557 562 public String getLanguage() { 563 return this.language; 564 } 565 566 573 public int hashCode() { 574 return this.language.hashCode(); 575 } 576 577 585 public String toString() { 586 return this.language; 587 } 588 589 601 public boolean equals(Object o) { 602 return ((o == this) || 603 ((o != null) && (this.hashCode() == o.hashCode()) && 604 (this.getClass().getName().equals(o.getClass().getName())))); 605 } 606 } 607 } 608 609 | Popular Tags |