1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 package net.sourceforge.servletspy;
23
24 import java.io.Serializable;
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.Iterator;
28 import java.util.List;
29
30 /***
31 * A <code>Node</code> represents the result of a serialization process.
32 *
33 * @author arno schumacher
34 */
35 public final class Node implements Comparable, Serializable {
36
37 /*** The serial version id. */
38 private static final long serialVersionUID = 3977867288743718964L;
39
40 /*** The parent node. */
41 private final Node parent;
42
43 /***
44 * In case a subject has already been serialized, this node represents the
45 * subgraph for that subject.
46 */
47 private Node referenceNode;
48
49 /*** The name of the node. */
50 private final String name;
51
52 /*** The result of the serialization process. */
53 private String value;
54
55 /*** The childs of this node. */
56 private List childs = new ArrayList();
57
58 /*** For debugging purpose... */
59 private String hint = "";
60
61 /***
62 * Constructor.
63 *
64 * @param pName
65 * The node name
66 * @param pValue
67 * The initial value of the node
68 */
69 public Node(final String pName, final String pValue) {
70 this(null, pName, pValue);
71 }
72
73 /***
74 * Constructor.
75 *
76 * @param pParent
77 * The parent node
78 * @param pName
79 * The node name
80 */
81 public Node(final Node pParent, final String pName) {
82 this(pParent, pName, null);
83 }
84
85 /***
86 * Constructor.
87 *
88 * @param pParent
89 * The parent node
90 * @param pName
91 * The node name
92 * @param pValue
93 * The initial value of the node
94 */
95 public Node(final Node pParent, final String pName, final String pValue) {
96 this.name = (pName == null) ? "<null>" : pName;
97 this.value = (pValue == null) ? "" : pValue;
98 this.parent = pParent;
99 }
100
101
102
103
104 public int compareTo(final Object pArg) {
105 if (pArg == null || !(pArg instanceof Node)) {
106 return 1;
107 }
108 final Node other = (Node) pArg;
109
110 if (this.name == null) {
111 return 1;
112 }
113
114 if (other.name == null) {
115 return -1;
116 }
117
118 if (!this.hasChildNodes() && other.hasChildNodes()) {
119 return -1;
120 }
121 if (this.hasChildNodes() && !other.hasChildNodes()) {
122 return 1;
123 }
124
125 return this.name.compareTo(other.name);
126 }
127
128 /***
129 * @return Returns the value.
130 */
131 public String getValue() {
132 return value;
133 }
134
135 /***
136 * @param pValue
137 * The value to set.
138 */
139 public void setValue(final String pValue) {
140 this.value = pValue;
141 }
142
143 /***
144 * @return Returns the name.
145 */
146 public String getName() {
147 return name;
148 }
149
150 /***
151 * @return Returns the parent.
152 */
153 public Node getParent() {
154 return parent;
155 }
156
157 /***
158 * @return Returns true if and only if the node has child nodes.
159 */
160 public boolean hasChildNodes() {
161 return childs.size() > 0;
162 }
163
164 /***
165 * Adds a child node.
166 *
167 * @param pChild
168 * The child.
169 */
170 public void addChildNode(final Node pChild) {
171 if (pChild != null) {
172 childs.add(pChild);
173 }
174 }
175
176 /***
177 * Returns the childs.
178 *
179 * @return The childs
180 */
181 public Iterator childNodes() {
182 Collections.sort(childs);
183 return Collections.unmodifiableList(childs).iterator();
184 }
185
186 /***
187 * Returns the number of childs.
188 *
189 * @return The number of childs.
190 */
191 public int getNumberOfChild() {
192 return childs.size();
193 }
194
195 /***
196 * Sets the reference node.
197 *
198 * @param pReferenceNode
199 * The refernce node, a pointer to another node in the
200 * serialization graph representing an already processed subject.
201 */
202 public void setReference(final Node pReferenceNode) {
203 if (hasChildNodes()) {
204 throw new IllegalStateException("Childs already added.");
205 }
206 this.referenceNode = pReferenceNode;
207 }
208
209 /***
210 * Returns the reference node.
211 *
212 * @return The refernce node, a pointer to another node in the serialization
213 * graph representing an already processed subject.
214 */
215 public Node getReference() {
216 return referenceNode;
217 }
218
219 /***
220 * Returns true if and only if this node does not hold a serialization
221 * graph, but points to an other node representing an already performed
222 * serialization.
223 *
224 * @return boolean value
225 */
226 public boolean hasReference() {
227 return referenceNode != null;
228 }
229
230 /***
231 * For debugging purpose only.
232 *
233 * @param pHint A hint
234 */
235 public void setDebugHint(final String pHint) {
236 this.hint = pHint;
237 }
238
239 /***
240 * For debugging purpose only.
241 *
242 * @return the debug hint
243 */
244 public String getDebugHint() {
245 return hint;
246 }
247 }