KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > knowgate > dataxslt > SAXValidate


1 /*
2   Copyright (C) 2003 Know Gate S.L. All rights reserved.
3                       C/Oņa, 107 1š2 28050 Madrid (Spain)
4
5   Redistribution and use in source and binary forms, with or without
6   modification, are permitted provided that the following conditions
7   are met:
8
9   1. Redistributions of source code must retain the above copyright
10      notice, this list of conditions and the following disclaimer.
11
12   2. The end-user documentation included with the redistribution,
13      if any, must include the following acknowledgment:
14      "This product includes software parts from hipergate
15      (http://www.hipergate.org/)."
16      Alternately, this acknowledgment may appear in the software itself,
17      if and wherever such third-party acknowledgments normally appear.
18
19   3. The name hipergate must not be used to endorse or promote products
20      derived from this software without prior written permission.
21      Products derived from this software may not be called hipergate,
22      nor may hipergate appear in their name, without prior written
23      permission.
24
25   This library is distributed in the hope that it will be useful,
26   but WITHOUT ANY WARRANTY; without even the implied warranty of
27   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
28
29   You should have received a copy of hipergate License with this code;
30   if not, visit http://www.hipergate.org or mail to info@hipergate.org
31 */

32
33 package com.knowgate.dataxslt;
34
35 import java.io.*;
36
37 import org.xml.sax.ContentHandler JavaDoc;
38 import org.xml.sax.ErrorHandler JavaDoc;
39 import org.xml.sax.helpers.DefaultHandler JavaDoc;
40 import org.xml.sax.helpers.XMLReaderFactory JavaDoc;
41 import org.xml.sax.InputSource JavaDoc;
42 import org.xml.sax.SAXException JavaDoc;
43 import org.xml.sax.SAXParseException JavaDoc;
44 import org.xml.sax.XMLReader JavaDoc;
45
46 /**
47  * SAXValidate Class.
48  * XML Document Parser Validator.
49  * @author Carlos Enrique Navarro Candil
50  * @version 1.0
51  */

52
53 public class SAXValidate extends DefaultHandler JavaDoc
54                         implements ErrorHandler JavaDoc {
55
56    private static final String JavaDoc
57            DEFAULT_PARSER = "org.apache.xerces.parsers.SAXParser";
58    private boolean schemavalidate = false;
59
60    /**
61     * Construye una instancia de la clase handler
62     */

63    public SAXValidate(boolean validateschema) {
64      this.schemavalidate = validateschema;
65    }
66
67    public void error (SAXParseException JavaDoc exception) throws SAXException JavaDoc {
68      System.out.println("ERROR: " + exception.getMessage());
69    }
70
71   /**
72    * Rutina principal para probar la utilidad SAXValidate.
73    */

74   static public void main(String JavaDoc[] args) {
75
76     if (args.length < 1 || args.length > 2) {
77       System.err.println("USO: java SAXValidate [-s] <xmlfile>");
78     } else {
79       boolean svalidate = false;
80       String JavaDoc filename = "";
81
82       if (args.length > 1) {
83         if (args[0].equals("-s")) {
84           svalidate = true;
85         }
86         filename = args[1];
87       } else {
88         filename = args[0];
89       }
90
91       SAXValidate test = new SAXValidate(svalidate);
92
93       try {
94         test.runTest(new FileReader(new File(filename).toString()),
95                    DEFAULT_PARSER);
96       } catch (Exception JavaDoc e) {
97         System.err.println("Error running test.");
98         System.err.println(e.getMessage());
99         e.printStackTrace(System.err);
100       }
101     }
102   }
103
104   /**
105    * Ejecuta el test
106    *
107    * @param xml stream xml que se quiere parsear
108    * @param parserName nombre de una clase parser "SAX2 compliant"
109    */

110
111   public void runTest(Reader JavaDoc xml, String JavaDoc parserName)
112                   throws IOException, ClassNotFoundException JavaDoc {
113
114     try {
115
116       // Obtiene una instancia del parser
117
XMLReader JavaDoc parser = XMLReaderFactory.createXMLReader(parserName);
118
119       // Configura los manejadores en el parser
120
parser.setContentHandler((ContentHandler JavaDoc)this);
121       parser.setErrorHandler((ErrorHandler JavaDoc)this);
122
123       parser.setFeature("http://xml.org/sax/features/validation", true);
124       if (schemavalidate) {
125         parser.setFeature("http://apache.org/xml/features/validation/schema", true);
126       }
127
128       // Parsea el documento
129
parser.parse(new InputSource JavaDoc(xml));
130
131     } catch (SAXParseException JavaDoc e) {
132       System.err.println(e.getMessage());
133     } catch (SAXException JavaDoc e) {
134       System.err.println(e.getMessage());
135     } catch (Exception JavaDoc e) {
136       System.err.println(e.toString());
137     }
138   }
139 }
140
Popular Tags