001: /*
002: ******************************************************************
003: Copyright (c) 2007, Jeff Martin, Tim Bacon
004: All rights reserved.
005:
006: Redistribution and use in source and binary forms, with or without
007: modification, are permitted provided that the following conditions
008: are met:
009:
010: * Redistributions of source code must retain the above copyright
011: notice, this list of conditions and the following disclaimer.
012: * Redistributions in binary form must reproduce the above
013: copyright notice, this list of conditions and the following
014: disclaimer in the documentation and/or other materials provided
015: with the distribution.
016: * Neither the name of the xmlunit.sourceforge.net nor the names
017: of its contributors may be used to endorse or promote products
018: derived from this software without specific prior written
019: permission.
020:
021: THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
022: "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
023: LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
024: FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
025: COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
026: INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
027: BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
028: LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
029: CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
030: LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
031: ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
032: POSSIBILITY OF SUCH DAMAGE.
033:
034: ******************************************************************
035: */
036:
037: package org.custommonkey.xmlunit.examples;
038:
039: import java.io.*;
040: import org.custommonkey.xmlunit.*;
041: import org.xml.sax.*;
042:
043: /**
044: * Code from "Validating XML Documents" section of User's Guide
045: */
046: public class ValidatingXMLDocuments {
047:
048: private String myXmlDocument = null;
049: private String myDTD = null;
050:
051: private void ValidatingAgainstTheDTDDefinedInDOCTYPE()
052: throws Exception {
053: InputSource is = new InputSource(new FileInputStream(
054: myXmlDocument));
055: Validator v = new Validator(is);
056: boolean isValid = v.isValid();
057: }
058:
059: private void ValidatingAPieceOfXMLThatDoesntContainADOCTYPE()
060: throws Exception {
061: String myPublicId = null;
062:
063: InputSource is = new InputSource(new FileInputStream(
064: myXmlDocument));
065: Validator v = new Validator(is, (new File(myDTD)).toURI()
066: .toURL().toString(), myPublicId);
067: boolean isValid = v.isValid();
068: }
069:
070: private void ValidatingAgainstALocalDTD() throws Exception {
071: InputSource is = new InputSource(new FileInputStream(
072: myXmlDocument));
073: Validator v = new Validator(is, (new File(myDTD)).toURI()
074: .toURL().toString());
075: boolean isValid = v.isValid();
076: }
077:
078: private void ValidatingAgainstDTDUsingApachesXMLResolverAndAXMLCatalog()
079: throws Exception {
080: InputSource is = new InputSource(new FileInputStream(
081: myXmlDocument));
082: XMLUnit.setControlEntityResolver(new CatalogResolver());
083: Validator v = new Validator(is);
084: boolean isValid = v.isValid();
085: }
086:
087: private void ValidatingAgainstALocalXMLSchema() throws Exception {
088: String myXmlSchemaFile = null;
089:
090: InputSource is = new InputSource(new FileInputStream(
091: myXmlDocument));
092: Validator v = new Validator(is);
093: v.useXMLSchema(true);
094: v.setJAXP12SchemaSource(new File(myXmlSchemaFile));
095: boolean isValid = v.isValid();
096: }
097:
098: private static class CatalogResolver implements EntityResolver {
099: public InputSource resolveEntity(String p, String s) {
100: return null;
101: }
102: }
103: }
|