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: BuilderXML.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.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  *                 ObjectLab is supporing JTreeMap
157  * 
158  * Based in London, we are world leaders in the design and development 
159  * of bespoke applications for the securities financing markets.
160  * 
161  * <a href="http://www.objectlab.co.uk/open">Click here to learn more about us</a>
162  *           ___  _     _           _   _          _
163  *          / _ \| |__ (_) ___  ___| |_| |    __ _| |__
164  *         | | | | '_ \| |/ _ \/ __| __| |   / _` | '_ \
165  *         | |_| | |_) | |  __/ (__| |_| |__| (_| | |_) |
166  *          \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
167  *                   |__/
168  *
169  *                     www.ObjectLab.co.uk
170  */