001: /*
002: Copyright (C) 2003 Know Gate S.L. All rights reserved.
003: C/Oņa, 107 1š2 28050 Madrid (Spain)
004:
005: Redistribution and use in source and binary forms, with or without
006: modification, are permitted provided that the following conditions
007: are met:
008:
009: 1. Redistributions of source code must retain the above copyright
010: notice, this list of conditions and the following disclaimer.
011:
012: 2. The end-user documentation included with the redistribution,
013: if any, must include the following acknowledgment:
014: "This product includes software parts from hipergate
015: (http://www.hipergate.org/)."
016: Alternately, this acknowledgment may appear in the software itself,
017: if and wherever such third-party acknowledgments normally appear.
018:
019: 3. The name hipergate must not be used to endorse or promote products
020: derived from this software without prior written permission.
021: Products derived from this software may not be called hipergate,
022: nor may hipergate appear in their name, without prior written
023: permission.
024:
025: This library is distributed in the hope that it will be useful,
026: but WITHOUT ANY WARRANTY; without even the implied warranty of
027: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
028:
029: You should have received a copy of hipergate License with this code;
030: if not, visit http://www.hipergate.org or mail to info@hipergate.org
031: */
032:
033: package com.knowgate.dataxslt;
034:
035: import java.io.*;
036:
037: import org.xml.sax.ContentHandler;
038: import org.xml.sax.ErrorHandler;
039: import org.xml.sax.helpers.DefaultHandler;
040: import org.xml.sax.helpers.XMLReaderFactory;
041: import org.xml.sax.InputSource;
042: import org.xml.sax.SAXException;
043: import org.xml.sax.SAXParseException;
044: import org.xml.sax.XMLReader;
045:
046: /**
047: * SAXValidate Class.
048: * XML Document Parser Validator.
049: * @author Carlos Enrique Navarro Candil
050: * @version 1.0
051: */
052:
053: public class SAXValidate extends DefaultHandler implements ErrorHandler {
054:
055: private static final String DEFAULT_PARSER = "org.apache.xerces.parsers.SAXParser";
056: private boolean schemavalidate = false;
057:
058: /**
059: * Construye una instancia de la clase handler
060: */
061: public SAXValidate(boolean validateschema) {
062: this .schemavalidate = validateschema;
063: }
064:
065: public void error(SAXParseException exception) throws SAXException {
066: System.out.println("ERROR: " + exception.getMessage());
067: }
068:
069: /**
070: * Rutina principal para probar la utilidad SAXValidate.
071: */
072: static public void main(String[] args) {
073:
074: if (args.length < 1 || args.length > 2) {
075: System.err.println("USO: java SAXValidate [-s] <xmlfile>");
076: } else {
077: boolean svalidate = false;
078: String filename = "";
079:
080: if (args.length > 1) {
081: if (args[0].equals("-s")) {
082: svalidate = true;
083: }
084: filename = args[1];
085: } else {
086: filename = args[0];
087: }
088:
089: SAXValidate test = new SAXValidate(svalidate);
090:
091: try {
092: test.runTest(new FileReader(new File(filename)
093: .toString()), DEFAULT_PARSER);
094: } catch (Exception e) {
095: System.err.println("Error running test.");
096: System.err.println(e.getMessage());
097: e.printStackTrace(System.err);
098: }
099: }
100: }
101:
102: /**
103: * Ejecuta el test
104: *
105: * @param xml stream xml que se quiere parsear
106: * @param parserName nombre de una clase parser "SAX2 compliant"
107: */
108:
109: public void runTest(Reader xml, String parserName)
110: throws IOException, ClassNotFoundException {
111:
112: try {
113:
114: // Obtiene una instancia del parser
115: XMLReader parser = XMLReaderFactory
116: .createXMLReader(parserName);
117:
118: // Configura los manejadores en el parser
119: parser.setContentHandler((ContentHandler) this );
120: parser.setErrorHandler((ErrorHandler) this );
121:
122: parser.setFeature("http://xml.org/sax/features/validation",
123: true);
124: if (schemavalidate) {
125: parser
126: .setFeature(
127: "http://apache.org/xml/features/validation/schema",
128: true);
129: }
130:
131: // Parsea el documento
132: parser.parse(new InputSource(xml));
133:
134: } catch (SAXParseException e) {
135: System.err.println(e.getMessage());
136: } catch (SAXException e) {
137: System.err.println(e.getMessage());
138: } catch (Exception e) {
139: System.err.println(e.toString());
140: }
141: }
142: }
|