001: /*
002: * <copyright>
003: *
004: * Copyright 2004 BBNT Solutions, LLC
005: * under sponsorship of the Defense Advanced Research Projects
006: * Agency (DARPA).
007: *
008: * You can redistribute this software and/or modify it under the
009: * terms of the Cougaar Open Source License as published on the
010: * Cougaar Open Source Website (www.cougaar.org).
011: *
012: * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
013: * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
014: * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
015: * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
016: * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
017: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
018: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
019: * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
020: * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
021: * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
022: * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
023: *
024: * </copyright>
025: */
026: package org.cougaar.tools.csmart.util;
027:
028: import org.apache.xerces.parsers.DOMParser;
029: import org.w3c.dom.Node;
030: import org.w3c.dom.NodeList;
031: import org.xml.sax.ErrorHandler;
032: import org.xml.sax.SAXException;
033: import org.xml.sax.SAXNotRecognizedException;
034: import org.xml.sax.SAXNotSupportedException;
035: import org.xml.sax.SAXParseException;
036:
037: import java.io.IOException;
038:
039: // A Valdating DOM Application
040: // with registered Error Handlers
041: public class SchemaValidate implements ErrorHandler {
042:
043: // Constructor
044: public SchemaValidate(String xmlFile) {
045: // Create a Xerces DOM Parser
046: DOMParser parser = new DOMParser();
047:
048: // Turn Validation on
049: try {
050: parser.setFeature("http://xml.org/sax/features/validation",
051: true);
052: parser.setFeature(
053: "http://apache.org/xml/features/validation/schema",
054: true);
055: parser
056: .setFeature(
057: "http://apache.org/xml/features/validation/schema-full-checking",
058: true);
059:
060: } catch (SAXNotRecognizedException e) {
061: System.err.println(e);
062: } catch (SAXNotSupportedException e) {
063: System.err.println(e);
064: }
065:
066: // Register Error Handler
067: parser.setErrorHandler(this );
068:
069: // Parse the Document
070: // and traverse the DOM
071: try {
072: parser.parse(xmlFile);
073: // Document document = parser.getDocument();
074: // traverse (document);
075: } catch (SAXException e) {
076: System.err.println(e);
077: } catch (IOException e) {
078: System.err.println(e);
079: } catch (Exception e) {
080: System.err.println(e);
081: }
082:
083: }
084:
085: // Traverse DOM Tree. Print out Element Names
086: private void traverse(Node node) {
087: int type = node.getNodeType();
088: if (type == Node.ELEMENT_NODE)
089: System.out.println(node.getNodeName());
090: NodeList children = node.getChildNodes();
091: if (children != null) {
092: for (int i = 0; i < children.getLength(); i++)
093: traverse(children.item(i));
094: }
095: }
096:
097: // Warning Event Handler
098: public void warning(SAXParseException e) throws SAXException {
099: System.err.println("Warning: " + e);
100: }
101:
102: // Error Event Handler
103: public void error(SAXParseException e) throws SAXException {
104: System.err.println("Error: " + e);
105: }
106:
107: // Fatal Error Event Handler
108: public void fatalError(SAXParseException e) throws SAXException {
109: System.err.println("Fatal Error: " + e);
110: }
111:
112: // Main Method
113: public static void main(String[] args) {
114: SchemaValidate validatingDOM = new SchemaValidate(args[0]);
115: }
116: }
|