1 /***
2 * Validator.java
3 *
4 * @author Created by Omnicore CodeGuide
5 */
6
7 package org.astrogrid.io.xml;
8
9 /*** Just checks to see if the given XML file is valid, throwing exceptions as usual if not */
10 import java.io.*;
11 import org.xml.sax.*;
12
13 import javax.xml.parsers.ParserConfigurationException;
14 import javax.xml.parsers.SAXParser;
15 import javax.xml.parsers.SAXParserFactory;
16 import org.xml.sax.helpers.ParserAdapter;
17
18 public class Validator implements ContentHandler, ErrorHandler {
19
20
21 PrintWriter out = null;
22
23 public Validator(Writer givenOut) {
24 this.out = new PrintWriter(givenOut);
25 }
26
27 public void skippedEntity(String name) throws SAXException {
28 }
29
30 /***
31 * Begin the scope of a prefix-URI Namespace mapping.
32 */
33 public void startPrefixMapping(String prefix, String uri) throws SAXException {
34
35 }
36
37 /***
38 * End the scope of a prefix-URI mapping.
39 */
40 public void endPrefixMapping(String prefix) throws SAXException {
41
42 }
43
44 /***
45 * Receive notification of the beginning of an element.
46 */
47 public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
48 out.print("Element: <"+qName);
49 for (int i = 0; i < atts.getLength(); i++) {
50 out.print(" "+atts.getQName(i)+"='"+atts.getValue(i)+"' ");
51 }
52 out.println(">");
53 }
54
55 /***
56 * Receive notification of the beginning of a document.
57 */
58 public void startDocument() throws SAXException {
59 }
60
61 /***
62 * Receive notification of a processing instruction.
63 */
64 public void processingInstruction(String target, String data) throws SAXException {
65 }
66
67 /***
68 * Receive notification of character data.
69 */
70 public void characters(char[] ch, int start, int length) throws SAXException {
71 if (length>0) {
72 out.println("String: '"+new String(ch, start, length)+"'");
73 }
74 }
75
76 /***
77 * Receive notification of the end of a document.
78 */
79 public void endDocument() throws SAXException {
80 out.println("--- COMPLETE! ----");
81 }
82
83 /***
84 * Receive notification of a recoverable error.
85 */
86 public void error(SAXParseException exception) throws SAXException {
87 out.println("ERROR: "+exception);
88 }
89
90 /***
91 * Receive notification of the end of an element.
92 */
93 public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
94
95 }
96
97 /***
98 * Receive notification of a warning.
99 */
100 public void warning(SAXParseException exception) throws SAXException {
101 out.println("WARNING: "+exception);
102 }
103
104 /***
105 * Receive notification of a non-recoverable error.
106 */
107 public void fatalError(SAXParseException exception) throws SAXException {
108 out.println("FATAL: "+exception);
109 }
110
111 /***
112 * Receive notification of ignorable whitespace in element content.
113 */
114 public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
115 }
116
117 /***
118 * Receive an object for locating the origin of SAX document events.
119 */
120 public void setDocumentLocator(Locator locator) {
121 }
122
123 /*** A convenience routine for validating any given filename, output going to the given Writer */
124 public static void validate(InputStream source, Writer out) throws ParserConfigurationException, SAXException, IOException {
125 Validator v = new Validator(out);
126
127 XMLReader parser;
128 SAXParserFactory spf = SAXParserFactory.newInstance();
129 spf.setValidating(true);
130 SAXParser sp = spf.newSAXParser();
131 parser = new ParserAdapter(sp.getParser());
132
133 parser.setContentHandler(v);
134 parser.setErrorHandler(v);
135 parser.parse(new InputSource(source));
136
137 out.flush();
138 }
139
140 /***
141 *
142 */
143 public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException {
144
145 validate(new FileInputStream("/home/mch/tmp/pal-ssa.metadata.xml"), new OutputStreamWriter(System.out));
146
147 }
148 }
149