/*
* File: GraphMLScanner.java
*
* @author rbolze
* @version generated by NetBeans XML module
*/
import java.util.*;
/**
*
* This is a scanner of DOM tree.
*
*
* @see org.w3c.dom.Document
* @see org.w3c.dom.Element
* @see org.w3c.dom.NamedNodeMap
*/
public class GraphMLScanner {
private org.w3c.dom.Document document;
/**
* Create new GraphMLScanner with org.w3c.dom.Document.
*/
public GraphMLScanner(String graphmlFileName){
try{
javax.xml.parsers.DocumentBuilderFactory builderFactory = javax.xml.parsers.DocumentBuilderFactory.newInstance();
javax.xml.parsers.DocumentBuilder builder = builderFactory.newDocumentBuilder();
this.document = builder.parse(new org.xml.sax.InputSource(graphmlFileName));
}catch(Exception e){
e.printStackTrace();
System.exit(1);
}
visitDocument();
}
/**
* Scan through org.w3c.dom.Document document.
*/
public void visitDocument() {
org.w3c.dom.Element element = document.getDocumentElement();
if ((element != null) && element.getTagName().equals("graphml")) {
visitElement_graphml(element);
}
if ((element != null) && element.getTagName().equals("graph")) {
visitElement_graph(element);
}
if ((element != null) && element.getTagName().equals("node")) {
visitElement_node(element);
}
if ((element != null) && element.getTagName().equals("edge")) {
visitElement_edge(element);
}
}
/**
* Scan through org.w3c.dom.Element named graphml.
*/
void visitElement_graphml(org.w3c.dom.Element element) { // <graphml>
// element.getValue();
org.w3c.dom.NodeList nodes = element.getChildNodes();
for (int i = 0; i < nodes.getLength(); i++) {
org.w3c.dom.Node node = nodes.item(i);
switch (node.getNodeType()) {
case org.w3c.dom.Node.CDATA_SECTION_NODE:
break;
case org.w3c.dom.Node.ELEMENT_NODE:
org.w3c.dom.Element nodeElement = (org.w3c.dom.Element)node;
if (nodeElement.getTagName().equals("graph")) {
visitElement_graph(nodeElement);
}
break;
case org.w3c.dom.Node.PROCESSING_INSTRUCTION_NODE:
break;
}
}
}
/**
* Scan through org.w3c.dom.Element named graph.
*/
void visitElement_graph(org.w3c.dom.Element element) { // <graph>
// element.getValue();
org.w3c.dom.NamedNodeMap attrs = element.getAttributes();
for (int i = 0; i < attrs.getLength(); i++) {
org.w3c.dom.Attr attr = (org.w3c.dom.Attr)attrs.item(i);
if (attr.getName().equals("id")) { // <graph id="???">
System.out.println(attr.getName()+"="+attr.getValue());
}
}
org.w3c.dom.NodeList nodes = element.getChildNodes();
for (int i = 0; i < nodes.getLength(); i++) {
org.w3c.dom.Node node = nodes.item(i);
switch (node.getNodeType()) {
case org.w3c.dom.Node.CDATA_SECTION_NODE:
break;
case org.w3c.dom.Node.ELEMENT_NODE:
org.w3c.dom.Element nodeElement = (org.w3c.dom.Element)node;
if (nodeElement.getTagName().equals("node")) {
visitElement_node(nodeElement);
}
if (nodeElement.getTagName().equals("edge")) {
visitElement_edge(nodeElement);
}
break;
case org.w3c.dom.Node.PROCESSING_INSTRUCTION_NODE:
break;
}
}
}
/**
* Scan through org.w3c.dom.Element named node.
*/
void visitElement_node(org.w3c.dom.Element element) { // <node>
org.w3c.dom.NamedNodeMap attrs = element.getAttributes();
for (int i = 0; i < attrs.getLength(); i++) {
org.w3c.dom.Attr attr = (org.w3c.dom.Attr)attrs.item(i);
if (attr.getName().equals("id")) { // <node id="???">
System.out.println(attr.getName()+"="+attr.getValue());
}
}
org.w3c.dom.NodeList nodes = element.getChildNodes();
for (int i = 0; i < nodes.getLength(); i++) {
org.w3c.dom.Node node = nodes.item(i);
switch (node.getNodeType()) {
case org.w3c.dom.Node.CDATA_SECTION_NODE:
break;
case org.w3c.dom.Node.ELEMENT_NODE:
org.w3c.dom.Element nodeElement = (org.w3c.dom.Element)node;
break;
case org.w3c.dom.Node.PROCESSING_INSTRUCTION_NODE:
break;
}
}
}
/**
* Scan through org.w3c.dom.Element named edge.
*/
void visitElement_edge(org.w3c.dom.Element element) { // <edge>
String source="";
String target="";
org.w3c.dom.NamedNodeMap attrs = element.getAttributes();
for (int i = 0; i < attrs.getLength(); i++) {
org.w3c.dom.Attr attr = (org.w3c.dom.Attr)attrs.item(i);
if (attr.getName().equals("target")) { // <edge target="???">
System.out.println(attr.getName()+"="+attr.getValue());
}
if (attr.getName().equals("source")) { // <edge source="???">
System.out.println(attr.getName()+"="+attr.getValue());
}
if (attr.getName().equals("id")) { // <edge id="???">
System.out.println(attr.getName()+"="+attr.getValue());
}
}
org.w3c.dom.NodeList nodes = element.getChildNodes();
for (int i = 0; i < nodes.getLength(); i++) {
org.w3c.dom.Node node = nodes.item(i);
switch (node.getNodeType()) {
case org.w3c.dom.Node.CDATA_SECTION_NODE:
break;
case org.w3c.dom.Node.ELEMENT_NODE:
org.w3c.dom.Element nodeElement = (org.w3c.dom.Element)node;
break;
case org.w3c.dom.Node.PROCESSING_INSTRUCTION_NODE:
break;
}
}
}
}
|