
import org.xml.sax.*;
import java.io.*;
import java.util.*;
import javax.xml.parsers.*;
import org.xml.sax.helpers.*;

public class SimpleSaxParser
extends 	DefaultHandler
{
	public void parse(File afile, String encoding)
	throws Exception
	{
		System.out.println("afile=" + afile.getAbsolutePath() + ", encoding=" + encoding);

		SAXParserFactory pf=SAXParserFactory.newInstance();
		pf.setValidating(false);
		pf.setNamespaceAware(true);

		SAXParser parser=pf.newSAXParser();

		InputStream fis = new FileInputStream(afile);
		InputSource is = null;

		if( encoding != null )
		{
			InputStreamReader ir = new InputStreamReader(fis, encoding);
			is = new InputSource(ir);
		}
		else
			is = new InputSource(fis);

//		InputStream fis = new FileInputStream(afile);
//		InputSource is = new InputSource(fis);

//		if( encoding != null )
//			is.setEncoding(encoding);

		parser.parse(is, this);

		fis.close();
	}


	public static void main(String[] args)
	{
		try
		{
			if( args.length <= 0 )
			{
				System.out.println("java SimpleSaxParser fileName encoding");
				System.exit(0);
			}

			String fname = args[0];
			String encoding = null;

			if( args.length >= 2 )
				encoding = args[1];

			InputStream is = new FileInputStream(fname);
			byte[] data = new byte[is.available()];
			is.read(data);
			is.close();

			if( encoding == null )
				System.out.println("content=" + new String(data));
			else
				System.out.println("content=" + new String(data,encoding));

			SimpleSaxParser ssp = new SimpleSaxParser();
			ssp.parse(new File(fname),encoding);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}

}

