//SAXNodes.java //Gerard Uffelman //XMLJ Homework #2 Answer Key Example import org.xml.sax.*; import org.xml.sax.helpers.DefaultHandler; import org.apache.xerces.parsers.SAXParser; import java.io.FileOutputStream; //output file stream import java.io.PrintStream; //FileOutputStream wrapper import java.io.IOException; //catch user io errors public class SAXNodes { public static void main(String[] args) { FileOutputStream fos; PrintStream ps; int nodeSize = 0; //Check input parameters if(args.length <= 2 || args.length > 3) { System.err.println("Invalid Command: Wrong number of arguments!"); System.err.println("Usage:\tjava SaxNodes graphFile nodeSize outputFile"); System.exit(0); } //Get maximum page size from input try{ Integer intWrapper = new Integer( args[1] ); nodeSize = intWrapper.intValue(); } catch(NumberFormatException nfe) { System.err.println("Error, invalid number: " + args[1]); return; } //Try to open file for output try{ fos = new FileOutputStream( args[2] ); } catch(IOException ioe) { System.err.println("Can't open output file: " + args[2]); return; } //Instantiate ps with fos ps = new PrintStream(fos); //Create NodeHandler class to handle SAX events and parse //input file. try{ NodeHandler handler = new NodeHandler(ps, nodeSize); SAXParser parser = new SAXParser(); parser.setContentHandler(handler); parser.setErrorHandler(handler); parser.parse(args[0]); } catch(Exception e){ e.printStackTrace(System.err); } ps.close(); //Close PrintStream } } //This class extends DefaultHandler and handles SAX events for us. //NodeHandler overloads startElement, startDocument, and endDocument from //DefaultHandler class NodeHandler extends DefaultHandler { PrintStream ps; int nodeSize; //Constructor takes reference to a printStram and nodeSize value public NodeHandler(PrintStream ps_in, int nodeSize_in) { nodeSize = nodeSize_in; if(ps_in != null) ps = ps_in; else System.err.println("Output PrintStream is null"); } //Write the following to our output file when the startDocument event occurs. public void startDocument() { ps.println(""); ps.println(""); ps.println("Report of Nodes with size greater than " + nodeSize + " "); ps.println(""); ps.println(""); ps.println("Node Report by size:\n
"); ps.println("
"); ps.println(""); ps.println(""); ps.println(""); } //Every time we encounter the start of a new element, check to see if it is a //node element. public void startElement(String uri, String localName, String rawName, Attributes attributes) { if(localName.equals("node")) { //get node information String weight = attributes.getValue("weight"); Integer wWrapper = new Integer(weight); int size = wWrapper.intValue(); String label = attributes.getValue("label"); String id = attributes.getValue("id"); //if the nodes is the right size, write its info to the output file. if(size > nodeSize ) { if(label != null) { ps.println("
  • Node ID: " + id + "
    \n" + " " + label + "
    \n Size: " + size + "
  • "); } else { ps.println("
  • Node ID: " + id + "
    \n" + "
    \n Size: " + size + "
  • "); } } } }//end startElement() }//end class NodeHandler