aboutsummaryrefslogtreecommitdiff
path: root/entomologist/src/org/monkey/comparator/bo/XMLParser.java
blob: 6254730c13c579a7f00728b873f62af57710c958 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
package org.monkey.comparator.bo;

import java.io.File;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

public class XMLParser {
	
	/*
	 * The method parses Monkey's XML Bug Report.
	 * The following is an example the XML file the method expects for parsing
	 * <?xml version="1.0"?>
		<crash category="npe" function="crashFunction" line="14" file="bug_null_pointer_exception.c" >
			<history>
			<epoch step="0" >
					<trace>
						<function name="crashFunction" line="14" file="bug_null_pointer_exception.c" depth="0" >
							<expressions>
								<expression name="crashStruct" >NULL</expression>
							</expressions>
						</function>
					</trace>
				</epoch>
			</history>
		</crash>
	 */
	public static Bug parse(String filePath) {
		Bug bug = new Bug();
		try {
			NodeList epochNodeList;
			NodeList traceNodeList;
			NodeList functionNodeList;
			NodeList expressionNodeList;
			NodeList tmpNodeList;
			Node epochNode;
			Node traceNode;
			Node functionNode;
			Node expressionNode;
			Node tmpNode = null;
			Element element;
			Epoch epoch = null;
			Function function = null;
			Expression expression = null;
			File xmlFile = new File(filePath);

			DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory
					.newInstance();
			DocumentBuilder documentBuilder = documentBuilderFactory
					.newDocumentBuilder();
			Document document = documentBuilder.parse(xmlFile);
			document.getDocumentElement().normalize();

			/* Setting Bug Attributes */
			element = document.getDocumentElement(); /*
													 * This should be the crash
													 * node
													 */
			bug.setCategory(element.getAttribute("category"));
			bug.setFileName(element.getAttribute("file"));
			bug.setFunctionName(element.getAttribute("function"));
			bug.setLineNo(Integer.parseInt(element.getAttribute("line")));

			/* History is consisting of a list of epoch nodes */
			epochNodeList = document.getElementsByTagName("epoch");
			//printAllNodes(epochNodeList);
			for (int i = 0; i < epochNodeList.getLength(); i++) {
				epochNode = epochNodeList.item(i);
				if (epochNode.getNodeType() == Node.ELEMENT_NODE) {
					epoch = new Epoch();
					bug.insertEpoch(epoch);
					epoch.setEpochStep(Integer.parseInt(epochNode
							.getAttributes().getNamedItem("step")
							.getNodeValue()));

					/* Get the list of trace nodes for this epoch node */
					traceNodeList = epochNode.getChildNodes();
					for (int j = 0; j < traceNodeList.getLength(); j++) {
						/* Get the list of function nodes for this trace node */
						traceNode = traceNodeList.item(j);
						if (traceNode.getNodeType() == Node.ELEMENT_NODE) {
							functionNodeList = traceNode.getChildNodes();
							for (int k = 0; k < functionNodeList.getLength(); k++) {
								functionNode = functionNodeList.item(k);
								if (functionNode.getNodeType() == Node.ELEMENT_NODE) {
									function = new Function();
									epoch.insertFunction(function);
									function.setFunctionName(functionNode
											.getAttributes()
											.getNamedItem("name")
											.getNodeValue());
									function.setLineNo(Integer
											.parseInt(functionNode
													.getAttributes()
													.getNamedItem("line")
													.getNodeValue()));
									function.setFileName(functionNode
											.getAttributes()
											.getNamedItem("file")
											.getNodeValue());

									/*
									 * Get the list of expression nodes for this
									 * function
									 */
									tmpNodeList = functionNode.getChildNodes();
									for (int t = 0; t < tmpNodeList.getLength(); t++) { // skipping the
										// unnecessary
										// node
										// named
										// <expressions></expressions>
										tmpNode = tmpNodeList.item(t);
										if (tmpNode.getNodeName().equals("expressions"))
											break;
									}
									expressionNodeList = tmpNode.getChildNodes();
									for (int l = 0; l < expressionNodeList
											.getLength(); l++) {
										expressionNode = expressionNodeList
												.item(l);
										if (expressionNode.getNodeType() == Node.ELEMENT_NODE) {
											expression = new Expression();
											function.insertExpression(expression);
											expression.setName(expressionNode
													.getAttributes()
													.getNamedItem("name")
													.getNodeValue());
											expression.setValue(expressionNode
													.getTextContent());
										}
									}
								} // end if functionNode
							} // end for functionNodeList
						} // end if traceNode
					} // end for traceNodeList
				} // end if epochNode
			} // end for epochNodeList
		} catch (Exception e) {
			System.err.println("Fatal error: unable to parse XML file. Entomologist will exit now.");
			e.printStackTrace();
			System.exit(1);
		}
		return bug;
	}

	public static void printBug(Bug bug) {
		Epoch epoch;
		Function function;
		Expression expression;
		System.out.println("Bug:");
		System.out.println("---------");
		System.out.println("Category: " + bug.getCategory());
		System.out.println("File Name: " + bug.getFileName());
		System.out.println("Function Name: " + bug.getFunctionName());
		System.out.println("Line Number: " + bug.getLineNo());
		System.out.println("------------------------------------------------------");
		for (int i = 0; i < bug.getEpochNum(); i++) {
			epoch = bug.getEpoch(i);
			System.out.println("Epoch Step: " + epoch.getEpochStep());
			System.out.println("---------------");
			for (int j = 0; j < epoch.getFunctionNum(); j++) {
				function = epoch.getFunction(j);
				System.out.println("Function: " + function.getFunctionName() + " file: " + function.getFileName() + " Lineno." + function.getLineNo());
				System.out.println("-----------------------------------------------------");
				for (int k = 0; k < function.getExpressionNum(); k++) {
					expression = function.getExpression(k);
					System.out.println(expression.getName() + " = " + expression.getValue());
				}
				System.out.println("");
			}
			System.out.println("");
		}
	}
	
	
	
	// This method is used for debugging
	/*
	private static void printAllNodes(NodeList nodeList) {
		NodeList childNodes;
		for (int i = 0; i < nodeList.getLength(); i++) {
			if (nodeList.item(i).getNodeType() == Node.ELEMENT_NODE) {
				System.out.println(nodeList.item(i).getNodeName() + " type ="
						+ nodeList.item(i).getNodeType());
				childNodes = nodeList.item(i).getChildNodes();
				printAllNodes(childNodes);
			}
		}
	}
	*/
}