/*
 * Simple parser written by Brion Swanson
 *
 * This parser very simply does a SAX-type parse on an XML file optionally
 * validating that file against the DTD specified in the DOCTYPE tag of the
 * XML file.  This class produces a one-line description of the exit status
 * of the parse either the document is well-formed, valid, or a combination
 * of the two.
 *
 */

import org.xml.sax.*;
import org.xml.sax.helpers.DefaultHandler;
import org.apache.xerces.parsers.SAXParser;
import java.io.*;

class SimpleParser extends DefaultHandler {
	 boolean isValid;
	 boolean isWellFormed;
	 SAXParser parser;

	 public SimpleParser () {
		  parser = new SAXParser ();
		  parser.setErrorHandler (this);
		  isValid = true;
		  isWellFormed = true;
	 }

	 public static void main (String args[]) {
		  SimpleParser sp = new SimpleParser ();
		  String filename = "";

		  if (args.length < 1 || args.length > 2) {
				sp.displayUsage ();
		  } else {
				filename = args[0];

				if (args.length == 2 && args[0].equals ("-v")) {
					 sp.setValidation (true);
					 filename = args[1];
				}
				try {
					 sp.parse (filename);
					 System.out.print ("\nDocument is");

					 if (sp.isWellFormedXML ()) {
						  System.out.print (" well-formed;");
					 } else {
						  System.out.print (" not well-formed;");
					 }

					 if (sp.isValidXML () && args.length == 2) {
						  System.out.print (" valid;");
					 } else if (args.length == 2) {	
						  System.out.print (" not valid;");
					 }

					 System.out.println ("");

				} catch (SAXException e) {
					 e.printStackTrace ();
					 System.out.println ("Document is not well-formed;");
				}
		  }
	 }

	 public void parse (String file) throws SAXException {
		  try {
				parser.parse (file);
		  } catch (IOException e) {
				System.err.println ("invalid file");
				System.exit (1);
		  }
	 }

	 public void setValidation (boolean value) {
		  try {
				parser.setFeature ("http://xml.org/sax/features/validation", 
										 value);
		  } catch (SAXNotRecognizedException ex) {
				System.err.println ("ERROR: SAX Not Recognized");
				// do nothing - continue without validation
		  } catch (SAXNotSupportedException ex) {
				System.err.println ("ERROR: SAX Not Supported");
				// do nothing - continue without validation
		  }
	 }

	 public boolean isValidXML () {
		  return isValid;
	 }

	 public boolean isWellFormedXML () {
		  return isWellFormed;
	 }

	 private void displayUsage () {
		  System.err.println ("Usage: SimpleParser [-v] XMLFILE");
		  System.err.println ("Where:\tv - turn on validation");
		  System.err.println ("\tXMLFILE - XML file to test for "+
									 "well-formedness");
	 }

/*****************************************************************************
 * DefaultHandler overrides
 ****************************************************************************/

	 public void error (SAXParseException e) throws SAXException {
		  isValid = false;
		  System.err.print ("Error: ");
		  System.err.println (e.getMessage ());
	 }

	 public void fatalError (SAXParseException e) throws SAXException {
		  isWellFormed = false;
		  isValid = false;
		  throw new SAXParseException (e.getMessage (), null);
	 }
}
