import java.net.Socket;
import java.io.InputStream;

import org.apache.xerces.parsers.SAXParser;
import org.apache.xerces.framework.XMLErrorReporter;
import org.apache.xerces.readers.DefaultReaderFactory;
import org.apache.xerces.readers.XMLEntityHandler;
import org.apache.xerces.utils.StringPool;
import org.xml.sax.DocumentHandler;
import org.xml.sax.InputSource;
import org.xml.sax.HandlerBase;
import org.xml.sax.SAXException;
import org.xml.sax.AttributeList;
import java.io.InputStreamReader;
import java.io.Reader;

public class StreamingSAXClient extends HandlerBase {
 	public void startDocument() throws SAXException {
		System.out.println("Start of document");
	}
	public void endDocument() throws SAXException {
		System.out.println("End of document");
	}
	public void startElement(String name, AttributeList attributes) throws SAXException {
		System.out.println("Saw start of "+name);
	}
	public void endElement(String name) throws SAXException {
		System.out.println("Saw end of "+name);
	}

	public static void main (String args[]) {
		try {
			Socket s = new Socket("localhost",6400);
			InputStream in = s.getInputStream();

			SAXParser p = new SAXParser();
			DocumentHandler h = new StreamingSAXClient();
            p.setReaderFactory(((StreamingSAXClient)h).new StreamingCharFactory());
			p.setDocumentHandler(h);

            p.parse(new InputSource(new InputStreamReader(in)));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
    
    public class StreamingCharFactory extends org.apache.xerces.readers.DefaultReaderFactory {
        /**
         * Create an entity reader for a character stream.
         *
         * @param enityHandler The entity handler.
         * @param errorReporter The error reporter.
         * @param sendCharDataAsCharArray true if char data should be reported using
         *                                char arrays instead of string handles.
         * @param reader The character stream.
         * @param stringPool The string pool.
         * @return The reader that will process the character data.
         * @exception java.lang.Exception
         */
        public XMLEntityHandler.EntityReader createCharReader(XMLEntityHandler entityHandler,
                                                              XMLErrorReporter errorReporter,
                                                              boolean sendCharDataAsCharArray,
                                                              Reader reader,
                                                              StringPool stringPool) throws Exception
                {
                    System.out.println("my factory");
                    return new org.apache.xerces.readers.StreamingCharReader(entityHandler, errorReporter, sendCharDataAsCharArray, reader, stringPool);
                }

        /**
         * Create an entity reader for a byte stream encoded in UTF-8.
         *
         * @param enityHandler The entity handler.
         * @param errorReporter The error reporter.
         * @param sendCharDataAsCharArray true if char data should be reported using
         *                                char arrays instead of string handles.
         * @param data The byte stream.
         * @param stringPool The string pool.
         * @return The reader that will process the UTF-8 data.
         * @exception java.lang.Exception
         */
        public XMLEntityHandler.EntityReader createUTF8Reader(XMLEntityHandler entityHandler,
                                                              XMLErrorReporter errorReporter,
                                                              boolean sendCharDataAsCharArray,
                                                              InputStream data,
                                                              StringPool stringPool) throws Exception
                {
                    System.out.println("my factory");
                    XMLEntityHandler.EntityReader reader;
                    reader = new org.apache.xerces.readers.StreamingCharReader(entityHandler, errorReporter, sendCharDataAsCharArray, new InputStreamReader(data, "UTF8"), stringPool);
                    return reader;
                }

    }

}
