-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFileReader.java
More file actions
127 lines (118 loc) · 3.39 KB
/
FileReader.java
File metadata and controls
127 lines (118 loc) · 3.39 KB
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
package restrictedGame;
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import datastructures.BinaryTreeNode;
import datastructures.DefaultBinaryTree;
import datastructures.DefaultBinaryTreeNode;
/**
* Parse xml file and create a decision tree
*
* @author Ching2 Huang
*
*/
public class FileReader {
private DefaultBinaryTree<String> tree; // the decision tree
private DefaultBinaryTreeNode<String> root; // the root of the tree
/**
* Constructor reads a file and parse the file
*/
public FileReader() {
try {
// Setup XML Document
DocumentBuilderFactory factory = DocumentBuilderFactory
.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
File xmlFile = new File("DecisionTree.xml");
Document document = builder.parse(xmlFile);
// navigate through the passed in document
parseTree(document);
} catch (ParserConfigurationException pce) {
// print out the exception if this exception happens
System.out.println("ParserConfigurationException");
} catch (SAXException saxe) {
// print out the exception if this exception happens
System.out.println("SAXException");
} catch (IOException ioe) {
// print out the exception if this exception happens
System.out.println("IOException");
}
}
/**
* takes in a document and add data to the tree
*
* @param document
*/
private void parseTree(Document doc) {
// create a tree
tree = new DefaultBinaryTree<String>();
root = parseNode(doc.getDocumentElement());
tree.setRoot(root);
}
/**
* Analyze a node
*
* @param node
* @return a new binary tree node
*/
private DefaultBinaryTreeNode<String> parseNode(Node node) {
// creates a question node
DefaultBinaryTreeNode<String> qNode = new DefaultBinaryTreeNode<String>();
// check if type of node
if (node.getNodeType() == Node.ELEMENT_NODE) {
Element currentElt = (Element) node;
// if it has statement attribute
if (currentElt.hasAttribute("statement")) {
// set the data for the node
qNode.setData(currentElt.getAttribute("statement"));
} else {
// otherwise, get the data and set it to the node
qNode.setData(node.getTextContent());
}
}
// recursive step: if the node has a child, call the method again to
// analyze the node
if (node.hasChildNodes()) {
// put all the child nodes in a list
NodeList childList = node.getChildNodes();
// check every node
for (int i = 0; i < childList.getLength(); i++) {
// if it's an element node
if (childList.item(i).getNodeType() == Node.ELEMENT_NODE) {
// if there's no left child
if (qNode.getLeftChild() == null) {
// set the left child
qNode.setLeftChild(parseNode(childList.item(i)));
} else {
// set the right child
qNode.setRightChild(parseNode(childList.item(i)));
}
}
}
}
return qNode;
}
/**
* print out the leaves/answers of the tree
*
* @return possible answers
*/
public String answers(BinaryTreeNode<String> node) {
return tree.printLeaves(node);
}
/**
* get decision tree
*
* @return the tree
*/
public DefaultBinaryTree<String> getTree() {
return tree;
}
}