package test;
/**
 * Title:
 * Description:
 * Copyright:    Copyright (c) 2001
 * Company:
 * @author Adam Najmanowicz
 * @version 1.0
 */
import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import java.io.RandomAccessFile;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import org.xml.sax.ext.DeclHandler;
import org.xml.sax.ext.LexicalHandler;
import org.apache.xerces.parsers.DOMParser;
import org.apache.xml.serialize.BaseMarkupSerializer;
import org.apache.xml.serialize.OutputFormat;
import org.w3c.dom.Node;

public class SimpleDTDLister implements DeclHandler, LexicalHandler{

	StringWriter sWriter;
	OutputFormat oFormat;

	static String sDTDContent = null;

	/**
	 * DeclHandler interface - implementation
	 */

	public void elementDecl(String name, String model) {//throws SAXException {
		System.out.println("elementDecl ->  <" + name + ">" );
	}
 	public void attributeDecl(String elementName, String attributeName, String type, String mode,
							  String defaultValue) {//throws SAXException {
	}
	public void internalEntityDecl(String name, String value) {//throws SAXException {
		if(name.charAt(0) != '%'){
			System.out.println("internalEntityDecl ->  &" + name + "; = \"" + value + "\"");
		}
	}
	public void externalEntityDecl(String name, String publicID,
								   String systemID) {//throws SAXException {
		System.out.println("externalEntityDecl ->  <" + name + "> ");
	}

	/**
	 * LexicalHandler interface - implementation
	 */
	public void startDTD(String name, String publicId, String systemId) throws SAXException {
		//System.out.println("startDTD(name=\"" + name + "\", publicId=\"" + publicId
		//				   + "\", systemId=\"" + systemId + "\");");
	}
	public void endDTD() throws SAXException {
		//System.out.println("endDTD()");
	}

	public void startEntity(String name) throws SAXException {
		//System.out.println("startEntity(name = \"" + name + "\")");
	}

	public void endEntity(String name) throws SAXException {
		//System.out.println("endEntity(name = \"" + name + "\")");
	}

	public void startCDATA() throws SAXException {
		//System.out.println("Method startCDATA() not yet implemented.");
	}

	public void endCDATA() throws SAXException {
		//System.out.println("Method endCDATA() not yet implemented.");
	}

	public void comment(char[] ch, int start, int length) throws SAXException {
		System.out.println("comment: " + new String(ch));
	}

	/**
	 * the test code
	 */
	public static void main(String[] args) {
		if (args.length <= 0) {
			System.out.println("Usage: java EntityLister URL");

			return;
		}

		String  document = args[0];

		try {
			RandomAccessFile reader = new RandomAccessFile(args[0], "r");
			byte[] dtdContent = new byte[(int)reader.length()];
			reader.read(dtdContent, 0, dtdContent.length);
			sDTDContent = new String(dtdContent);

			XMLReader parser = XMLReaderFactory.createXMLReader("org.apache.xerces.parsers.SAXParser");

			SimpleDTDLister simpleDTDLister = new SimpleDTDLister();

			// I want my own declaration handler to retrieve elements and entities
			parser.setProperty("http://xml.org/sax/properties/declaration-handler", simpleDTDLister);

			// I want my own lexical handler to retrieve comments
			parser.setProperty("http://xml.org/sax/properties/lexical-handler",simpleDTDLister);

			parser.parse(new InputSource(new StringReader("<?xml version=\"1.0\"?><!DOCTYPE p SYSTEM \"" + document + "\"><p/>")));
		}catch (Exception e) {
			System.out.println("Could not read document because " + e.getMessage());
		}
	}

}
