Source code: com/aendvari/common/osm/Osm.java
1 /*
2 * Osm.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.osm;
11
12 import java.util.Iterator;
13
14 import com.aendvari.common.osm.OsmNode;
15 import com.aendvari.common.osm.SimpleOsmPath;
16 import com.aendvari.common.osm.QueryOsmPath;
17
18 /**
19 * <p>Represents the entire Object Space Model. It is used as the
20 * root of the tree. The class also contains factory methods to create internal
21 * objects (such as the {@link OsmNode} class).</p>
22 *
23 * @author Trevor Milne
24 *
25 */
26
27 public class Osm extends OsmNode
28 {
29 /* Constructors. */
30
31
32 /**
33 * Constructs an <code>Osm</code> instance.
34 *
35 */
36
37 public Osm()
38 {
39 super("OsmRoot", null);
40 }
41
42 /**
43 * Constructs an <code>Osm</code> instance as a copy of the supplied <code>Osm<code>.
44 *
45 * Any objects stored as values or attributes are not copied, only their references
46 * are maintained.
47 *
48 * @param source The {@link Osm} to copy.
49 *
50 */
51
52 public Osm(Osm source)
53 {
54 this();
55
56 // clone the source Osm
57 this.cloneOsm(source);
58 }
59
60 /**
61 * Returns a duplicate of this <code>Osm</code>.
62 *
63 * Any objects stored as values or attributes are not copied, only their references
64 * are maintained.
65 *
66 * @return The copied {@link Osm}.
67 *
68 */
69
70 public Osm cloneOsm()
71 {
72 // copy this osm into a new one
73 Osm osm = new Osm();
74 osm.cloneOsm(this);
75
76 return osm;
77 }
78
79 /**
80 * Duplicates the supplied {@link Osm}. Existing nodes in this OSM will be
81 * removed.
82 *
83 * Any objects stored as values or attributes are not copied, only their references
84 * are maintained.
85 *
86 * @param source The {@link Osm} to duplicate.
87 *
88 */
89
90 public void cloneOsm(Osm source)
91 {
92 // create copy of source
93 OsmNode copy = source.cloneNode(true);
94
95 // remove any existing children
96 this.removeChildNodes();
97
98 // add children of copy
99 Iterator childIterator = copy.children.iterator();
100
101 while (childIterator.hasNext())
102 {
103 OsmNode node = (OsmNode)childIterator.next();
104 this.appendChild(node);
105 }
106 }
107
108
109 /* Building. */
110
111
112 /**
113 * Creates an {@link OsmNode} object.
114 *
115 * @param name The node's name.
116 *
117 * @return A new {@link OsmNode} object.
118 *
119 */
120
121 public OsmNode createNode(String name)
122 {
123 return new OsmNode(name, null);
124 }
125
126 /**
127 * Creates an {@link OsmNode} object with the supplied value.
128 *
129 * @param name The node's name.
130 * @param value The node's value.
131 *
132 * @return A new {@link OsmNode} object.
133 *
134 */
135
136 public OsmNode createNode(String name, Object value)
137 {
138 return new OsmNode(name, value);
139 }
140
141 /**
142 * Creates an {@link OsmNode} object as a copy of the supplied node.
143 * Only the value name, value, and attributes are copied.
144 *
145 * @param node The {@link OsmNode} to copy.
146 *
147 * @return A new {@link OsmNode} object.
148 *
149 */
150
151 public OsmNode createNode(OsmNode node)
152 {
153 return new OsmNode(node);
154 }
155 }
156