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.swing.example;
34
35 import java.io.File;
36 import java.io.IOException;
37 import java.io.InputStream;
38 import java.text.ParseException;
39
40 import javax.xml.parsers.DocumentBuilder;
41 import javax.xml.parsers.DocumentBuilderFactory;
42 import javax.xml.parsers.ParserConfigurationException;
43
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 import lombok.extern.slf4j.Slf4j;
51 import net.sf.jtreemap.swing.TreeMapNode;
52 import net.sf.jtreemap.swing.TreeMapNodeBuilder;
53 import net.sf.jtreemap.swing.Value;
54 import net.sf.jtreemap.swing.ValuePercent;
55
56
57
58
59
60
61 @Slf4j
62 public class BuilderXML {
63 private static final String THE_FILE_DON_T_CORRESPOND_TO_THE_TREE_MAP_DTD = "The file don't correspond to the TreeMap.dtd (";
64 private static final String BRANCH = "branch";
65 private static final String LEAF = "leaf";
66 private static final String LABEL = "label";
67 private static final String WEIGHT = "weight";
68 private static final String VALUE = "value";
69 private Document document;
70 private final TreeMapNodeBuilder builder;
71
72
73
74
75
76
77
78
79
80 public BuilderXML(final String stFileName) throws ParseException {
81 this.builder = new TreeMapNodeBuilder();
82 parse(new File(stFileName));
83 }
84
85
86
87
88
89
90
91
92
93 public BuilderXML(final File stFile) throws ParseException {
94 this.builder = new TreeMapNodeBuilder();
95 parse(stFile);
96 }
97
98
99
100
101
102
103
104
105
106 public BuilderXML(final InputStream stream) throws ParseException {
107 this.builder = new TreeMapNodeBuilder();
108 parse(stream);
109 }
110
111
112
113
114
115
116 public TreeMapNode getRoot() {
117 return this.builder.getRoot();
118 }
119
120 private void build(final Element elmt, final TreeMapNode parent) throws ParseException {
121 TreeMapNode tmn = null;
122 if (elmt.getElementsByTagName(LABEL).getLength() == 0) {
123 throw new ParseException("The file don't correspond to the TreeMap.dtd", 0);
124 }
125 String label = ((Element) elmt.getElementsByTagName(LABEL).item(0)).getChildNodes().item(0).getNodeValue();
126
127 tmn = this.builder.buildBranch(label, parent);
128
129 final NodeList children = elmt.getChildNodes();
130 for (int i = 0; i < children.getLength(); i++) {
131 final Node node = children.item(i);
132 if (node instanceof Element) {
133 final Element child = (Element) node;
134
135 final String childName = child.getTagName();
136 if (BRANCH.equals(childName)) {
137 build(child, tmn);
138 } else if (LEAF.equals(childName)) {
139 final NodeList labels = child.getElementsByTagName(LABEL);
140 label = ((Element) labels.item(0)).getChildNodes().item(0).getNodeValue();
141 final NodeList values = child.getElementsByTagName(VALUE);
142 final String valueString = ((Element) values.item(0)).getChildNodes().item(0).getNodeValue();
143 final NodeList weights = child.getElementsByTagName(WEIGHT);
144 final String weightString = ((Element) weights.item(0)).getChildNodes().item(0).getNodeValue();
145 final Value value = new ValuePercent(Double.parseDouble(valueString));
146 final double weight = Double.parseDouble(weightString);
147
148 this.builder.buildLeaf(label, weight, value, tmn);
149 }
150 }
151 }
152 }
153
154 private void parse(final File stFile) throws ParseException {
155 try {
156 final DocumentBuilderFactory fabrique = DocumentBuilderFactory.newInstance();
157
158 final DocumentBuilder constructeur = fabrique.newDocumentBuilder();
159 this.document = constructeur.parse(stFile);
160
161 final Element root = this.document.getDocumentElement();
162
163 build(root, null);
164 } catch (final ParserConfigurationException | SAXException | IOException e) {
165 throw new ParseException(THE_FILE_DON_T_CORRESPOND_TO_THE_TREE_MAP_DTD + e.getMessage() + ")", 0);
166 }
167 }
168
169 private void parse(final InputStream stream) throws ParseException {
170 try {
171 final DocumentBuilderFactory fabrique = DocumentBuilderFactory.newInstance();
172
173 final DocumentBuilder constructeur = fabrique.newDocumentBuilder();
174 this.document = constructeur.parse(stream);
175
176 final Element root = this.document.getDocumentElement();
177
178 build(root, null);
179 } catch (final ParserConfigurationException | SAXException | IOException e) {
180 log.error("IO Issue", e);
181 throw new ParseException(THE_FILE_DON_T_CORRESPOND_TO_THE_TREE_MAP_DTD + e.getMessage() + ")", 0);
182 }
183 }
184 }
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200