|
29 | 29 | */ |
30 | 30 | package com.jcabi.xml; |
31 | 31 |
|
| 32 | +import com.jcabi.log.Logger; |
32 | 33 | import java.io.File; |
33 | 34 | import java.io.FileNotFoundException; |
34 | 35 | import java.io.IOException; |
35 | 36 | import java.io.InputStream; |
| 37 | +import java.io.StringReader; |
36 | 38 | import java.io.StringWriter; |
37 | 39 | import java.net.URI; |
38 | 40 | import java.net.URL; |
39 | 41 | import java.nio.file.Path; |
40 | 42 | import java.util.ArrayList; |
| 43 | +import java.util.Collection; |
41 | 44 | import java.util.Collections; |
42 | 45 | import java.util.List; |
| 46 | +import java.util.concurrent.CopyOnWriteArrayList; |
| 47 | +import javax.xml.XMLConstants; |
43 | 48 | import javax.xml.namespace.NamespaceContext; |
44 | 49 | import javax.xml.namespace.QName; |
45 | 50 | import javax.xml.parsers.DocumentBuilder; |
|
55 | 60 | import javax.xml.transform.dom.DOMResult; |
56 | 61 | import javax.xml.transform.dom.DOMSource; |
57 | 62 | import javax.xml.transform.stream.StreamResult; |
| 63 | +import javax.xml.transform.stream.StreamSource; |
| 64 | +import javax.xml.validation.Schema; |
| 65 | +import javax.xml.validation.SchemaFactory; |
| 66 | +import javax.xml.validation.Validator; |
58 | 67 | import javax.xml.xpath.XPath; |
59 | 68 | import javax.xml.xpath.XPathConstants; |
60 | 69 | import javax.xml.xpath.XPathExpressionException; |
61 | 70 | import javax.xml.xpath.XPathFactory; |
62 | 71 | import net.sf.saxon.xpath.XPathFactoryImpl; |
63 | 72 | import org.w3c.dom.Document; |
64 | | -import org.w3c.dom.Element; |
65 | 73 | import org.w3c.dom.Node; |
66 | 74 | import org.w3c.dom.NodeList; |
| 75 | +import org.xml.sax.ErrorHandler; |
| 76 | +import org.xml.sax.SAXException; |
| 77 | +import org.xml.sax.SAXParseException; |
67 | 78 |
|
68 | 79 | /** |
69 | 80 | * Implementation of {@link XML}. |
@@ -404,6 +415,62 @@ public XML merge(final NamespaceContext ctx) { |
404 | 415 | ); |
405 | 416 | } |
406 | 417 |
|
| 418 | + @Override |
| 419 | + public Collection<SAXParseException> validate(final XML xsd) { |
| 420 | + final Schema schema; |
| 421 | + try { |
| 422 | + schema = SchemaFactory |
| 423 | + .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI) |
| 424 | + .newSchema(new StreamSource(new StringReader(xsd.toString()))); |
| 425 | + } catch (final SAXException ex) { |
| 426 | + throw new IllegalStateException( |
| 427 | + String.format("Failed to create XSD schema from %s", xsd), |
| 428 | + ex |
| 429 | + ); |
| 430 | + } |
| 431 | + return this.validate(schema); |
| 432 | + } |
| 433 | + |
| 434 | + @Override |
| 435 | + public Collection<SAXParseException> validate() { |
| 436 | + final Schema schema; |
| 437 | + try { |
| 438 | + schema = SchemaFactory |
| 439 | + .newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI) |
| 440 | + .newSchema(); |
| 441 | + } catch (final SAXException ex) { |
| 442 | + throw new IllegalStateException( |
| 443 | + "Failed to create XSD schema", |
| 444 | + ex |
| 445 | + ); |
| 446 | + } |
| 447 | + return this.validate(schema); |
| 448 | + } |
| 449 | + |
| 450 | + /** |
| 451 | + * Validate against schema. |
| 452 | + * @param schema The XSD schema |
| 453 | + * @return List of errors |
| 454 | + */ |
| 455 | + public Collection<SAXParseException> validate(final Schema schema) { |
| 456 | + final Collection<SAXParseException> errors = |
| 457 | + new CopyOnWriteArrayList<>(); |
| 458 | + final Validator validator = schema.newValidator(); |
| 459 | + validator.setErrorHandler(new XMLDocument.ValidationHandler(errors)); |
| 460 | + try { |
| 461 | + validator.validate(new DOMSource(this.cache)); |
| 462 | + } catch (final SAXException | IOException ex) { |
| 463 | + throw new IllegalStateException(ex); |
| 464 | + } |
| 465 | + if (Logger.isDebugEnabled(this)) { |
| 466 | + Logger.debug( |
| 467 | + this, "%s detected %d error(s)", |
| 468 | + schema.getClass().getName(), errors.size() |
| 469 | + ); |
| 470 | + } |
| 471 | + return errors; |
| 472 | + } |
| 473 | + |
407 | 474 | /** |
408 | 475 | * Clones a node and imports it in a new document. |
409 | 476 | * @param node A node to clone. |
@@ -550,4 +617,39 @@ private static DocumentBuilderFactory configuredDFactory() { |
550 | 617 | factory.setNamespaceAware(true); |
551 | 618 | return factory; |
552 | 619 | } |
| 620 | + |
| 621 | + /** |
| 622 | + * Validation error handler. |
| 623 | + * |
| 624 | + * @since 0.1 |
| 625 | + */ |
| 626 | + static final class ValidationHandler implements ErrorHandler { |
| 627 | + /** |
| 628 | + * Errors. |
| 629 | + */ |
| 630 | + private final transient Collection<SAXParseException> errors; |
| 631 | + |
| 632 | + /** |
| 633 | + * Constructor. |
| 634 | + * @param errs Collection of errors |
| 635 | + */ |
| 636 | + ValidationHandler(final Collection<SAXParseException> errs) { |
| 637 | + this.errors = errs; |
| 638 | + } |
| 639 | + |
| 640 | + @Override |
| 641 | + public void warning(final SAXParseException error) { |
| 642 | + this.errors.add(error); |
| 643 | + } |
| 644 | + |
| 645 | + @Override |
| 646 | + public void error(final SAXParseException error) { |
| 647 | + this.errors.add(error); |
| 648 | + } |
| 649 | + |
| 650 | + @Override |
| 651 | + public void fatalError(final SAXParseException error) { |
| 652 | + this.errors.add(error); |
| 653 | + } |
| 654 | + } |
553 | 655 | } |
0 commit comments