package sample;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.PrintWriter;
import java.io.StringReader;
import java.io.StringWriter;

import org.cyberneko.html.HTMLConfiguration;
import org.cyberneko.html.filters.DefaultFilter;
import org.cyberneko.html.filters.Identity;
import org.cyberneko.html.filters.Writer;

import org.apache.xerces.util.XMLAttributesImpl;
import org.apache.xerces.xni.Augmentations;
import org.apache.xerces.xni.QName;
import org.apache.xerces.xni.XMLAttributes;
import org.apache.xerces.xni.XMLLocator;
import org.apache.xerces.xni.XMLString;
import org.apache.xerces.xni.XNIException;
import org.apache.xerces.xni.parser.XMLDocumentFilter;
import org.apache.xerces.xni.parser.XMLInputSource;

public class Script2 extends DefaultFilter {

	protected static final String AUGMENTATIONS =
		"http://cyberneko.org/html/features/augmentations";

	protected static final String FILTERS =
		"http://cyberneko.org/html/properties/filters";

	protected static final String SCRIPT_TYPE = "text/x-nekoscript";

	protected HTMLConfiguration fConfiguration;

	protected StringBuffer fBuffer;

	protected String fSystemId;

	protected int fScriptCount;

	public Script2(HTMLConfiguration config) {
		fConfiguration = config;
	}

	public void startDocument(
		XMLLocator locator,
		String encoding,
		Augmentations augs)
		throws XNIException {
		fBuffer = null;
		fSystemId = locator != null ? locator.getLiteralSystemId() : null;
		fScriptCount = 0;

		super.startDocument(locator, encoding, augs);
	}

	public void startElement(
		QName element,
		XMLAttributes attrs,
		Augmentations augs)
		throws XNIException {
		System.out.println("start element" + element.rawname);
		super.startElement(element, attrs, augs);
	}

	public void emptyElement(
		QName element,
		XMLAttributes attrs,
		Augmentations augs)
		throws XNIException {
		System.out.println("empty element" + element.rawname);

		super.emptyElement(element, attrs, augs);
	}

	public void characters(XMLString text, Augmentations augs)
		throws XNIException {
		System.out.println(
			"characters" + new String(text.ch, text.offset, text.length));

		super.characters(text, augs);

	}

	public void endElement(QName element, Augmentations augs)
		throws XNIException {
		System.out.println("end Element " + element.rawname);
		super.endElement(element, augs);
	}

	public static void main(String[] argv) throws Exception {
		HTMLConfiguration parser = new HTMLConfiguration();
		parser.setFeature(AUGMENTATIONS, true);
		XMLDocumentFilter[] filters =
			{ new Script2(parser), new Identity(), new Writer()};
		parser.setProperty(FILTERS, filters);
		for (int i = 0; i < argv.length; i++) {
			parser.parse(new XMLInputSource(null, argv[i], null));
		}
	}

}
