1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37 package jnlp.sample.servlet;
38
39 import java.io.PrintWriter;
40 import java.io.StringWriter;
41
42
43
44
45 public class XMLNode
46 {
47 private boolean _isElement;
48
49 private String _name;
50
51 private XMLAttribute _attr;
52
53 private XMLNode _parent;
54
55 private XMLNode _nested;
56
57 private XMLNode _next;
58
59
60
61
62 public XMLNode( String name )
63 {
64 this( name, null, null, null );
65 _isElement = false;
66 }
67
68
69
70
71 public XMLNode( String name, XMLAttribute attr )
72 {
73 this( name, attr, null, null );
74 }
75
76
77
78
79 public XMLNode( String name, XMLAttribute attr, XMLNode nested, XMLNode next )
80 {
81 _isElement = true;
82 _name = name;
83 _attr = attr;
84 _nested = nested;
85 _next = next;
86 _parent = null;
87 }
88
89 public String getName()
90 {
91 return _name;
92 }
93
94 public XMLAttribute getAttributes()
95 {
96 return _attr;
97 }
98
99 public XMLNode getNested()
100 {
101 return _nested;
102 }
103
104 public XMLNode getNext()
105 {
106 return _next;
107 }
108
109 public boolean isElement()
110 {
111 return _isElement;
112 }
113
114 public void setParent( XMLNode parent )
115 {
116 _parent = parent;
117 }
118
119 public XMLNode getParent()
120 {
121 return _parent;
122 }
123
124 public void setNext( XMLNode next )
125 {
126 _next = next;
127 }
128
129 public void setNested( XMLNode nested )
130 {
131 _nested = nested;
132 }
133
134 public boolean equals( Object o )
135 {
136 if ( o == null || !( o instanceof XMLNode ) )
137 {
138 return false;
139 }
140 XMLNode other = (XMLNode) o;
141 boolean result =
142 match( _name, other._name ) && match( _attr, other._attr ) && match( _nested, other._nested ) &&
143 match( _next, other._next );
144 return result;
145 }
146
147 public String getAttribute( String name )
148 {
149 XMLAttribute cur = _attr;
150 while ( cur != null )
151 {
152 if ( name.equals( cur.getName() ) )
153 {
154 return cur.getValue();
155 }
156 cur = cur.getNext();
157 }
158 return "";
159 }
160
161 private static boolean match( Object o1, Object o2 )
162 {
163 if ( o1 == null )
164 {
165 return ( o2 == null );
166 }
167 return o1.equals( o2 );
168 }
169
170 public void printToStream( PrintWriter out )
171 {
172 printToStream( out, 0 );
173 }
174
175 public void printToStream( PrintWriter out, int n )
176 {
177 if ( !isElement() )
178 {
179 out.print( _name );
180 }
181 else
182 {
183 if ( _nested == null )
184 {
185 String attrString = ( _attr == null ) ? "" : ( " " + _attr.toString() );
186 lineln( out, n, "<" + _name + attrString + "/>" );
187 }
188 else
189 {
190 String attrString = ( _attr == null ) ? "" : ( " " + _attr.toString() );
191 lineln( out, n, "<" + _name + attrString + ">" );
192 _nested.printToStream( out, n + 1 );
193 if ( _nested.isElement() )
194 {
195 lineln( out, n, "</" + _name + ">" );
196 }
197 else
198 {
199 out.print( "</" + _name + ">" );
200 }
201 }
202 }
203 if ( _next != null )
204 {
205 _next.printToStream( out, n );
206 }
207 }
208
209 private static void lineln( PrintWriter out, int indent, String s )
210 {
211 out.println( "" );
212 for ( int i = 0; i < indent; i++ )
213 {
214 out.print( " " );
215 }
216 out.print( s );
217 }
218
219 public String toString()
220 {
221 StringWriter sw = new StringWriter( 1000 );
222 PrintWriter pw = new PrintWriter( sw );
223 printToStream( pw );
224 pw.close();
225 return sw.toString();
226 }
227 }
228
229