aboutsummaryrefslogtreecommitdiff
path: root/entomologist/src/org/monkey/comparator/bo/XMLParser.java
diff options
context:
space:
mode:
Diffstat (limited to 'entomologist/src/org/monkey/comparator/bo/XMLParser.java')
-rw-r--r--entomologist/src/org/monkey/comparator/bo/XMLParser.java196
1 files changed, 196 insertions, 0 deletions
diff --git a/entomologist/src/org/monkey/comparator/bo/XMLParser.java b/entomologist/src/org/monkey/comparator/bo/XMLParser.java
new file mode 100644
index 0000000..6254730
--- /dev/null
+++ b/entomologist/src/org/monkey/comparator/bo/XMLParser.java
@@ -0,0 +1,196 @@
1package org.monkey.comparator.bo;
2
3import java.io.File;
4
5import javax.xml.parsers.DocumentBuilder;
6import javax.xml.parsers.DocumentBuilderFactory;
7
8import org.w3c.dom.Document;
9import org.w3c.dom.Element;
10import org.w3c.dom.Node;
11import org.w3c.dom.NodeList;
12
13public class XMLParser {
14
15 /*
16 * The method parses Monkey's XML Bug Report.
17 * The following is an example the XML file the method expects for parsing
18 * <?xml version="1.0"?>
19 <crash category="npe" function="crashFunction" line="14" file="bug_null_pointer_exception.c" >
20 <history>
21 <epoch step="0" >
22 <trace>
23 <function name="crashFunction" line="14" file="bug_null_pointer_exception.c" depth="0" >
24 <expressions>
25 <expression name="crashStruct" >NULL</expression>
26 </expressions>
27 </function>
28 </trace>
29 </epoch>
30 </history>
31 </crash>
32 */
33 public static Bug parse(String filePath) {
34 Bug bug = new Bug();
35 try {
36 NodeList epochNodeList;
37 NodeList traceNodeList;
38 NodeList functionNodeList;
39 NodeList expressionNodeList;
40 NodeList tmpNodeList;
41 Node epochNode;
42 Node traceNode;
43 Node functionNode;
44 Node expressionNode;
45 Node tmpNode = null;
46 Element element;
47 Epoch epoch = null;
48 Function function = null;
49 Expression expression = null;
50 File xmlFile = new File(filePath);
51
52 DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
53 .newInstance();
54 DocumentBuilder documentBuilder = documentBuilderFactory
55 .newDocumentBuilder();
56 Document document = documentBuilder.parse(xmlFile);
57 document.getDocumentElement().normalize();
58
59 /* Setting Bug Attributes */
60 element = document.getDocumentElement(); /*
61 * This should be the crash
62 * node
63 */
64 bug.setCategory(element.getAttribute("category"));
65 bug.setFileName(element.getAttribute("file"));
66 bug.setFunctionName(element.getAttribute("function"));
67 bug.setLineNo(Integer.parseInt(element.getAttribute("line")));
68
69 /* History is consisting of a list of epoch nodes */
70 epochNodeList = document.getElementsByTagName("epoch");
71 //printAllNodes(epochNodeList);
72 for (int i = 0; i < epochNodeList.getLength(); i++) {
73 epochNode = epochNodeList.item(i);
74 if (epochNode.getNodeType() == Node.ELEMENT_NODE) {
75 epoch = new Epoch();
76 bug.insertEpoch(epoch);
77 epoch.setEpochStep(Integer.parseInt(epochNode
78 .getAttributes().getNamedItem("step")
79 .getNodeValue()));
80
81 /* Get the list of trace nodes for this epoch node */
82 traceNodeList = epochNode.getChildNodes();
83 for (int j = 0; j < traceNodeList.getLength(); j++) {
84 /* Get the list of function nodes for this trace node */
85 traceNode = traceNodeList.item(j);
86 if (traceNode.getNodeType() == Node.ELEMENT_NODE) {
87 functionNodeList = traceNode.getChildNodes();
88 for (int k = 0; k < functionNodeList.getLength(); k++) {
89 functionNode = functionNodeList.item(k);
90 if (functionNode.getNodeType() == Node.ELEMENT_NODE) {
91 function = new Function();
92 epoch.insertFunction(function);
93 function.setFunctionName(functionNode
94 .getAttributes()
95 .getNamedItem("name")
96 .getNodeValue());
97 function.setLineNo(Integer
98 .parseInt(functionNode
99 .getAttributes()
100 .getNamedItem("line")
101 .getNodeValue()));
102 function.setFileName(functionNode
103 .getAttributes()
104 .getNamedItem("file")
105 .getNodeValue());
106
107 /*
108 * Get the list of expression nodes for this
109 * function
110 */
111 tmpNodeList = functionNode.getChildNodes();
112 for (int t = 0; t < tmpNodeList.getLength(); t++) { // skipping the
113 // unnecessary
114 // node
115 // named
116 // <expressions></expressions>
117 tmpNode = tmpNodeList.item(t);
118 if (tmpNode.getNodeName().equals("expressions"))
119 break;
120 }
121 expressionNodeList = tmpNode.getChildNodes();
122 for (int l = 0; l < expressionNodeList
123 .getLength(); l++) {
124 expressionNode = expressionNodeList
125 .item(l);
126 if (expressionNode.getNodeType() == Node.ELEMENT_NODE) {
127 expression = new Expression();
128 function.insertExpression(expression);
129 expression.setName(expressionNode
130 .getAttributes()
131 .getNamedItem("name")
132 .getNodeValue());
133 expression.setValue(expressionNode
134 .getTextContent());
135 }
136 }
137 } // end if functionNode
138 } // end for functionNodeList
139 } // end if traceNode
140 } // end for traceNodeList
141 } // end if epochNode
142 } // end for epochNodeList
143 } catch (Exception e) {
144 System.err.println("Fatal error: unable to parse XML file. Entomologist will exit now.");
145 e.printStackTrace();
146 System.exit(1);
147 }
148 return bug;
149 }
150
151 public static void printBug(Bug bug) {
152 Epoch epoch;
153 Function function;
154 Expression expression;
155 System.out.println("Bug:");
156 System.out.println("---------");
157 System.out.println("Category: " + bug.getCategory());
158 System.out.println("File Name: " + bug.getFileName());
159 System.out.println("Function Name: " + bug.getFunctionName());
160 System.out.println("Line Number: " + bug.getLineNo());
161 System.out.println("------------------------------------------------------");
162 for (int i = 0; i < bug.getEpochNum(); i++) {
163 epoch = bug.getEpoch(i);
164 System.out.println("Epoch Step: " + epoch.getEpochStep());
165 System.out.println("---------------");
166 for (int j = 0; j < epoch.getFunctionNum(); j++) {
167 function = epoch.getFunction(j);
168 System.out.println("Function: " + function.getFunctionName() + " file: " + function.getFileName() + " Lineno." + function.getLineNo());
169 System.out.println("-----------------------------------------------------");
170 for (int k = 0; k < function.getExpressionNum(); k++) {
171 expression = function.getExpression(k);
172 System.out.println(expression.getName() + " = " + expression.getValue());
173 }
174 System.out.println("");
175 }
176 System.out.println("");
177 }
178 }
179
180
181
182 // This method is used for debugging
183 /*
184 private static void printAllNodes(NodeList nodeList) {
185 NodeList childNodes;
186 for (int i = 0; i < nodeList.getLength(); i++) {
187 if (nodeList.item(i).getNodeType() == Node.ELEMENT_NODE) {
188 System.out.println(nodeList.item(i).getNodeName() + " type ="
189 + nodeList.item(i).getNodeType());
190 childNodes = nodeList.item(i).getChildNodes();
191 printAllNodes(childNodes);
192 }
193 }
194 }
195 */
196}