// XMLReader.java

// Note: use the files jaxp.jar and crimson.jar in /usr/class/cs108/jar/ --
// in your CLASSPATH.
// They are newer than the xml.jar the handout mentions.
// This codes to the SAX-1 standard which was recently updated to SAX-2,
// so you will get some (harmless) deprecation warnings


import java.io.*;

import org.xml.sax.*;

import javax.xml.parsers.SAXParserFactory;  
import javax.xml.parsers.ParserConfigurationException;  
import javax.xml.parsers.SAXParser;  


/**
 This is a simple class that can read state out of an XML file
 using a SAX state-machine parser.
 
 In this case, we support data like this, where the flip
 node switches x,y...
	 
	<?xml version="1.0" encoding="UTF-8"?>

	<dots>
		<dot x="81" y="67" />
		<dot x="175" y="122" />
		<flip>
			<dot x="175" y="122" />
			<dot x="209" y="71" />
		</flip>
		<dot x="209" y="71" />
	</dots>

*/
public class XMLReader extends HandlerBase
{
	
	public static void main (String argv [])
	{
		if (argv.length != 1) {
			System.err.println ("Usage: cmd filename");
			System.exit (1);
		}
		
		
		try {
			XMLReader xr = new XMLReader();
			InputStream in = new BufferedInputStream( new FileInputStream(new File(argv[0])));
			xr.read(in);
		} catch (Throwable t) {
			t.printStackTrace ();
		}

	}		


	//===========================================================
	// SAX DocumentHandler methods
	//===========================================================

	public void setDocumentLocator (Locator l)
	{
	}

	public void startDocument ()
	throws SAXException
	{
		//System.out.println("startDocument");
	}

	public void endDocument ()
	throws SAXException
	{
		//System.out.println("startDocument");
	}
	
	
	
	public void read(File file) {
		try {
			InputStream in = new BufferedInputStream( new FileInputStream(file));
			read(in);
		}
		catch (IOException ioe) {
			ioe.printStackTrace();
		}
	}
	
	
	/**
	 Read the XML in the given file
	*/
	public void read(InputStream stream)  {
		try {
			SAXParserFactory factory = SAXParserFactory.newInstance();
			SAXParser saxParser = factory.newSAXParser();
			clear();
			saxParser.parse(stream, this);
		}
		catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	

	public XMLReader() {
		clear();
	}
	
	

	
	
	// State we keep track of -- like a state machine,
	// where startElement() etc. keep getting called
	private int x;
	private int y;
	private boolean flip;
	
	
	public void clear() {
		x = -1;
		y = -1;
		flip = false;
	}
	
	/**
	 Called for each node
	 -look at the node name
	 -process that node if appropriate
	 -or, update our state to affect future calls to startElem()
	 or characters()
	*/
	public void startElement (String name, AttributeList attrs)
	throws SAXException
	{
		//System.out.println("start element:" + name);
		if (name.equals("dot")) {
			x = Integer.parseInt(attrs.getValue("x"));
			y = Integer.parseInt(attrs.getValue("y"));
			
			if (flip) {
				int temp = x; x = y; y = temp;
			}
			
			// do something with our x,y state (could wait for endElement)
			System.out.println(x);
			System.out.println(y);
		}
		else if (name.equals("flip")) {
			flip = true;
		}
	}

	public void endElement (String name)
	throws SAXException
	{
		//System.out.println("end element:" + name);
		
		if (name.equals("flip")) {
			flip = false;
		}
	}

	// Called for characters between nodes
	public void characters (char buf [], int offset, int len)
	throws SAXException
	{
		//String s = new String(buf, offset, len);
		//s = s.trim();
		//if (!s.equals("")) {
			//System.out.println("characters:" + s);
		//}
	}
}
