// XEdit.java
/*
 Implements the XML search/replace for CS193j HW4.
 The routine XML load/save code is provided (it's basically
 the same as the lecture example).
 Students need to define the searchReplace() method
 to perform the actual search/replace.
*/

import java.io.*;

// Standard imports for XML
import javax.xml.parsers.*;
import org.xml.sax.*;
import org.xml.sax.helpers.*;
import org.w3c.dom.*;


// This is where the XmlDocument subclass lives
// -- see saveXML()
import org.apache.crimson.tree.*;

class XEdit {
	private String tag;		// the tag constraint, or null
	private String search;	// the search string
	private String replace;	// the replace string (defaults to "")
	
	private Document document;	// the XML DOM
	
	// Create and run an XEdit based on the command line args
	// -s search -r replace -t tag file.xml
	public static void main(String[] args) {
		// Make an XEdit based on the args
		XEdit x = new XEdit(args);
		
		// Load the XML document
		x.loadXML(new File(args[args.length-1]));
		
		
		x.searchReplace(x.document.getDocumentElement(), false);
		
		// Print the XML doc to standard output
		x.saveXML(System.out);
		
	}
	
	// Init an XEDIT based on the command line args
	public XEdit(String[] args) {
		int i = 0;
		replace = "";
		
		while (i < args.length-1) {
			if (args[i].equals("-t")) {
				tag = args[i+1];
				i += 2;
			}
			else if (args[i].equals("-r")) {
				replace = args[i+1];
				i += 2;
			}
			else if (args[i].equals("-s")) {
				search = args[i+1];
				i += 2;
			}
		}
		
		if (i >= args.length) {
			throw new RuntimeException("Usage -s search [-r replace] [-t tag] file.xml");
		}
	}


	/*
	 Parse the given file into our DOM document ivar.
	*/
	private void loadXML(File file)  {
		
		try {
			// The following is the standard incantation to get a Document object
	        DocumentBuilderFactory dbf =
	            DocumentBuilderFactory.newInstance();

	        dbf.setValidating(false);
	        
	        DocumentBuilder db = null;
	        try {
	            db = dbf.newDocumentBuilder();
	        } catch (ParserConfigurationException pce) {
				pce.printStackTrace();
	        }

			// Parse the XML to build the whole doc tree
			document  = db.parse(file);
		}
		catch (SAXException e) {
			System.err.println("XML parse err:" + e.getMessage());
		}
		catch (IOException e) {
			System.err.println("IO err:" + e.getMessage());
		}
	}


	/*
	 Write our DOM document to the given output stream.
	*/
	public void saveXML(OutputStream os)  {
		try {
			Writer out = new OutputStreamWriter ( os );
									
			// 1. Cast the doc down to an XmlDocument
			// (Nick's trick)
			XmlDocument x = (XmlDocument) document;
			
			// 2. XmlDocument knows how to write itself out Woo Hoo!
			x.write(out, "UTF-8");
							
		}
		catch (Exception e) {
			System.err.println("Save XML err:" + e);
		}
	}

	
	
	/*
	 Iterates over the DOM tree to implement the search/replace
	 defined by our search/replace/tag ivars.
	*/
	public void searchReplace(Node n, boolean replacing) {
		// YOUR CODE HERE


	}
	

}
