Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/aendvari/common/model/osm/OsmModelNode.java


1   /*
2    * OsmModelNode.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 com.aendvari.common.model.ModelNode;
16  import com.aendvari.common.model.ModelTree;
17  
18  import com.aendvari.common.osm.Osm;
19  import com.aendvari.common.osm.OsmNode;
20  import com.aendvari.common.osm.QueryOsmPath;
21  
22  
23  /**
24   * <p>An OSM implementation of the {@link ModelNode} interface.</p>
25   *
26   * @author  Scott Milne
27   *
28   */
29  
30  public class OsmModelNode implements ModelNode
31  {
32    /* Variables */
33  
34    /** The OsmNode */
35    protected OsmNode modelNode;
36  
37  
38    /* Constructors. */
39  
40  
41    /**
42     * Constructs an <code>OsmModelNode</code> instance.
43     *
44     */
45  
46    protected OsmModelNode()
47    {
48      modelNode = null;
49    }
50  
51    /**
52     * Constructs an <code>OsmModelNode</code> instance wrapping the supplied {@link OsmNode}.
53     *
54     * @param    setModelNode        The {@link OsmNode} to wrap.
55     *
56     */
57  
58    protected OsmModelNode( OsmNode setModelNode )
59    {
60      modelNode = setModelNode;
61    }
62  
63  
64    /* Accessors. */
65  
66  
67    /**
68     * Sets the internal {@link OsmNode} object wrapped by this {@link ModelNode}.
69     *
70     * @param    setModelNode        The {@link OsmNode} to wrap.
71     *
72     */
73  
74    public void setOsmNode( OsmNode setModelNode )
75    {
76      modelNode = setModelNode;
77    }
78  
79    /**
80     * Returns the internal {@link OsmNode} object wrapped by this {@link ModelNode}.
81     *
82     * @return                  The {@link OsmNode} wrapped by this {@link ModelNode}.
83     *
84     */
85  
86    public OsmNode getOsmNode()
87    {
88      return modelNode;
89    }
90  
91    /**
92     * Returns the instance of {@link ModelTree} from which the {@link ModelNode} is within.
93     *
94     * @return                  The {@link ModelTree} instance this node is within.
95     *
96     */
97  
98    public ModelTree getOwnerModelTree()
99    {
100     return new OsmModelTree( modelNode.getOwnerOsm() );
101   }
102 
103   /**
104    * Returns the value of the node.
105    *
106    * @return                  A String value of the node.
107    *
108    */
109 
110   public String getNodeValue()
111   {
112     if( modelNode != null && modelNode.getNodeValue() != null )
113       return modelNode.getNodeValue().toString();
114 
115     return "";
116   }
117 
118   /**
119    * Sets the value of the node.
120    *
121    * @param    value            An String value for the node.
122    *
123    */
124 
125   public void setNodeValue( String value )
126   {
127     if( modelNode != null )
128     {
129       modelNode.setNodeValue(value);
130     }
131   }
132 
133   /**
134    * Returns the name of this node.
135    *
136    * @return                  The name of this node.
137    *
138    */
139 
140   public String getNodeName()
141   {
142     return modelNode.getNodeName();
143   }
144 
145 
146   /**
147    * Returns a string representation of the node's position in the hierarchy.
148    *
149    * @return                  The position of this node in the hierarchy.
150    *
151    */
152 
153   public String getNodePath()
154   {
155     return modelNode.getNodePath();
156   }
157 
158   /**
159    * Returns the attributes of this node.
160    *
161    * @return                  A <code>Map</code> of all attibutes.
162    *
163    */
164 
165   public Map getAttributes()
166   {
167     return modelNode.getAttributes();
168   }
169 
170   /**
171    * Returns the string value of the specified attribute.
172    *
173    * @param                  The name of the attribute.
174    *
175    * @return                  The string of the attribute.
176    *
177    */
178 
179   public String getAttribute( String name )
180   {
181     Object value = modelNode.getAttribute(name);
182 
183     if (value == null)
184       return "";
185     else
186       return value.toString();
187   }
188 
189   /**
190    * Sets the string value of the attribute.
191    *
192    * @param    name            The name of the attribute.
193    * @param    value            The value of the attribute.
194    *
195    */
196 
197   public void setAttribute( String name, String value )
198   {
199     modelNode.setAttribute(name, value);
200   }
201 
202 
203   /* Node access. */
204 
205 
206   /**
207    * The parent of this node. All nodes, except the root ({@link ModelNode})
208    * node may have a parent. If a node has just been created and not yet added to the tree, or if
209    * it has been removed from the tree, this is null.
210    *
211    * @return                  The parent of this node.
212    *
213    */
214 
215   public ModelNode getParentNode()
216   {
217     OsmNode parentNode = modelNode.getParentNode();
218 
219     if( parentNode != null )
220     {
221       return new OsmModelNode(parentNode);
222     }
223 
224     return null;
225   }
226 
227   /**
228    * The first child of this node. If there is no such node, this returns null.
229    *
230    * @return                  The first child node.
231    *
232    */
233 
234   public ModelNode getFirstChild()
235   {
236     OsmNode node = modelNode.getFirstChild();
237 
238     if( node != null )
239     {
240       return new OsmModelNode(node);
241     }
242 
243     return null;
244   }
245 
246   /**
247    * The last child of this node. If there is no such node, this returns null.
248    *
249    * @return                  The last child node.
250    *
251    */
252 
253   public ModelNode getLastChild()
254   {
255     OsmNode node = modelNode.getLastChild();
256 
257     if( node != null )
258     {
259       return new OsmModelNode(node);
260     }
261 
262     return null;
263   }
264 
265   /**
266    * The node immediately preceding this node. If there is no such node,
267    * this returns null.
268    *
269    * @return                  The preceding sibling node.
270    *
271    */
272 
273   public ModelNode getPreviousSibling()
274   {
275     OsmNode node = modelNode.getPreviousSibling();
276 
277     if( node != null )
278     {
279       return new OsmModelNode(node);
280     }
281 
282     return null;
283   }
284 
285   /**
286    * The node immediately following this node. If there is no such node,
287    * this returns null.
288    *
289    * @return                  The following sibling node.
290    *
291    */
292 
293   public ModelNode getNextSibling()
294   {
295     OsmNode node = modelNode.getNextSibling();
296 
297     if( node != null )
298     {
299       return new OsmModelNode(node);
300     }
301 
302     return null;
303   }
304 
305   /**
306    * Returns an <code>Iterator</code> of {@link ModelNode}'s using the path provided.
307    *
308    * @return                  An <code>Iterator<code> of this nodes' children as {@link ModelNode}'s.
309    *
310    */
311 
312   public List getChildNodes()
313   {
314     // convert internal OsmNode's to ModelNode's
315     ArrayList children = new ArrayList();
316     Iterator childIterator = modelNode.getChildNodes().iterator();
317 
318     while (childIterator.hasNext())
319     {
320       OsmNode child = (OsmNode)childIterator.next();
321       children.add(new OsmModelNode(child));
322     }
323 
324     return children;
325   }
326 
327   /**
328    * Returns whether this node has any children.
329    *
330    * @return                  True if this node has children, false otherwise.
331    *
332    */
333 
334   public boolean hasChildNodes()
335   {
336     return modelNode.hasChildNodes();
337   }
338 
339 
340   /* Node manipulation. */
341 
342 
343   /**
344    * Adds the node <code>newChild</code> to the end of the list of children of this node.
345    * If the <code>newChild</code> is already in the tree, it is first removed.
346    *
347    * @param    newChild          The node to add.
348    *
349    * @return                  The node added.
350    *
351    */
352 
353   public ModelNode appendChild(ModelNode newChild)
354   {
355     OsmNode childNode = ((OsmModelNode)newChild).getOsmNode();
356     OsmNode node = modelNode.appendChild(childNode);
357     return new OsmModelNode(node);
358   }
359 
360   /**
361    * Inserts the node <code>newChild</code> before the existing child node <code>refChild</code>.
362    * If <code>refChild</code> is null, <code>newChild</code> is inserted at the end of the list
363    * of children. If the <code>newChild</code> is already in the tree, it is first removed.
364    *
365    * @param    newChild          The node to insert.
366    * @param    refChild          The reference node.
367    *
368    * @return                  The node inserted.
369    *
370    */
371 
372   public ModelNode insertBefore(ModelNode newChild, ModelNode refChild)
373   {
374     OsmNode xNewChild = ((OsmModelNode)newChild).getOsmNode();
375     OsmNode xRefChild = ((OsmModelNode)refChild).getOsmNode();
376 
377     OsmNode node = modelNode.insertBefore(xNewChild, xRefChild);
378     return new OsmModelNode(node);
379   }
380 
381   /**
382    * Replaces the child node <code>oldChild</code> with <code>newChild</code>, and returns
383    * the <code>oldChild</code> node. If the <code>newChild</code> is already in the tree,
384    * it is first removed.
385    *
386    * @param    newChild          The node to replace.
387    * @param    oldChild          The node being replaced.
388    *
389    * @return                  The node replaced.
390    *
391    */
392 
393   public ModelNode replaceChild(ModelNode newChild, ModelNode oldChild)
394   {
395     OsmNode xNewChild = ((OsmModelNode)newChild).getOsmNode();
396     OsmNode xOldChild = ((OsmModelNode)oldChild).getOsmNode();
397 
398     OsmNode node = modelNode.replaceChild(xNewChild, xOldChild);
399     return new OsmModelNode(node);
400   }
401 
402   /**
403    * Removes the child node indicated by <code>oldChild</code> from the list of children,
404    * and returns it.
405    *
406    * @param    oldChild          The node to remove.
407    *
408    * @return                  The removed node.
409    *
410    */
411 
412   public ModelNode removeChild(ModelNode oldChild)
413   {
414     OsmNode xOldNode = ((OsmModelNode)oldChild).getOsmNode();
415     OsmNode node = modelNode.removeChild(xOldNode);
416     return new OsmModelNode(node);
417   }
418 
419   /**
420    * Display the string value of the node.
421    *
422    */
423 
424   public String toString()
425   {
426     return modelNode.toString();
427   }
428 }
429