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.BufferedReader;
36 import java.io.File;
37 import java.io.FileReader;
38 import java.io.IOException;
39 import java.text.ParseException;
40 import java.util.Date;
41 import java.util.HashMap;
42 import java.util.Iterator;
43 import java.util.LinkedList;
44 import java.util.StringTokenizer;
45
46 import net.sf.jtreemap.ktreemap.TreeMapNode;
47 import net.sf.jtreemap.ktreemap.TreeMapNodeBuilder;
48
49 /**
50 * Parse a TM3 file to build the tree. <BR>
51 * See <a href=http://www.cs.umd.edu/hcil/treemap/doc4.1/create_TM3_file.html>
52 * how to create your own TM3 data file </a> from hcil Treemap site.
53 *
54 * @author Laurent DUTHEIL
55 */
56 public class BuilderTM3 extends TreeMapNodeBuilder {
57 private static String fieldWeight = "";
58 private HashMap<TreeMapNode, TM3Bean> leaves = new HashMap<TreeMapNode, TM3Bean>();
59
60 /**
61 * Constructor
62 *
63 * @param tm3File tm3 file
64 * @throws IOException
65 */
66 public BuilderTM3(File tm3File) throws IOException {
67 parse(tm3File);
68 }
69
70 /**
71 * Set the weights of all the TreeMapNode with the values of the fieldName.
72 *
73 * @param fieldName name of the field to set the weights
74 */
75 public void setWeights() {
76 if ("".equals(getFieldWeight())) {
77 for (TreeMapNode node : leaves.keySet()) {
78 node.setWeight(1);
79 }
80 } else {
81 for (TreeMapNode node : leaves.keySet()) {
82 TM3Bean bean = leaves.get(node);
83 node.setWeight(getWeight(bean));
84 }
85 }
86 }
87
88 /**
89 * @param st StringTokenizer which contains the hierarchy path
90 * @param mapNodeValues HashMap with fields and their values
91 */
92 private void createNodes(StringTokenizer st, TM3Bean beanLeaf) {
93
94 LinkedList<String> hierarchyPath = new LinkedList<String>();
95 while (st.hasMoreTokens()) {
96 hierarchyPath.add(st.nextToken());
97 }
98
99 TreeMapNode node = getRoot();
100 if (node == null) {
101 TM3Bean bean = new TM3Bean();
102 bean.setLabel(hierarchyPath.get(0));
103 node = buildBranch(bean, null);
104 }
105 for (int i = 1; i < hierarchyPath.size() - 1; i++) {
106
107 boolean found = false;
108 TreeMapNode child = null;
109 for (Iterator<TreeMapNode> iter = node.getChildren().iterator(); iter
110 .hasNext();) {
111 child = iter.next();
112 TM3Bean value = (TM3Bean)child.getValue();
113 if (value.getLabel().equals(hierarchyPath.get(i))) {
114 found = true;
115 break;
116 }
117 }
118 if (found) {
119 node = child;
120 } else {
121 TM3Bean bean = new TM3Bean();
122 bean.setLabel(hierarchyPath.get(i));
123 node = buildBranch(bean, node);
124 }
125 }
126
127
128 beanLeaf.setLabel(hierarchyPath.getLast());
129 TreeMapNode leafNode = buildLeaf(beanLeaf, node);
130 leaves.put(leafNode, beanLeaf);
131 }
132
133 /**
134 * @param tm3File TM3 file
135 * @throws IOException
136 */
137 private void parse(File tm3File) throws IOException {
138 BufferedReader in = new BufferedReader(new FileReader(tm3File));
139 String line = "";
140
141 line = in.readLine();
142 StringTokenizer st = new StringTokenizer(line, "\t");
143 TM3Bean.fieldNames.clear();
144 while (st.hasMoreTokens()) {
145 TM3Bean.fieldNames.add(st.nextToken());
146 }
147
148
149 line = in.readLine();
150 st = new StringTokenizer(line, "\t");
151 TM3Bean.fieldTypes.clear();
152 while (st.hasMoreTokens()) {
153 TM3Bean.fieldTypes.add(st.nextToken());
154 }
155
156 leaves.clear();
157
158
159 while ((line = in.readLine()) != null) {
160 st = new StringTokenizer(line, "\t");
161 TM3Bean bean = new TM3Bean();
162
163 for (int i = 0; i < TM3Bean.fieldNames.size(); i++) {
164 Object value;
165 if (TM3Bean.FLOAT.equals(TM3Bean.fieldTypes.get(i))) {
166 value = new Double(Double.parseDouble(st.nextToken()));
167 } else if (TM3Bean.INTEGER.equals(TM3Bean.fieldTypes.get(i))) {
168 value = new Integer(Integer.parseInt(st.nextToken()));
169 } else if (TM3Bean.DATE.equals(TM3Bean.fieldTypes.get(i))) {
170 try {
171 value = TM3Bean.DATE_FORMAT.parse(st.nextToken());
172 } catch (ParseException e) {
173 value = null;
174 }
175 } else {
176 value = st.nextToken();
177 }
178 bean.setValue(TM3Bean.fieldNames.get(i), value);
179 }
180
181
182 if (!st.hasMoreTokens()) {
183
184 throw new IOException("the file didn't contains the hierarchy path");
185 }
186
187
188 createNodes(st, bean);
189 }
190
191 }
192
193 @Override
194 public double getWeight(Object value) {
195 if (value instanceof TM3Bean) {
196 TM3Bean bean = (TM3Bean)value;
197 Object weight = bean.getValue(getFieldWeight());
198 if (weight instanceof Number) {
199 Number number = (Number)weight;
200 return number.doubleValue();
201 } else if (weight instanceof Date) {
202 Date date = (Date)weight;
203 return date.getTime();
204 }
205
206 }
207 return 1;
208 }
209
210 /**
211 * @return the fieldWeight
212 */
213 public static String getFieldWeight() {
214 return fieldWeight;
215 }
216
217 /**
218 * @param fieldWeight the fieldWeight to set
219 */
220 public static void setFieldWeight(String fieldWeight) {
221 BuilderTM3.fieldWeight = fieldWeight;
222 }
223 }
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239