Document dc=null;  javax.xml.parsers.DocumentBuilderFactory dbf = javax.xml.parsers.DocumentBuilderFactory.newInstance(); dbf.setIgnoringElementContentWhitespace(true); javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();  if(inStream!=null) dc = db.parse(inStream); else if(inString!=null) dc = db.parse(new org.xml.sax.InputSource(new StringReader(inString))); else { throw new Exception("No input given"); }  NodeList nl = dc.getElementsByTagName( "NETWORK" );  if(nl.getLength()==0) { throw new BIFFormatException( "NETWORK tag not found" ); }  NodeList templist = ((Element)nl.item(0)).getElementsByTagName( "NAME" ); graphName = templist.item(0).getFirstChild().getNodeValue();    nl = dc.getElementsByTagName("VARIABLE"); for(int i=0; i<nl.getLength(); i++) {  templist = ((Element)nl.item(i)).getElementsByTagName("NAME"); if(templist.getLength()>1) throw new BIFFormatException("More than one name tags found for variable no. "+(i+1));  String nodename = ((org.w3c.dom.Node)templist.item(0)).getFirstChild().getNodeValue(); GraphNode n = new GraphNode( nodename, nodename, GraphNode.NORMAL ); m_nodes.addElement(n);  templist = ((Element)nl.item(i)).getElementsByTagName("PROPERTY"); for(int j=0; j<templist.getLength(); j++) { if( ((org.w3c.dom.Node)templist.item(j)).getFirstChild().getNodeValue().startsWith("position") ) { String xy = templist.item(j).getFirstChild().getNodeValue();   n.x = Integer.parseInt( xy.substring(xy.indexOf('(')+1, xy.indexOf(',')).trim() ); n.y = Integer.parseInt( xy.substring(xy.indexOf(',')+1, xy.indexOf(')')).trim() ); break; } }  templist = ((Element)nl.item(i)).getElementsByTagName("OUTCOME"); n.outcomes = new String[templist.getLength()]; for(int j=0; j<templist.getLength(); j++) { n.outcomes[j] = templist.item(j).getFirstChild().getNodeValue();  } }   nl = dc.getElementsByTagName("DEFINITION"); for(int i=0; i<nl.getLength(); i++) {  templist = ((Element)nl.item(i)).getElementsByTagName("FOR");  String nid = templist.item(0).getFirstChild().getNodeValue();   GraphNode n = (GraphNode)m_nodes.elementAt(0); for(int j=1; j<m_nodes.size() && !n.ID.equals(nid); j++) n = (GraphNode)m_nodes.elementAt(j);   templist = ((Element)nl.item(i)).getElementsByTagName("GIVEN"); int parntOutcomes = 1; for(int j=0; j<templist.getLength(); j++) { nid = templist.item(j).getFirstChild().getNodeValue();  GraphNode n2 = (GraphNode)m_nodes.elementAt(0); for(int k=1; k<m_nodes.size() && !n2.ID.equals(nid); k++) n2 = (GraphNode)m_nodes.elementAt(k); m_edges.addElement( new GraphEdge(m_nodes.indexOf(n2), m_nodes.indexOf(n), 1) );  parntOutcomes *= n2.outcomes.length; }   templist = ((Element)nl.item(i)).getElementsByTagName("TABLE"); if(templist.getLength()>1) throw new BIFFormatException("More than one Probability Table for "+n.ID);  String probs = templist.item(0).getFirstChild().getNodeValue(); StringTokenizer tk = new StringTokenizer(probs, " \n\t");  if(parntOutcomes*n.outcomes.length > tk.countTokens()) throw new BIFFormatException("Probability Table for "+n.ID+" contains more values than it should"); else if(parntOutcomes*n.outcomes.length < tk.countTokens()) throw new BIFFormatException("Probability Table for "+n.ID+" contains less values than it should"); else { n.probs = new double[parntOutcomes][n.outcomes.length]; for(int r=0; r<parntOutcomes; r++) for(int c=0; c<n.outcomes.length; c++) try { n.probs[r][c] =  Double.parseDouble( tk.nextToken() ); } catch(NumberFormatException ne) { throw ne; } } }             int tmpEdges[], noOfEdgesOfNode[]=new int[m_nodes.size()], noOfPrntsOfNode[]=new int[m_nodes.size()]; for(int i=0; i<m_edges.size(); i++) { GraphEdge e = (GraphEdge)m_edges.elementAt(i); noOfEdgesOfNode[e.src]++; noOfPrntsOfNode[e.dest]++; }  for(int i=0; i<m_edges.size(); i++) { GraphEdge e  = (GraphEdge)m_edges.elementAt(i); GraphNode n  = (GraphNode)m_nodes.elementAt(e.src); GraphNode n2 = (GraphNode)m_nodes.elementAt(e.dest); if(n.edges==null) { n.edges = new int[noOfEdgesOfNode[e.src]][2]; for(int k=0; k<n.edges.length; k++) n.edges[k][0]=-1; } if(n2.prnts==null) { n2.prnts = new int[noOfPrntsOfNode[e.dest]]; for(int k=0; k<n2.prnts.length; k++) n2.prnts[k]=-1; }  int k=0; while(n.edges[k][0]!=-1) k++; n.edges[k][0] = e.dest; n.edges[k][1] = e.type;  k=0; while(n2.prnts[k]!=-1) k++; n2.prnts[k] = e.src; }    return graphName; }
Document dc=null;  javax.xml.parsers.DocumentBuilderFactory dbf = javax.xml.parsers.DocumentBuilderFactory.newInstance(); dbf.setIgnoringElementContentWhitespace(true); javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();  if(inStream!=null) dc = db.parse(inStream); else if(inString!=null) dc = db.parse(new org.xml.sax.InputSource(new StringReader(inString))); else { throw new Exception("No input given"); }  NodeList nl = dc.getElementsByTagName( "NETWORK" );  if(nl.getLength()==0) { throw new BIFFormatException( "NETWORK tag not found" ); }   NodeList templist = ((Element)nl.item(0)).getElementsByTagName( "NAME" ); graphName = templist.item(0).getFirstChild().getNodeValue();     nl = dc.getElementsByTagName("VARIABLE"); for(int i=0; i<nl.getLength(); i++) {  templist = ((Element)nl.item(i)).getElementsByTagName("NAME"); if(templist.getLength()>1) throw new BIFFormatException("More than one name tags found for "+ "variable no. "+(i+1));  String nodename = ((org.w3c.dom.Node)templist.item(0)).getFirstChild().getNodeValue(); GraphNode n = new GraphNode( nodename, nodename, GraphNode.NORMAL ); m_nodes.addElement(n);  templist = ((Element)nl.item(i)).getElementsByTagName("PROPERTY"); for(int j=0; j<templist.getLength(); j++) { if( ((org.w3c.dom.Node)templist.item(j)).getFirstChild() .getNodeValue().startsWith("position") ) { String xy = templist.item(j).getFirstChild().getNodeValue();      n.x = Integer.parseInt( xy.substring(xy.indexOf('(')+ 1, xy.indexOf(',')).trim() ); n.y = Integer.parseInt( xy.substring(xy.indexOf(',')+ 1, xy.indexOf(')')).trim() ); break; } }  templist = ((Element)nl.item(i)).getElementsByTagName("OUTCOME"); n.outcomes = new String[templist.getLength()]; for(int j=0; j<templist.getLength(); j++) { n.outcomes[j] = templist.item(j).getFirstChild().getNodeValue();  } }   nl = dc.getElementsByTagName("DEFINITION"); for(int i=0; i<nl.getLength(); i++) {  templist = ((Element)nl.item(i)).getElementsByTagName("FOR");  String nid = templist.item(0).getFirstChild().getNodeValue();   GraphNode n = (GraphNode)m_nodes.elementAt(0); for(int j=1; j<m_nodes.size() && !n.ID.equals(nid); j++) n = (GraphNode)m_nodes.elementAt(j);   templist = ((Element)nl.item(i)).getElementsByTagName("GIVEN"); int parntOutcomes = 1;  for(int j=0; j<templist.getLength(); j++) { nid = templist.item(j).getFirstChild().getNodeValue();  GraphNode n2 = (GraphNode)m_nodes.elementAt(0); for(int k=1; k<m_nodes.size() && !n2.ID.equals(nid); k++) n2 = (GraphNode)m_nodes.elementAt(k); m_edges.addElement( new GraphEdge(m_nodes.indexOf(n2), m_nodes.indexOf(n), 1) );  parntOutcomes *= n2.outcomes.length; }   templist = ((Element)nl.item(i)).getElementsByTagName("TABLE"); if(templist.getLength()>1) throw new BIFFormatException("More than one Probability Table for "+ n.ID);  String probs = templist.item(0).getFirstChild().getNodeValue(); StringTokenizer tk = new StringTokenizer(probs, " \n\t");  if(parntOutcomes*n.outcomes.length > tk.countTokens()) throw new BIFFormatException("Probability Table for "+n.ID+ " contains more values than it should"); else if(parntOutcomes*n.outcomes.length < tk.countTokens()) throw new BIFFormatException("Probability Table for "+n.ID+ " contains less values than it should"); else { n.probs = new double[parntOutcomes][n.outcomes.length]; for(int r=0; r<parntOutcomes; r++) for(int c=0; c<n.outcomes.length; c++) try { n.probs[r][c] =  Double.parseDouble( tk.nextToken() ); } catch(NumberFormatException ne) { throw ne; } } }              int tmpEdges[], noOfEdgesOfNode[]=new int[m_nodes.size()]; int noOfPrntsOfNode[]=new int[m_nodes.size()]; for(int i=0; i<m_edges.size(); i++) { GraphEdge e = (GraphEdge)m_edges.elementAt(i); noOfEdgesOfNode[e.src]++; noOfPrntsOfNode[e.dest]++; }  for(int i=0; i<m_edges.size(); i++) { GraphEdge e  = (GraphEdge)m_edges.elementAt(i); GraphNode n  = (GraphNode)m_nodes.elementAt(e.src); GraphNode n2 = (GraphNode)m_nodes.elementAt(e.dest); if(n.edges==null) { n.edges = new int[noOfEdgesOfNode[e.src]][2]; for(int k=0; k<n.edges.length; k++) n.edges[k][0]=-1; } if(n2.prnts==null) { n2.prnts = new int[noOfPrntsOfNode[e.dest]]; for(int k=0; k<n2.prnts.length; k++) n2.prnts[k]=-1; }  int k=0; while(n.edges[k][0]!=-1) k++; n.edges[k][0] = e.dest; n.edges[k][1] = e.type;  k=0; while(n2.prnts[k]!=-1) k++; n2.prnts[k] = e.src; }    return graphName; }
    public String parse() throws Exception {	Document dc=null;	javax.xml.parsers.DocumentBuilderFactory dbf = javax.xml.parsers.DocumentBuilderFactory.newInstance();	dbf.setIgnoringElementContentWhitespace(true);	javax.xml.parsers.DocumentBuilder db = dbf.newDocumentBuilder();		if(inStream!=null)	    dc = db.parse(inStream);	else if(inString!=null)	    dc = db.parse(new org.xml.sax.InputSource(new StringReader(inString)));	else	    { throw new Exception("No input given"); }		NodeList nl = dc.getElementsByTagName( "NETWORK" );	if(nl.getLength()==0) {	    throw new BIFFormatException( "NETWORK tag not found" );	}	NodeList templist = ((Element)nl.item(0)).getElementsByTagName( "NAME" );  //take only the first network node	graphName = templist.item(0).getFirstChild().getNodeValue();	//System.out.println("The name of the network is "+templist.item(0).getFirstChild().getNodeValue());		//Get all the variables										       	nl = dc.getElementsByTagName("VARIABLE");	for(int i=0; i<nl.getLength(); i++) {	    templist = ((Element)nl.item(i)).getElementsByTagName("NAME");	    if(templist.getLength()>1)		throw new BIFFormatException("More than one name tags found for variable no. "+(i+1));	    String nodename = ((org.w3c.dom.Node)templist.item(0)).getFirstChild().getNodeValue();	    GraphNode n = new GraphNode( nodename, nodename, GraphNode.NORMAL ); 	    m_nodes.addElement(n);	    //getting nodes position	    templist = ((Element)nl.item(i)).getElementsByTagName("PROPERTY");	    for(int j=0; j<templist.getLength(); j++) {		if( ((org.w3c.dom.Node)templist.item(j)).getFirstChild().getNodeValue().startsWith("position") ) {		    String xy = templist.item(j).getFirstChild().getNodeValue();		    //System.out.println("x: "+xy.substring(xy.indexOf('(')+1, xy.indexOf(','))+" y: "+		    //	       xy.substring(xy.indexOf(',')+1, xy.indexOf(')'))  );		    n.x = Integer.parseInt( xy.substring(xy.indexOf('(')+1, xy.indexOf(',')).trim() );		    n.y = Integer.parseInt( xy.substring(xy.indexOf(',')+1, xy.indexOf(')')).trim() );		    break;		}	    }	    //getting all the outcomes of the node	    templist = ((Element)nl.item(i)).getElementsByTagName("OUTCOME");	    n.outcomes = new String[templist.getLength()];	    for(int j=0; j<templist.getLength(); j++) {		n.outcomes[j] = templist.item(j).getFirstChild().getNodeValue();		//System.out.println("Outcome["+j+"]: "+n.outcomes[j]);	    }	} //end for (for variables)	//Get all the edges and probability tables by getting all the definitions	nl = dc.getElementsByTagName("DEFINITION");	for(int i=0; i<nl.getLength(); i++) {	    	    templist = ((Element)nl.item(i)).getElementsByTagName("FOR");	    //the Label of the node the edges are coming into 	    String nid = templist.item(0).getFirstChild().getNodeValue();	    //getting the GraphNode object with the above label	    GraphNode n = (GraphNode)m_nodes.elementAt(0);	    for(int j=1; j<m_nodes.size() && !n.ID.equals(nid); j++)		n = (GraphNode)m_nodes.elementAt(j);	    templist = ((Element)nl.item(i)).getElementsByTagName("GIVEN");	    int parntOutcomes = 1; //for creating the probability table later on 	    for(int j=0; j<templist.getLength(); j++) {  //creating all the edges coming into the node		nid = templist.item(j).getFirstChild().getNodeValue();				GraphNode n2 = (GraphNode)m_nodes.elementAt(0);		for(int k=1; k<m_nodes.size() && !n2.ID.equals(nid); k++) 		    n2 = (GraphNode)m_nodes.elementAt(k);		m_edges.addElement( new GraphEdge(m_nodes.indexOf(n2), m_nodes.indexOf(n), 1) );				parntOutcomes *= n2.outcomes.length;	    }	    	    //creating the probability table for the node	    templist = ((Element)nl.item(i)).getElementsByTagName("TABLE");	    if(templist.getLength()>1)		throw new BIFFormatException("More than one Probability Table for "+n.ID);	    String probs = templist.item(0).getFirstChild().getNodeValue();	    StringTokenizer tk = new StringTokenizer(probs, " \n\t");	    	    if(parntOutcomes*n.outcomes.length > tk.countTokens())		throw new BIFFormatException("Probability Table for "+n.ID+" contains more values than it should");	    else if(parntOutcomes*n.outcomes.length < tk.countTokens())		throw new BIFFormatException("Probability Table for "+n.ID+" contains less values than it should");	    else {		n.probs = new double[parntOutcomes][n.outcomes.length];		for(int r=0; r<parntOutcomes; r++)     //row		    for(int c=0; c<n.outcomes.length; c++) //column			try {			n.probs[r][c] =  Double.parseDouble( tk.nextToken() );			}			catch(NumberFormatException ne) { throw ne; }	    } // end of creating probability table	} //endfor (for edges)	//int tmpMatrix[][] = new int[m_nodes.size()][m_nodes.size()];	//for(int i=0; i<m_edges.size(); i++)	//    tmpMatrix[((GraphEdge)m_edges.elementAt(i)).src]	//	       [((GraphEdge)m_edges.elementAt(i)).dest] = ((GraphEdge)m_edges.elementAt(i)).type;	//for(int i=0; i<m_nodes.size(); i++) {	//    GraphNode n = (GraphNode)m_nodes.elementAt(i);	//    n.edges = tmpMatrix[i];      	//}		//Adding parents, and those edges to a node which are coming out from it	int tmpEdges[], noOfEdgesOfNode[]=new int[m_nodes.size()], noOfPrntsOfNode[]=new int[m_nodes.size()];	for(int i=0; i<m_edges.size(); i++) {	    GraphEdge e = (GraphEdge)m_edges.elementAt(i);	    noOfEdgesOfNode[e.src]++;	    noOfPrntsOfNode[e.dest]++;	}	for(int i=0; i<m_edges.size(); i++) {	    GraphEdge e  = (GraphEdge)m_edges.elementAt(i);	    GraphNode n  = (GraphNode)m_nodes.elementAt(e.src);	    GraphNode n2 = (GraphNode)m_nodes.elementAt(e.dest);	    if(n.edges==null) {		n.edges = new int[noOfEdgesOfNode[e.src]][2];		for(int k=0; k<n.edges.length; k++)		    n.edges[k][0]=-1;	    }	    if(n2.prnts==null) {		n2.prnts = new int[noOfPrntsOfNode[e.dest]];		for(int k=0; k<n2.prnts.length; k++)		    n2.prnts[k]=-1;	    }	    int k=0;	    while(n.edges[k][0]!=-1) k++;	    n.edges[k][0] = e.dest;	    n.edges[k][1] = e.type;	    k=0;	    while(n2.prnts[k]!=-1) k++;	    n2.prnts[k] = e.src;	    	}	//processGraph();	//setAppropriateSize();	return graphName;    } //end readBIF