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$
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 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   * Parse a XML file to build the tree. <BR>
58   *
59   * @author Laurent Dutheil
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       * Constructor
74       *
75       * @param stFileName
76       *            XML file name
77       * @throws ParseException
78       *             if the file don't correspond to the TreeMap.dtd
79       */
80      public BuilderXML(final String stFileName) throws ParseException {
81          this.builder = new TreeMapNodeBuilder();
82          parse(new File(stFileName));
83      }
84  
85      /**
86       * Constructor
87       *
88       * @param stFile
89       *            XML file object
90       * @throws ParseException
91       *             if the file don't correspond to the TreeMap.dtd
92       */
93      public BuilderXML(final File stFile) throws ParseException {
94          this.builder = new TreeMapNodeBuilder();
95          parse(stFile);
96      }
97  
98      /**
99       * Constructor
100      *
101      * @param stream
102      *            <code>InputStream </code> representing XML file object
103      * @throws ParseException
104      *             if the file don't correspond to the TreeMap.dtd
105      */
106     public BuilderXML(final InputStream stream) throws ParseException {
107         this.builder = new TreeMapNodeBuilder();
108         parse(stream);
109     }
110 
111     /**
112      * get the build root
113      *
114      * @return the build root
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  *                 ObjectLab is supporing JTreeMap
187  *
188  * Based in London, we are world leaders in the design and development
189  * of bespoke applications for the securities financing markets.
190  *
191  * <a href="http://www.objectlab.co.uk/open">Click here to learn more about us</a>
192  *           ___  _     _           _   _          _
193  *          / _ \| |__ (_) ___  ___| |_| |    __ _| |__
194  *         | | | | '_ \| |/ _ \/ __| __| |   / _` | '_ \
195  *         | |_| | |_) | |  __/ (__| |_| |__| (_| | |_) |
196  *          \___/|_.__// |\___|\___|\__|_____\__,_|_.__/
197  *                   |__/
198  *
199  *                     www.ObjectLab.co.uk
200  */