import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.SAXException;
import org.xml.sax.Attributes;
import org.xml.sax.SAXParseException;
/**
 * Insert the type's description here.
 * Creation date: (21/11/2001 18:18:33)
 * @author: Administrator
 */
public class MyEventHandler extends MyErrorHandler
{
	private PrintWriter out;
	private String encoding;
/**
 * Insert the method's description here.
 * Creation date: (21/11/2001 18:34:31)
 */
public MyEventHandler()
{
	super();
	setOut(new PrintWriter(new OutputStreamWriter(System.out)));
	setEncoding("UTF-8");
}
public MyEventHandler(PrintWriter out, String encoding)
{
	super();
	setOut(out);
	setEncoding(encoding);
}
public void characters(char[] ch, int start, int length) throws SAXException
{
	/*
	Selon la documentation de l'API SAX, une application utilisant
	un parseur SAX ne doit pas essayer de lire le contenu du tableau
	en dehors de l'intervalle spécifié.
	Ainsi, on construit tout d'abord un objet de type String contenant
	uniquement les charactères que l'on cherche à récupérer
	*/
	out.print(normalize(new String(ch, start, length)));
	
	/*
	Si on n'utilise pas la méthode flush() sur l'objet out de type
	PrintWriter, rien n'apparaît à la console.
	*/
	out.flush();
}
public void endElement(String uri, String lName, String qName)
{
	out.print("</");
	out.print(qName);
	out.print('>');
	out.flush();
}
/**
 * Insert the method's description here.
 * Creation date: (22/11/2001 16:03:20)
 * @return java.lang.String
 */
public java.lang.String getEncoding() {
	return encoding;
}
/**
 * Insert the method's description here.
 * Creation date: (22/11/2001 11:11:20)
 * @return java.io.PrintWriter
 */
public java.io.PrintWriter getOut()
{
	return out;
}
public void ignorableWhitespace(char ch[], int start, int length) throws SAXException
{
	characters(ch, start, length);
}
private String normalize(String s)
{
	StringBuffer str = new StringBuffer();

	int len = (s != null) ? s.length() : 0;
	for (int i = 0; i < len; i++)
	{
		char ch = s.charAt(i);
		switch (ch)
		{
			case '<':
				str.append("&lt;");
				break;
			case '>':
				str.append("&gt;");
				break;
			case '&':
				str.append("&amp;");
				break;
			case '"':
				str.append("&quot;");
				break;
			case '\'':
				str.append("&apos;");
				break;
			default:
				str.append(ch);
		}
	}
	return str.toString();
}
public void processingInstruction(String target, String data)
{
	out.print("<?");
	out.print(target);
	if (data != null && data.length() > 0)
	{
		out.print(' ');
		out.print(data);
	}
	out.println("?>");
	out.flush();
}
/**
 * Insert the method's description here.
 * Creation date: (22/11/2001 16:03:20)
 * @param newEncoding java.lang.String
 */
public void setEncoding(java.lang.String newEncoding) {
	encoding = newEncoding;
}
/**
 * Insert the method's description here.
 * Creation date: (22/11/2001 11:11:20)
 * @param newOut java.io.PrintWriter
 */
public void setOut(java.io.PrintWriter newOut)
{
	out = newOut;
}
public void startDocument()
{
	out.println("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
	out.flush();
}
public void startElement(String uri, String lName, String qName, Attributes attrs)
{
	out.print('<');
	out.print(qName);
	int len = attrs.getLength();
	for (int i = 0; i < len; i++)
	{
		out.print(' ');
		out.print(attrs.getQName(i));
		out.print("=\"");
		out.print(normalize(attrs.getValue(i)));
		out.print('"');
	}
	out.print('>');
	out.flush();
}
}
