Source code: com/aendvari/common/model/osm/OsmModelTree.java
1 /*
2 * OsmModelTree.java
3 *
4 * Copyright (c) 2001, 2002 Aendvari, Ltd. All Rights Reserved.
5 *
6 * See the file LICENSE for terms of use.
7 *
8 */
9
10 package com.aendvari.common.model.osm;
11
12 import java.util.*;
13 import java.io.*;
14
15 import java.lang.reflect.*;
16
17 import com.aendvari.common.util.ResourceLoader;
18
19 import com.aendvari.common.model.ModelNode;
20 import com.aendvari.common.model.ModelTree;
21 import com.aendvari.common.model.ModelException;
22 import com.aendvari.common.model.ModelParserException;
23
24 import com.aendvari.common.osm.Osm;
25 import com.aendvari.common.osm.OsmReader;
26 import com.aendvari.common.osm.OsmNode;
27 import com.aendvari.common.osm.QueryOsmPath;
28 import com.aendvari.common.osm.SimpleOsmPath;
29
30
31 /**
32 * <p>An OSM implementation of the {@link ModelNode} interface.</p>
33 *
34 * @author Scott Milne
35 *
36 */
37
38 public class OsmModelTree implements ModelTree
39 {
40 /* Variables */
41
42 /** The OSM that represents the model tree */
43 protected Osm modelTreeOsm;
44
45
46 /* Constructors. */
47
48
49 /**
50 * Constructs an <code>OsmModelTree</code> instance.
51 *
52 */
53
54 public OsmModelTree()
55 {
56 // create modelTreeOsm hierarchy
57 modelTreeOsm = new Osm();
58 }
59
60 /**
61 * Constructs an <code>OsmModelNode</code> instance wrapping the supplied {@link Osm}.
62 *
63 * @param setModelTree The {@link Osm} to wrap.
64 *
65 */
66
67 public OsmModelTree( Osm setModelTree )
68 {
69 modelTreeOsm = setModelTree;
70 }
71
72
73 /* Accessors. */
74
75
76 /**
77 * Sets the internal {@link Osm} object wrapped by this {@link ModelTree}.
78 *
79 * @param setModelTree The {@link Osm} to wrap.
80 *
81 */
82
83 public void setModelTree( Osm setModelTree )
84 {
85 modelTreeOsm = setModelTree;
86 }
87
88 /**
89 * Returns the internal {@link Osm} object wrapped by this {@link ModelTree}.
90 *
91 * @return The {@link Osm} wrapped by this {@link ModelTree}.
92 *
93 */
94
95 public Osm getModelTree()
96 {
97 return modelTreeOsm;
98 }
99
100 /**
101 * Creates a new {@link ModelTree} object.
102 *
103 * @return A new {@link ModelTree} object.
104 *
105 */
106
107 public ModelTree createTree()
108 {
109 return new OsmModelTree();
110 }
111
112 /**
113 * Returns the {@link ModelNode} of the OSM model space.
114 *
115 * @return A {@link ModelNode} representing the root node of the {@link Osm}.
116 *
117 */
118
119 public ModelNode getRootNode()
120 {
121 return new OsmModelNode(modelTreeOsm);
122 }
123
124 /**
125 * Creates a {@link ModelNode} object from an {@link OsmNode}
126 *
127 * @param osmNode The {@link OsmNode}.
128 *
129 * @return A new {@link ModelNode} object.
130 *
131 */
132
133 public ModelNode createNode(OsmNode osmNode)
134 {
135 return new OsmModelNode(osmNode);
136 }
137
138 /**
139 * Creates a {@link ModelNode} object.
140 *
141 * @param name The node's name.
142 *
143 * @return A new {@link ModelNode} object.
144 *
145 */
146
147 public ModelNode createNode(String name)
148 {
149 // create a new node for the message
150 OsmNode osmNode = modelTreeOsm.createNode(name);
151
152 return new OsmModelNode(osmNode);
153 }
154
155 /**
156 * Creates a {@link ModelNode} object with the supplied value.
157 *
158 * @param name The node's name.
159 * @param value The node's value.
160 *
161 * @return A new {@link ModelNode} object.
162 *
163 */
164
165 public ModelNode createNode(String name, String value)
166 {
167 // create a new node for the message
168 OsmNode osmNode = modelTreeOsm.createNode(name, value);
169
170 // set the value in case it was created
171 osmNode.setNodeValue(value);
172
173 // return the new node
174 return new OsmModelNode(osmNode);
175 }
176
177 /**
178 * Returns the {@link ModelNode} using the path provided.
179 *
180 * @param node The starting position for the search.
181 * @param path The query expression.
182 *
183 * @return A {@link ModelNode} using the path provided.
184 *
185 * @exception ModelException The search could not be performed.
186 *
187 */
188
189 public ModelNode getNode( ModelNode node, String path ) throws ModelException
190 {
191 // transform the given model node into an OSM node
192 OsmNode parentOsmNode = ((OsmModelNode)node).getOsmNode();
193
194 // get the node for the messages
195 OsmNode osmNode = QueryOsmPath.selectNode(parentOsmNode, path);
196
197 // return node, if found
198 if (osmNode != null)
199 return new OsmModelNode(osmNode);
200 else
201 return null;
202 }
203
204 /**
205 * Returns a <code>List</code> of {@link ModelNode}'s using the path provided.
206 *
207 * @param node The starting position for the search.
208 * @param path The query expression.
209 *
210 * @return A <code>List</code> of {@link ModelNode}'s using the path provided.
211 *
212 * @exception ModelException The search could not be performed.
213 *
214 */
215
216 public List getNodes( ModelNode node, String path ) throws ModelException
217 {
218 // transform the given model node into an OSM node
219 OsmNode parentOsmNode = ((OsmModelNode)node).getOsmNode();
220
221 // get the node for the messages
222 Collection osmNodes = QueryOsmPath.selectNodes(parentOsmNode, path);
223 Iterator osmIterator = osmNodes.iterator();
224
225 // convert to collection of OsmModelNode's
226 ArrayList nodes = new ArrayList();
227
228 while (osmIterator.hasNext())
229 {
230 nodes.add( new OsmModelNode((OsmNode)osmIterator.next()) );
231 }
232
233 return nodes;
234 }
235
236
237 /**
238 * Replaces the current {@link ModelTree} with data prodivided by the file.
239 *
240 * @param xmlFile A path to the XML file.
241 *
242 * @exception ModelParserException The file could not be parsed.
243 *
244 */
245
246 public void loadFromFile( String xmlFile ) throws ModelParserException
247 {
248 try
249 {
250 FileInputStream in = new FileInputStream(xmlFile);
251
252 InputStreamReader inputReader = new InputStreamReader(in);
253
254 BufferedReader bufferedReader = new BufferedReader(inputReader);
255
256 modelTreeOsm = OsmReader.read(bufferedReader);
257 }
258 catch (Exception exception)
259 {
260 throw new ModelParserException(exception);
261 }
262 }
263
264 /**
265 * Replaces the current {@link ModelTree} with the given XML stream.
266 *
267 * @param xmlStream A {@link java.io.InputStream} instance.
268 *
269 * @exception ModelParserException The stream could not be parsed.
270 *
271 */
272
273 public void loadFromStream( InputStream xmlStream ) throws ModelParserException
274 {
275 try
276 {
277 InputStreamReader inputReader = new InputStreamReader(xmlStream);
278
279 BufferedReader bufferedReader = new BufferedReader(inputReader);
280
281 modelTreeOsm = OsmReader.read(bufferedReader);
282 }
283 catch (Exception exception)
284 {
285 throw new ModelParserException(exception);
286 }
287 }
288 }
289