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 package net.sf.jtreemap.ktreemap.example;
34
35 import java.io.File;
36 import java.io.IOException;
37 import java.text.ParseException;
38
39 import javax.xml.parsers.DocumentBuilder;
40 import javax.xml.parsers.DocumentBuilderFactory;
41 import javax.xml.parsers.ParserConfigurationException;
42
43 import net.sf.jtreemap.ktreemap.*;
44 import org.w3c.dom.Document;
45 import org.w3c.dom.Element;
46 import org.w3c.dom.Node;
47 import org.w3c.dom.NodeList;
48 import org.xml.sax.SAXException;
49
50 /**
51 * Parse a XML file to build the tree. <BR>
52 *
53 * @author Laurent Dutheil
54 */
55
56 public class BuilderXML extends TreeMapNodeBuilder {
57 private static final String BRANCH = "branch";
58 private static final String LEAF = "leaf";
59 private static final String LABEL = "label";
60 private static final String WEIGHT = "weight";
61 private static final String VALUE = "value";
62 private Document document;
63
64 /**
65 * Constructor
66 *
67 * @param file XML file name
68 * @throws ParseException if the file don't correspond to the TreeMap.dtd
69 */
70 public BuilderXML(File file) throws ParseException {
71 parse(file);
72 }
73
74 private void build(Element elmt, TreeMapNode parent) throws ParseException {
75 TreeMapNode tmn = null;
76 if (elmt.getElementsByTagName(LABEL).getLength() == 0) {
77 throw new ParseException("The file don't correspond to the TreeMap.dtd",
78 0);
79 }
80 String label = ((Element) elmt.getElementsByTagName(LABEL).item(0))
81 .getChildNodes().item(0).getNodeValue();
82
83 XMLBean bean = new XMLBean();
84 bean.setLabel(label);
85
86 tmn = buildBranch(bean, parent);
87
88 NodeList children = elmt.getChildNodes();
89 for (int i = 0; i < children.getLength(); i++) {
90 Node node = children.item(i);
91 if (node instanceof Element) {
92 Element child = (Element) node;
93
94 String childName = child.getTagName();
95 if (BRANCH.equals(childName)) {
96 build(child, tmn);
97 } else if (LEAF.equals(childName)) {
98 NodeList labels = child.getElementsByTagName(LABEL);
99 label = ((Element) labels.item(0)).getChildNodes().item(0)
100 .getNodeValue();
101 NodeList values = child.getElementsByTagName(VALUE);
102 String valueString = ((Element) values.item(0)).getChildNodes().item(
103 0).getNodeValue();
104 NodeList weights = child.getElementsByTagName(WEIGHT);
105 String weightString = ((Element) weights.item(0)).getChildNodes()
106 .item(0).getNodeValue();
107
108 XMLBean beanChild = new XMLBean();
109 beanChild.setLabel(label);
110 beanChild.setValue(Double.valueOf(valueString)
111 .doubleValue());
112 beanChild.setWeight(Double.valueOf(weightString).doubleValue());
113
114 buildLeaf(beanChild, tmn);
115
116 }
117
118 }
119 }
120
121 }
122
123 private void parse(File file) throws ParseException {
124 try {
125 DocumentBuilderFactory fabrique = DocumentBuilderFactory.newInstance();
126
127 DocumentBuilder constructeur = fabrique.newDocumentBuilder();
128 this.document = constructeur.parse(file);
129
130 Element root = this.document.getDocumentElement();
131
132 build(root, null);
133 } catch (ParserConfigurationException e) {
134 throw new ParseException("The file don't correspond to the TreeMap.dtd ("
135 + e.getMessage() + ")", 0);
136 } catch (SAXException e) {
137 throw new ParseException("The file don't correspond to the TreeMap.dtd ("
138 + e.getMessage() + ")", 0);
139 } catch (IOException e) {
140 throw new ParseException("The file don't correspond to the TreeMap.dtd ("
141 + e.getMessage() + ")", 0);
142 }
143
144 }
145
146 @Override
147 public double getWeight(Object value) {
148 if (value instanceof XMLBean) {
149 XMLBean bean = (XMLBean)value;
150 return bean.getWeight();
151 }
152 return 0;
153 }
154 }
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170