001package edu.pdx.cs410J.xml;
002
003import java.io.*;
004import javax.xml.parsers.*;
005import org.xml.sax.*;
006import org.xml.sax.helpers.DefaultHandler;
007
008/**
009 * This program parses and XML file and validates it for correctness
010 * against its DTD.
011 */
012public class ValidateXml extends DefaultHandler {
013
014  private static PrintStream err = System.err;
015
016  public void error(SAXParseException ex) {
017    err.println("ERROR at (" + ex.getLineNumber() + ":" +
018                ex.getColumnNumber() + "): " + ex.getMessage());
019  }
020
021  public void fatalError(SAXParseException ex) {
022    err.println("FATAL at (" + ex.getLineNumber() + ":" +
023                ex.getColumnNumber() + "): " + ex.getMessage());
024  }
025
026  public void warning(SAXParseException ex) {
027    err.println("WARNING at (" + ex.getLineNumber() + ":" +
028                ex.getColumnNumber() + "): " + ex.getMessage());
029  }
030
031  public static void main(String[] args) {
032    try {
033      SAXParserFactory factory = SAXParserFactory.newInstance();
034      factory.setValidating(true);
035
036      SAXParser parser = factory.newSAXParser();
037      parser.parse(new File(args[0]), new ValidateXml());
038
039    } catch (ParserConfigurationException ex) {
040      ex.printStackTrace(System.err);
041
042    } catch (SAXException ex) {
043      err.println("Parsing exception: " + ex);
044      
045    } catch (IOException ex) {
046      err.println("IOException: " + ex);
047    }
048  }
049}