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 status xml doc * methods are: * load( path_to_input_file ) * exists( String map, String hostname ) returns index or -1 * add( map, host, badlist, state, paged, since ) * update( map, host, badlist, state, paged ) * delete( map, host ) * save( path_to_output_xml_file ) * getPage( index ) * getState( index ) * getDown( index ) */ public class status { protected int statusnum = 0; protected ArrayList statusmap = new ArrayList(); protected ArrayList statusname = new ArrayList(); protected ArrayList statuslabel = new ArrayList(); protected ArrayList statusdown = new ArrayList(); protected ArrayList statusstate = new ArrayList(); protected ArrayList statussince = new ArrayList(); protected ArrayList statuspage = 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 ) // update { statusmap.set( x, map ); statusname.set( x, host ); statuslabel.set( x, label ); statusdown.set( x, badlist ); statusstate.set( x, state ); // no since statuspage.set( x, paged ); } return( x ); } //---------- // add status entry of map host badlist status since paged //---------- void add( String map, String host, String label, String badlist, String state, String paged, String since ) { statusmap.add( map ); statusname.add( host ); statuslabel.add( label ); statusdown.add( badlist ); statusstate.add( state ); statussince.add( since ); statuspage.add( paged ); statusnum++; // System.out.println( "add map:" + map + " host:" + host + " label:" + label + " badlist:" + badlist + " state:" + state + " paged:" + paged + " since:" + since + " #:" + statusnum ); } //---------- // 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( "status" ); for ( int i = 0; i < statusnum; i++ ) { String smap = (String)statusmap.get( i ); String sname = (String)statusname.get( i ); String slabel = (String)statuslabel.get( i ); String sdown = (String)statusdown.get( i ); String sstate = (String)statusstate.get( i ); String ssince = (String)statussince.get( i ); String spage = (String)statuspage.get( i ); //dump empty entries if ( smap.length() > 0 && sname.length() > 0 && sdown.length() > 0 && sstate.length() > 0 && ssince.length() > 0 && spage.length() > 0 && slabel.length() > 0 ) { kid = doc.createElement( "system" ); item = doc.createElement( "mapname" ); item.appendChild( doc.createTextNode( smap )); kid.appendChild (item); item = doc.createElement ("hostname"); item.appendChild( doc.createTextNode( sname )); kid.appendChild( item ); item = doc.createElement ("label"); item.appendChild( doc.createTextNode( slabel )); kid.appendChild( item ); item = doc.createElement( "down" ); item.appendChild( doc.createTextNode( sdown )); kid.appendChild( item ); item = doc.createElement( "state" ); item.appendChild( doc.createTextNode( sstate )); kid.appendChild( item ); item = doc.createElement( "since" ); item.appendChild( doc.createTextNode( ssince )); kid.appendChild( item ); item = doc.createElement( "page" ); item.appendChild( doc.createTextNode( spage )); 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 //----------