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 96 2006-11-06 20:05:53Z ekingulen $
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.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 net.sf.jtreemap.swing.TreeMapNode;
45  import net.sf.jtreemap.swing.TreeMapNodeBuilder;
46  import net.sf.jtreemap.swing.Value;
47  import net.sf.jtreemap.swing.ValuePercent;
48  
49  import org.w3c.dom.Document;
50  import org.w3c.dom.Element;
51  import org.w3c.dom.Node;
52  import org.w3c.dom.NodeList;
53  import org.xml.sax.SAXException;
54  
55  /**
56   * Parse a XML file to build the tree. <BR>
57   * 
58   * @author Laurent Dutheil
59   */
60  
61  public class BuilderXML {
62      private static final String BRANCH = "branch";
63  
64      private static final String LEAF = "leaf";
65  
66      private static final String LABEL = "label";
67  
68      private static final String WEIGHT = "weight";
69  
70      private static final String VALUE = "value";
71  
72      private Document document;
73  
74      private TreeMapNodeBuilder builder;
75  
76      /**
77       * Constructor
78       * 
79       * @param stFileName
80       *            XML file name
81       * @throws ParseException
82       *             if the file don't correspond to the TreeMap.dtd
83       */
84      public BuilderXML(final String stFileName) throws ParseException {
85          this.builder = new TreeMapNodeBuilder();
86          parse(new File(stFileName));
87      }
88  
89      /**
90       * Constructor
91       * 
92       * @param stFile
93       *            XML file object
94       * @throws ParseException
95       *             if the file don't correspond to the TreeMap.dtd
96       */
97      public BuilderXML(final File stFile) throws ParseException {
98          this.builder = new TreeMapNodeBuilder();
99          parse(stFile);
100     }
101 
102     /**
103      * Constructor
104      * 
105      * @param stream
106      *            <code>InputStream </code> representing XML file object
107      * @throws ParseException
108      *             if the file don't correspond to the TreeMap.dtd
109      */
110     public BuilderXML(final InputStream stream) throws ParseException {
111         this.builder = new TreeMapNodeBuilder();
112         parse(stream);
113     }
114 
115     /**
116      * get the build root
117      * 
118      * @return the build root
119      */
120     public TreeMapNode getRoot() {
121         return this.builder.getRoot();
122     }
123 
124     private void build(final Element elmt, final TreeMapNode parent) throws ParseException {
125         TreeMapNode tmn = null;
126         if (elmt.getElementsByTagName(LABEL).getLength() == 0) {
127             throw new ParseException("The file don't correspond to the TreeMap.dtd", 0);
128         }
129         String label = ((Element) elmt.getElementsByTagName(LABEL).item(0)).getChildNodes().item(0).getNodeValue();
130 
131         tmn = this.builder.buildBranch(label, parent);
132 
133         final NodeList children = elmt.getChildNodes();
134         for (int i = 0; i < children.getLength(); i++) {
135             final Node node = children.item(i);
136             if (node instanceof Element) {
137                 final Element child = (Element) node;
138 
139                 final String childName = child.getTagName();
140                 if (BRANCH.equals(childName)) {
141                     build(child, tmn);
142                 } else if (LEAF.equals(childName)) {
143                     final NodeList labels = child.getElementsByTagName(LABEL);
144                     label = ((Element) labels.item(0)).getChildNodes().item(0).getNodeValue();
145                     final NodeList values = child.getElementsByTagName(VALUE);
146                     final String valueString = ((Element) values.item(0)).getChildNodes().item(0).getNodeValue();
147                     final NodeList weights = child.getElementsByTagName(WEIGHT);
148                     final String weightString = ((Element) weights.item(0)).getChildNodes().item(0).getNodeValue();
149                     final Value value = new ValuePercent(Double.valueOf(valueString).doubleValue());
150                     final double weight = Double.valueOf(weightString).doubleValue();
151 
152                     this.builder.buildLeaf(label, weight, value, tmn);
153                 }
154 
155             }
156         }
157     }
158 
159     private void parse(final File stFile) throws ParseException {
160         try {
161             final DocumentBuilderFactory fabrique = DocumentBuilderFactory.newInstance();
162 
163             final DocumentBuilder constructeur = fabrique.newDocumentBuilder();
164             this.document = constructeur.parse(stFile);
165 
166             final Element root = this.document.getDocumentElement();
167 
168             build(root, null);
169         } catch (final ParserConfigurationException e) {
170             throw new ParseException("The file don't correspond to the TreeMap.dtd (" + e.getMessage() + ")", 0);
171         } catch (final SAXException e) {
172             throw new ParseException("The file don't correspond to the TreeMap.dtd (" + e.getMessage() + ")", 0);
173         } catch (final IOException e) {
174             throw new ParseException("The file don't correspond to the TreeMap.dtd (" + e.getMessage() + ")", 0);
175         }
176     }
177 
178     private void parse(final InputStream stream) throws ParseException {
179         try {
180             final DocumentBuilderFactory fabrique = DocumentBuilderFactory.newInstance();
181 
182             final DocumentBuilder constructeur = fabrique.newDocumentBuilder();
183             this.document = constructeur.parse(stream);
184 
185             final Element root = this.document.getDocumentElement();
186 
187             build(root, null);
188         } catch (final ParserConfigurationException e) {
189             throw new ParseException("The file don't correspond to the TreeMap.dtd (" + e.getMessage() + ")", 0);
190         } catch (final SAXException e) {
191             throw new ParseException("The file don't correspond to the TreeMap.dtd (" + e.getMessage() + ")", 0);
192         } catch (final IOException e) {
193             e.printStackTrace();
194             throw new ParseException("The file don't correspond to the TreeMap.dtd (" + e.getMessage() + ")", 0);
195         }
196     }
197 }
198 /*
199  *                 ObjectLab is supporing JTreeMap
200  * 
201  * Based in London, we are world leaders in the design and development 
202  * of bespoke applications for the securities financing markets.
203  * 
204  * <a href="http://www.objectlab.co.uk/open">Click here to learn more about us</a>
205  *           ___  _     _           _   _          _
206  *          / _ \| |__ (_) ___  ___| |_| |    __ _| |__
207  *         | | | | '_ \| |/ _ \/ __| __| |   / _` | '_ \
208  *         | |_| | |_) | |  __/ (__| |_| |__| (_| | |_) |
209  *          \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
210  *                   |__/
211  *
212  *                     www.ObjectLab.co.uk
213  */