1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/src/share/classes/sun/util/xml/XMLElement.java Wed Jun 24 16:38:24 2009 +0200
1.3 @@ -0,0 +1,2874 @@
1.4 +/* XMLElement.java
1.5 + *
1.6 + * $Revision: 1.4 $
1.7 + * $Date: 2002/03/24 10:27:59 $
1.8 + * $Name: RELEASE_2_2_1 $
1.9 + *
1.10 + * This file is part of NanoXML 2 Lite.
1.11 + * Copyright (C) 2000-2002 Marc De Scheemaecker, All Rights Reserved.
1.12 + *
1.13 + * This software is provided 'as-is', without any express or implied warranty.
1.14 + * In no event will the authors be held liable for any damages arising from the
1.15 + * use of this software.
1.16 + *
1.17 + * Permission is granted to anyone to use this software for any purpose,
1.18 + * including commercial applications, and to alter it and redistribute it
1.19 + * freely, subject to the following restrictions:
1.20 + *
1.21 + * 1. The origin of this software must not be misrepresented; you must not
1.22 + * claim that you wrote the original software. If you use this software in
1.23 + * a product, an acknowledgment in the product documentation would be
1.24 + * appreciated but is not required.
1.25 + *
1.26 + * 2. Altered source versions must be plainly marked as such, and must not be
1.27 + * misrepresented as being the original software.
1.28 + *
1.29 + * 3. This notice may not be removed or altered from any source distribution.
1.30 + *****************************************************************************/
1.31 +
1.32 +
1.33 +package sun.util.xml;
1.34 +
1.35 +
1.36 +import java.io.ByteArrayOutputStream;
1.37 +import java.io.CharArrayReader;
1.38 +import java.io.IOException;
1.39 +import java.io.OutputStreamWriter;
1.40 +import java.io.Reader;
1.41 +import java.io.StringReader;
1.42 +import java.io.Writer;
1.43 +import java.util.Enumeration;
1.44 +import java.util.Hashtable;
1.45 +import java.util.Vector;
1.46 +
1.47 +
1.48 +/**
1.49 + * XMLElement is a representation of an XML object. The object is able to parse
1.50 + * XML code.
1.51 + * <P><DL>
1.52 + * <DT><B>Parsing XML Data</B></DT>
1.53 + * <DD>
1.54 + * You can parse XML data using the following code:
1.55 + * <UL><CODE>
1.56 + * XMLElement xml = new XMLElement();<BR>
1.57 + * FileReader reader = new FileReader("filename.xml");<BR>
1.58 + * xml.parseFromReader(reader);
1.59 + * </CODE></UL></DD></DL>
1.60 + * <DL><DT><B>Retrieving Attributes</B></DT>
1.61 + * <DD>
1.62 + * You can enumerate the attributes of an element using the method
1.63 + * {@link #enumerateAttributeNames() enumerateAttributeNames}.
1.64 + * The attribute values can be retrieved using the method
1.65 + * {@link #getStringAttribute(java.lang.String) getStringAttribute}.
1.66 + * The following example shows how to list the attributes of an element:
1.67 + * <UL><CODE>
1.68 + * XMLElement element = ...;<BR>
1.69 + * Enumeration enumV = element.getAttributeNames();<BR>
1.70 + * while (enumV.hasMoreElements()) {<BR>
1.71 + * String key = (String) enumV.nextElement();<BR>
1.72 + * String value = element.getStringAttribute(key);<BR>
1.73 + * System.out.println(key + " = " + value);<BR>
1.74 + * }
1.75 + * </CODE></UL></DD></DL>
1.76 + * <DL><DT><B>Retrieving Child Elements</B></DT>
1.77 + * <DD>
1.78 + * You can enumerate the children of an element using
1.79 + * {@link #enumerateChildren() enumerateChildren}.
1.80 + * The number of child elements can be retrieved using
1.81 + * {@link #countChildren() countChildren}.
1.82 + * </DD></DL>
1.83 + * <DL><DT><B>Elements Containing Character Data</B></DT>
1.84 + * <DD>
1.85 + * If an elements contains character data, like in the following example:
1.86 + * <UL><CODE>
1.87 + * <title>The Title</title>
1.88 + * </CODE></UL>
1.89 + * you can retrieve that data using the method
1.90 + * {@link #getContent() getContent}.
1.91 + * </DD></DL>
1.92 + * <DL><DT><B>Subclassing XMLElement</B></DT>
1.93 + * <DD>
1.94 + * When subclassing XMLElement, you need to override the method
1.95 + * {@link #createAnotherElement() createAnotherElement}
1.96 + * which has to return a new copy of the receiver.
1.97 + * </DD></DL>
1.98 + * <P>
1.99 + *
1.100 + * @see nanoxml.XMLParseException
1.101 + *
1.102 + * @author Marc De Scheemaecker
1.103 + * <<A href="mailto:cyberelf@mac.com">cyberelf@mac.com</A>>
1.104 + * @version $Name: RELEASE_2_2_1 $, $Revision: 1.4 $
1.105 + */
1.106 +class XMLElement {
1.107 +
1.108 + /**
1.109 + * Serialization serial version ID.
1.110 + */
1.111 + static final long serialVersionUID = 6685035139346394777L;
1.112 +
1.113 +
1.114 + /**
1.115 + * Major version of NanoXML. Classes with the same major and minor
1.116 + * version are binary compatible. Classes with the same major version
1.117 + * are source compatible. If the major version is different, you may
1.118 + * need to modify the client source code.
1.119 + *
1.120 + * @see nanoxml.XMLElement#NANOXML_MINOR_VERSION
1.121 + */
1.122 + public static final int NANOXML_MAJOR_VERSION = 2;
1.123 +
1.124 +
1.125 + /**
1.126 + * Minor version of NanoXML. Classes with the same major and minor
1.127 + * version are binary compatible. Classes with the same major version
1.128 + * are source compatible. If the major version is different, you may
1.129 + * need to modify the client source code.
1.130 + *
1.131 + * @see nanoxml.XMLElement#NANOXML_MAJOR_VERSION
1.132 + */
1.133 + public static final int NANOXML_MINOR_VERSION = 2;
1.134 +
1.135 +
1.136 + /**
1.137 + * The attributes given to the element.
1.138 + *
1.139 + * <dl><dt><b>Invariants:</b></dt><dd>
1.140 + * <ul><li>The field can be empty.
1.141 + * <li>The field is never <code>null</code>.
1.142 + * <li>The keys and the values are strings.
1.143 + * </ul></dd></dl>
1.144 + */
1.145 + private Hashtable attributes;
1.146 +
1.147 +
1.148 + /**
1.149 + * Child elements of the element.
1.150 + *
1.151 + * <dl><dt><b>Invariants:</b></dt><dd>
1.152 + * <ul><li>The field can be empty.
1.153 + * <li>The field is never <code>null</code>.
1.154 + * <li>The elements are instances of <code>XMLElement</code>
1.155 + * or a subclass of <code>XMLElement</code>.
1.156 + * </ul></dd></dl>
1.157 + */
1.158 + private Vector children;
1.159 +
1.160 +
1.161 + /**
1.162 + * The name of the element.
1.163 + *
1.164 + * <dl><dt><b>Invariants:</b></dt><dd>
1.165 + * <ul><li>The field is <code>null</code> iff the element is not
1.166 + * initialized by either parse or setName.
1.167 + * <li>If the field is not <code>null</code>, it's not empty.
1.168 + * <li>If the field is not <code>null</code>, it contains a valid
1.169 + * XML identifier.
1.170 + * </ul></dd></dl>
1.171 + */
1.172 + private String name;
1.173 +
1.174 +
1.175 + /**
1.176 + * The #PCDATA content of the object.
1.177 + *
1.178 + * <dl><dt><b>Invariants:</b></dt><dd>
1.179 + * <ul><li>The field is <code>null</code> iff the element is not a
1.180 + * #PCDATA element.
1.181 + * <li>The field can be any string, including the empty string.
1.182 + * </ul></dd></dl>
1.183 + */
1.184 + private String contents;
1.185 +
1.186 +
1.187 + /**
1.188 + * Conversion table for &...; entities. The keys are the entity names
1.189 + * without the & and ; delimiters.
1.190 + *
1.191 + * <dl><dt><b>Invariants:</b></dt><dd>
1.192 + * <ul><li>The field is never <code>null</code>.
1.193 + * <li>The field always contains the following associations:
1.194 + * "lt" => "<", "gt" => ">",
1.195 + * "quot" => "\"", "apos" => "'",
1.196 + * "amp" => "&"
1.197 + * <li>The keys are strings
1.198 + * <li>The values are char arrays
1.199 + * </ul></dd></dl>
1.200 + */
1.201 + private Hashtable entities;
1.202 +
1.203 +
1.204 + /**
1.205 + * The line number where the element starts.
1.206 + *
1.207 + * <dl><dt><b>Invariants:</b></dt><dd>
1.208 + * <ul><li><code>lineNr >= 0</code>
1.209 + * </ul></dd></dl>
1.210 + */
1.211 + private int lineNr;
1.212 +
1.213 +
1.214 + /**
1.215 + * <code>true</code> if the case of the element and attribute names
1.216 + * are case insensitive.
1.217 + */
1.218 + private boolean ignoreCase;
1.219 +
1.220 +
1.221 + /**
1.222 + * <code>true</code> if the leading and trailing whitespace of #PCDATA
1.223 + * sections have to be ignored.
1.224 + */
1.225 + private boolean ignoreWhitespace;
1.226 +
1.227 +
1.228 + /**
1.229 + * Character read too much.
1.230 + * This character provides push-back functionality to the input reader
1.231 + * without having to use a PushbackReader.
1.232 + * If there is no such character, this field is '\0'.
1.233 + */
1.234 + private char charReadTooMuch;
1.235 +
1.236 +
1.237 + /**
1.238 + * The reader provided by the caller of the parse method.
1.239 + *
1.240 + * <dl><dt><b>Invariants:</b></dt><dd>
1.241 + * <ul><li>The field is not <code>null</code> while the parse method
1.242 + * is running.
1.243 + * </ul></dd></dl>
1.244 + */
1.245 + private Reader reader;
1.246 +
1.247 +
1.248 + /**
1.249 + * The current line number in the source content.
1.250 + *
1.251 + * <dl><dt><b>Invariants:</b></dt><dd>
1.252 + * <ul><li>parserLineNr > 0 while the parse method is running.
1.253 + * </ul></dd></dl>
1.254 + */
1.255 + private int parserLineNr;
1.256 +
1.257 +
1.258 + /**
1.259 + * Creates and initializes a new XML element.
1.260 + * Calling the construction is equivalent to:
1.261 + * <ul><code>new XMLElement(new Hashtable(), false, true)
1.262 + * </code></ul>
1.263 + *
1.264 + * <dl><dt><b>Postconditions:</b></dt><dd>
1.265 + * <ul><li>countChildren() => 0
1.266 + * <li>enumerateChildren() => empty enumeration
1.267 + * <li>enumeratePropertyNames() => empty enumeration
1.268 + * <li>getChildren() => empty vector
1.269 + * <li>getContent() => ""
1.270 + * <li>getLineNr() => 0
1.271 + * <li>getName() => null
1.272 + * </ul></dd></dl>
1.273 + *
1.274 + * @see nanoxml.XMLElement#XMLElement(java.util.Hashtable)
1.275 + * XMLElement(Hashtable)
1.276 + * @see nanoxml.XMLElement#XMLElement(boolean)
1.277 + * @see nanoxml.XMLElement#XMLElement(java.util.Hashtable,boolean)
1.278 + * XMLElement(Hashtable, boolean)
1.279 + */
1.280 + public XMLElement()
1.281 + {
1.282 + this(new Hashtable(), false, true, true);
1.283 + }
1.284 +
1.285 +
1.286 + /**
1.287 + * Creates and initializes a new XML element.
1.288 + * Calling the construction is equivalent to:
1.289 + * <ul><code>new XMLElement(entities, false, true)
1.290 + * </code></ul>
1.291 + *
1.292 + * @param entities
1.293 + * The entity conversion table.
1.294 + *
1.295 + * </dl><dl><dt><b>Preconditions:</b></dt><dd>
1.296 + * <ul><li><code>entities != null</code>
1.297 + * </ul></dd></dl>
1.298 + *
1.299 + * <dl><dt><b>Postconditions:</b></dt><dd>
1.300 + * <ul><li>countChildren() => 0
1.301 + * <li>enumerateChildren() => empty enumeration
1.302 + * <li>enumeratePropertyNames() => empty enumeration
1.303 + * <li>getChildren() => empty vector
1.304 + * <li>getContent() => ""
1.305 + * <li>getLineNr() => 0
1.306 + * <li>getName() => null
1.307 + * </ul></dd></dl><dl>
1.308 + *
1.309 + * @see nanoxml.XMLElement#XMLElement()
1.310 + * @see nanoxml.XMLElement#XMLElement(boolean)
1.311 + * @see nanoxml.XMLElement#XMLElement(java.util.Hashtable,boolean)
1.312 + * XMLElement(Hashtable, boolean)
1.313 + */
1.314 + public XMLElement(Hashtable entities)
1.315 + {
1.316 + this(entities, false, true, true);
1.317 + }
1.318 +
1.319 +
1.320 + /**
1.321 + * Creates and initializes a new XML element.
1.322 + * Calling the construction is equivalent to:
1.323 + * <ul><code>new XMLElement(new Hashtable(), skipLeadingWhitespace, true)
1.324 + * </code></ul>
1.325 + *
1.326 + * @param skipLeadingWhitespace
1.327 + * <code>true</code> if leading and trailing whitespace in PCDATA
1.328 + * content has to be removed.
1.329 + *
1.330 + * </dl><dl><dt><b>Postconditions:</b></dt><dd>
1.331 + * <ul><li>countChildren() => 0
1.332 + * <li>enumerateChildren() => empty enumeration
1.333 + * <li>enumeratePropertyNames() => empty enumeration
1.334 + * <li>getChildren() => empty vector
1.335 + * <li>getContent() => ""
1.336 + * <li>getLineNr() => 0
1.337 + * <li>getName() => null
1.338 + * </ul></dd></dl><dl>
1.339 + *
1.340 + * @see nanoxml.XMLElement#XMLElement()
1.341 + * @see nanoxml.XMLElement#XMLElement(java.util.Hashtable)
1.342 + * XMLElement(Hashtable)
1.343 + * @see nanoxml.XMLElement#XMLElement(java.util.Hashtable,boolean)
1.344 + * XMLElement(Hashtable, boolean)
1.345 + */
1.346 + public XMLElement(boolean skipLeadingWhitespace)
1.347 + {
1.348 + this(new Hashtable(), skipLeadingWhitespace, true, true);
1.349 + }
1.350 +
1.351 +
1.352 + /**
1.353 + * Creates and initializes a new XML element.
1.354 + * Calling the construction is equivalent to:
1.355 + * <ul><code>new XMLElement(entities, skipLeadingWhitespace, true)
1.356 + * </code></ul>
1.357 + *
1.358 + * @param entities
1.359 + * The entity conversion table.
1.360 + * @param skipLeadingWhitespace
1.361 + * <code>true</code> if leading and trailing whitespace in PCDATA
1.362 + * content has to be removed.
1.363 + *
1.364 + * </dl><dl><dt><b>Preconditions:</b></dt><dd>
1.365 + * <ul><li><code>entities != null</code>
1.366 + * </ul></dd></dl>
1.367 + *
1.368 + * <dl><dt><b>Postconditions:</b></dt><dd>
1.369 + * <ul><li>countChildren() => 0
1.370 + * <li>enumerateChildren() => empty enumeration
1.371 + * <li>enumeratePropertyNames() => empty enumeration
1.372 + * <li>getChildren() => empty vector
1.373 + * <li>getContent() => ""
1.374 + * <li>getLineNr() => 0
1.375 + * <li>getName() => null
1.376 + * </ul></dd></dl><dl>
1.377 + *
1.378 + * @see nanoxml.XMLElement#XMLElement()
1.379 + * @see nanoxml.XMLElement#XMLElement(boolean)
1.380 + * @see nanoxml.XMLElement#XMLElement(java.util.Hashtable)
1.381 + * XMLElement(Hashtable)
1.382 + */
1.383 + public XMLElement(Hashtable entities,
1.384 + boolean skipLeadingWhitespace)
1.385 + {
1.386 + this(entities, skipLeadingWhitespace, true, true);
1.387 + }
1.388 +
1.389 +
1.390 + /**
1.391 + * Creates and initializes a new XML element.
1.392 + *
1.393 + * @param entities
1.394 + * The entity conversion table.
1.395 + * @param skipLeadingWhitespace
1.396 + * <code>true</code> if leading and trailing whitespace in PCDATA
1.397 + * content has to be removed.
1.398 + * @param ignoreCase
1.399 + * <code>true</code> if the case of element and attribute names have
1.400 + * to be ignored.
1.401 + *
1.402 + * </dl><dl><dt><b>Preconditions:</b></dt><dd>
1.403 + * <ul><li><code>entities != null</code>
1.404 + * </ul></dd></dl>
1.405 + *
1.406 + * <dl><dt><b>Postconditions:</b></dt><dd>
1.407 + * <ul><li>countChildren() => 0
1.408 + * <li>enumerateChildren() => empty enumeration
1.409 + * <li>enumeratePropertyNames() => empty enumeration
1.410 + * <li>getChildren() => empty vector
1.411 + * <li>getContent() => ""
1.412 + * <li>getLineNr() => 0
1.413 + * <li>getName() => null
1.414 + * </ul></dd></dl><dl>
1.415 + *
1.416 + * @see nanoxml.XMLElement#XMLElement()
1.417 + * @see nanoxml.XMLElement#XMLElement(boolean)
1.418 + * @see nanoxml.XMLElement#XMLElement(java.util.Hashtable)
1.419 + * XMLElement(Hashtable)
1.420 + * @see nanoxml.XMLElement#XMLElement(java.util.Hashtable,boolean)
1.421 + * XMLElement(Hashtable, boolean)
1.422 + */
1.423 + public XMLElement(Hashtable entities,
1.424 + boolean skipLeadingWhitespace,
1.425 + boolean ignoreCase)
1.426 + {
1.427 + this(entities, skipLeadingWhitespace, true, ignoreCase);
1.428 + }
1.429 +
1.430 +
1.431 + /**
1.432 + * Creates and initializes a new XML element.
1.433 + * <P>
1.434 + * This constructor should <I>only</I> be called from
1.435 + * {@link #createAnotherElement() createAnotherElement}
1.436 + * to create child elements.
1.437 + *
1.438 + * @param entities
1.439 + * The entity conversion table.
1.440 + * @param skipLeadingWhitespace
1.441 + * <code>true</code> if leading and trailing whitespace in PCDATA
1.442 + * content has to be removed.
1.443 + * @param fillBasicConversionTable
1.444 + * <code>true</code> if the basic entities need to be added to
1.445 + * the entity list.
1.446 + * @param ignoreCase
1.447 + * <code>true</code> if the case of element and attribute names have
1.448 + * to be ignored.
1.449 + *
1.450 + * </dl><dl><dt><b>Preconditions:</b></dt><dd>
1.451 + * <ul><li><code>entities != null</code>
1.452 + * <li>if <code>fillBasicConversionTable == false</code>
1.453 + * then <code>entities</code> contains at least the following
1.454 + * entries: <code>amp</code>, <code>lt</code>, <code>gt</code>,
1.455 + * <code>apos</code> and <code>quot</code>
1.456 + * </ul></dd></dl>
1.457 + *
1.458 + * <dl><dt><b>Postconditions:</b></dt><dd>
1.459 + * <ul><li>countChildren() => 0
1.460 + * <li>enumerateChildren() => empty enumeration
1.461 + * <li>enumeratePropertyNames() => empty enumeration
1.462 + * <li>getChildren() => empty vector
1.463 + * <li>getContent() => ""
1.464 + * <li>getLineNr() => 0
1.465 + * <li>getName() => null
1.466 + * </ul></dd></dl><dl>
1.467 + *
1.468 + * @see nanoxml.XMLElement#createAnotherElement()
1.469 + */
1.470 + protected XMLElement(Hashtable entities,
1.471 + boolean skipLeadingWhitespace,
1.472 + boolean fillBasicConversionTable,
1.473 + boolean ignoreCase)
1.474 + {
1.475 + this.ignoreWhitespace = skipLeadingWhitespace;
1.476 + this.ignoreCase = ignoreCase;
1.477 + this.name = null;
1.478 + this.contents = "";
1.479 + this.attributes = new Hashtable();
1.480 + this.children = new Vector();
1.481 + this.entities = entities;
1.482 + this.lineNr = 0;
1.483 + Enumeration enumV = this.entities.keys();
1.484 + while (enumV.hasMoreElements()) {
1.485 + Object key = enumV.nextElement();
1.486 + Object value = this.entities.get(key);
1.487 + if (value instanceof String) {
1.488 + value = ((String) value).toCharArray();
1.489 + this.entities.put(key, value);
1.490 + }
1.491 + }
1.492 + if (fillBasicConversionTable) {
1.493 + this.entities.put("amp", new char[] { '&' });
1.494 + this.entities.put("quot", new char[] { '"' });
1.495 + this.entities.put("apos", new char[] { '\'' });
1.496 + this.entities.put("lt", new char[] { '<' });
1.497 + this.entities.put("gt", new char[] { '>' });
1.498 + }
1.499 + }
1.500 +
1.501 +
1.502 + /**
1.503 + * Adds a child element.
1.504 + *
1.505 + * @param child
1.506 + * The child element to add.
1.507 + *
1.508 + * </dl><dl><dt><b>Preconditions:</b></dt><dd>
1.509 + * <ul><li><code>child != null</code>
1.510 + * <li><code>child.getName() != null</code>
1.511 + * <li><code>child</code> does not have a parent element
1.512 + * </ul></dd></dl>
1.513 + *
1.514 + * <dl><dt><b>Postconditions:</b></dt><dd>
1.515 + * <ul><li>countChildren() => old.countChildren() + 1
1.516 + * <li>enumerateChildren() => old.enumerateChildren() + child
1.517 + * <li>getChildren() => old.enumerateChildren() + child
1.518 + * </ul></dd></dl><dl>
1.519 + *
1.520 + * @see nanoxml.XMLElement#countChildren()
1.521 + * @see nanoxml.XMLElement#enumerateChildren()
1.522 + * @see nanoxml.XMLElement#getChildren()
1.523 + * @see nanoxml.XMLElement#removeChild(nanoxml.XMLElement)
1.524 + * removeChild(XMLElement)
1.525 + */
1.526 + public void addChild(XMLElement child)
1.527 + {
1.528 + this.children.addElement(child);
1.529 + }
1.530 +
1.531 +
1.532 + /**
1.533 + * Adds or modifies an attribute.
1.534 + *
1.535 + * @param name
1.536 + * The name of the attribute.
1.537 + * @param value
1.538 + * The value of the attribute.
1.539 + *
1.540 + * </dl><dl><dt><b>Preconditions:</b></dt><dd>
1.541 + * <ul><li><code>name != null</code>
1.542 + * <li><code>name</code> is a valid XML identifier
1.543 + * <li><code>value != null</code>
1.544 + * </ul></dd></dl>
1.545 + *
1.546 + * <dl><dt><b>Postconditions:</b></dt><dd>
1.547 + * <ul><li>enumerateAttributeNames()
1.548 + * => old.enumerateAttributeNames() + name
1.549 + * <li>getAttribute(name) => value
1.550 + * </ul></dd></dl><dl>
1.551 + *
1.552 + * @see nanoxml.XMLElement#setDoubleAttribute(java.lang.String, double)
1.553 + * setDoubleAttribute(String, double)
1.554 + * @see nanoxml.XMLElement#setIntAttribute(java.lang.String, int)
1.555 + * setIntAttribute(String, int)
1.556 + * @see nanoxml.XMLElement#enumerateAttributeNames()
1.557 + * @see nanoxml.XMLElement#getAttribute(java.lang.String)
1.558 + * getAttribute(String)
1.559 + * @see nanoxml.XMLElement#getAttribute(java.lang.String, java.lang.Object)
1.560 + * getAttribute(String, Object)
1.561 + * @see nanoxml.XMLElement#getAttribute(java.lang.String,
1.562 + * java.util.Hashtable,
1.563 + * java.lang.String, boolean)
1.564 + * getAttribute(String, Hashtable, String, boolean)
1.565 + * @see nanoxml.XMLElement#getStringAttribute(java.lang.String)
1.566 + * getStringAttribute(String)
1.567 + * @see nanoxml.XMLElement#getStringAttribute(java.lang.String,
1.568 + * java.lang.String)
1.569 + * getStringAttribute(String, String)
1.570 + * @see nanoxml.XMLElement#getStringAttribute(java.lang.String,
1.571 + * java.util.Hashtable,
1.572 + * java.lang.String, boolean)
1.573 + * getStringAttribute(String, Hashtable, String, boolean)
1.574 + */
1.575 + public void setAttribute(String name,
1.576 + Object value)
1.577 + {
1.578 + if (this.ignoreCase) {
1.579 + name = name.toUpperCase();
1.580 + }
1.581 + this.attributes.put(name, value.toString());
1.582 + }
1.583 +
1.584 +
1.585 + /**
1.586 + * Adds or modifies an attribute.
1.587 + *
1.588 + * @param name
1.589 + * The name of the attribute.
1.590 + * @param value
1.591 + * The value of the attribute.
1.592 + *
1.593 + * @deprecated Use {@link #setAttribute(java.lang.String, java.lang.Object)
1.594 + * setAttribute} instead.
1.595 + */
1.596 + public void addProperty(String name,
1.597 + Object value)
1.598 + {
1.599 + this.setAttribute(name, value);
1.600 + }
1.601 +
1.602 +
1.603 + /**
1.604 + * Adds or modifies an attribute.
1.605 + *
1.606 + * @param name
1.607 + * The name of the attribute.
1.608 + * @param value
1.609 + * The value of the attribute.
1.610 + *
1.611 + * </dl><dl><dt><b>Preconditions:</b></dt><dd>
1.612 + * <ul><li><code>name != null</code>
1.613 + * <li><code>name</code> is a valid XML identifier
1.614 + * </ul></dd></dl>
1.615 + *
1.616 + * <dl><dt><b>Postconditions:</b></dt><dd>
1.617 + * <ul><li>enumerateAttributeNames()
1.618 + * => old.enumerateAttributeNames() + name
1.619 + * <li>getIntAttribute(name) => value
1.620 + * </ul></dd></dl><dl>
1.621 + *
1.622 + * @see nanoxml.XMLElement#setDoubleAttribute(java.lang.String, double)
1.623 + * setDoubleAttribute(String, double)
1.624 + * @see nanoxml.XMLElement#setAttribute(java.lang.String, java.lang.Object)
1.625 + * setAttribute(String, Object)
1.626 + * @see nanoxml.XMLElement#removeAttribute(java.lang.String)
1.627 + * removeAttribute(String)
1.628 + * @see nanoxml.XMLElement#enumerateAttributeNames()
1.629 + * @see nanoxml.XMLElement#getIntAttribute(java.lang.String)
1.630 + * getIntAttribute(String)
1.631 + * @see nanoxml.XMLElement#getIntAttribute(java.lang.String, int)
1.632 + * getIntAttribute(String, int)
1.633 + * @see nanoxml.XMLElement#getIntAttribute(java.lang.String,
1.634 + * java.util.Hashtable,
1.635 + * java.lang.String, boolean)
1.636 + * getIntAttribute(String, Hashtable, String, boolean)
1.637 + */
1.638 + public void setIntAttribute(String name,
1.639 + int value)
1.640 + {
1.641 + if (this.ignoreCase) {
1.642 + name = name.toUpperCase();
1.643 + }
1.644 + this.attributes.put(name, Integer.toString(value));
1.645 + }
1.646 +
1.647 +
1.648 + /**
1.649 + * Adds or modifies an attribute.
1.650 + *
1.651 + * @param name
1.652 + * The name of the attribute.
1.653 + * @param value
1.654 + * The value of the attribute.
1.655 + *
1.656 + * @deprecated Use {@link #setIntAttribute(java.lang.String, int)
1.657 + * setIntAttribute} instead.
1.658 + */
1.659 + public void addProperty(String key,
1.660 + int value)
1.661 + {
1.662 + this.setIntAttribute(key, value);
1.663 + }
1.664 +
1.665 +
1.666 + /**
1.667 + * Adds or modifies an attribute.
1.668 + *
1.669 + * @param name
1.670 + * The name of the attribute.
1.671 + * @param value
1.672 + * The value of the attribute.
1.673 + *
1.674 + * </dl><dl><dt><b>Preconditions:</b></dt><dd>
1.675 + * <ul><li><code>name != null</code>
1.676 + * <li><code>name</code> is a valid XML identifier
1.677 + * </ul></dd></dl>
1.678 + *
1.679 + * <dl><dt><b>Postconditions:</b></dt><dd>
1.680 + * <ul><li>enumerateAttributeNames()
1.681 + * => old.enumerateAttributeNames() + name
1.682 + * <li>getDoubleAttribute(name) => value
1.683 + * </ul></dd></dl><dl>
1.684 + *
1.685 + * @see nanoxml.XMLElement#setIntAttribute(java.lang.String, int)
1.686 + * setIntAttribute(String, int)
1.687 + * @see nanoxml.XMLElement#setAttribute(java.lang.String, java.lang.Object)
1.688 + * setAttribute(String, Object)
1.689 + * @see nanoxml.XMLElement#removeAttribute(java.lang.String)
1.690 + * removeAttribute(String)
1.691 + * @see nanoxml.XMLElement#enumerateAttributeNames()
1.692 + * @see nanoxml.XMLElement#getDoubleAttribute(java.lang.String)
1.693 + * getDoubleAttribute(String)
1.694 + * @see nanoxml.XMLElement#getDoubleAttribute(java.lang.String, double)
1.695 + * getDoubleAttribute(String, double)
1.696 + * @see nanoxml.XMLElement#getDoubleAttribute(java.lang.String,
1.697 + * java.util.Hashtable,
1.698 + * java.lang.String, boolean)
1.699 + * getDoubleAttribute(String, Hashtable, String, boolean)
1.700 + */
1.701 + public void setDoubleAttribute(String name,
1.702 + double value)
1.703 + {
1.704 + if (this.ignoreCase) {
1.705 + name = name.toUpperCase();
1.706 + }
1.707 + this.attributes.put(name, Double.toString(value));
1.708 + }
1.709 +
1.710 +
1.711 + /**
1.712 + * Adds or modifies an attribute.
1.713 + *
1.714 + * @param name
1.715 + * The name of the attribute.
1.716 + * @param value
1.717 + * The value of the attribute.
1.718 + *
1.719 + * @deprecated Use {@link #setDoubleAttribute(java.lang.String, double)
1.720 + * setDoubleAttribute} instead.
1.721 + */
1.722 + public void addProperty(String name,
1.723 + double value)
1.724 + {
1.725 + this.setDoubleAttribute(name, value);
1.726 + }
1.727 +
1.728 +
1.729 + /**
1.730 + * Returns the number of child elements of the element.
1.731 + *
1.732 + * <dl><dt><b>Postconditions:</b></dt><dd>
1.733 + * <ul><li><code>result >= 0</code>
1.734 + * </ul></dd></dl>
1.735 + *
1.736 + * @see nanoxml.XMLElement#addChild(nanoxml.XMLElement)
1.737 + * addChild(XMLElement)
1.738 + * @see nanoxml.XMLElement#enumerateChildren()
1.739 + * @see nanoxml.XMLElement#getChildren()
1.740 + * @see nanoxml.XMLElement#removeChild(nanoxml.XMLElement)
1.741 + * removeChild(XMLElement)
1.742 + */
1.743 + public int countChildren()
1.744 + {
1.745 + return this.children.size();
1.746 + }
1.747 +
1.748 +
1.749 + /**
1.750 + * Enumerates the attribute names.
1.751 + *
1.752 + * <dl><dt><b>Postconditions:</b></dt><dd>
1.753 + * <ul><li><code>result != null</code>
1.754 + * </ul></dd></dl>
1.755 + *
1.756 + * @see nanoxml.XMLElement#setDoubleAttribute(java.lang.String, double)
1.757 + * setDoubleAttribute(String, double)
1.758 + * @see nanoxml.XMLElement#setIntAttribute(java.lang.String, int)
1.759 + * setIntAttribute(String, int)
1.760 + * @see nanoxml.XMLElement#setAttribute(java.lang.String, java.lang.Object)
1.761 + * setAttribute(String, Object)
1.762 + * @see nanoxml.XMLElement#removeAttribute(java.lang.String)
1.763 + * removeAttribute(String)
1.764 + * @see nanoxml.XMLElement#getAttribute(java.lang.String)
1.765 + * getAttribute(String)
1.766 + * @see nanoxml.XMLElement#getAttribute(java.lang.String, java.lang.Object)
1.767 + * getAttribute(String, String)
1.768 + * @see nanoxml.XMLElement#getAttribute(java.lang.String,
1.769 + * java.util.Hashtable,
1.770 + * java.lang.String, boolean)
1.771 + * getAttribute(String, Hashtable, String, boolean)
1.772 + * @see nanoxml.XMLElement#getStringAttribute(java.lang.String)
1.773 + * getStringAttribute(String)
1.774 + * @see nanoxml.XMLElement#getStringAttribute(java.lang.String,
1.775 + * java.lang.String)
1.776 + * getStringAttribute(String, String)
1.777 + * @see nanoxml.XMLElement#getStringAttribute(java.lang.String,
1.778 + * java.util.Hashtable,
1.779 + * java.lang.String, boolean)
1.780 + * getStringAttribute(String, Hashtable, String, boolean)
1.781 + * @see nanoxml.XMLElement#getIntAttribute(java.lang.String)
1.782 + * getIntAttribute(String)
1.783 + * @see nanoxml.XMLElement#getIntAttribute(java.lang.String, int)
1.784 + * getIntAttribute(String, int)
1.785 + * @see nanoxml.XMLElement#getIntAttribute(java.lang.String,
1.786 + * java.util.Hashtable,
1.787 + * java.lang.String, boolean)
1.788 + * getIntAttribute(String, Hashtable, String, boolean)
1.789 + * @see nanoxml.XMLElement#getDoubleAttribute(java.lang.String)
1.790 + * getDoubleAttribute(String)
1.791 + * @see nanoxml.XMLElement#getDoubleAttribute(java.lang.String, double)
1.792 + * getDoubleAttribute(String, double)
1.793 + * @see nanoxml.XMLElement#getDoubleAttribute(java.lang.String,
1.794 + * java.util.Hashtable,
1.795 + * java.lang.String, boolean)
1.796 + * getDoubleAttribute(String, Hashtable, String, boolean)
1.797 + * @see nanoxml.XMLElement#getBooleanAttribute(java.lang.String,
1.798 + * java.lang.String,
1.799 + * java.lang.String, boolean)
1.800 + * getBooleanAttribute(String, String, String, boolean)
1.801 + */
1.802 + public Enumeration enumerateAttributeNames()
1.803 + {
1.804 + return this.attributes.keys();
1.805 + }
1.806 +
1.807 +
1.808 + /**
1.809 + * Enumerates the attribute names.
1.810 + *
1.811 + * @deprecated Use {@link #enumerateAttributeNames()
1.812 + * enumerateAttributeNames} instead.
1.813 + */
1.814 + public Enumeration enumeratePropertyNames()
1.815 + {
1.816 + return this.enumerateAttributeNames();
1.817 + }
1.818 +
1.819 +
1.820 + /**
1.821 + * Enumerates the child elements.
1.822 + *
1.823 + * <dl><dt><b>Postconditions:</b></dt><dd>
1.824 + * <ul><li><code>result != null</code>
1.825 + * </ul></dd></dl>
1.826 + *
1.827 + * @see nanoxml.XMLElement#addChild(nanoxml.XMLElement)
1.828 + * addChild(XMLElement)
1.829 + * @see nanoxml.XMLElement#countChildren()
1.830 + * @see nanoxml.XMLElement#getChildren()
1.831 + * @see nanoxml.XMLElement#removeChild(nanoxml.XMLElement)
1.832 + * removeChild(XMLElement)
1.833 + */
1.834 + public Enumeration enumerateChildren()
1.835 + {
1.836 + return this.children.elements();
1.837 + }
1.838 +
1.839 +
1.840 + /**
1.841 + * Returns the child elements as a Vector. It is safe to modify this
1.842 + * Vector.
1.843 + *
1.844 + * <dl><dt><b>Postconditions:</b></dt><dd>
1.845 + * <ul><li><code>result != null</code>
1.846 + * </ul></dd></dl>
1.847 + *
1.848 + * @see nanoxml.XMLElement#addChild(nanoxml.XMLElement)
1.849 + * addChild(XMLElement)
1.850 + * @see nanoxml.XMLElement#countChildren()
1.851 + * @see nanoxml.XMLElement#enumerateChildren()
1.852 + * @see nanoxml.XMLElement#removeChild(nanoxml.XMLElement)
1.853 + * removeChild(XMLElement)
1.854 + */
1.855 + public Vector getChildren()
1.856 + {
1.857 + try {
1.858 + return (Vector) this.children.clone();
1.859 + } catch (Exception e) {
1.860 + // this never happens, however, some Java compilers are so
1.861 + // braindead that they require this exception clause
1.862 + return null;
1.863 + }
1.864 + }
1.865 +
1.866 +
1.867 + /**
1.868 + * Returns the PCDATA content of the object. If there is no such content,
1.869 + * <CODE>null</CODE> is returned.
1.870 + *
1.871 + * @deprecated Use {@link #getContent() getContent} instead.
1.872 + */
1.873 + public String getContents()
1.874 + {
1.875 + return this.getContent();
1.876 + }
1.877 +
1.878 +
1.879 + /**
1.880 + * Returns the PCDATA content of the object. If there is no such content,
1.881 + * <CODE>null</CODE> is returned.
1.882 + *
1.883 + * @see nanoxml.XMLElement#setContent(java.lang.String)
1.884 + * setContent(String)
1.885 + */
1.886 + public String getContent()
1.887 + {
1.888 + return this.contents;
1.889 + }
1.890 +
1.891 +
1.892 + /**
1.893 + * Returns the line nr in the source data on which the element is found.
1.894 + * This method returns <code>0</code> there is no associated source data.
1.895 + *
1.896 + * <dl><dt><b>Postconditions:</b></dt><dd>
1.897 + * <ul><li><code>result >= 0</code>
1.898 + * </ul></dd></dl>
1.899 + */
1.900 + public int getLineNr()
1.901 + {
1.902 + return this.lineNr;
1.903 + }
1.904 +
1.905 +
1.906 + /**
1.907 + * Returns an attribute of the element.
1.908 + * If the attribute doesn't exist, <code>null</code> is returned.
1.909 + *
1.910 + * @param name The name of the attribute.
1.911 + *
1.912 + * </dl><dl><dt><b>Preconditions:</b></dt><dd>
1.913 + * <ul><li><code>name != null</code>
1.914 + * <li><code>name</code> is a valid XML identifier
1.915 + * </ul></dd></dl><dl>
1.916 + *
1.917 + * @see nanoxml.XMLElement#setAttribute(java.lang.String, java.lang.Object)
1.918 + * setAttribute(String, Object)
1.919 + * @see nanoxml.XMLElement#removeAttribute(java.lang.String)
1.920 + * removeAttribute(String)
1.921 + * @see nanoxml.XMLElement#enumerateAttributeNames()
1.922 + * @see nanoxml.XMLElement#getAttribute(java.lang.String, java.lang.Object)
1.923 + * getAttribute(String, Object)
1.924 + * @see nanoxml.XMLElement#getAttribute(java.lang.String,
1.925 + * java.util.Hashtable,
1.926 + * java.lang.String, boolean)
1.927 + * getAttribute(String, Hashtable, String, boolean)
1.928 + */
1.929 + public Object getAttribute(String name)
1.930 + {
1.931 + return this.getAttribute(name, null);
1.932 + }
1.933 +
1.934 +
1.935 + /**
1.936 + * Returns an attribute of the element.
1.937 + * If the attribute doesn't exist, <code>defaultValue</code> is returned.
1.938 + *
1.939 + * @param name The name of the attribute.
1.940 + * @param defaultValue Key to use if the attribute is missing.
1.941 + *
1.942 + * </dl><dl><dt><b>Preconditions:</b></dt><dd>
1.943 + * <ul><li><code>name != null</code>
1.944 + * <li><code>name</code> is a valid XML identifier
1.945 + * </ul></dd></dl><dl>
1.946 + *
1.947 + * @see nanoxml.XMLElement#setAttribute(java.lang.String, java.lang.Object)
1.948 + * setAttribute(String, Object)
1.949 + * @see nanoxml.XMLElement#removeAttribute(java.lang.String)
1.950 + * removeAttribute(String)
1.951 + * @see nanoxml.XMLElement#enumerateAttributeNames()
1.952 + * @see nanoxml.XMLElement#getAttribute(java.lang.String)
1.953 + * getAttribute(String)
1.954 + * @see nanoxml.XMLElement#getAttribute(java.lang.String,
1.955 + * java.util.Hashtable,
1.956 + * java.lang.String, boolean)
1.957 + * getAttribute(String, Hashtable, String, boolean)
1.958 + */
1.959 + public Object getAttribute(String name,
1.960 + Object defaultValue)
1.961 + {
1.962 + if (this.ignoreCase) {
1.963 + name = name.toUpperCase();
1.964 + }
1.965 + Object value = this.attributes.get(name);
1.966 + if (value == null) {
1.967 + value = defaultValue;
1.968 + }
1.969 + return value;
1.970 + }
1.971 +
1.972 +
1.973 + /**
1.974 + * Returns an attribute by looking up a key in a hashtable.
1.975 + * If the attribute doesn't exist, the value corresponding to defaultKey
1.976 + * is returned.
1.977 + * <P>
1.978 + * As an example, if valueSet contains the mapping <code>"one" =>
1.979 + * "1"</code>
1.980 + * and the element contains the attribute <code>attr="one"</code>, then
1.981 + * <code>getAttribute("attr", mapping, defaultKey, false)</code> returns
1.982 + * <code>"1"</code>.
1.983 + *
1.984 + * @param name
1.985 + * The name of the attribute.
1.986 + * @param valueSet
1.987 + * Hashtable mapping keys to values.
1.988 + * @param defaultKey
1.989 + * Key to use if the attribute is missing.
1.990 + * @param allowLiterals
1.991 + * <code>true</code> if literals are valid.
1.992 + *
1.993 + * </dl><dl><dt><b>Preconditions:</b></dt><dd>
1.994 + * <ul><li><code>name != null</code>
1.995 + * <li><code>name</code> is a valid XML identifier
1.996 + * <li><code>valueSet</code> != null
1.997 + * <li>the keys of <code>valueSet</code> are strings
1.998 + * </ul></dd></dl><dl>
1.999 + *
1.1000 + * @see nanoxml.XMLElement#setAttribute(java.lang.String, java.lang.Object)
1.1001 + * setAttribute(String, Object)
1.1002 + * @see nanoxml.XMLElement#removeAttribute(java.lang.String)
1.1003 + * removeAttribute(String)
1.1004 + * @see nanoxml.XMLElement#enumerateAttributeNames()
1.1005 + * @see nanoxml.XMLElement#getAttribute(java.lang.String)
1.1006 + * getAttribute(String)
1.1007 + * @see nanoxml.XMLElement#getAttribute(java.lang.String, java.lang.Object)
1.1008 + * getAttribute(String, Object)
1.1009 + */
1.1010 + public Object getAttribute(String name,
1.1011 + Hashtable valueSet,
1.1012 + String defaultKey,
1.1013 + boolean allowLiterals)
1.1014 + {
1.1015 + if (this.ignoreCase) {
1.1016 + name = name.toUpperCase();
1.1017 + }
1.1018 + Object key = this.attributes.get(name);
1.1019 + Object result;
1.1020 + if (key == null) {
1.1021 + key = defaultKey;
1.1022 + }
1.1023 + result = valueSet.get(key);
1.1024 + if (result == null) {
1.1025 + if (allowLiterals) {
1.1026 + result = key;
1.1027 + } else {
1.1028 + throw this.invalidValue(name, (String) key);
1.1029 + }
1.1030 + }
1.1031 + return result;
1.1032 + }
1.1033 +
1.1034 +
1.1035 + /**
1.1036 + * Returns an attribute of the element.
1.1037 + * If the attribute doesn't exist, <code>null</code> is returned.
1.1038 + *
1.1039 + * @param name The name of the attribute.
1.1040 + *
1.1041 + * </dl><dl><dt><b>Preconditions:</b></dt><dd>
1.1042 + * <ul><li><code>name != null</code>
1.1043 + * <li><code>name</code> is a valid XML identifier
1.1044 + * </ul></dd></dl><dl>
1.1045 + *
1.1046 + * @see nanoxml.XMLElement#setAttribute(java.lang.String, java.lang.Object)
1.1047 + * setAttribute(String, Object)
1.1048 + * @see nanoxml.XMLElement#removeAttribute(java.lang.String)
1.1049 + * removeAttribute(String)
1.1050 + * @see nanoxml.XMLElement#enumerateAttributeNames()
1.1051 + * @see nanoxml.XMLElement#getStringAttribute(java.lang.String,
1.1052 + * java.lang.String)
1.1053 + * getStringAttribute(String, String)
1.1054 + * @see nanoxml.XMLElement#getStringAttribute(java.lang.String,
1.1055 + * java.util.Hashtable,
1.1056 + * java.lang.String, boolean)
1.1057 + * getStringAttribute(String, Hashtable, String, boolean)
1.1058 + */
1.1059 + public String getStringAttribute(String name)
1.1060 + {
1.1061 + return this.getStringAttribute(name, null);
1.1062 + }
1.1063 +
1.1064 +
1.1065 + /**
1.1066 + * Returns an attribute of the element.
1.1067 + * If the attribute doesn't exist, <code>defaultValue</code> is returned.
1.1068 + *
1.1069 + * @param name The name of the attribute.
1.1070 + * @param defaultValue Key to use if the attribute is missing.
1.1071 + *
1.1072 + * </dl><dl><dt><b>Preconditions:</b></dt><dd>
1.1073 + * <ul><li><code>name != null</code>
1.1074 + * <li><code>name</code> is a valid XML identifier
1.1075 + * </ul></dd></dl><dl>
1.1076 + *
1.1077 + * @see nanoxml.XMLElement#setAttribute(java.lang.String, java.lang.Object)
1.1078 + * setAttribute(String, Object)
1.1079 + * @see nanoxml.XMLElement#removeAttribute(java.lang.String)
1.1080 + * removeAttribute(String)
1.1081 + * @see nanoxml.XMLElement#enumerateAttributeNames()
1.1082 + * @see nanoxml.XMLElement#getStringAttribute(java.lang.String)
1.1083 + * getStringAttribute(String)
1.1084 + * @see nanoxml.XMLElement#getStringAttribute(java.lang.String,
1.1085 + * java.util.Hashtable,
1.1086 + * java.lang.String, boolean)
1.1087 + * getStringAttribute(String, Hashtable, String, boolean)
1.1088 + */
1.1089 + public String getStringAttribute(String name,
1.1090 + String defaultValue)
1.1091 + {
1.1092 + return (String) this.getAttribute(name, defaultValue);
1.1093 + }
1.1094 +
1.1095 +
1.1096 + /**
1.1097 + * Returns an attribute by looking up a key in a hashtable.
1.1098 + * If the attribute doesn't exist, the value corresponding to defaultKey
1.1099 + * is returned.
1.1100 + * <P>
1.1101 + * As an example, if valueSet contains the mapping <code>"one" =>
1.1102 + * "1"</code>
1.1103 + * and the element contains the attribute <code>attr="one"</code>, then
1.1104 + * <code>getAttribute("attr", mapping, defaultKey, false)</code> returns
1.1105 + * <code>"1"</code>.
1.1106 + *
1.1107 + * @param name
1.1108 + * The name of the attribute.
1.1109 + * @param valueSet
1.1110 + * Hashtable mapping keys to values.
1.1111 + * @param defaultKey
1.1112 + * Key to use if the attribute is missing.
1.1113 + * @param allowLiterals
1.1114 + * <code>true</code> if literals are valid.
1.1115 + *
1.1116 + * </dl><dl><dt><b>Preconditions:</b></dt><dd>
1.1117 + * <ul><li><code>name != null</code>
1.1118 + * <li><code>name</code> is a valid XML identifier
1.1119 + * <li><code>valueSet</code> != null
1.1120 + * <li>the keys of <code>valueSet</code> are strings
1.1121 + * <li>the values of <code>valueSet</code> are strings
1.1122 + * </ul></dd></dl><dl>
1.1123 + *
1.1124 + * @see nanoxml.XMLElement#setAttribute(java.lang.String, java.lang.Object)
1.1125 + * setAttribute(String, Object)
1.1126 + * @see nanoxml.XMLElement#removeAttribute(java.lang.String)
1.1127 + * removeAttribute(String)
1.1128 + * @see nanoxml.XMLElement#enumerateAttributeNames()
1.1129 + * @see nanoxml.XMLElement#getStringAttribute(java.lang.String)
1.1130 + * getStringAttribute(String)
1.1131 + * @see nanoxml.XMLElement#getStringAttribute(java.lang.String,
1.1132 + * java.lang.String)
1.1133 + * getStringAttribute(String, String)
1.1134 + */
1.1135 + public String getStringAttribute(String name,
1.1136 + Hashtable valueSet,
1.1137 + String defaultKey,
1.1138 + boolean allowLiterals)
1.1139 + {
1.1140 + return (String) this.getAttribute(name, valueSet, defaultKey,
1.1141 + allowLiterals);
1.1142 + }
1.1143 +
1.1144 +
1.1145 + /**
1.1146 + * Returns an attribute of the element.
1.1147 + * If the attribute doesn't exist, <code>0</code> is returned.
1.1148 + *
1.1149 + * @param name The name of the attribute.
1.1150 + *
1.1151 + * </dl><dl><dt><b>Preconditions:</b></dt><dd>
1.1152 + * <ul><li><code>name != null</code>
1.1153 + * <li><code>name</code> is a valid XML identifier
1.1154 + * </ul></dd></dl><dl>
1.1155 + *
1.1156 + * @see nanoxml.XMLElement#setIntAttribute(java.lang.String, int)
1.1157 + * setIntAttribute(String, int)
1.1158 + * @see nanoxml.XMLElement#enumerateAttributeNames()
1.1159 + * @see nanoxml.XMLElement#getIntAttribute(java.lang.String, int)
1.1160 + * getIntAttribute(String, int)
1.1161 + * @see nanoxml.XMLElement#getIntAttribute(java.lang.String,
1.1162 + * java.util.Hashtable,
1.1163 + * java.lang.String, boolean)
1.1164 + * getIntAttribute(String, Hashtable, String, boolean)
1.1165 + */
1.1166 + public int getIntAttribute(String name)
1.1167 + {
1.1168 + return this.getIntAttribute(name, 0);
1.1169 + }
1.1170 +
1.1171 +
1.1172 + /**
1.1173 + * Returns an attribute of the element.
1.1174 + * If the attribute doesn't exist, <code>defaultValue</code> is returned.
1.1175 + *
1.1176 + * @param name The name of the attribute.
1.1177 + * @param defaultValue Key to use if the attribute is missing.
1.1178 + *
1.1179 + * </dl><dl><dt><b>Preconditions:</b></dt><dd>
1.1180 + * <ul><li><code>name != null</code>
1.1181 + * <li><code>name</code> is a valid XML identifier
1.1182 + * </ul></dd></dl><dl>
1.1183 + *
1.1184 + * @see nanoxml.XMLElement#setIntAttribute(java.lang.String, int)
1.1185 + * setIntAttribute(String, int)
1.1186 + * @see nanoxml.XMLElement#enumerateAttributeNames()
1.1187 + * @see nanoxml.XMLElement#getIntAttribute(java.lang.String)
1.1188 + * getIntAttribute(String)
1.1189 + * @see nanoxml.XMLElement#getIntAttribute(java.lang.String,
1.1190 + * java.util.Hashtable,
1.1191 + * java.lang.String, boolean)
1.1192 + * getIntAttribute(String, Hashtable, String, boolean)
1.1193 + */
1.1194 + public int getIntAttribute(String name,
1.1195 + int defaultValue)
1.1196 + {
1.1197 + if (this.ignoreCase) {
1.1198 + name = name.toUpperCase();
1.1199 + }
1.1200 + String value = (String) this.attributes.get(name);
1.1201 + if (value == null) {
1.1202 + return defaultValue;
1.1203 + } else {
1.1204 + try {
1.1205 + return Integer.parseInt(value);
1.1206 + } catch (NumberFormatException e) {
1.1207 + throw this.invalidValue(name, value);
1.1208 + }
1.1209 + }
1.1210 + }
1.1211 +
1.1212 +
1.1213 + /**
1.1214 + * Returns an attribute by looking up a key in a hashtable.
1.1215 + * If the attribute doesn't exist, the value corresponding to defaultKey
1.1216 + * is returned.
1.1217 + * <P>
1.1218 + * As an example, if valueSet contains the mapping <code>"one" => 1</code>
1.1219 + * and the element contains the attribute <code>attr="one"</code>, then
1.1220 + * <code>getIntAttribute("attr", mapping, defaultKey, false)</code> returns
1.1221 + * <code>1</code>.
1.1222 + *
1.1223 + * @param name
1.1224 + * The name of the attribute.
1.1225 + * @param valueSet
1.1226 + * Hashtable mapping keys to values.
1.1227 + * @param defaultKey
1.1228 + * Key to use if the attribute is missing.
1.1229 + * @param allowLiteralNumbers
1.1230 + * <code>true</code> if literal numbers are valid.
1.1231 + *
1.1232 + * </dl><dl><dt><b>Preconditions:</b></dt><dd>
1.1233 + * <ul><li><code>name != null</code>
1.1234 + * <li><code>name</code> is a valid XML identifier
1.1235 + * <li><code>valueSet</code> != null
1.1236 + * <li>the keys of <code>valueSet</code> are strings
1.1237 + * <li>the values of <code>valueSet</code> are Integer objects
1.1238 + * <li><code>defaultKey</code> is either <code>null</code>, a
1.1239 + * key in <code>valueSet</code> or an integer.
1.1240 + * </ul></dd></dl><dl>
1.1241 + *
1.1242 + * @see nanoxml.XMLElement#setIntAttribute(java.lang.String, int)
1.1243 + * setIntAttribute(String, int)
1.1244 + * @see nanoxml.XMLElement#enumerateAttributeNames()
1.1245 + * @see nanoxml.XMLElement#getIntAttribute(java.lang.String)
1.1246 + * getIntAttribute(String)
1.1247 + * @see nanoxml.XMLElement#getIntAttribute(java.lang.String, int)
1.1248 + * getIntAttribute(String, int)
1.1249 + */
1.1250 + public int getIntAttribute(String name,
1.1251 + Hashtable valueSet,
1.1252 + String defaultKey,
1.1253 + boolean allowLiteralNumbers)
1.1254 + {
1.1255 + if (this.ignoreCase) {
1.1256 + name = name.toUpperCase();
1.1257 + }
1.1258 + Object key = this.attributes.get(name);
1.1259 + Integer result;
1.1260 + if (key == null) {
1.1261 + key = defaultKey;
1.1262 + }
1.1263 + try {
1.1264 + result = (Integer) valueSet.get(key);
1.1265 + } catch (ClassCastException e) {
1.1266 + throw this.invalidValueSet(name);
1.1267 + }
1.1268 + if (result == null) {
1.1269 + if (! allowLiteralNumbers) {
1.1270 + throw this.invalidValue(name, (String) key);
1.1271 + }
1.1272 + try {
1.1273 + result = Integer.valueOf((String) key);
1.1274 + } catch (NumberFormatException e) {
1.1275 + throw this.invalidValue(name, (String) key);
1.1276 + }
1.1277 + }
1.1278 + return result.intValue();
1.1279 + }
1.1280 +
1.1281 +
1.1282 + /**
1.1283 + * Returns an attribute of the element.
1.1284 + * If the attribute doesn't exist, <code>0.0</code> is returned.
1.1285 + *
1.1286 + * @param name The name of the attribute.
1.1287 + *
1.1288 + * </dl><dl><dt><b>Preconditions:</b></dt><dd>
1.1289 + * <ul><li><code>name != null</code>
1.1290 + * <li><code>name</code> is a valid XML identifier
1.1291 + * </ul></dd></dl><dl>
1.1292 + *
1.1293 + * @see nanoxml.XMLElement#setDoubleAttribute(java.lang.String, double)
1.1294 + * setDoubleAttribute(String, double)
1.1295 + * @see nanoxml.XMLElement#enumerateAttributeNames()
1.1296 + * @see nanoxml.XMLElement#getDoubleAttribute(java.lang.String, double)
1.1297 + * getDoubleAttribute(String, double)
1.1298 + * @see nanoxml.XMLElement#getDoubleAttribute(java.lang.String,
1.1299 + * java.util.Hashtable,
1.1300 + * java.lang.String, boolean)
1.1301 + * getDoubleAttribute(String, Hashtable, String, boolean)
1.1302 + */
1.1303 + public double getDoubleAttribute(String name)
1.1304 + {
1.1305 + return this.getDoubleAttribute(name, 0.);
1.1306 + }
1.1307 +
1.1308 +
1.1309 + /**
1.1310 + * Returns an attribute of the element.
1.1311 + * If the attribute doesn't exist, <code>defaultValue</code> is returned.
1.1312 + *
1.1313 + * @param name The name of the attribute.
1.1314 + * @param defaultValue Key to use if the attribute is missing.
1.1315 + *
1.1316 + * </dl><dl><dt><b>Preconditions:</b></dt><dd>
1.1317 + * <ul><li><code>name != null</code>
1.1318 + * <li><code>name</code> is a valid XML identifier
1.1319 + * </ul></dd></dl><dl>
1.1320 + *
1.1321 + * @see nanoxml.XMLElement#setDoubleAttribute(java.lang.String, double)
1.1322 + * setDoubleAttribute(String, double)
1.1323 + * @see nanoxml.XMLElement#enumerateAttributeNames()
1.1324 + * @see nanoxml.XMLElement#getDoubleAttribute(java.lang.String)
1.1325 + * getDoubleAttribute(String)
1.1326 + * @see nanoxml.XMLElement#getDoubleAttribute(java.lang.String,
1.1327 + * java.util.Hashtable,
1.1328 + * java.lang.String, boolean)
1.1329 + * getDoubleAttribute(String, Hashtable, String, boolean)
1.1330 + */
1.1331 + public double getDoubleAttribute(String name,
1.1332 + double defaultValue)
1.1333 + {
1.1334 + if (this.ignoreCase) {
1.1335 + name = name.toUpperCase();
1.1336 + }
1.1337 + String value = (String) this.attributes.get(name);
1.1338 + if (value == null) {
1.1339 + return defaultValue;
1.1340 + } else {
1.1341 + try {
1.1342 + return Double.valueOf(value).doubleValue();
1.1343 + } catch (NumberFormatException e) {
1.1344 + throw this.invalidValue(name, value);
1.1345 + }
1.1346 + }
1.1347 + }
1.1348 +
1.1349 +
1.1350 + /**
1.1351 + * Returns an attribute by looking up a key in a hashtable.
1.1352 + * If the attribute doesn't exist, the value corresponding to defaultKey
1.1353 + * is returned.
1.1354 + * <P>
1.1355 + * As an example, if valueSet contains the mapping <code>"one" =>
1.1356 + * 1.0</code>
1.1357 + * and the element contains the attribute <code>attr="one"</code>, then
1.1358 + * <code>getDoubleAttribute("attr", mapping, defaultKey, false)</code>
1.1359 + * returns <code>1.0</code>.
1.1360 + *
1.1361 + * @param name
1.1362 + * The name of the attribute.
1.1363 + * @param valueSet
1.1364 + * Hashtable mapping keys to values.
1.1365 + * @param defaultKey
1.1366 + * Key to use if the attribute is missing.
1.1367 + * @param allowLiteralNumbers
1.1368 + * <code>true</code> if literal numbers are valid.
1.1369 + *
1.1370 + * </dl><dl><dt><b>Preconditions:</b></dt><dd>
1.1371 + * <ul><li><code>name != null</code>
1.1372 + * <li><code>name</code> is a valid XML identifier
1.1373 + * <li><code>valueSet != null</code>
1.1374 + * <li>the keys of <code>valueSet</code> are strings
1.1375 + * <li>the values of <code>valueSet</code> are Double objects
1.1376 + * <li><code>defaultKey</code> is either <code>null</code>, a
1.1377 + * key in <code>valueSet</code> or a double.
1.1378 + * </ul></dd></dl><dl>
1.1379 + *
1.1380 + * @see nanoxml.XMLElement#setDoubleAttribute(java.lang.String, double)
1.1381 + * setDoubleAttribute(String, double)
1.1382 + * @see nanoxml.XMLElement#enumerateAttributeNames()
1.1383 + * @see nanoxml.XMLElement#getDoubleAttribute(java.lang.String)
1.1384 + * getDoubleAttribute(String)
1.1385 + * @see nanoxml.XMLElement#getDoubleAttribute(java.lang.String, double)
1.1386 + * getDoubleAttribute(String, double)
1.1387 + */
1.1388 + public double getDoubleAttribute(String name,
1.1389 + Hashtable valueSet,
1.1390 + String defaultKey,
1.1391 + boolean allowLiteralNumbers)
1.1392 + {
1.1393 + if (this.ignoreCase) {
1.1394 + name = name.toUpperCase();
1.1395 + }
1.1396 + Object key = this.attributes.get(name);
1.1397 + Double result;
1.1398 + if (key == null) {
1.1399 + key = defaultKey;
1.1400 + }
1.1401 + try {
1.1402 + result = (Double) valueSet.get(key);
1.1403 + } catch (ClassCastException e) {
1.1404 + throw this.invalidValueSet(name);
1.1405 + }
1.1406 + if (result == null) {
1.1407 + if (! allowLiteralNumbers) {
1.1408 + throw this.invalidValue(name, (String) key);
1.1409 + }
1.1410 + try {
1.1411 + result = Double.valueOf((String) key);
1.1412 + } catch (NumberFormatException e) {
1.1413 + throw this.invalidValue(name, (String) key);
1.1414 + }
1.1415 + }
1.1416 + return result.doubleValue();
1.1417 + }
1.1418 +
1.1419 +
1.1420 + /**
1.1421 + * Returns an attribute of the element.
1.1422 + * If the attribute doesn't exist, <code>defaultValue</code> is returned.
1.1423 + * If the value of the attribute is equal to <code>trueValue</code>,
1.1424 + * <code>true</code> is returned.
1.1425 + * If the value of the attribute is equal to <code>falseValue</code>,
1.1426 + * <code>false</code> is returned.
1.1427 + * If the value doesn't match <code>trueValue</code> or
1.1428 + * <code>falseValue</code>, an exception is thrown.
1.1429 + *
1.1430 + * @param name The name of the attribute.
1.1431 + * @param trueValue The value associated with <code>true</code>.
1.1432 + * @param falseValue The value associated with <code>true</code>.
1.1433 + * @param defaultValue Value to use if the attribute is missing.
1.1434 + *
1.1435 + * </dl><dl><dt><b>Preconditions:</b></dt><dd>
1.1436 + * <ul><li><code>name != null</code>
1.1437 + * <li><code>name</code> is a valid XML identifier
1.1438 + * <li><code>trueValue</code> and <code>falseValue</code>
1.1439 + * are different strings.
1.1440 + * </ul></dd></dl><dl>
1.1441 + *
1.1442 + * @see nanoxml.XMLElement#setAttribute(java.lang.String, java.lang.Object)
1.1443 + * setAttribute(String, Object)
1.1444 + * @see nanoxml.XMLElement#removeAttribute(java.lang.String)
1.1445 + * removeAttribute(String)
1.1446 + * @see nanoxml.XMLElement#enumerateAttributeNames()
1.1447 + */
1.1448 + public boolean getBooleanAttribute(String name,
1.1449 + String trueValue,
1.1450 + String falseValue,
1.1451 + boolean defaultValue)
1.1452 + {
1.1453 + if (this.ignoreCase) {
1.1454 + name = name.toUpperCase();
1.1455 + }
1.1456 + Object value = this.attributes.get(name);
1.1457 + if (value == null) {
1.1458 + return defaultValue;
1.1459 + } else if (value.equals(trueValue)) {
1.1460 + return true;
1.1461 + } else if (value.equals(falseValue)) {
1.1462 + return false;
1.1463 + } else {
1.1464 + throw this.invalidValue(name, (String) value);
1.1465 + }
1.1466 + }
1.1467 +
1.1468 +
1.1469 + /**
1.1470 + * Returns an attribute by looking up a key in a hashtable.
1.1471 + *
1.1472 + * @deprecated Use {@link #getIntAttribute(java.lang.String,
1.1473 + * java.util.Hashtable, java.lang.String, boolean)
1.1474 + * getIntAttribute} instead.
1.1475 + */
1.1476 + public int getIntProperty(String name,
1.1477 + Hashtable valueSet,
1.1478 + String defaultKey)
1.1479 + {
1.1480 + return this.getIntAttribute(name, valueSet, defaultKey, false);
1.1481 + }
1.1482 +
1.1483 +
1.1484 + /**
1.1485 + * Returns an attribute.
1.1486 + *
1.1487 + * @deprecated Use {@link #getStringAttribute(java.lang.String)
1.1488 + * getStringAttribute} instead.
1.1489 + */
1.1490 + public String getProperty(String name)
1.1491 + {
1.1492 + return this.getStringAttribute(name);
1.1493 + }
1.1494 +
1.1495 +
1.1496 + /**
1.1497 + * Returns an attribute.
1.1498 + *
1.1499 + * @deprecated Use {@link #getStringAttribute(java.lang.String,
1.1500 + * java.lang.String) getStringAttribute} instead.
1.1501 + */
1.1502 + public String getProperty(String name,
1.1503 + String defaultValue)
1.1504 + {
1.1505 + return this.getStringAttribute(name, defaultValue);
1.1506 + }
1.1507 +
1.1508 +
1.1509 + /**
1.1510 + * Returns an attribute.
1.1511 + *
1.1512 + * @deprecated Use {@link #getIntAttribute(java.lang.String, int)
1.1513 + * getIntAttribute} instead.
1.1514 + */
1.1515 + public int getProperty(String name,
1.1516 + int defaultValue)
1.1517 + {
1.1518 + return this.getIntAttribute(name, defaultValue);
1.1519 + }
1.1520 +
1.1521 +
1.1522 + /**
1.1523 + * Returns an attribute.
1.1524 + *
1.1525 + * @deprecated Use {@link #getDoubleAttribute(java.lang.String, double)
1.1526 + * getDoubleAttribute} instead.
1.1527 + */
1.1528 + public double getProperty(String name,
1.1529 + double defaultValue)
1.1530 + {
1.1531 + return this.getDoubleAttribute(name, defaultValue);
1.1532 + }
1.1533 +
1.1534 +
1.1535 + /**
1.1536 + * Returns an attribute.
1.1537 + *
1.1538 + * @deprecated Use {@link #getBooleanAttribute(java.lang.String,
1.1539 + * java.lang.String, java.lang.String, boolean)
1.1540 + * getBooleanAttribute} instead.
1.1541 + */
1.1542 + public boolean getProperty(String key,
1.1543 + String trueValue,
1.1544 + String falseValue,
1.1545 + boolean defaultValue)
1.1546 + {
1.1547 + return this.getBooleanAttribute(key, trueValue, falseValue,
1.1548 + defaultValue);
1.1549 + }
1.1550 +
1.1551 +
1.1552 + /**
1.1553 + * Returns an attribute by looking up a key in a hashtable.
1.1554 + *
1.1555 + * @deprecated Use {@link #getAttribute(java.lang.String,
1.1556 + * java.util.Hashtable, java.lang.String, boolean)
1.1557 + * getAttribute} instead.
1.1558 + */
1.1559 + public Object getProperty(String name,
1.1560 + Hashtable valueSet,
1.1561 + String defaultKey)
1.1562 + {
1.1563 + return this.getAttribute(name, valueSet, defaultKey, false);
1.1564 + }
1.1565 +
1.1566 +
1.1567 + /**
1.1568 + * Returns an attribute by looking up a key in a hashtable.
1.1569 + *
1.1570 + * @deprecated Use {@link #getStringAttribute(java.lang.String,
1.1571 + * java.util.Hashtable, java.lang.String, boolean)
1.1572 + * getStringAttribute} instead.
1.1573 + */
1.1574 + public String getStringProperty(String name,
1.1575 + Hashtable valueSet,
1.1576 + String defaultKey)
1.1577 + {
1.1578 + return this.getStringAttribute(name, valueSet, defaultKey, false);
1.1579 + }
1.1580 +
1.1581 +
1.1582 + /**
1.1583 + * Returns an attribute by looking up a key in a hashtable.
1.1584 + *
1.1585 + * @deprecated Use {@link #getIntAttribute(java.lang.String,
1.1586 + * java.util.Hashtable, java.lang.String, boolean)
1.1587 + * getIntAttribute} instead.
1.1588 + */
1.1589 + public int getSpecialIntProperty(String name,
1.1590 + Hashtable valueSet,
1.1591 + String defaultKey)
1.1592 + {
1.1593 + return this.getIntAttribute(name, valueSet, defaultKey, true);
1.1594 + }
1.1595 +
1.1596 +
1.1597 + /**
1.1598 + * Returns an attribute by looking up a key in a hashtable.
1.1599 + *
1.1600 + * @deprecated Use {@link #getDoubleAttribute(java.lang.String,
1.1601 + * java.util.Hashtable, java.lang.String, boolean)
1.1602 + * getDoubleAttribute} instead.
1.1603 + */
1.1604 + public double getSpecialDoubleProperty(String name,
1.1605 + Hashtable valueSet,
1.1606 + String defaultKey)
1.1607 + {
1.1608 + return this.getDoubleAttribute(name, valueSet, defaultKey, true);
1.1609 + }
1.1610 +
1.1611 +
1.1612 + /**
1.1613 + * Returns the name of the element.
1.1614 + *
1.1615 + * @see nanoxml.XMLElement#setName(java.lang.String) setName(String)
1.1616 + */
1.1617 + public String getName()
1.1618 + {
1.1619 + return this.name;
1.1620 + }
1.1621 +
1.1622 +
1.1623 + /**
1.1624 + * Returns the name of the element.
1.1625 + *
1.1626 + * @deprecated Use {@link #getName() getName} instead.
1.1627 + */
1.1628 + public String getTagName()
1.1629 + {
1.1630 + return this.getName();
1.1631 + }
1.1632 +
1.1633 +
1.1634 + /**
1.1635 + * Reads one XML element from a java.io.Reader and parses it.
1.1636 + *
1.1637 + * @param reader
1.1638 + * The reader from which to retrieve the XML data.
1.1639 + *
1.1640 + * </dl><dl><dt><b>Preconditions:</b></dt><dd>
1.1641 + * <ul><li><code>reader != null</code>
1.1642 + * <li><code>reader</code> is not closed
1.1643 + * </ul></dd></dl>
1.1644 + *
1.1645 + * <dl><dt><b>Postconditions:</b></dt><dd>
1.1646 + * <ul><li>the state of the receiver is updated to reflect the XML element
1.1647 + * parsed from the reader
1.1648 + * <li>the reader points to the first character following the last
1.1649 + * '>' character of the XML element
1.1650 + * </ul></dd></dl><dl>
1.1651 + *
1.1652 + * @throws java.io.IOException
1.1653 + * If an error occured while reading the input.
1.1654 + * @throws nanoxml.XMLParseException
1.1655 + * If an error occured while parsing the read data.
1.1656 + */
1.1657 + public void parseFromReader(Reader reader)
1.1658 + throws IOException, XMLParseException
1.1659 + {
1.1660 + this.parseFromReader(reader, /*startingLineNr*/ 1);
1.1661 + }
1.1662 +
1.1663 +
1.1664 + /**
1.1665 + * Reads one XML element from a java.io.Reader and parses it.
1.1666 + *
1.1667 + * @param reader
1.1668 + * The reader from which to retrieve the XML data.
1.1669 + * @param startingLineNr
1.1670 + * The line number of the first line in the data.
1.1671 + *
1.1672 + * </dl><dl><dt><b>Preconditions:</b></dt><dd>
1.1673 + * <ul><li><code>reader != null</code>
1.1674 + * <li><code>reader</code> is not closed
1.1675 + * </ul></dd></dl>
1.1676 + *
1.1677 + * <dl><dt><b>Postconditions:</b></dt><dd>
1.1678 + * <ul><li>the state of the receiver is updated to reflect the XML element
1.1679 + * parsed from the reader
1.1680 + * <li>the reader points to the first character following the last
1.1681 + * '>' character of the XML element
1.1682 + * </ul></dd></dl><dl>
1.1683 + *
1.1684 + * @throws java.io.IOException
1.1685 + * If an error occured while reading the input.
1.1686 + * @throws nanoxml.XMLParseException
1.1687 + * If an error occured while parsing the read data.
1.1688 + */
1.1689 + public void parseFromReader(Reader reader,
1.1690 + int startingLineNr)
1.1691 + throws IOException, XMLParseException
1.1692 + {
1.1693 + this.name = null;
1.1694 + this.contents = "";
1.1695 + this.attributes = new Hashtable();
1.1696 + this.children = new Vector();
1.1697 + this.charReadTooMuch = '\0';
1.1698 + this.reader = reader;
1.1699 + this.parserLineNr = startingLineNr;
1.1700 +
1.1701 + for (;;) {
1.1702 + char ch = this.scanWhitespace();
1.1703 +
1.1704 + if (ch != '<') {
1.1705 + throw this.expectedInput("<");
1.1706 + }
1.1707 +
1.1708 + ch = this.readChar();
1.1709 +
1.1710 + if ((ch == '!') || (ch == '?')) {
1.1711 + this.skipSpecialTag(0);
1.1712 + } else {
1.1713 + this.unreadChar(ch);
1.1714 + this.scanElement(this);
1.1715 + return;
1.1716 + }
1.1717 + }
1.1718 + }
1.1719 +
1.1720 +
1.1721 + /**
1.1722 + * Reads one XML element from a String and parses it.
1.1723 + *
1.1724 + * @param reader
1.1725 + * The reader from which to retrieve the XML data.
1.1726 + *
1.1727 + * </dl><dl><dt><b>Preconditions:</b></dt><dd>
1.1728 + * <ul><li><code>string != null</code>
1.1729 + * <li><code>string.length() > 0</code>
1.1730 + * </ul></dd></dl>
1.1731 + *
1.1732 + * <dl><dt><b>Postconditions:</b></dt><dd>
1.1733 + * <ul><li>the state of the receiver is updated to reflect the XML element
1.1734 + * parsed from the reader
1.1735 + * </ul></dd></dl><dl>
1.1736 + *
1.1737 + * @throws nanoxml.XMLParseException
1.1738 + * If an error occured while parsing the string.
1.1739 + */
1.1740 + public void parseString(String string)
1.1741 + throws XMLParseException
1.1742 + {
1.1743 + try {
1.1744 + this.parseFromReader(new StringReader(string),
1.1745 + /*startingLineNr*/ 1);
1.1746 + } catch (IOException e) {
1.1747 + // Java exception handling suxx
1.1748 + }
1.1749 + }
1.1750 +
1.1751 +
1.1752 + /**
1.1753 + * Reads one XML element from a String and parses it.
1.1754 + *
1.1755 + * @param reader
1.1756 + * The reader from which to retrieve the XML data.
1.1757 + * @param offset
1.1758 + * The first character in <code>string</code> to scan.
1.1759 + *
1.1760 + * </dl><dl><dt><b>Preconditions:</b></dt><dd>
1.1761 + * <ul><li><code>string != null</code>
1.1762 + * <li><code>offset < string.length()</code>
1.1763 + * <li><code>offset >= 0</code>
1.1764 + * </ul></dd></dl>
1.1765 + *
1.1766 + * <dl><dt><b>Postconditions:</b></dt><dd>
1.1767 + * <ul><li>the state of the receiver is updated to reflect the XML element
1.1768 + * parsed from the reader
1.1769 + * </ul></dd></dl><dl>
1.1770 + *
1.1771 + * @throws nanoxml.XMLParseException
1.1772 + * If an error occured while parsing the string.
1.1773 + */
1.1774 + public void parseString(String string,
1.1775 + int offset)
1.1776 + throws XMLParseException
1.1777 + {
1.1778 + this.parseString(string.substring(offset));
1.1779 + }
1.1780 +
1.1781 +
1.1782 + /**
1.1783 + * Reads one XML element from a String and parses it.
1.1784 + *
1.1785 + * @param reader
1.1786 + * The reader from which to retrieve the XML data.
1.1787 + * @param offset
1.1788 + * The first character in <code>string</code> to scan.
1.1789 + * @param end
1.1790 + * The character where to stop scanning.
1.1791 + * This character is not scanned.
1.1792 + *
1.1793 + * </dl><dl><dt><b>Preconditions:</b></dt><dd>
1.1794 + * <ul><li><code>string != null</code>
1.1795 + * <li><code>end <= string.length()</code>
1.1796 + * <li><code>offset < end</code>
1.1797 + * <li><code>offset >= 0</code>
1.1798 + * </ul></dd></dl>
1.1799 + *
1.1800 + * <dl><dt><b>Postconditions:</b></dt><dd>
1.1801 + * <ul><li>the state of the receiver is updated to reflect the XML element
1.1802 + * parsed from the reader
1.1803 + * </ul></dd></dl><dl>
1.1804 + *
1.1805 + * @throws nanoxml.XMLParseException
1.1806 + * If an error occured while parsing the string.
1.1807 + */
1.1808 + public void parseString(String string,
1.1809 + int offset,
1.1810 + int end)
1.1811 + throws XMLParseException
1.1812 + {
1.1813 + this.parseString(string.substring(offset, end));
1.1814 + }
1.1815 +
1.1816 +
1.1817 + /**
1.1818 + * Reads one XML element from a String and parses it.
1.1819 + *
1.1820 + * @param reader
1.1821 + * The reader from which to retrieve the XML data.
1.1822 + * @param offset
1.1823 + * The first character in <code>string</code> to scan.
1.1824 + * @param end
1.1825 + * The character where to stop scanning.
1.1826 + * This character is not scanned.
1.1827 + * @param startingLineNr
1.1828 + * The line number of the first line in the data.
1.1829 + *
1.1830 + * </dl><dl><dt><b>Preconditions:</b></dt><dd>
1.1831 + * <ul><li><code>string != null</code>
1.1832 + * <li><code>end <= string.length()</code>
1.1833 + * <li><code>offset < end</code>
1.1834 + * <li><code>offset >= 0</code>
1.1835 + * </ul></dd></dl>
1.1836 + *
1.1837 + * <dl><dt><b>Postconditions:</b></dt><dd>
1.1838 + * <ul><li>the state of the receiver is updated to reflect the XML element
1.1839 + * parsed from the reader
1.1840 + * </ul></dd></dl><dl>
1.1841 + *
1.1842 + * @throws nanoxml.XMLParseException
1.1843 + * If an error occured while parsing the string.
1.1844 + */
1.1845 + public void parseString(String string,
1.1846 + int offset,
1.1847 + int end,
1.1848 + int startingLineNr)
1.1849 + throws XMLParseException
1.1850 + {
1.1851 + string = string.substring(offset, end);
1.1852 + try {
1.1853 + this.parseFromReader(new StringReader(string), startingLineNr);
1.1854 + } catch (IOException e) {
1.1855 + // Java exception handling suxx
1.1856 + }
1.1857 + }
1.1858 +
1.1859 +
1.1860 + /**
1.1861 + * Reads one XML element from a char array and parses it.
1.1862 + *
1.1863 + * @param reader
1.1864 + * The reader from which to retrieve the XML data.
1.1865 + * @param offset
1.1866 + * The first character in <code>string</code> to scan.
1.1867 + * @param end
1.1868 + * The character where to stop scanning.
1.1869 + * This character is not scanned.
1.1870 + *
1.1871 + * </dl><dl><dt><b>Preconditions:</b></dt><dd>
1.1872 + * <ul><li><code>input != null</code>
1.1873 + * <li><code>end <= input.length</code>
1.1874 + * <li><code>offset < end</code>
1.1875 + * <li><code>offset >= 0</code>
1.1876 + * </ul></dd></dl>
1.1877 + *
1.1878 + * <dl><dt><b>Postconditions:</b></dt><dd>
1.1879 + * <ul><li>the state of the receiver is updated to reflect the XML element
1.1880 + * parsed from the reader
1.1881 + * </ul></dd></dl><dl>
1.1882 + *
1.1883 + * @throws nanoxml.XMLParseException
1.1884 + * If an error occured while parsing the string.
1.1885 + */
1.1886 + public void parseCharArray(char[] input,
1.1887 + int offset,
1.1888 + int end)
1.1889 + throws XMLParseException
1.1890 + {
1.1891 + this.parseCharArray(input, offset, end, /*startingLineNr*/ 1);
1.1892 + }
1.1893 +
1.1894 +
1.1895 + /**
1.1896 + * Reads one XML element from a char array and parses it.
1.1897 + *
1.1898 + * @param reader
1.1899 + * The reader from which to retrieve the XML data.
1.1900 + * @param offset
1.1901 + * The first character in <code>string</code> to scan.
1.1902 + * @param end
1.1903 + * The character where to stop scanning.
1.1904 + * This character is not scanned.
1.1905 + * @param startingLineNr
1.1906 + * The line number of the first line in the data.
1.1907 + *
1.1908 + * </dl><dl><dt><b>Preconditions:</b></dt><dd>
1.1909 + * <ul><li><code>input != null</code>
1.1910 + * <li><code>end <= input.length</code>
1.1911 + * <li><code>offset < end</code>
1.1912 + * <li><code>offset >= 0</code>
1.1913 + * </ul></dd></dl>
1.1914 + *
1.1915 + * <dl><dt><b>Postconditions:</b></dt><dd>
1.1916 + * <ul><li>the state of the receiver is updated to reflect the XML element
1.1917 + * parsed from the reader
1.1918 + * </ul></dd></dl><dl>
1.1919 + *
1.1920 + * @throws nanoxml.XMLParseException
1.1921 + * If an error occured while parsing the string.
1.1922 + */
1.1923 + public void parseCharArray(char[] input,
1.1924 + int offset,
1.1925 + int end,
1.1926 + int startingLineNr)
1.1927 + throws XMLParseException
1.1928 + {
1.1929 + try {
1.1930 + Reader reader = new CharArrayReader(input, offset, end);
1.1931 + this.parseFromReader(reader, startingLineNr);
1.1932 + } catch (IOException e) {
1.1933 + // This exception will never happen.
1.1934 + }
1.1935 + }
1.1936 +
1.1937 +
1.1938 + /**
1.1939 + * Removes a child element.
1.1940 + *
1.1941 + * @param child
1.1942 + * The child element to remove.
1.1943 + *
1.1944 + * </dl><dl><dt><b>Preconditions:</b></dt><dd>
1.1945 + * <ul><li><code>child != null</code>
1.1946 + * <li><code>child</code> is a child element of the receiver
1.1947 + * </ul></dd></dl>
1.1948 + *
1.1949 + * <dl><dt><b>Postconditions:</b></dt><dd>
1.1950 + * <ul><li>countChildren() => old.countChildren() - 1
1.1951 + * <li>enumerateChildren() => old.enumerateChildren() - child
1.1952 + * <li>getChildren() => old.enumerateChildren() - child
1.1953 + * </ul></dd></dl><dl>
1.1954 + *
1.1955 + * @see nanoxml.XMLElement#addChild(nanoxml.XMLElement)
1.1956 + * addChild(XMLElement)
1.1957 + * @see nanoxml.XMLElement#countChildren()
1.1958 + * @see nanoxml.XMLElement#enumerateChildren()
1.1959 + * @see nanoxml.XMLElement#getChildren()
1.1960 + */
1.1961 + public void removeChild(XMLElement child)
1.1962 + {
1.1963 + this.children.removeElement(child);
1.1964 + }
1.1965 +
1.1966 +
1.1967 + /**
1.1968 + * Removes an attribute.
1.1969 + *
1.1970 + * @param name
1.1971 + * The name of the attribute.
1.1972 + *
1.1973 + * </dl><dl><dt><b>Preconditions:</b></dt><dd>
1.1974 + * <ul><li><code>name != null</code>
1.1975 + * <li><code>name</code> is a valid XML identifier
1.1976 + * </ul></dd></dl>
1.1977 + *
1.1978 + * <dl><dt><b>Postconditions:</b></dt><dd>
1.1979 + * <ul><li>enumerateAttributeNames()
1.1980 + * => old.enumerateAttributeNames() - name
1.1981 + * <li>getAttribute(name) => <code>null</code>
1.1982 + * </ul></dd></dl><dl>
1.1983 + *
1.1984 + * @see nanoxml.XMLElement#enumerateAttributeNames()
1.1985 + * @see nanoxml.XMLElement#setDoubleAttribute(java.lang.String, double)
1.1986 + * setDoubleAttribute(String, double)
1.1987 + * @see nanoxml.XMLElement#setIntAttribute(java.lang.String, int)
1.1988 + * setIntAttribute(String, int)
1.1989 + * @see nanoxml.XMLElement#setAttribute(java.lang.String, java.lang.Object)
1.1990 + * setAttribute(String, Object)
1.1991 + * @see nanoxml.XMLElement#getAttribute(java.lang.String)
1.1992 + * getAttribute(String)
1.1993 + * @see nanoxml.XMLElement#getAttribute(java.lang.String, java.lang.Object)
1.1994 + * getAttribute(String, Object)
1.1995 + * @see nanoxml.XMLElement#getAttribute(java.lang.String,
1.1996 + * java.util.Hashtable,
1.1997 + * java.lang.String, boolean)
1.1998 + * getAttribute(String, Hashtable, String, boolean)
1.1999 + * @see nanoxml.XMLElement#getStringAttribute(java.lang.String)
1.2000 + * getStringAttribute(String)
1.2001 + * @see nanoxml.XMLElement#getStringAttribute(java.lang.String,
1.2002 + * java.lang.String)
1.2003 + * getStringAttribute(String, String)
1.2004 + * @see nanoxml.XMLElement#getStringAttribute(java.lang.String,
1.2005 + * java.util.Hashtable,
1.2006 + * java.lang.String, boolean)
1.2007 + * getStringAttribute(String, Hashtable, String, boolean)
1.2008 + * @see nanoxml.XMLElement#getIntAttribute(java.lang.String)
1.2009 + * getIntAttribute(String)
1.2010 + * @see nanoxml.XMLElement#getIntAttribute(java.lang.String, int)
1.2011 + * getIntAttribute(String, int)
1.2012 + * @see nanoxml.XMLElement#getIntAttribute(java.lang.String,
1.2013 + * java.util.Hashtable,
1.2014 + * java.lang.String, boolean)
1.2015 + * getIntAttribute(String, Hashtable, String, boolean)
1.2016 + * @see nanoxml.XMLElement#getDoubleAttribute(java.lang.String)
1.2017 + * getDoubleAttribute(String)
1.2018 + * @see nanoxml.XMLElement#getDoubleAttribute(java.lang.String, double)
1.2019 + * getDoubleAttribute(String, double)
1.2020 + * @see nanoxml.XMLElement#getDoubleAttribute(java.lang.String,
1.2021 + * java.util.Hashtable,
1.2022 + * java.lang.String, boolean)
1.2023 + * getDoubleAttribute(String, Hashtable, String, boolean)
1.2024 + * @see nanoxml.XMLElement#getBooleanAttribute(java.lang.String,
1.2025 + * java.lang.String,
1.2026 + * java.lang.String, boolean)
1.2027 + * getBooleanAttribute(String, String, String, boolean)
1.2028 + */
1.2029 + public void removeAttribute(String name)
1.2030 + {
1.2031 + if (this.ignoreCase) {
1.2032 + name = name.toUpperCase();
1.2033 + }
1.2034 + this.attributes.remove(name);
1.2035 + }
1.2036 +
1.2037 +
1.2038 + /**
1.2039 + * Removes an attribute.
1.2040 + *
1.2041 + * @param name
1.2042 + * The name of the attribute.
1.2043 + *
1.2044 + * @deprecated Use {@link #removeAttribute(java.lang.String)
1.2045 + * removeAttribute} instead.
1.2046 + */
1.2047 + public void removeProperty(String name)
1.2048 + {
1.2049 + this.removeAttribute(name);
1.2050 + }
1.2051 +
1.2052 +
1.2053 + /**
1.2054 + * Removes an attribute.
1.2055 + *
1.2056 + * @param name
1.2057 + * The name of the attribute.
1.2058 + *
1.2059 + * @deprecated Use {@link #removeAttribute(java.lang.String)
1.2060 + * removeAttribute} instead.
1.2061 + */
1.2062 + public void removeChild(String name)
1.2063 + {
1.2064 + this.removeAttribute(name);
1.2065 + }
1.2066 +
1.2067 +
1.2068 + /**
1.2069 + * Creates a new similar XML element.
1.2070 + * <P>
1.2071 + * You should override this method when subclassing XMLElement.
1.2072 + */
1.2073 + protected XMLElement createAnotherElement()
1.2074 + {
1.2075 + return new XMLElement(this.entities,
1.2076 + this.ignoreWhitespace,
1.2077 + false,
1.2078 + this.ignoreCase);
1.2079 + }
1.2080 +
1.2081 +
1.2082 + /**
1.2083 + * Changes the content string.
1.2084 + *
1.2085 + * @param content
1.2086 + * The new content string.
1.2087 + */
1.2088 + public void setContent(String content)
1.2089 + {
1.2090 + this.contents = content;
1.2091 + }
1.2092 +
1.2093 +
1.2094 + /**
1.2095 + * Changes the name of the element.
1.2096 + *
1.2097 + * @param name
1.2098 + * The new name.
1.2099 + *
1.2100 + * @deprecated Use {@link #setName(java.lang.String) setName} instead.
1.2101 + */
1.2102 + public void setTagName(String name)
1.2103 + {
1.2104 + this.setName(name);
1.2105 + }
1.2106 +
1.2107 +
1.2108 + /**
1.2109 + * Changes the name of the element.
1.2110 + *
1.2111 + * @param name
1.2112 + * The new name.
1.2113 + *
1.2114 + * </dl><dl><dt><b>Preconditions:</b></dt><dd>
1.2115 + * <ul><li><code>name != null</code>
1.2116 + * <li><code>name</code> is a valid XML identifier
1.2117 + * </ul></dd></dl>
1.2118 + *
1.2119 + * @see nanoxml.XMLElement#getName()
1.2120 + */
1.2121 + public void setName(String name)
1.2122 + {
1.2123 + this.name = name;
1.2124 + }
1.2125 +
1.2126 +
1.2127 + /**
1.2128 + * Writes the XML element to a string.
1.2129 + *
1.2130 + * @see nanoxml.XMLElement#write(java.io.Writer) write(Writer)
1.2131 + */
1.2132 + public String toString()
1.2133 + {
1.2134 + try {
1.2135 + ByteArrayOutputStream out = new ByteArrayOutputStream();
1.2136 + OutputStreamWriter writer = new OutputStreamWriter(out);
1.2137 + this.write(writer);
1.2138 + writer.flush();
1.2139 + return new String(out.toByteArray());
1.2140 + } catch (IOException e) {
1.2141 + // Java exception handling suxx
1.2142 + return super.toString();
1.2143 + }
1.2144 + }
1.2145 +
1.2146 +
1.2147 + /**
1.2148 + * Writes the XML element to a writer.
1.2149 + *
1.2150 + * @param writer
1.2151 + * The writer to write the XML data to.
1.2152 + *
1.2153 + * </dl><dl><dt><b>Preconditions:</b></dt><dd>
1.2154 + * <ul><li><code>writer != null</code>
1.2155 + * <li><code>writer</code> is not closed
1.2156 + * </ul></dd></dl>
1.2157 + *
1.2158 + * @throws java.io.IOException
1.2159 + * If the data could not be written to the writer.
1.2160 + *
1.2161 + * @see nanoxml.XMLElement#toString()
1.2162 + */
1.2163 + public void write(Writer writer)
1.2164 + throws IOException
1.2165 + {
1.2166 + if (this.name == null) {
1.2167 + this.writeEncoded(writer, this.contents);
1.2168 + return;
1.2169 + }
1.2170 + writer.write('<');
1.2171 + writer.write(this.name);
1.2172 + if (! this.attributes.isEmpty()) {
1.2173 + Enumeration enumV = this.attributes.keys();
1.2174 + while (enumV.hasMoreElements()) {
1.2175 + writer.write(' ');
1.2176 + String key = (String) enumV.nextElement();
1.2177 + String value = (String) this.attributes.get(key);
1.2178 + writer.write(key);
1.2179 + writer.write('='); writer.write('"');
1.2180 + this.writeEncoded(writer, value);
1.2181 + writer.write('"');
1.2182 + }
1.2183 + }
1.2184 + if ((this.contents != null) && (this.contents.length() > 0)) {
1.2185 + writer.write('>');
1.2186 + this.writeEncoded(writer, this.contents);
1.2187 + writer.write('<'); writer.write('/');
1.2188 + writer.write(this.name);
1.2189 + writer.write('>');
1.2190 + } else if (this.children.isEmpty()) {
1.2191 + writer.write('/'); writer.write('>');
1.2192 + } else {
1.2193 + writer.write('>');
1.2194 + Enumeration enumV = this.enumerateChildren();
1.2195 + while (enumV.hasMoreElements()) {
1.2196 + XMLElement child = (XMLElement) enumV.nextElement();
1.2197 + child.write(writer);
1.2198 + }
1.2199 + writer.write('<'); writer.write('/');
1.2200 + writer.write(this.name);
1.2201 + writer.write('>');
1.2202 + }
1.2203 + }
1.2204 +
1.2205 +
1.2206 + /**
1.2207 + * Writes a string encoded to a writer.
1.2208 + *
1.2209 + * @param writer
1.2210 + * The writer to write the XML data to.
1.2211 + * @param str
1.2212 + * The string to write encoded.
1.2213 + *
1.2214 + * </dl><dl><dt><b>Preconditions:</b></dt><dd>
1.2215 + * <ul><li><code>writer != null</code>
1.2216 + * <li><code>writer</code> is not closed
1.2217 + * <li><code>str != null</code>
1.2218 + * </ul></dd></dl>
1.2219 + */
1.2220 + protected void writeEncoded(Writer writer,
1.2221 + String str)
1.2222 + throws IOException
1.2223 + {
1.2224 + for (int i = 0; i < str.length(); i += 1) {
1.2225 + char ch = str.charAt(i);
1.2226 + switch (ch) {
1.2227 + case '<':
1.2228 + writer.write('&'); writer.write('l'); writer.write('t');
1.2229 + writer.write(';');
1.2230 + break;
1.2231 + case '>':
1.2232 + writer.write('&'); writer.write('g'); writer.write('t');
1.2233 + writer.write(';');
1.2234 + break;
1.2235 + case '&':
1.2236 + writer.write('&'); writer.write('a'); writer.write('m');
1.2237 + writer.write('p'); writer.write(';');
1.2238 + break;
1.2239 + case '"':
1.2240 + writer.write('&'); writer.write('q'); writer.write('u');
1.2241 + writer.write('o'); writer.write('t'); writer.write(';');
1.2242 + break;
1.2243 + case '\'':
1.2244 + writer.write('&'); writer.write('a'); writer.write('p');
1.2245 + writer.write('o'); writer.write('s'); writer.write(';');
1.2246 + break;
1.2247 + default:
1.2248 + int unicode = (int) ch;
1.2249 + if ((unicode < 32) || (unicode > 126)) {
1.2250 + writer.write('&'); writer.write('#');
1.2251 + writer.write('x');
1.2252 + writer.write(Integer.toString(unicode, 16));
1.2253 + writer.write(';');
1.2254 + } else {
1.2255 + writer.write(ch);
1.2256 + }
1.2257 + }
1.2258 + }
1.2259 + }
1.2260 +
1.2261 +
1.2262 + /**
1.2263 + * Scans an identifier from the current reader.
1.2264 + * The scanned identifier is appended to <code>result</code>.
1.2265 + *
1.2266 + * @param result
1.2267 + * The buffer in which the scanned identifier will be put.
1.2268 + *
1.2269 + * </dl><dl><dt><b>Preconditions:</b></dt><dd>
1.2270 + * <ul><li><code>result != null</code>
1.2271 + * <li>The next character read from the reader is a valid first
1.2272 + * character of an XML identifier.
1.2273 + * </ul></dd></dl>
1.2274 + *
1.2275 + * <dl><dt><b>Postconditions:</b></dt><dd>
1.2276 + * <ul><li>The next character read from the reader won't be an identifier
1.2277 + * character.
1.2278 + * </ul></dd></dl><dl>
1.2279 + */
1.2280 + protected void scanIdentifier(StringBuffer result)
1.2281 + throws IOException
1.2282 + {
1.2283 + for (;;) {
1.2284 + char ch = this.readChar();
1.2285 + if (((ch < 'A') || (ch > 'Z')) && ((ch < 'a') || (ch > 'z'))
1.2286 + && ((ch < '0') || (ch > '9')) && (ch != '_') && (ch != '.')
1.2287 + && (ch != ':') && (ch != '-') && (ch <= '\u007E')) {
1.2288 + this.unreadChar(ch);
1.2289 + return;
1.2290 + }
1.2291 + result.append(ch);
1.2292 + }
1.2293 + }
1.2294 +
1.2295 +
1.2296 + /**
1.2297 + * This method scans an identifier from the current reader.
1.2298 + *
1.2299 + * @return the next character following the whitespace.
1.2300 + */
1.2301 + protected char scanWhitespace()
1.2302 + throws IOException
1.2303 + {
1.2304 + for (;;) {
1.2305 + char ch = this.readChar();
1.2306 + switch (ch) {
1.2307 + case ' ':
1.2308 + case '\t':
1.2309 + case '\n':
1.2310 + case '\r':
1.2311 + break;
1.2312 + default:
1.2313 + return ch;
1.2314 + }
1.2315 + }
1.2316 + }
1.2317 +
1.2318 +
1.2319 + /**
1.2320 + * This method scans an identifier from the current reader.
1.2321 + * The scanned whitespace is appended to <code>result</code>.
1.2322 + *
1.2323 + * @return the next character following the whitespace.
1.2324 + *
1.2325 + * </dl><dl><dt><b>Preconditions:</b></dt><dd>
1.2326 + * <ul><li><code>result != null</code>
1.2327 + * </ul></dd></dl>
1.2328 + */
1.2329 + protected char scanWhitespace(StringBuffer result)
1.2330 + throws IOException
1.2331 + {
1.2332 + for (;;) {
1.2333 + char ch = this.readChar();
1.2334 + switch (ch) {
1.2335 + case ' ':
1.2336 + case '\t':
1.2337 + case '\n':
1.2338 + result.append(ch);
1.2339 + case '\r':
1.2340 + break;
1.2341 + default:
1.2342 + return ch;
1.2343 + }
1.2344 + }
1.2345 + }
1.2346 +
1.2347 +
1.2348 + /**
1.2349 + * This method scans a delimited string from the current reader.
1.2350 + * The scanned string without delimiters is appended to
1.2351 + * <code>string</code>.
1.2352 + *
1.2353 + * </dl><dl><dt><b>Preconditions:</b></dt><dd>
1.2354 + * <ul><li><code>string != null</code>
1.2355 + * <li>the next char read is the string delimiter
1.2356 + * </ul></dd></dl>
1.2357 + */
1.2358 + protected void scanString(StringBuffer string)
1.2359 + throws IOException
1.2360 + {
1.2361 + char delimiter = this.readChar();
1.2362 + if ((delimiter != '\'') && (delimiter != '"')) {
1.2363 + throw this.expectedInput("' or \"");
1.2364 + }
1.2365 + for (;;) {
1.2366 + char ch = this.readChar();
1.2367 + if (ch == delimiter) {
1.2368 + return;
1.2369 + } else if (ch == '&') {
1.2370 + this.resolveEntity(string);
1.2371 + } else {
1.2372 + string.append(ch);
1.2373 + }
1.2374 + }
1.2375 + }
1.2376 +
1.2377 +
1.2378 + /**
1.2379 + * Scans a #PCDATA element. CDATA sections and entities are resolved.
1.2380 + * The next < char is skipped.
1.2381 + * The scanned data is appended to <code>data</code>.
1.2382 + *
1.2383 + * </dl><dl><dt><b>Preconditions:</b></dt><dd>
1.2384 + * <ul><li><code>data != null</code>
1.2385 + * </ul></dd></dl>
1.2386 + */
1.2387 + protected void scanPCData(StringBuffer data)
1.2388 + throws IOException
1.2389 + {
1.2390 + for (;;) {
1.2391 + char ch = this.readChar();
1.2392 + if (ch == '<') {
1.2393 + ch = this.readChar();
1.2394 + if (ch == '!') {
1.2395 + this.checkCDATA(data);
1.2396 + } else {
1.2397 + this.unreadChar(ch);
1.2398 + return;
1.2399 + }
1.2400 + } else if (ch == '&') {
1.2401 + this.resolveEntity(data);
1.2402 + } else {
1.2403 + data.append(ch);
1.2404 + }
1.2405 + }
1.2406 + }
1.2407 +
1.2408 +
1.2409 + /**
1.2410 + * Scans a special tag and if the tag is a CDATA section, append its
1.2411 + * content to <code>buf</code>.
1.2412 + *
1.2413 + * </dl><dl><dt><b>Preconditions:</b></dt><dd>
1.2414 + * <ul><li><code>buf != null</code>
1.2415 + * <li>The first < has already been read.
1.2416 + * </ul></dd></dl>
1.2417 + */
1.2418 + protected boolean checkCDATA(StringBuffer buf)
1.2419 + throws IOException
1.2420 + {
1.2421 + char ch = this.readChar();
1.2422 + if (ch != '[') {
1.2423 + this.unreadChar(ch);
1.2424 + this.skipSpecialTag(0);
1.2425 + return false;
1.2426 + } else if (! this.checkLiteral("CDATA[")) {
1.2427 + this.skipSpecialTag(1); // one [ has already been read
1.2428 + return false;
1.2429 + } else {
1.2430 + int delimiterCharsSkipped = 0;
1.2431 + while (delimiterCharsSkipped < 3) {
1.2432 + ch = this.readChar();
1.2433 + switch (ch) {
1.2434 + case ']':
1.2435 + if (delimiterCharsSkipped < 2) {
1.2436 + delimiterCharsSkipped += 1;
1.2437 + } else {
1.2438 + buf.append(']');
1.2439 + buf.append(']');
1.2440 + delimiterCharsSkipped = 0;
1.2441 + }
1.2442 + break;
1.2443 + case '>':
1.2444 + if (delimiterCharsSkipped < 2) {
1.2445 + for (int i = 0; i < delimiterCharsSkipped; i++) {
1.2446 + buf.append(']');
1.2447 + }
1.2448 + delimiterCharsSkipped = 0;
1.2449 + buf.append('>');
1.2450 + } else {
1.2451 + delimiterCharsSkipped = 3;
1.2452 + }
1.2453 + break;
1.2454 + default:
1.2455 + for (int i = 0; i < delimiterCharsSkipped; i += 1) {
1.2456 + buf.append(']');
1.2457 + }
1.2458 + buf.append(ch);
1.2459 + delimiterCharsSkipped = 0;
1.2460 + }
1.2461 + }
1.2462 + return true;
1.2463 + }
1.2464 + }
1.2465 +
1.2466 +
1.2467 + /**
1.2468 + * Skips a comment.
1.2469 + *
1.2470 + * </dl><dl><dt><b>Preconditions:</b></dt><dd>
1.2471 + * <ul><li>The first <!-- has already been read.
1.2472 + * </ul></dd></dl>
1.2473 + */
1.2474 + protected void skipComment()
1.2475 + throws IOException
1.2476 + {
1.2477 + int dashesToRead = 2;
1.2478 + while (dashesToRead > 0) {
1.2479 + char ch = this.readChar();
1.2480 + if (ch == '-') {
1.2481 + dashesToRead -= 1;
1.2482 + } else {
1.2483 + dashesToRead = 2;
1.2484 + }
1.2485 + }
1.2486 + if (this.readChar() != '>') {
1.2487 + throw this.expectedInput(">");
1.2488 + }
1.2489 + }
1.2490 +
1.2491 +
1.2492 + /**
1.2493 + * Skips a special tag or comment.
1.2494 + *
1.2495 + * @param bracketLevel The number of open square brackets ([) that have
1.2496 + * already been read.
1.2497 + *
1.2498 + * </dl><dl><dt><b>Preconditions:</b></dt><dd>
1.2499 + * <ul><li>The first <! has already been read.
1.2500 + * <li><code>bracketLevel >= 0</code>
1.2501 + * </ul></dd></dl>
1.2502 + */
1.2503 + protected void skipSpecialTag(int bracketLevel)
1.2504 + throws IOException
1.2505 + {
1.2506 + int tagLevel = 1; // <
1.2507 + char stringDelimiter = '\0';
1.2508 + if (bracketLevel == 0) {
1.2509 + char ch = this.readChar();
1.2510 + if (ch == '[') {
1.2511 + bracketLevel += 1;
1.2512 + } else if (ch == '-') {
1.2513 + ch = this.readChar();
1.2514 + if (ch == '[') {
1.2515 + bracketLevel += 1;
1.2516 + } else if (ch == ']') {
1.2517 + bracketLevel -= 1;
1.2518 + } else if (ch == '-') {
1.2519 + this.skipComment();
1.2520 + return;
1.2521 + }
1.2522 + }
1.2523 + }
1.2524 + while (tagLevel > 0) {
1.2525 + char ch = this.readChar();
1.2526 + if (stringDelimiter == '\0') {
1.2527 + if ((ch == '"') || (ch == '\'')) {
1.2528 + stringDelimiter = ch;
1.2529 + } else if (bracketLevel <= 0) {
1.2530 + if (ch == '<') {
1.2531 + tagLevel += 1;
1.2532 + } else if (ch == '>') {
1.2533 + tagLevel -= 1;
1.2534 + }
1.2535 + }
1.2536 + if (ch == '[') {
1.2537 + bracketLevel += 1;
1.2538 + } else if (ch == ']') {
1.2539 + bracketLevel -= 1;
1.2540 + }
1.2541 + } else {
1.2542 + if (ch == stringDelimiter) {
1.2543 + stringDelimiter = '\0';
1.2544 + }
1.2545 + }
1.2546 + }
1.2547 + }
1.2548 +
1.2549 +
1.2550 + /**
1.2551 + * Scans the data for literal text.
1.2552 + * Scanning stops when a character does not match or after the complete
1.2553 + * text has been checked, whichever comes first.
1.2554 + *
1.2555 + * @param literal the literal to check.
1.2556 + *
1.2557 + * </dl><dl><dt><b>Preconditions:</b></dt><dd>
1.2558 + * <ul><li><code>literal != null</code>
1.2559 + * </ul></dd></dl>
1.2560 + */
1.2561 + protected boolean checkLiteral(String literal)
1.2562 + throws IOException
1.2563 + {
1.2564 + int length = literal.length();
1.2565 + for (int i = 0; i < length; i += 1) {
1.2566 + if (this.readChar() != literal.charAt(i)) {
1.2567 + return false;
1.2568 + }
1.2569 + }
1.2570 + return true;
1.2571 + }
1.2572 +
1.2573 +
1.2574 + /**
1.2575 + * Reads a character from a reader.
1.2576 + */
1.2577 + protected char readChar()
1.2578 + throws IOException
1.2579 + {
1.2580 + if (this.charReadTooMuch != '\0') {
1.2581 + char ch = this.charReadTooMuch;
1.2582 + this.charReadTooMuch = '\0';
1.2583 + return ch;
1.2584 + } else {
1.2585 + int i = this.reader.read();
1.2586 + if (i < 0) {
1.2587 + throw this.unexpectedEndOfData();
1.2588 + } else if (i == 10) {
1.2589 + this.parserLineNr += 1;
1.2590 + return '\n';
1.2591 + } else {
1.2592 + return (char) i;
1.2593 + }
1.2594 + }
1.2595 + }
1.2596 +
1.2597 +
1.2598 + /**
1.2599 + * Scans an XML element.
1.2600 + *
1.2601 + * @param elt The element that will contain the result.
1.2602 + *
1.2603 + * </dl><dl><dt><b>Preconditions:</b></dt><dd>
1.2604 + * <ul><li>The first < has already been read.
1.2605 + * <li><code>elt != null</code>
1.2606 + * </ul></dd></dl>
1.2607 + */
1.2608 + protected void scanElement(XMLElement elt)
1.2609 + throws IOException
1.2610 + {
1.2611 + StringBuffer buf = new StringBuffer();
1.2612 + this.scanIdentifier(buf);
1.2613 + String name = buf.toString();
1.2614 + elt.setName(name);
1.2615 + char ch = this.scanWhitespace();
1.2616 + while ((ch != '>') && (ch != '/')) {
1.2617 + buf.setLength(0);
1.2618 + this.unreadChar(ch);
1.2619 + this.scanIdentifier(buf);
1.2620 + String key = buf.toString();
1.2621 + ch = this.scanWhitespace();
1.2622 + if (ch != '=') {
1.2623 + throw this.expectedInput("=");
1.2624 + }
1.2625 + this.unreadChar(this.scanWhitespace());
1.2626 + buf.setLength(0);
1.2627 + this.scanString(buf);
1.2628 + elt.setAttribute(key, buf);
1.2629 + ch = this.scanWhitespace();
1.2630 + }
1.2631 + if (ch == '/') {
1.2632 + ch = this.readChar();
1.2633 + if (ch != '>') {
1.2634 + throw this.expectedInput(">");
1.2635 + }
1.2636 + return;
1.2637 + }
1.2638 + buf.setLength(0);
1.2639 + ch = this.scanWhitespace(buf);
1.2640 + if (ch != '<') {
1.2641 + this.unreadChar(ch);
1.2642 + this.scanPCData(buf);
1.2643 + } else {
1.2644 + for (;;) {
1.2645 + ch = this.readChar();
1.2646 + if (ch == '!') {
1.2647 + if (this.checkCDATA(buf)) {
1.2648 + this.scanPCData(buf);
1.2649 + break;
1.2650 + } else {
1.2651 + ch = this.scanWhitespace(buf);
1.2652 + if (ch != '<') {
1.2653 + this.unreadChar(ch);
1.2654 + this.scanPCData(buf);
1.2655 + break;
1.2656 + }
1.2657 + }
1.2658 + } else {
1.2659 + if ((ch != '/') || this.ignoreWhitespace) {
1.2660 + buf.setLength(0);
1.2661 + }
1.2662 + if (ch == '/') {
1.2663 + this.unreadChar(ch);
1.2664 + }
1.2665 + break;
1.2666 + }
1.2667 + }
1.2668 + }
1.2669 + if (buf.length() == 0) {
1.2670 + while (ch != '/') {
1.2671 + if (ch == '!') {
1.2672 + ch = this.readChar();
1.2673 + if (ch != '-') {
1.2674 + throw this.expectedInput("Comment or Element");
1.2675 + }
1.2676 + ch = this.readChar();
1.2677 + if (ch != '-') {
1.2678 + throw this.expectedInput("Comment or Element");
1.2679 + }
1.2680 + this.skipComment();
1.2681 + } else {
1.2682 + this.unreadChar(ch);
1.2683 + XMLElement child = this.createAnotherElement();
1.2684 + this.scanElement(child);
1.2685 + elt.addChild(child);
1.2686 + }
1.2687 + ch = this.scanWhitespace();
1.2688 + if (ch != '<') {
1.2689 + throw this.expectedInput("<");
1.2690 + }
1.2691 + ch = this.readChar();
1.2692 + }
1.2693 + this.unreadChar(ch);
1.2694 + } else {
1.2695 + if (this.ignoreWhitespace) {
1.2696 + elt.setContent(buf.toString().trim());
1.2697 + } else {
1.2698 + elt.setContent(buf.toString());
1.2699 + }
1.2700 + }
1.2701 + ch = this.readChar();
1.2702 + if (ch != '/') {
1.2703 + throw this.expectedInput("/");
1.2704 + }
1.2705 + this.unreadChar(this.scanWhitespace());
1.2706 + if (! this.checkLiteral(name)) {
1.2707 + throw this.expectedInput(name);
1.2708 + }
1.2709 + if (this.scanWhitespace() != '>') {
1.2710 + throw this.expectedInput(">");
1.2711 + }
1.2712 + }
1.2713 +
1.2714 +
1.2715 + /**
1.2716 + * Resolves an entity. The name of the entity is read from the reader.
1.2717 + * The value of the entity is appended to <code>buf</code>.
1.2718 + *
1.2719 + * @param buf Where to put the entity value.
1.2720 + *
1.2721 + * </dl><dl><dt><b>Preconditions:</b></dt><dd>
1.2722 + * <ul><li>The first & has already been read.
1.2723 + * <li><code>buf != null</code>
1.2724 + * </ul></dd></dl>
1.2725 + */
1.2726 + protected void resolveEntity(StringBuffer buf)
1.2727 + throws IOException
1.2728 + {
1.2729 + char ch = '\0';
1.2730 + StringBuffer keyBuf = new StringBuffer();
1.2731 + for (;;) {
1.2732 + ch = this.readChar();
1.2733 + if (ch == ';') {
1.2734 + break;
1.2735 + }
1.2736 + keyBuf.append(ch);
1.2737 + }
1.2738 + String key = keyBuf.toString();
1.2739 + if (key.charAt(0) == '#') {
1.2740 + try {
1.2741 + if (key.charAt(1) == 'x') {
1.2742 + ch = (char) Integer.parseInt(key.substring(2), 16);
1.2743 + } else {
1.2744 + ch = (char) Integer.parseInt(key.substring(1), 10);
1.2745 + }
1.2746 + } catch (NumberFormatException e) {
1.2747 + throw this.unknownEntity(key);
1.2748 + }
1.2749 + buf.append(ch);
1.2750 + } else {
1.2751 + char[] value = (char[]) this.entities.get(key);
1.2752 + if (value == null) {
1.2753 + throw this.unknownEntity(key);
1.2754 + }
1.2755 + buf.append(value);
1.2756 + }
1.2757 + }
1.2758 +
1.2759 +
1.2760 + /**
1.2761 + * Pushes a character back to the read-back buffer.
1.2762 + *
1.2763 + * @param ch The character to push back.
1.2764 + *
1.2765 + * </dl><dl><dt><b>Preconditions:</b></dt><dd>
1.2766 + * <ul><li>The read-back buffer is empty.
1.2767 + * <li><code>ch != '\0'</code>
1.2768 + * </ul></dd></dl>
1.2769 + */
1.2770 + protected void unreadChar(char ch)
1.2771 + {
1.2772 + this.charReadTooMuch = ch;
1.2773 + }
1.2774 +
1.2775 +
1.2776 + /**
1.2777 + * Creates a parse exception for when an invalid valueset is given to
1.2778 + * a method.
1.2779 + *
1.2780 + * @param name The name of the entity.
1.2781 + *
1.2782 + * </dl><dl><dt><b>Preconditions:</b></dt><dd>
1.2783 + * <ul><li><code>name != null</code>
1.2784 + * </ul></dd></dl>
1.2785 + */
1.2786 + protected XMLParseException invalidValueSet(String name)
1.2787 + {
1.2788 + String msg = "Invalid value set (entity name = \"" + name + "\")";
1.2789 + return new XMLParseException(this.getName(), this.parserLineNr, msg);
1.2790 + }
1.2791 +
1.2792 +
1.2793 + /**
1.2794 + * Creates a parse exception for when an invalid value is given to a
1.2795 + * method.
1.2796 + *
1.2797 + * @param name The name of the entity.
1.2798 + * @param value The value of the entity.
1.2799 + *
1.2800 + * </dl><dl><dt><b>Preconditions:</b></dt><dd>
1.2801 + * <ul><li><code>name != null</code>
1.2802 + * <li><code>value != null</code>
1.2803 + * </ul></dd></dl>
1.2804 + */
1.2805 + protected XMLParseException invalidValue(String name,
1.2806 + String value)
1.2807 + {
1.2808 + String msg = "Attribute \"" + name + "\" does not contain a valid "
1.2809 + + "value (\"" + value + "\")";
1.2810 + return new XMLParseException(this.getName(), this.parserLineNr, msg);
1.2811 + }
1.2812 +
1.2813 +
1.2814 + /**
1.2815 + * Creates a parse exception for when the end of the data input has been
1.2816 + * reached.
1.2817 + */
1.2818 + protected XMLParseException unexpectedEndOfData()
1.2819 + {
1.2820 + String msg = "Unexpected end of data reached";
1.2821 + return new XMLParseException(this.getName(), this.parserLineNr, msg);
1.2822 + }
1.2823 +
1.2824 +
1.2825 + /**
1.2826 + * Creates a parse exception for when a syntax error occured.
1.2827 + *
1.2828 + * @param context The context in which the error occured.
1.2829 + *
1.2830 + * </dl><dl><dt><b>Preconditions:</b></dt><dd>
1.2831 + * <ul><li><code>context != null</code>
1.2832 + * <li><code>context.length() > 0</code>
1.2833 + * </ul></dd></dl>
1.2834 + */
1.2835 + protected XMLParseException syntaxError(String context)
1.2836 + {
1.2837 + String msg = "Syntax error while parsing " + context;
1.2838 + return new XMLParseException(this.getName(), this.parserLineNr, msg);
1.2839 + }
1.2840 +
1.2841 +
1.2842 + /**
1.2843 + * Creates a parse exception for when the next character read is not
1.2844 + * the character that was expected.
1.2845 + *
1.2846 + * @param charSet The set of characters (in human readable form) that was
1.2847 + * expected.
1.2848 + *
1.2849 + * </dl><dl><dt><b>Preconditions:</b></dt><dd>
1.2850 + * <ul><li><code>charSet != null</code>
1.2851 + * <li><code>charSet.length() > 0</code>
1.2852 + * </ul></dd></dl>
1.2853 + */
1.2854 + protected XMLParseException expectedInput(String charSet)
1.2855 + {
1.2856 + String msg = "Expected: " + charSet;
1.2857 + return new XMLParseException(this.getName(), this.parserLineNr, msg);
1.2858 + }
1.2859 +
1.2860 +
1.2861 + /**
1.2862 + * Creates a parse exception for when an entity could not be resolved.
1.2863 + *
1.2864 + * @param name The name of the entity.
1.2865 + *
1.2866 + * </dl><dl><dt><b>Preconditions:</b></dt><dd>
1.2867 + * <ul><li><code>name != null</code>
1.2868 + * <li><code>name.length() > 0</code>
1.2869 + * </ul></dd></dl>
1.2870 + */
1.2871 + protected XMLParseException unknownEntity(String name)
1.2872 + {
1.2873 + String msg = "Unknown or invalid entity: &" + name + ";";
1.2874 + return new XMLParseException(this.getName(), this.parserLineNr, msg);
1.2875 + }
1.2876 +
1.2877 +}