1 /* 2 * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 */ 25 26 /** 27 * <p> 28 * Provides an API for validation of XML documents. <em>Validation</em> is the 29 * process of verifying that an XML document is an instance of a specified XML 30 * <em>schema</em>. An XML schema defines the content model (also called a 31 * <em>grammar</em> or <em>vocabulary</em>) that its instance documents will 32 * represent. 33 * 34 * <p> 35 * There are a number of popular technologies available for creating an XML schema. 36 * Some of the most popular ones include: 37 * 38 * <ul> 39 * <li><strong>Document Type Definition (DTD)</strong> 40 * - XML's built-in schema language. 41 * </li> 42 * <li><strong><a href="http://www.w3.org/XML/Schema">W3C XML Schema (WXS)</a></strong> - 43 * an object-oriented XML schema language. WXS also provides a type system 44 * for constraining the character data of an XML document. WXS is maintained 45 * by the <a href="http://www.w3.org">World Wide Web Consortium (W3C)</a> 46 * and is a W3C Recommendation (that is, a ratified W3C standard specification). 47 * </li> 48 * <li><strong><a href="http://www.relaxng.org">RELAX NG (RNG)</a></strong> - 49 * a pattern-based, user-friendly XML schema language. RNG schemas may 50 * also use types to constrain XML character data. RNG is maintained by 51 * the <a href="http://www.oasis-open.org">Organization for the Advancement 52 * of Structured Information Standards (OASIS)</a> and is both an OASIS 53 * and an <a href="http://www.iso.org">ISO (International Organization 54 * for Standardization)</a> standard. 55 * </li> 56 * <li><strong><a href="http://www.schematron.com/">Schematron</a></strong> - 57 * a rules-based XML schema language. Whereas DTD, WXS, and RNG are designed 58 * to express the structure of a content model, Schematron is designed to 59 * enforce individual rules that are difficult or impossible to express 60 * with other schema languages. Schematron is intended to supplement a 61 * schema written in structural schema language such as the aforementioned. 62 * Schematron is in the process of becoming an ISO standard. 63 * </li> 64 * </ul> 65 * <p> 66 * While JAXP supports validation as a feature of an XML parser, represented by 67 * either a {@link javax.xml.parsers.SAXParser} or {@link javax.xml.parsers.DocumentBuilder} 68 * instance, the {@code Validation} API is more preferable. 69 * 70 * <p> 71 * The JAXP validation API decouples the validation of an instance document from 72 * the parsing of an XML document. This is advantageous for several reasons, 73 * some of which are: 74 * 75 * <ul> 76 * <li><strong>Support for additional schema langauges.</strong> 77 * The JAXP parser implementations support only a subset of the available 78 * XML schema languages. The Validation API provides a standard mechanism 79 * through which applications may take of advantage of specialization 80 * validation libraries which support additional schema languages. 81 * </li> 82 * <li><strong>Easy runtime coupling of an XML instance and schema.</strong> 83 * Specifying the location of a schema to use for validation with JAXP 84 * parsers can be confusing. The Validation API makes this process simple 85 * (see <a href="#example-1">example</a> below). 86 * </li> 87 * </ul> 88 * <p> 89 * <a id="example-1"><strong>Usage example</strong>.</a> The following example 90 * demonstrates validating an XML document with the Validation API 91 * (for readability, some exception handling is not shown): 92 * 93 * <pre> 94 * 95 * // parse an XML document into a DOM tree 96 * DocumentBuilder parser = DocumentBuilderFactory.newInstance().newDocumentBuilder(); 97 * Document document = parser.parse(new File("instance.xml")); 98 * 99 * // create a SchemaFactory capable of understanding WXS schemas 100 * SchemaFactory factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI); 101 * 102 * // load a WXS schema, represented by a Schema instance 103 * Source schemaFile = new StreamSource(new File("mySchema.xsd")); 104 * Schema schema = factory.newSchema(schemaFile); 105 * 106 * // create a Validator instance, which can be used to validate an instance document 107 * Validator validator = schema.newValidator(); 108 * 109 * // validate the DOM tree 110 * try { 111 * validator.validate(new DOMSource(document)); 112 * } catch (SAXException e) { 113 * // instance document is invalid! 114 * } 115 * </pre> 116 * <p> 117 * The JAXP parsing API has been integrated with the Validation API. Applications 118 * may create a {@link javax.xml.validation.Schema} with the validation API 119 * and associate it with a {@link javax.xml.parsers.DocumentBuilderFactory} or 120 * a {@link javax.xml.parsers.SAXParserFactory} instance by using the 121 * {@link javax.xml.parsers.DocumentBuilderFactory#setSchema(Schema)} and 122 * {@link javax.xml.parsers.SAXParserFactory#setSchema(Schema)} methods. 123 * <strong>You should not</strong> both set a schema and call <code>setValidating(true)</code> 124 * on a parser factory. The former technique will cause parsers to use the new 125 * validation API; the latter will cause parsers to use their own internal validation 126 * facilities. <strong>Turning on both of these options simultaneously will cause 127 * either redundant behavior or error conditions.</strong> 128 * 129 * 130 * @since 1.5 131 */ 132 133 package javax.xml.validation;