import org.xml.sax.*;
import org.xml.sax.helpers.*;
import java.io.*;
import java.util.*;
/**
 * XParser is DefaultHandler for parsing XML with SAX.
 * It provides some ease of use functionality as well as character control.
 * Inherit from this to do general parsing.
 * */
public class XParser extends DefaultHandler {
	/**
	 * The CharArrayWriter used to concatenate characters to in the DefaultHandler
	 * */
	protected CharArrayWriter contents = new CharArrayWriter();
	/**
	 * writes some characters from the character stream to the char Array writer
	 * @param ch the array to copy chars from
	 * @param start the inital index
	 * @param length how many character to copy
	 * @throws SAXException in case of XML parse error
	 * */
	public void characters( char[] ch, int start, int length ) throws SAXException {
		contents.write( ch, start, length );
	}
	/**
	 * Called when an element is starting
	 * @param namespaceURI the namespace
	 * @param localName the name of the tag
	 * @param qName
	 * @param attr the attributes of this tag
	 * @throws SAXException in case of XML parse error
	 * */
	public void startElement( String namespaceURI, String localName, String qName, Attributes attr ) throws SAXException {
		contents.reset();
	}
	/**
	 * Called when an element is ending
	 * @param namespaceURI the namespace
	 * @param localName the name of the tag
	 * @param qName
	 * @param attr the attributes of this tag
	 * @throws SAXException in case of XML parse error
	 * */
	public void endElement( String namespaceURI, String localName, String qName ) throws SAXException{
		super.endElement(namespaceURI,localName,qName);
	}
	/**
	 * parses strings into ints
	 * @param value to convert to int
	 * @return int the int value of the string
	 * */
	int parseInt(String value) {
		try {
			return Integer.parseInt(value);
		} catch (NumberFormatException e) {
			return 0;
		}
	}
	/**
	 * parses strings doubleo doubles
	 * @param value to convert to double
	 * @return double the double value of the string
	 * */
	double parseDouble(String value) {
		try {
			return Double.parseDouble(value);
		} catch (NumberFormatException e) {
			return 0.0;
		}
	}
}
