View Javadoc

1   /*
2    * ObjectLab, http://www.objectlab.co.uk/open is supporting JTreeMap.
3    * 
4    * Based in London, we are world leaders in the design and development 
5    * of bespoke applications for the securities financing markets.
6    * 
7    * <a href="http://www.objectlab.co.uk/open">Click here to learn more</a>
8    *           ___  _     _           _   _          _
9    *          / _ \| |__ (_) ___  ___| |_| |    __ _| |__
10   *         | | | | '_ \| |/ _ \/ __| __| |   / _` | '_ \
11   *         | |_| | |_) | |  __/ (__| |_| |__| (_| | |_) |
12   *          \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
13   *                   |__/
14   *
15   *                     www.ObjectLab.co.uk
16   *
17   * $Id: BuilderTM3.java 75 2006-10-24 23:00:51Z benoitx $
18   * 
19   * Copyright 2006 the original author or authors.
20   *
21   * Licensed under the Apache License, Version 2.0 (the "License"); you may not
22   * use this file except in compliance with the License. You may obtain a copy of
23   * the License at
24   *
25   * http://www.apache.org/licenses/LICENSE-2.0
26   *
27   * Unless required by applicable law or agreed to in writing, software
28   * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
29   * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
30   * License for the specific language governing permissions and limitations under
31   * the License.
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      // read the hierarchy path
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       // looking for the child
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     // create the leaf
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     // read the field names
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     // read the field types
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     // read the values
159     while ((line = in.readLine()) != null) {
160       st = new StringTokenizer(line, "\t");
161       TM3Bean bean = new TM3Bean();
162       // the values are formated
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       // if we have not the path (the node names of parents)
182       if (!st.hasMoreTokens()) {
183         // we throw an exception, because we can't build the treemap
184         throw new IOException("the file didn't contains the hierarchy path");
185       }
186 
187       // create the nodes
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  *                 ObjectLab is supporing JTreeMap
226  * 
227  * Based in London, we are world leaders in the design and development 
228  * of bespoke applications for the securities financing markets.
229  * 
230  * <a href="http://www.objectlab.co.uk/open">Click here to learn more about us</a>
231  *           ___  _     _           _   _          _
232  *          / _ \| |__ (_) ___  ___| |_| |    __ _| |__
233  *         | | | | '_ \| |/ _ \/ __| __| |   / _` | '_ \
234  *         | |_| | |_) | |  __/ (__| |_| |__| (_| | |_) |
235  *          \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
236  *                   |__/
237  *
238  *                     www.ObjectLab.co.uk
239  */