import java.io.File; import java.io.FileWriter; import java.io.PrintWriter; import java.io.FileWriter; import java.io.FileInputStream; import java.io.IOException; import java.util.ArrayList; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.FactoryConfigurationError; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.DocumentBuilder; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; import org.w3c.dom.Document; import org.apache.xerces.dom.DocumentImpl; import org.w3c.dom.DOMException; /** * manage the missed xml doc which tracks the number of failures for each map/host * public methods are: * load( path_to_input_file ) * save( path_to_output_xml_file ) * update( map, host ) increment miss count for one * getCount( map, hostname ) retrieve miss count * private------------- * exists( String map, String hostname ) returns index or -1 if host needs to be added * add( map, host ) add one */ public class missed { protected int missnum = 0; protected ArrayList mapname = new ArrayList(); protected ArrayList hostname = new ArrayList(); protected ArrayList count = new ArrayList(); //---------- // getDataFromNode of type 3 and not empty //---------- String getDataFromNode( org.w3c.dom.Node nod ) { org.w3c.dom.NodeList nodes = nod.getChildNodes(); for ( int i=0; i< nodes.getLength(); i++ ) { org.w3c.dom.Node node = nodes.item(i); int type = node.getNodeType(); String val = node.getNodeValue(); String name = node.getNodeName(); if ( type == 3 ) { if ( val != null ) { return( val ); } } } return( null ); } //---------- // getDataFromNode type 3 whose type 1 parents match str //---------- String getDataFromNode( org.w3c.dom.Node nod, String str ) { org.w3c.dom.NodeList nodes = nod.getChildNodes(); for ( int i=0; i< nodes.getLength(); i++ ) { org.w3c.dom.Node node = nodes.item(i); int type = node.getNodeType(); String val = node.getNodeValue(); String name = node.getNodeName(); // System.out.println( "type: " + type + " nodename: " + name + " value: " + val ); if ( type == 1 && name != null ) { if ( name.equals( str )) { if ( node.hasChildNodes() ) { org.w3c.dom.NodeList nads = node.getChildNodes(); for ( int j = 0; j < nads.getLength(); j++ ) { node = nads.item(j); type = node.getNodeType(); val = node.getNodeValue(); name = node.getNodeName(); if ( type == 3 && val != null ) { return( val ); } } } } } } return( null ); } //---------- // recurseNodes //---------- void recurseNodes( org.w3c.dom.NodeList nodes ) throws IOException { for (int i=0; i= 0 ) { retval = Integer.parseInt( (String)count.get( x )); } return( retval ); } //---------- // exists - return index of map/host in lists or -1 //---------- int exists( String map, String host ) { int retval = -1; for ( int i = 0; i < missnum; i++ ) { if ( map.equals( (String)mapname.get( i ) ) && host.equals( (String)hostname.get( i ) ) ) { retval = i; } } return( retval ); } //---------- // increment count entry of map host or add one if not exists // return void //---------- void update( String map, String host ) { int x = exists( map, host ); if ( x >= 0 ) // update { String y = (String)count.get( x ); int z = Integer.parseInt( y ); z++; y = Integer.toString( z ); count.set( x, y ); } else { add( map, host ); } } //---------- // add entry of map host with initial count of 1 //---------- void add( String map, String host ) { mapname.add( map ); hostname.add( host ); count.add( "1" ); missnum++; } //---------- // listToTags dump arraylists into doc form //---------- void save( String fname ) { Document doc; org.w3c.dom.Element root = null; org.w3c.dom.Element item = null; org.w3c.dom.Element kid = null; doc = new DocumentImpl(); root = doc.createElement( "missed" ); for ( int i = 0; i < missnum; i++ ) { String mmap = (String)mapname.get( i ); String mname = (String)hostname.get( i ); String mcount = (String)count.get( i ); //dump empty entries if ( mmap.length() > 0 && mname.length() > 0 && mcount.length() > 0 ) { kid = doc.createElement( "system" ); item = doc.createElement( "mapname" ); item.appendChild( doc.createTextNode( mmap )); kid.appendChild (item); item = doc.createElement ("hostname"); item.appendChild( doc.createTextNode( mname )); kid.appendChild( item ); item = doc.createElement( "count" ); item.appendChild( doc.createTextNode( mcount )); kid.appendChild( item ); root.appendChild( kid ); } } int level = 0; try { FileWriter xFile = new FileWriter( fname ); PrintWriter newFile = new PrintWriter( xFile ); printTree( root, newFile, level ); newFile.close(); } catch (IOException ioe) { // I/O error ioe.printStackTrace(); return; } } // ------------------ // print a tree in xml code by calling printBranches on every type 1 node // ------------------ void printTree( org.w3c.dom.Node rootNode, PrintWriter Out, int level ) { int type = rootNode.getNodeType(); String name = rootNode.getNodeName(); Out.println(""); if ( type == 1 ) Out.println("<" + name + ">"); printBranches( rootNode, Out, level ); if ( type == 1 ) Out.println(""); } // ------------------ // print a branch in xml code // ------------------ void printBranches( org.w3c.dom.Node rootNode, PrintWriter Out, int level ) { int j; org.w3c.dom.NodeList nodes = rootNode.getChildNodes(); for (int i=0; i"); } if ( type == 3 ) { String s = node.getNodeValue(); if ( s.trim().compareTo( "" ) != 0 ) { Out.print( s ); } } if ( node.hasChildNodes() ) { printBranches( node, Out, level + 1 ); } if ( type == 1 ) { Out.println(""); } } } // eo printTree //---------- } // eoclass //----------