root / import / ext3D / trunk / install-extension3d / IzPack / src / lib / net / n3 / nanoxml / XMLElement.java @ 15280
History | View | Annotate | Download (11.5 KB)
1 |
/* XMLElement.java NanoXML/Java
|
---|---|
2 |
*
|
3 |
* $Revision: 1.1 $
|
4 |
* $Date: 2006/06/14 07:29:07 $
|
5 |
* $Name: $
|
6 |
*
|
7 |
* This file is part of NanoXML 2 for Java.
|
8 |
* Copyright (C) 2001 Marc De Scheemaecker, All Rights Reserved.
|
9 |
*
|
10 |
* This software is provided 'as-is', without any express or implied warranty.
|
11 |
* In no event will the authors be held liable for any damages arising from the
|
12 |
* use of this software.
|
13 |
*
|
14 |
* Permission is granted to anyone to use this software for any purpose,
|
15 |
* including commercial applications, and to alter it and redistribute it
|
16 |
* freely, subject to the following restrictions:
|
17 |
*
|
18 |
* 1. The origin of this software must not be misrepresented; you must not
|
19 |
* claim that you wrote the original software. If you use this software in
|
20 |
* a product, an acknowledgment in the product documentation would be
|
21 |
* appreciated but is not required.
|
22 |
*
|
23 |
* 2. Altered source versions must be plainly marked as such, and must not be
|
24 |
* misrepresented as being the original software.
|
25 |
*
|
26 |
* 3. This notice may not be removed or altered from any source distribution.
|
27 |
*/
|
28 |
|
29 |
package net.n3.nanoxml; |
30 |
|
31 |
|
32 |
import java.io.Serializable; |
33 |
import java.util.Enumeration; |
34 |
import java.util.Properties; |
35 |
import java.util.Vector; |
36 |
|
37 |
|
38 |
/**
|
39 |
* XMLElement is an XML element. The standard NanoXML builder generates a
|
40 |
* tree of such elements.
|
41 |
*
|
42 |
* @see net.n3.nanoxml.StdXMLBuilder
|
43 |
*
|
44 |
* @author Marc De Scheemaecker
|
45 |
* @version $Name: $, $Revision: 1.1 $
|
46 |
*/
|
47 |
public class XMLElement |
48 |
implements Serializable |
49 |
{ |
50 |
|
51 |
/**
|
52 |
* Necessary for serialization.
|
53 |
*/
|
54 |
static final long serialVersionUID = -2383376380548624920L; |
55 |
|
56 |
|
57 |
/**
|
58 |
* No line number defined.
|
59 |
*/
|
60 |
public static final int NO_LINE = -1; |
61 |
|
62 |
|
63 |
/**
|
64 |
* The attributes of the element.
|
65 |
*/
|
66 |
private Properties attributes; |
67 |
|
68 |
|
69 |
/**
|
70 |
* The child elements.
|
71 |
*/
|
72 |
private Vector children; |
73 |
|
74 |
|
75 |
/**
|
76 |
* The name of the element.
|
77 |
*/
|
78 |
private String name; |
79 |
|
80 |
|
81 |
/**
|
82 |
* The content of the element.
|
83 |
*/
|
84 |
private String content; |
85 |
|
86 |
|
87 |
/**
|
88 |
* The system ID of the source data where this element is located.
|
89 |
*/
|
90 |
private String systemID; |
91 |
|
92 |
|
93 |
/**
|
94 |
* The line in the source data where this element starts.
|
95 |
*/
|
96 |
private int lineNr; |
97 |
|
98 |
|
99 |
/**
|
100 |
* Creates an empty element to be used for #PCDATA content.
|
101 |
*/
|
102 |
public XMLElement()
|
103 |
{ |
104 |
this(null, null, NO_LINE); |
105 |
} |
106 |
|
107 |
|
108 |
/**
|
109 |
* Creates an empty element.
|
110 |
*
|
111 |
* @param name the name of the element.
|
112 |
*/
|
113 |
public XMLElement(String name) |
114 |
{ |
115 |
this(name, null, NO_LINE); |
116 |
} |
117 |
|
118 |
|
119 |
/**
|
120 |
* Creates an empty element.
|
121 |
*
|
122 |
* @param name the name of the element.
|
123 |
* @param systemID the system ID of the XML data where the element starts.
|
124 |
* @param lineNr the line in the XML data where the element starts.
|
125 |
*/
|
126 |
public XMLElement(String name, |
127 |
String systemID,
|
128 |
int lineNr)
|
129 |
{ |
130 |
this.attributes = new Properties(); |
131 |
this.children = new Vector(8); |
132 |
this.name = name;
|
133 |
this.content = null; |
134 |
this.lineNr = lineNr;
|
135 |
this.systemID = systemID;
|
136 |
} |
137 |
|
138 |
|
139 |
/**
|
140 |
* Cleans up the object when it's destroyed.
|
141 |
*/
|
142 |
protected void finalize() |
143 |
throws Throwable |
144 |
{ |
145 |
this.attributes = null; |
146 |
this.children = null; |
147 |
this.name = null; |
148 |
this.content = null; |
149 |
this.systemID = null; |
150 |
super.finalize();
|
151 |
} |
152 |
|
153 |
|
154 |
/**
|
155 |
* Returns the name of the element.
|
156 |
*
|
157 |
* @return the name, or null if the element only contains #PCDATA.
|
158 |
*/
|
159 |
public String getName() |
160 |
{ |
161 |
return this.name; |
162 |
} |
163 |
|
164 |
|
165 |
/**
|
166 |
* Sets the name.
|
167 |
*
|
168 |
* @param name the non-null name.
|
169 |
*/
|
170 |
public void setName(String name) |
171 |
{ |
172 |
if (name == null) { |
173 |
throw new IllegalArgumentException("name must not be null"); |
174 |
} |
175 |
|
176 |
this.name = name;
|
177 |
} |
178 |
|
179 |
|
180 |
/**
|
181 |
* Adds a child element.
|
182 |
*
|
183 |
* @param child the non-null child to add.
|
184 |
*/
|
185 |
public void addChild(XMLElement child) |
186 |
{ |
187 |
if (child == null) { |
188 |
throw new IllegalArgumentException("child must not be null"); |
189 |
} |
190 |
|
191 |
if ((child.getName() == null) && (! this.children.isEmpty())) { |
192 |
XMLElement lastChild = (XMLElement) this.children.lastElement();
|
193 |
|
194 |
if (lastChild.getName() == null) { |
195 |
lastChild.setContent(lastChild.getContent() |
196 |
+ child.getContent()); |
197 |
return;
|
198 |
} |
199 |
} |
200 |
|
201 |
this.children.addElement(child);
|
202 |
} |
203 |
|
204 |
|
205 |
/**
|
206 |
* Removes a child element.
|
207 |
*
|
208 |
* @param child the non-null child to remove.
|
209 |
*/
|
210 |
public void removeChild(XMLElement child) |
211 |
{ |
212 |
if (child == null) { |
213 |
throw new IllegalArgumentException("child must not be null"); |
214 |
} |
215 |
|
216 |
this.children.removeElement(child);
|
217 |
} |
218 |
|
219 |
|
220 |
/**
|
221 |
* Removes the child located at a certain index.
|
222 |
*
|
223 |
* @param index the index of the child, where the first child has index 0.
|
224 |
*/
|
225 |
public void removeChildAtIndex(int index) |
226 |
{ |
227 |
this.children.removeElementAt(index);
|
228 |
} |
229 |
|
230 |
|
231 |
/**
|
232 |
* Returns an enumeration of all child elements.
|
233 |
*
|
234 |
* @return the non-null enumeration
|
235 |
*/
|
236 |
public Enumeration enumerateChildren() |
237 |
{ |
238 |
return this.children.elements(); |
239 |
} |
240 |
|
241 |
|
242 |
/**
|
243 |
* Returns whether the element is a leaf element.
|
244 |
*
|
245 |
* @return true if the element has no children.
|
246 |
*/
|
247 |
public boolean isLeaf() |
248 |
{ |
249 |
return this.children.isEmpty(); |
250 |
} |
251 |
|
252 |
|
253 |
/**
|
254 |
* Returns whether the element has children.
|
255 |
*
|
256 |
* @return true if the element has children.
|
257 |
*/
|
258 |
public boolean hasChildren() |
259 |
{ |
260 |
return (! this.children.isEmpty()); |
261 |
} |
262 |
|
263 |
|
264 |
/**
|
265 |
* Returns the number of children.
|
266 |
*
|
267 |
* @return the count.
|
268 |
*/
|
269 |
public int getChildrenCount() |
270 |
{ |
271 |
return this.children.size(); |
272 |
} |
273 |
|
274 |
|
275 |
/**
|
276 |
* Returns a vector containing all the child elements.
|
277 |
*
|
278 |
* @return the vector.
|
279 |
*/
|
280 |
public Vector getChildren() |
281 |
{ |
282 |
return this.children; |
283 |
} |
284 |
|
285 |
|
286 |
/**
|
287 |
* Returns the child at a specific index.
|
288 |
*
|
289 |
* @return the non-null child
|
290 |
*
|
291 |
* @throws java.lang.ArrayIndexOutOfBoundsException
|
292 |
* if the index is out of bounds.
|
293 |
*/
|
294 |
public XMLElement getChildAtIndex(int index) |
295 |
throws ArrayIndexOutOfBoundsException |
296 |
{ |
297 |
return (XMLElement) this.children.elementAt(index); |
298 |
} |
299 |
|
300 |
|
301 |
/**
|
302 |
* Searches a child element.
|
303 |
*
|
304 |
* @param name the name of the child to search for.
|
305 |
*
|
306 |
* @return the child element, or null if no such child was found.
|
307 |
*/
|
308 |
public XMLElement getFirstChildNamed(String name) |
309 |
{ |
310 |
Enumeration enum = this.children.elements(); |
311 |
|
312 |
while (enum.hasMoreElements()) { |
313 |
XMLElement child = (XMLElement) enum.nextElement();
|
314 |
String cName = child.getName();
|
315 |
|
316 |
if (cName != null && cName.equals(name)) { |
317 |
return child;
|
318 |
} |
319 |
} |
320 |
|
321 |
return null; |
322 |
} |
323 |
|
324 |
|
325 |
/**
|
326 |
* Returns a vector of all child elements named <I>name</I>.
|
327 |
*
|
328 |
* @param name the name of the children to search for.
|
329 |
*
|
330 |
* @return the non-null vector of child elements.
|
331 |
*/
|
332 |
public Vector getChildrenNamed(String name) |
333 |
{ |
334 |
Vector result = new Vector(this.children.size()); |
335 |
Enumeration enum = this.children.elements(); |
336 |
|
337 |
while (enum.hasMoreElements()) { |
338 |
XMLElement child = (XMLElement) enum.nextElement();
|
339 |
String cName = child.getName();
|
340 |
|
341 |
if (cName != null && cName.equals(name)) { |
342 |
result.addElement(child); |
343 |
} |
344 |
} |
345 |
|
346 |
return result;
|
347 |
} |
348 |
|
349 |
|
350 |
/**
|
351 |
* Returns the value of an attribute.
|
352 |
*
|
353 |
* @param name the non-null name of the attribute.
|
354 |
*
|
355 |
* @return the value, or null if the attribute does not exist.
|
356 |
*/
|
357 |
public String getAttribute(String name) |
358 |
{ |
359 |
return this.getAttribute(name, null); |
360 |
} |
361 |
|
362 |
|
363 |
/**
|
364 |
* Returns the value of an attribute.
|
365 |
*
|
366 |
* @param name the non-null name of the attribute.
|
367 |
* @param defaultValue the default value of the attribute.
|
368 |
*
|
369 |
* @return the value, or defaultValue if the attribute does not exist.
|
370 |
*/
|
371 |
public String getAttribute(String name, |
372 |
String defaultValue)
|
373 |
{ |
374 |
return this.attributes.getProperty(name, defaultValue); |
375 |
} |
376 |
|
377 |
|
378 |
/**
|
379 |
* Sets an attribute.
|
380 |
*
|
381 |
* @param name the non-null name of the attribute.
|
382 |
* @param value the non-null value of the attribute.
|
383 |
*/
|
384 |
public void setAttribute(String name, |
385 |
String value)
|
386 |
{ |
387 |
this.attributes.put(name, value);
|
388 |
} |
389 |
|
390 |
|
391 |
/**
|
392 |
* Removes an attribute.
|
393 |
*
|
394 |
* @param name the non-null name of the attribute.
|
395 |
*/
|
396 |
public void removeAttribute(String name) |
397 |
{ |
398 |
this.attributes.remove(name);
|
399 |
} |
400 |
|
401 |
|
402 |
/**
|
403 |
* Returns an enumeration of all attribute names.
|
404 |
*
|
405 |
* @return the non-null enumeration.
|
406 |
*/
|
407 |
public Enumeration enumerateAttributeNames() |
408 |
{ |
409 |
return this.attributes.keys(); |
410 |
} |
411 |
|
412 |
|
413 |
/**
|
414 |
* Returns whether an attribute exists.
|
415 |
*
|
416 |
* @return true if the attribute exists.
|
417 |
*/
|
418 |
public boolean hasAttribute(String name) |
419 |
{ |
420 |
return this.attributes.containsKey(name); |
421 |
} |
422 |
|
423 |
|
424 |
/**
|
425 |
* Returns all attributes as a Properties object.
|
426 |
*
|
427 |
* @return the non-null set.
|
428 |
*/
|
429 |
public Properties getAttributes() |
430 |
{ |
431 |
return this.attributes; |
432 |
} |
433 |
|
434 |
|
435 |
/**
|
436 |
* Returns the system ID of the data where the element started.
|
437 |
*
|
438 |
* @return the system ID, or null if unknown.
|
439 |
*
|
440 |
* @see #getLineNr
|
441 |
*/
|
442 |
public String getSystemID() |
443 |
{ |
444 |
return this.systemID; |
445 |
} |
446 |
|
447 |
|
448 |
/**
|
449 |
* Returns the line number in the data where the element started.
|
450 |
*
|
451 |
* @return the line number, or NO_LINE if unknown.
|
452 |
*
|
453 |
* @see #NO_LINE
|
454 |
* @see #getSystemID
|
455 |
*/
|
456 |
public int getLineNr() |
457 |
{ |
458 |
return this.lineNr; |
459 |
} |
460 |
|
461 |
|
462 |
/**
|
463 |
* Return the #PCDATA content of the element. If the element has a
|
464 |
* combination of #PCDATA content and child elements, the #PCDATA
|
465 |
* sections can be retrieved as unnamed child objects. In this case,
|
466 |
* this method returns null.
|
467 |
*
|
468 |
* @return the content.
|
469 |
*/
|
470 |
public String getContent() |
471 |
{ |
472 |
return this.content; |
473 |
} |
474 |
|
475 |
|
476 |
/**
|
477 |
* Sets the #PCDATA content. It is an error to call this method with a
|
478 |
* non-null value if there are child objects.
|
479 |
*
|
480 |
* @param content the (possibly null) content.
|
481 |
*/
|
482 |
public void setContent(String content) |
483 |
{ |
484 |
this.content = content;
|
485 |
} |
486 |
|
487 |
} |