|
From: <ian...@us...> - 2008-01-03 18:57:31
|
Revision: 669
http://ogoglio.svn.sourceforge.net/ogoglio/?rev=669&view=rev
Author: iansmith
Date: 2008-01-03 10:57:35 -0800 (Thu, 03 Jan 2008)
Log Message:
-----------
Rolled in support for mail stuff trevor did.
Modified Paths:
--------------
maven/trunk/ogoglio-appdev/src/main/java/com/ogoglio/mail/MailClient.java
maven/trunk/ogoglio-integration-test/src/test/java/com/ogoglio/client/test/ClientTest.java
maven/trunk/ogoglio-server/src/main/java/com/ogoglio/site/AccountServlet.java
maven/trunk/ogoglio-server/src/test/java/com/ogoglio/persist/test/PersistTest.java
Added Paths:
-----------
maven/trunk/ogoglio-common/src/main/java/nanoxml/XMLElement_Localized.java
Modified: maven/trunk/ogoglio-appdev/src/main/java/com/ogoglio/mail/MailClient.java
===================================================================
--- maven/trunk/ogoglio-appdev/src/main/java/com/ogoglio/mail/MailClient.java 2008-01-03 02:14:27 UTC (rev 668)
+++ maven/trunk/ogoglio-appdev/src/main/java/com/ogoglio/mail/MailClient.java 2008-01-03 18:57:35 UTC (rev 669)
@@ -23,6 +23,7 @@
private File outputDir = null;
+ public static final String FAKE_MAIL_PREFIX="Mail-Message-";
/**
* Sends all mail to individual files in outputDir
*/
@@ -58,7 +59,7 @@
}
private void sendToDisk(String to, String from, String subject, String body) throws IOException {
- FileOutputStream output = new FileOutputStream(new File(outputDir, "Mail-Message-" + System.currentTimeMillis()));
+ FileOutputStream output = new FileOutputStream(new File(outputDir, FAKE_MAIL_PREFIX + System.currentTimeMillis()));
output.write(("to: " + to + "\n").getBytes());
output.write(("from: " + from + "\n").getBytes());
output.write(("subject: " + subject + "\n").getBytes());
Added: maven/trunk/ogoglio-common/src/main/java/nanoxml/XMLElement_Localized.java
===================================================================
--- maven/trunk/ogoglio-common/src/main/java/nanoxml/XMLElement_Localized.java (rev 0)
+++ maven/trunk/ogoglio-common/src/main/java/nanoxml/XMLElement_Localized.java 2008-01-03 18:57:35 UTC (rev 669)
@@ -0,0 +1,2958 @@
+/* XMLElement.java
+ *
+ * $Revision: 1.1 $
+ * $Date: 2005/10/13 15:48:55 $
+ * $Name: $
+ *
+ * This file is part of NanoXML 2 Lite.
+ * Copyright (C) 2000-2002 Marc De Scheemaecker, All Rights Reserved.
+ *
+ * This software is provided 'as-is', without any express or implied warranty.
+ * In no event will the authors be held liable for any damages arising from the
+ * use of this software.
+ *
+ * Permission is granted to anyone to use this software for any purpose,
+ * including commercial applications, and to alter it and redistribute it
+ * freely, subject to the following restrictions:
+ *
+ * 1. The origin of this software must not be misrepresented; you must not
+ * claim that you wrote the original software. If you use this software in
+ * a product, an acknowledgment in the product documentation would be
+ * appreciated but is not required.
+ *
+ * 2. Altered source versions must be plainly marked as such, and must not be
+ * misrepresented as being the original software.
+ *
+ * 3. This notice may not be removed or altered from any source distribution.
+ *****************************************************************************/
+
+// TFS added some basic functions. Look for the ones without Javadocs. :-O
+// IES: fixed internationalization: now we FORCE it to EN_us
+package nanoxml;
+
+import java.io.ByteArrayOutputStream;
+import java.io.CharArrayReader;
+import java.io.IOException;
+import java.io.OutputStreamWriter;
+import java.io.Reader;
+import java.io.StringReader;
+import java.io.Writer;
+import java.text.ParseException;
+import java.util.Enumeration;
+import java.util.Hashtable;
+import java.util.Locale;
+import java.util.Vector;
+
+/**
+ * XMLElement is a representation of an XML object. The object is able to parse XML code.
+ * <P>
+ * <DL>
+ * <DT><B>Parsing XML Data</B></DT>
+ * <DD> You can parse XML data using the following code:
+ * <UL>
+ * <CODE> XMLElement xml = new XMLElement();<BR>
+ * FileReader reader = new FileReader("filename.xml");<BR>
+ * xml.parseFromReader(reader); </CODE>
+ * </UL>
+ * </DD>
+ * </DL>
+ * <DL>
+ * <DT><B>Retrieving Attributes</B></DT>
+ * <DD> You can enumerate the attributes of an element using the method {@link #enumerateAttributeNames() enumerateAttributeNames}. The attribute values can be retrieved using the method {@link #getStringAttribute(java.lang.String) getStringAttribute}. The following example shows how to list the attributes of an element:
+ * <UL>
+ * <CODE> XMLElement element = ...;<BR>
+ * Enumeration enum = element.getAttributeNames();<BR>
+ * while (enum.hasMoreElements()) {<BR>
+ * String key = (String) enum.nextElement();<BR>
+ * String value = element.getStringAttribute(key);<BR>
+ * System.out.println(key + " = " + value);<BR> } </CODE>
+ * </UL>
+ * </DD>
+ * </DL>
+ * <DL>
+ * <DT><B>Retrieving Child Elements</B></DT>
+ * <DD> You can enumerate the children of an element using {@link #enumerateChildren() enumerateChildren}. The number of child elements can be retrieved using {@link #countChildren() countChildren}. </DD>
+ * </DL>
+ * <DL>
+ * <DT><B>Elements Containing Character Data</B></DT>
+ * <DD> If an elements contains character data, like in the following example:
+ * <UL>
+ * <CODE> <title>The Title</title> </CODE>
+ * </UL>
+ * you can retrieve that data using the method {@link #getContent() getContent}. </DD>
+ * </DL>
+ * <DL>
+ * <DT><B>Subclassing XMLElement</B></DT>
+ * <DD> When subclassing XMLElement, you need to override the method {@link #createAnotherElement() createAnotherElement} which has to return a new copy of the receiver. </DD>
+ * </DL>
+ * <P>
+ *
+ * @see nanoxml.XMLParseException
+ *
+ * @author Marc De Scheemaecker <<A href="mailto:cyb...@ma...">cyb...@ma...</A>>
+ * @version $Name: $, $Revision: 1.1 $
+ */
+public class XMLElement_Localized {
+
+ /**
+ * Serialization serial version ID.
+ */
+ static final long serialVersionUID = 6685035139346394777L;
+
+ /**
+ * Major version of NanoXML. Classes with the same major and minor version are binary compatible. Classes with the same major version are source compatible. If the major version is different, you may need to modify the client source code.
+ *
+ * @see nanoxml.XMLElement#NANOXML_MINOR_VERSION
+ */
+ public static final int NANOXML_MAJOR_VERSION = 2;
+
+ /**
+ * Minor version of NanoXML. Classes with the same major and minor version are binary compatible. Classes with the same major version are source compatible. If the major version is different, you may need to modify the client source code.
+ *
+ * @see nanoxml.XMLElement#NANOXML_MAJOR_VERSION
+ */
+ public static final int NANOXML_MINOR_VERSION = 2;
+
+ /**
+ * For dealing with files generated by us and distributed with system. We *always* use
+ * the US locale. Trust us, you don't want multiple file formats.
+ */
+ private static java.text.NumberFormat enUSFormat = java.text.NumberFormat.getInstance(Locale.US);
+
+ /*
+ * True if we have already updated the enUSFormat properly.
+ */
+ private static boolean haveUpdatedenUSFormat = false;
+
+ /**
+ * The attributes given to the element.
+ *
+ * <dl>
+ * <dt><b>Invariants:</b></dt>
+ * <dd>
+ * <ul>
+ * <li>The field can be empty.
+ * <li>The field is never <code>null</code>.
+ * <li>The keys and the values are strings.
+ * </ul>
+ * </dd>
+ * </dl>
+ */
+ private Hashtable attributes;
+
+ /**
+ * Child elements of the element.
+ *
+ * <dl>
+ * <dt><b>Invariants:</b></dt>
+ * <dd>
+ * <ul>
+ * <li>The field can be empty.
+ * <li>The field is never <code>null</code>.
+ * <li>The elements are instances of <code>XMLElement</code> or a subclass of <code>XMLElement</code>.
+ * </ul>
+ * </dd>
+ * </dl>
+ */
+ private Vector children;
+
+ /**
+ * The name of the element.
+ *
+ * <dl>
+ * <dt><b>Invariants:</b></dt>
+ * <dd>
+ * <ul>
+ * <li>The field is <code>null</code> iff the element is not initialized by either parse or setName.
+ * <li>If the field is not <code>null</code>, it's not empty.
+ * <li>If the field is not <code>null</code>, it contains a valid XML identifier.
+ * </ul>
+ * </dd>
+ * </dl>
+ */
+ private String name;
+
+ /**
+ * The #PCDATA content of the object.
+ *
+ * <dl>
+ * <dt><b>Invariants:</b></dt>
+ * <dd>
+ * <ul>
+ * <li>The field is <code>null</code> iff the element is not a #PCDATA element.
+ * <li>The field can be any string, including the empty string.
+ * </ul>
+ * </dd>
+ * </dl>
+ */
+ private String contents;
+
+ /**
+ * Conversion table for &...; entities. The keys are the entity names without the & and ; delimiters.
+ *
+ * <dl>
+ * <dt><b>Invariants:</b></dt>
+ * <dd>
+ * <ul>
+ * <li>The field is never <code>null</code>.
+ * <li>The field always contains the following associations: "lt" => "<", "gt" => ">", "quot" => "\"", "apos" => "'", "amp" => "&"
+ * <li>The keys are strings
+ * <li>The values are char arrays
+ * </ul>
+ * </dd>
+ * </dl>
+ */
+ private Hashtable entities;
+
+ /**
+ * The line number where the element starts.
+ *
+ * <dl>
+ * <dt><b>Invariants:</b></dt>
+ * <dd>
+ * <ul>
+ * <li><code>lineNr >= 0</code>
+ * </ul>
+ * </dd>
+ * </dl>
+ */
+ private int lineNr;
+
+ /**
+ * <code>false</code> if the case of the element and attribute names are case insensitive.
+ */
+ private boolean ignoreCase;
+
+ /**
+ * <code>true</code> if the leading and trailing whitespace of #PCDATA sections have to be ignored.
+ */
+ private boolean ignoreWhitespace;
+
+ /**
+ * Character read too much. This character provides push-back functionality to the input reader without having to use a PushbackReader. If there is no such character, this field is '\0'.
+ */
+ private char charReadTooMuch;
+
+ /**
+ * The reader provided by the caller of the parse method.
+ *
+ * <dl>
+ * <dt><b>Invariants:</b></dt>
+ * <dd>
+ * <ul>
+ * <li>The field is not <code>null</code> while the parse method is running.
+ * </ul>
+ * </dd>
+ * </dl>
+ */
+ private Reader reader;
+
+ /**
+ * The current line number in the source content.
+ *
+ * <dl>
+ * <dt><b>Invariants:</b></dt>
+ * <dd>
+ * <ul>
+ * <li>parserLineNr > 0 while the parse method is running.
+ * </ul>
+ * </dd>
+ * </dl>
+ */
+ private int parserLineNr;
+
+ /**
+ * Creates and initializes a new XML element. Calling the construction is equivalent to:
+ * <ul>
+ * <code>new XMLElement(new Hashtable(), false, true);
+ * setName(name);
+ * </code>
+ * </ul>
+ */
+ public XMLElement_Localized(String name) {
+ this(new Hashtable(), false, true, false);
+ setName(name);
+ }
+
+ /**
+ * Creates and initializes a new XML element. Calling the construction is equivalent to:
+ * <ul>
+ * <code>new XMLElement(new Hashtable(), false, true);
+ * setName(name);
+ * setContent(content);
+ * </code>
+ * </ul>
+ */
+ public XMLElement_Localized(String name, String content) {
+ this(new Hashtable(), false, true, false);
+ setName(name);
+ setContent(content);
+ }
+
+ /**
+ * Creates and initializes a new XML element. Calling the construction is equivalent to:
+ * <ul>
+ * <code>new XMLElement(new Hashtable(), false, true)
+ * </code>
+ * </ul>
+ *
+ * <dl>
+ * <dt><b>Postconditions:</b></dt>
+ * <dd>
+ * <ul>
+ * <li>countChildren() => 0
+ * <li>enumerateChildren() => empty enumeration
+ * <li>enumeratePropertyNames() => empty enumeration
+ * <li>getChildren() => empty vector
+ * <li>getContent() => ""
+ * <li>getLineNr() => 0
+ * <li>getName() => null
+ * </ul>
+ * </dd>
+ * </dl>
+ *
+ * @see nanoxml.XMLElement#XMLElement(java.util.Hashtable) XMLElement(Hashtable)
+ * @see nanoxml.XMLElement#XMLElement(boolean)
+ * @see nanoxml.XMLElement#XMLElement(java.util.Hashtable,boolean) XMLElement(Hashtable, boolean)
+ */
+ public XMLElement_Localized() {
+ this(new Hashtable(), false, true, false);
+ }
+
+ /**
+ * Creates and initializes a new XML element. Calling the construction is equivalent to:
+ * <ul>
+ * <code>new XMLElement(entities, false, true)
+ * </code>
+ * </ul>
+ *
+ * @param entities
+ * The entity conversion table.
+ *
+ * </dl>
+ * <dl>
+ * <dt><b>Preconditions:</b></dt>
+ * <dd>
+ * <ul>
+ * <li><code>entities != null</code>
+ * </ul>
+ * </dd>
+ * </dl>
+ *
+ * <dl>
+ * <dt><b>Postconditions:</b></dt>
+ * <dd>
+ * <ul>
+ * <li>countChildren() => 0
+ * <li>enumerateChildren() => empty enumeration
+ * <li>enumeratePropertyNames() => empty enumeration
+ * <li>getChildren() => empty vector
+ * <li>getContent() => ""
+ * <li>getLineNr() => 0
+ * <li>getName() => null
+ * </ul>
+ * </dd>
+ * </dl>
+ * <dl>
+ *
+ * @see nanoxml.XMLElement#XMLElement()
+ * @see nanoxml.XMLElement#XMLElement(boolean)
+ * @see nanoxml.XMLElement#XMLElement(java.util.Hashtable,boolean) XMLElement(Hashtable, boolean)
+ */
+ public XMLElement_Localized(Hashtable entities) {
+ this(entities, false, true, false);
+ }
+
+ /**
+ * Creates and initializes a new XML element. Calling the construction is equivalent to:
+ * <ul>
+ * <code>new XMLElement(new Hashtable(), skipLeadingWhitespace, true)
+ * </code>
+ * </ul>
+ *
+ * @param skipLeadingWhitespace
+ * <code>true</code> if leading and trailing whitespace in PCDATA content has to be removed.
+ *
+ * </dl>
+ * <dl>
+ * <dt><b>Postconditions:</b></dt>
+ * <dd>
+ * <ul>
+ * <li>countChildren() => 0
+ * <li>enumerateChildren() => empty enumeration
+ * <li>enumeratePropertyNames() => empty enumeration
+ * <li>getChildren() => empty vector
+ * <li>getContent() => ""
+ * <li>getLineNr() => 0
+ * <li>getName() => null
+ * </ul>
+ * </dd>
+ * </dl>
+ * <dl>
+ *
+ * @see nanoxml.XMLElement#XMLElement()
+ * @see nanoxml.XMLElement#XMLElement(java.util.Hashtable) XMLElement(Hashtable)
+ * @see nanoxml.XMLElement#XMLElement(java.util.Hashtable,boolean) XMLElement(Hashtable, boolean)
+ */
+ public XMLElement_Localized(boolean skipLeadingWhitespace) {
+ this(new Hashtable(), skipLeadingWhitespace, true, false);
+ }
+
+ /**
+ * Creates and initializes a new XML element. Calling the construction is equivalent to:
+ * <ul>
+ * <code>new XMLElement(entities, skipLeadingWhitespace, true)
+ * </code>
+ * </ul>
+ *
+ * @param entities
+ * The entity conversion table.
+ * @param skipLeadingWhitespace
+ * <code>true</code> if leading and trailing whitespace in PCDATA content has to be removed.
+ *
+ * </dl>
+ * <dl>
+ * <dt><b>Preconditions:</b></dt>
+ * <dd>
+ * <ul>
+ * <li><code>entities != null</code>
+ * </ul>
+ * </dd>
+ * </dl>
+ *
+ * <dl>
+ * <dt><b>Postconditions:</b></dt>
+ * <dd>
+ * <ul>
+ * <li>countChildren() => 0
+ * <li>enumerateChildren() => empty enumeration
+ * <li>enumeratePropertyNames() => empty enumeration
+ * <li>getChildren() => empty vector
+ * <li>getContent() => ""
+ * <li>getLineNr() => 0
+ * <li>getName() => null
+ * </ul>
+ * </dd>
+ * </dl>
+ * <dl>
+ *
+ * @see nanoxml.XMLElement#XMLElement()
+ * @see nanoxml.XMLElement#XMLElement(boolean)
+ * @see nanoxml.XMLElement#XMLElement(java.util.Hashtable) XMLElement(Hashtable)
+ */
+ public XMLElement_Localized(Hashtable entities, boolean skipLeadingWhitespace) {
+ this(entities, skipLeadingWhitespace, true, false);
+ }
+
+ /**
+ * Creates and initializes a new XML element.
+ *
+ * @param entities
+ * The entity conversion table.
+ * @param skipLeadingWhitespace
+ * <code>true</code> if leading and trailing whitespace in PCDATA content has to be removed.
+ * @param ignoreCase
+ * <code>true</code> if the case of element and attribute names have to be ignored.
+ *
+ * </dl>
+ * <dl>
+ * <dt><b>Preconditions:</b></dt>
+ * <dd>
+ * <ul>
+ * <li><code>entities != null</code>
+ * </ul>
+ * </dd>
+ * </dl>
+ *
+ * <dl>
+ * <dt><b>Postconditions:</b></dt>
+ * <dd>
+ * <ul>
+ * <li>countChildren() => 0
+ * <li>enumerateChildren() => empty enumeration
+ * <li>enumeratePropertyNames() => empty enumeration
+ * <li>getChildren() => empty vector
+ * <li>getContent() => ""
+ * <li>getLineNr() => 0
+ * <li>getName() => null
+ * </ul>
+ * </dd>
+ * </dl>
+ * <dl>
+ *
+ * @see nanoxml.XMLElement#XMLElement()
+ * @see nanoxml.XMLElement#XMLElement(boolean)
+ * @see nanoxml.XMLElement#XMLElement(java.util.Hashtable) XMLElement(Hashtable)
+ * @see nanoxml.XMLElement#XMLElement(java.util.Hashtable,boolean) XMLElement(Hashtable, boolean)
+ */
+ public XMLElement_Localized(Hashtable entities, boolean skipLeadingWhitespace, boolean ignoreCase) {
+ this(entities, skipLeadingWhitespace, true, ignoreCase);
+ }
+
+ /**
+ * Creates and initializes a new XML element.
+ * <P>
+ * This constructor should <I>only</I> be called from {@link #createAnotherElement() createAnotherElement} to create child elements.
+ *
+ * @param entities
+ * The entity conversion table.
+ * @param skipLeadingWhitespace
+ * <code>true</code> if leading and trailing whitespace in PCDATA content has to be removed.
+ * @param fillBasicConversionTable
+ * <code>true</code> if the basic entities need to be added to the entity list.
+ * @param ignoreCase
+ * <code>true</code> if the case of element and attribute names have to be ignored.
+ *
+ * </dl>
+ * <dl>
+ * <dt><b>Preconditions:</b></dt>
+ * <dd>
+ * <ul>
+ * <li><code>entities != null</code>
+ * <li>if <code>fillBasicConversionTable == false</code> then <code>entities</code> contains at least the following entries: <code>amp</code>, <code>lt</code>, <code>gt</code>, <code>apos</code> and <code>quot</code>
+ * </ul>
+ * </dd>
+ * </dl>
+ *
+ * <dl>
+ * <dt><b>Postconditions:</b></dt>
+ * <dd>
+ * <ul>
+ * <li>countChildren() => 0
+ * <li>enumerateChildren() => empty enumeration
+ * <li>enumeratePropertyNames() => empty enumeration
+ * <li>getChildren() => empty vector
+ * <li>getContent() => ""
+ * <li>getLineNr() => 0
+ * <li>getName() => null
+ * </ul>
+ * </dd>
+ * </dl>
+ * <dl>
+ *
+ * @see nanoxml.XMLElement#createAnotherElement()
+ */
+ protected XMLElement_Localized(Hashtable entities, boolean skipLeadingWhitespace, boolean fillBasicConversionTable, boolean ignoreCase) {
+ this.ignoreWhitespace = skipLeadingWhitespace;
+ this.ignoreCase = ignoreCase;
+ this.name = null;
+ this.contents = "";
+ this.attributes = new Hashtable();
+ this.children = new Vector();
+ this.entities = entities;
+ this.lineNr = 0;
+ Enumeration enumerator = this.entities.keys();
+ while (enumerator.hasMoreElements()) {
+ Object key = enumerator.nextElement();
+ Object value = this.entities.get(key);
+ if (value instanceof String) {
+ value = ((String) value).toCharArray();
+ this.entities.put(key, value);
+ }
+ }
+ if (fillBasicConversionTable) {
+ this.entities.put("amp", new char[] { '&' });
+ this.entities.put("quot", new char[] { '"' });
+ this.entities.put("apos", new char[] { '\'' });
+ this.entities.put("lt", new char[] { '<' });
+ this.entities.put("gt", new char[] { '>' });
+ }
+ }
+
+ /**
+ * Adds a child element.
+ *
+ * @param child
+ * The child element to add.
+ *
+ * </dl>
+ * <dl>
+ * <dt><b>Preconditions:</b></dt>
+ * <dd>
+ * <ul>
+ * <li><code>child != null</code>
+ * <li><code>child.getName() != null</code>
+ * <li><code>child</code> does not have a parent element
+ * </ul>
+ * </dd>
+ * </dl>
+ *
+ * <dl>
+ * <dt><b>Postconditions:</b></dt>
+ * <dd>
+ * <ul>
+ * <li>countChildren() => old.countChildren() + 1
+ * <li>enumerateChildren() => old.enumerateChildren() + child
+ * <li>getChildren() => old.enumerateChildren() + child
+ * </ul>
+ * </dd>
+ * </dl>
+ * <dl>
+ *
+ * @see nanoxml.XMLElement#countChildren()
+ * @see nanoxml.XMLElement#enumerateChildren()
+ * @see nanoxml.XMLElement#getChildren()
+ * @see nanoxml.XMLElement#removeChild(nanoxml.XMLElement) removeChild(XMLElement)
+ */
+ public void addChild(XMLElement_Localized child) {
+ this.children.addElement(child);
+ }
+
+ public void removeChildren(String name) {
+ XMLElement[] elements = this.getChildren(name);
+ for (int i = 0; i < elements.length; i++) {
+ removeChild(elements[i]);
+ }
+ }
+
+ /**
+ * Adds or modifies an attribute.
+ *
+ * @param name
+ * The name of the attribute.
+ * @param value
+ * The value of the attribute.
+ *
+ * </dl>
+ * <dl>
+ * <dt><b>Preconditions:</b></dt>
+ * <dd>
+ * <ul>
+ * <li><code>name != null</code>
+ * <li><code>name</code> is a valid XML identifier
+ * <li><code>value != null</code>
+ * </ul>
+ * </dd>
+ * </dl>
+ *
+ * <dl>
+ * <dt><b>Postconditions:</b></dt>
+ * <dd>
+ * <ul>
+ * <li>enumerateAttributeNames() => old.enumerateAttributeNames() + name
+ * <li>getAttribute(name) => value
+ * </ul>
+ * </dd>
+ * </dl>
+ * <dl>
+ *
+ * @see nanoxml.XMLElement#setDoubleAttribute(java.lang.String, double) setDoubleAttribute(String, double)
+ * @see nanoxml.XMLElement#setIntAttribute(java.lang.String, int) setIntAttribute(String, int)
+ * @see nanoxml.XMLElement#enumerateAttributeNames()
+ * @see nanoxml.XMLElement#getAttribute(java.lang.String) getAttribute(String)
+ * @see nanoxml.XMLElement#getAttribute(java.lang.String, java.lang.Object) getAttribute(String, Object)
+ * @see nanoxml.XMLElement#getAttribute(java.lang.String, java.util.Hashtable, java.lang.String, boolean) getAttribute(String, Hashtable, String, boolean)
+ * @see nanoxml.XMLElement#getStringAttribute(java.lang.String) getStringAttribute(String)
+ * @see nanoxml.XMLElement#getStringAttribute(java.lang.String, java.lang.String) getStringAttribute(String, String)
+ * @see nanoxml.XMLElement#getStringAttribute(java.lang.String, java.util.Hashtable, java.lang.String, boolean) getStringAttribute(String, Hashtable, String, boolean)
+ */
+ public void setAttribute(String name, Object value) {
+ if (this.ignoreCase) {
+ name = name.toUpperCase();
+ }
+ this.attributes.put(name, value);
+ }
+
+ public void setAttribute(String name, long value) {
+ setAttribute(name, new Long(value));
+ }
+
+ public void setAttribute(String name, boolean value) {
+ setAttribute(name, new Boolean(value));
+ }
+
+ public void setAttribute(String name, double value) {
+ //Object[] args = { new Double(value) };
+ setAttribute(name, new Double(value));
+ }
+
+ public void setAttribute(String name, float value) {
+ //Object[] args = { new Float(value) };
+ setAttribute(name, new Double(value));
+ }
+
+ public void setTypedAttribute(String name, String raw) {
+ boolean longOk=false;
+ boolean doubleOk=false;
+ boolean dump=false;
+ long l=-123L;
+ double d=-123.0;
+
+ if (raw.indexOf('@')!=-1) {
+ dump=true;
+ }
+ //try long in US format... note: you try this BEFORE you try the double since
+ //the double can also parse the long
+ try {
+ l=enUSFormat.parse(raw).longValue();
+ longOk=true;
+ } catch (ParseException e) {
+
+ }
+
+ //try understanding it as a double in US formate (xx.yyy)
+ try {
+ d=enUSFormat.parse(raw).doubleValue();
+ doubleOk=true;
+ } catch (ParseException ignored) {
+
+ }
+
+ if (dump) {
+ System.out.println("FART PARSE: Parsing "+raw+" and got "+longOk+" and "+doubleOk+" with "+l+" and "+d);
+ }
+
+ if ((longOk) && (doubleOk)) {
+ System.out.println("FART GOT BOTH OK:"+raw);
+ setAttribute(name, d);
+ return;
+ }
+
+ if (longOk) {
+ System.out.println("FART GOT LONG ONLY:"+raw);
+ setAttribute(name,l);
+ return;
+ }
+
+ if (doubleOk) {
+ System.out.println("FART GOT DOUBLE ONLY:"+raw);
+ setAttribute(name, d);
+ return;
+ }
+
+
+ //try boolean
+ if ((raw.equalsIgnoreCase("true")) || (raw.equalsIgnoreCase("false"))) {
+ raw=raw.toLowerCase();
+ setAttribute(name, Boolean.valueOf(raw));
+ return;
+ }
+
+ //hope it's a string
+ setAttribute(name, raw);
+ }
+
+ /**
+ * Adds or modifies an attribute.
+ *
+ * @param name
+ * The name of the attribute.
+ * @param value
+ * The value of the attribute.
+ *
+ * </dl>
+ * <dl>
+ * <dt><b>Preconditions:</b></dt>
+ * <dd>
+ * <ul>
+ * <li><code>name != null</code>
+ * <li><code>name</code> is a valid XML identifier
+ * </ul>
+ * </dd>
+ * </dl>
+ *
+ * <dl>
+ * <dt><b>Postconditions:</b></dt>
+ * <dd>
+ * <ul>
+ * <li>enumerateAttributeNames() => old.enumerateAttributeNames() + name
+ * <li>getIntAttribute(name) => value
+ * </ul>
+ * </dd>
+ * </dl>
+ * <dl>
+ *
+ * @see nanoxml.XMLElement#setDoubleAttribute(java.lang.String, double) setDoubleAttribute(String, double)
+ * @see nanoxml.XMLElement#setAttribute(java.lang.String, java.lang.Object) setAttribute(String, Object)
+ * @see nanoxml.XMLElement#removeAttribute(java.lang.String) removeAttribute(String)
+ * @see nanoxml.XMLElement#enumerateAttributeNames()
+ * @see nanoxml.XMLElement#getIntAttribute(java.lang.String) getIntAttribute(String)
+ * @see nanoxml.XMLElement#getIntAttribute(java.lang.String, int) getIntAttribute(String, int)
+ * @see nanoxml.XMLElement#getIntAttribute(java.lang.String, java.util.Hashtable, java.lang.String, boolean) getIntAttribute(String, Hashtable, String, boolean)
+ */
+ public void setIntAttribute(String name, int value) {
+ if (this.ignoreCase) {
+ name = name.toUpperCase();
+ }
+ this.attributes.put(name, new Integer(value));
+ }
+
+ /**
+ * Adds or modifies an attribute.
+ *
+ * @param name
+ * The name of the attribute.
+ * @param value
+ * The value of the attribute.
+ *
+ * </dl>
+ * <dl>
+ * <dt><b>Preconditions:</b></dt>
+ * <dd>
+ * <ul>
+ * <li><code>name != null</code>
+ * <li><code>name</code> is a valid XML identifier
+ * </ul>
+ * </dd>
+ * </dl>
+ *
+ * <dl>
+ * <dt><b>Postconditions:</b></dt>
+ * <dd>
+ * <ul>
+ * <li>enumerateAttributeNames() => old.enumerateAttributeNames() + name
+ * <li>getDoubleAttribute(name) => value
+ * </ul>
+ * </dd>
+ * </dl>
+ * <dl>
+ *
+ * @see nanoxml.XMLElement#setIntAttribute(java.lang.String, int) setIntAttribute(String, int)
+ * @see nanoxml.XMLElement#setAttribute(java.lang.String, java.lang.Object) setAttribute(String, Object)
+ * @see nanoxml.XMLElement#removeAttribute(java.lang.String) removeAttribute(String)
+ * @see nanoxml.XMLElement#enumerateAttributeNames()
+ * @see nanoxml.XMLElement#getDoubleAttribute(java.lang.String) getDoubleAttribute(String)
+ * @see nanoxml.XMLElement#getDoubleAttribute(java.lang.String, double) getDoubleAttribute(String, double)
+ * @see nanoxml.XMLElement#getDoubleAttribute(java.lang.String, java.util.Hashtable, java.lang.String, boolean) getDoubleAttribute(String, Hashtable, String, boolean)
+ */
+ public void setDoubleAttribute(String name, double value) {
+ if (this.ignoreCase) {
+ name = name.toUpperCase();
+ }
+ this.attributes.put(name, new Double(value));
+ }
+
+ /**
+ * Returns the number of child elements of the element.
+ *
+ * <dl>
+ * <dt><b>Postconditions:</b></dt>
+ * <dd>
+ * <ul>
+ * <li><code>result >= 0</code>
+ * </ul>
+ * </dd>
+ * </dl>
+ *
+ * @see nanoxml.XMLElement#addChild(nanoxml.XMLElement) addChild(XMLElement)
+ * @see nanoxml.XMLElement#enumerateChildren()
+ * @see nanoxml.XMLElement#getChildren()
+ * @see nanoxml.XMLElement#removeChild(nanoxml.XMLElement) removeChild(XMLElement)
+ */
+ public int countChildren() {
+ return this.children.size();
+ }
+
+ /**
+ * Enumerates the attribute names.
+ *
+ * <dl>
+ * <dt><b>Postconditions:</b></dt>
+ * <dd>
+ * <ul>
+ * <li><code>result != null</code>
+ * </ul>
+ * </dd>
+ * </dl>
+ *
+ * @see nanoxml.XMLElement#setDoubleAttribute(java.lang.String, double) setDoubleAttribute(String, double)
+ * @see nanoxml.XMLElement#setIntAttribute(java.lang.String, int) setIntAttribute(String, int)
+ * @see nanoxml.XMLElement#setAttribute(java.lang.String, java.lang.Object) setAttribute(String, Object)
+ * @see nanoxml.XMLElement#removeAttribute(java.lang.String) removeAttribute(String)
+ * @see nanoxml.XMLElement#getAttribute(java.lang.String) getAttribute(String)
+ * @see nanoxml.XMLElement#getAttribute(java.lang.String, java.lang.Object) getAttribute(String, String)
+ * @see nanoxml.XMLElement#getAttribute(java.lang.String, java.util.Hashtable, java.lang.String, boolean) getAttribute(String, Hashtable, String, boolean)
+ * @see nanoxml.XMLElement#getStringAttribute(java.lang.String) getStringAttribute(String)
+ * @see nanoxml.XMLElement#getStringAttribute(java.lang.String, java.lang.String) getStringAttribute(String, String)
+ * @see nanoxml.XMLElement#getStringAttribute(java.lang.String, java.util.Hashtable, java.lang.String, boolean) getStringAttribute(String, Hashtable, String, boolean)
+ * @see nanoxml.XMLElement#getIntAttribute(java.lang.String) getIntAttribute(String)
+ * @see nanoxml.XMLElement#getIntAttribute(java.lang.String, int) getIntAttribute(String, int)
+ * @see nanoxml.XMLElement#getIntAttribute(java.lang.String, java.util.Hashtable, java.lang.String, boolean) getIntAttribute(String, Hashtable, String, boolean)
+ * @see nanoxml.XMLElement#getDoubleAttribute(java.lang.String) getDoubleAttribute(String)
+ * @see nanoxml.XMLElement#getDoubleAttribute(java.lang.String, double) getDoubleAttribute(String, double)
+ * @see nanoxml.XMLElement#getDoubleAttribute(java.lang.String, java.util.Hashtable, java.lang.String, boolean) getDoubleAttribute(String, Hashtable, String, boolean)
+ * @see nanoxml.XMLElement#getBooleanAttribute(java.lang.String, java.lang.String, java.lang.String, boolean) getBooleanAttribute(String, String, String, boolean)
+ */
+ public Enumeration enumerateAttributeNames() {
+ return this.attributes.keys();
+ }
+
+ /**
+ * Enumerates the child elements.
+ *
+ * <dl>
+ * <dt><b>Postconditions:</b></dt>
+ * <dd>
+ * <ul>
+ * <li><code>result != null</code>
+ * </ul>
+ * </dd>
+ * </dl>
+ *
+ * @see nanoxml.XMLElement#addChild(nanoxml.XMLElement) addChild(XMLElement)
+ * @see nanoxml.XMLElement#countChildren()
+ * @see nanoxml.XMLElement#getChildren()
+ * @see nanoxml.XMLElement#removeChild(nanoxml.XMLElement) removeChild(XMLElement)
+ */
+ public Enumeration enumerateChildren() {
+ return this.children.elements();
+ }
+
+ /**
+ * Returns the child elements as a Vector. It is safe to modify this Vector.
+ *
+ * <dl>
+ * <dt><b>Postconditions:</b></dt>
+ * <dd>
+ * <ul>
+ * <li><code>result != null</code>
+ * </ul>
+ * </dd>
+ * </dl>
+ *
+ * @see nanoxml.XMLElement#addChild(nanoxml.XMLElement) addChild(XMLElement)
+ * @see nanoxml.XMLElement#countChildren()
+ * @see nanoxml.XMLElement#enumerateChildren()
+ * @see nanoxml.XMLElement#removeChild(nanoxml.XMLElement) removeChild(XMLElement)
+ */
+ public Vector getChildren() {
+ try {
+ return (Vector) this.children.clone();
+ } catch (Exception e) {
+ // this never happens, however, some Java compilers are so
+ // braindead that they require this exception clause
+ return null;
+ }
+ }
+
+ /**
+ * Returns the first child element with this name or null if none matches.
+ */
+ public XMLElement getChild(String name) {
+ XMLElement[] kids = (XMLElement[]) getChildren().toArray(new XMLElement[0]);
+ for (int i = 0; i < kids.length; i++) {
+ if (kids[i].getName().equals(name)) {
+ return kids[i];
+ }
+ }
+ return null;
+ }
+
+ /**
+ * Returns all children with this name.
+ */
+ public XMLElement[] getChildren(String name) {
+ Vector results = new Vector();
+ XMLElement[] kids = (XMLElement[]) getChildren().toArray(new XMLElement[0]);
+ for (int i = 0; i < kids.length; i++) {
+ if (kids[i].getName().equals(name)) {
+ results.add(kids[i]);
+ }
+ }
+ return (XMLElement[]) results.toArray(new XMLElement[0]);
+ }
+
+ public XMLElement[] getAncestors(String name) {
+ Vector results = new Vector();
+ XMLElement[] kids = (XMLElement[]) getChildren().toArray(new XMLElement[0]);
+ for (int i = 0; i < kids.length; i++) {
+ if (kids[i].getName().equals(name)) {
+ results.add(kids[i]);
+ }
+ XMLElement[] subChildren = kids[i].getAncestors(name);
+ for (int j = 0; j < subChildren.length; j++) {
+ results.add(subChildren[j]);
+ }
+ }
+ return (XMLElement[]) results.toArray(new XMLElement[0]);
+
+ }
+
+ /**
+ * Returns the PCDATA content of the object. If there is no such content, <CODE>null</CODE> is returned.
+ *
+ * @see nanoxml.XMLElement#setContent(java.lang.String) setContent(String)
+ */
+ public String getContent() {
+ return this.contents;
+ }
+
+ /**
+ * Returns the line nr in the source data on which the element is found. This method returns <code>0</code> there is no associated source data.
+ *
+ * <dl>
+ * <dt><b>Postconditions:</b></dt>
+ * <dd>
+ * <ul>
+ * <li><code>result >= 0</code>
+ * </ul>
+ * </dd>
+ * </dl>
+ */
+ public int getLineNr() {
+ return this.lineNr;
+ }
+
+ /**
+ * Returns an attribute of the element. If the attribute doesn't exist, <code>null</code> is returned.
+ *
+ * @param name
+ * The name of the attribute.
+ *
+ * </dl>
+ * <dl>
+ * <dt><b>Preconditions:</b></dt>
+ * <dd>
+ * <ul>
+ * <li><code>name != null</code>
+ * <li><code>name</code> is a valid XML identifier
+ * </ul>
+ * </dd>
+ * </dl>
+ * <dl>
+ *
+ * @see nanoxml.XMLElement#setAttribute(java.lang.String, java.lang.Object) setAttribute(String, Object)
+ * @see nanoxml.XMLElement#removeAttribute(java.lang.String) removeAttribute(String)
+ * @see nanoxml.XMLElement#enumerateAttributeNames()
+ * @see nanoxml.XMLElement#getAttribute(java.lang.String, java.lang.Object) getAttribute(String, Object)
+ * @see nanoxml.XMLElement#getAttribute(java.lang.String, java.util.Hashtable, java.lang.String, boolean) getAttribute(String, Hashtable, String, boolean)
+ */
+ public Object getAttribute(String name) {
+ return this.getAttribute(name, null);
+ }
+
+ /**
+ * Returns an attribute of the element. If the attribute doesn't exist, <code>defaultValue</code> is returned.
+ *
+ * @param name
+ * The name of the attribute.
+ * @param defaultValue
+ * Key to use if the attribute is missing.
+ *
+ * </dl>
+ * <dl>
+ * <dt><b>Preconditions:</b></dt>
+ * <dd>
+ * <ul>
+ * <li><code>name != null</code>
+ * <li><code>name</code> is a valid XML identifier
+ * </ul>
+ * </dd>
+ * </dl>
+ * <dl>
+ *
+ * @see nanoxml.XMLElement#setAttribute(java.lang.String, java.lang.Object) setAttribute(String, Object)
+ * @see nanoxml.XMLElement#removeAttribute(java.lang.String) removeAttribute(String)
+ * @see nanoxml.XMLElement#enumerateAttributeNames()
+ * @see nanoxml.XMLElement#getAttribute(java.lang.String) getAttribute(String)
+ * @see nanoxml.XMLElement#getAttribute(java.lang.String, java.util.Hashtable, java.lang.String, boolean) getAttribute(String, Hashtable, String, boolean)
+ */
+ public Object getAttribute(String name, Object defaultValue) {
+ if (this.ignoreCase) {
+ name = name.toUpperCase();
+ }
+ Object value = this.attributes.get(name);
+ if (value == null) {
+ value = defaultValue;
+ }
+ return value;
+ }
+
+ /**
+ * Returns an attribute by looking up a key in a hashtable. If the attribute doesn't exist, the value corresponding to defaultKey is returned.
+ * <P>
+ * As an example, if valueSet contains the mapping <code>"one" =>
+ * "1"</code> and the element contains the attribute <code>attr="one"</code>, then <code>getAttribute("attr", mapping, defaultKey, false)</code> returns <code>"1"</code>.
+ *
+ * @param name
+ * The name of the attribute.
+ * @param valueSet
+ * Hashtable mapping keys to values.
+ * @param defaultKey
+ * Key to use if the attribute is missing.
+ * @param allowLiterals
+ * <code>true</code> if literals are valid.
+ *
+ * </dl>
+ * <dl>
+ * <dt><b>Preconditions:</b></dt>
+ * <dd>
+ * <ul>
+ * <li><code>name != null</code>
+ * <li><code>name</code> is a valid XML identifier
+ * <li><code>valueSet</code> != null
+ * <li>the keys of <code>valueSet</code> are strings
+ * </ul>
+ * </dd>
+ * </dl>
+ * <dl>
+ *
+ * @see nanoxml.XMLElement#setAttribute(java.lang.String, java.lang.Object) setAttribute(String, Object)
+ * @see nanoxml.XMLElement#removeAttribute(java.lang.String) removeAttribute(String)
+ * @see nanoxml.XMLElement#enumerateAttributeNames()
+ * @see nanoxml.XMLElement#getAttribute(java.lang.String) getAttribute(String)
+ * @see nanoxml.XMLElement#getAttribute(java.lang.String, java.lang.Object) getAttribute(String, Object)
+ */
+ public Object getAttribute(String name, Hashtable valueSet, String defaultKey, boolean allowLiterals) {
+ if (this.ignoreCase) {
+ name = name.toUpperCase();
+ }
+ Object key = this.attributes.get(name);
+ Object result;
+ if (key == null) {
+ key = defaultKey;
+ }
+ result = valueSet.get(key);
+ if (result == null) {
+ if (allowLiterals) {
+ result = key;
+ } else {
+ throw this.invalidValue(name, (String) key);
+ }
+ }
+ return result;
+ }
+
+ /**
+ * Returns an attribute of the element. If the attribute doesn't exist, <code>null</code> is returned.
+ *
+ * @param name
+ * The name of the attribute.
+ *
+ * </dl>
+ * <dl>
+ * <dt><b>Preconditions:</b></dt>
+ * <dd>
+ * <ul>
+ * <li><code>name != null</code>
+ * <li><code>name</code> is a valid XML identifier
+ * </ul>
+ * </dd>
+ * </dl>
+ * <dl>
+ *
+ * @see nanoxml.XMLElement#setAttribute(java.lang.String, java.lang.Object) setAttribute(String, Object)
+ * @see nanoxml.XMLElement#removeAttribute(java.lang.String) removeAttribute(String)
+ * @see nanoxml.XMLElement#enumerateAttributeNames()
+ * @see nanoxml.XMLElement#getStringAttribute(java.lang.String, java.lang.String) getStringAttribute(String, String)
+ * @see nanoxml.XMLElement#getStringAttribute(java.lang.String, java.util.Hashtable, java.lang.String, boolean) getStringAttribute(String, Hashtable, String, boolean)
+ */
+ public String getStringAttribute(String name) {
+ return this.getStringAttribute(name, null);
+ }
+
+ public boolean getBooleanAttribute(String name) {
+ return this.getBooleanAttribute(name, false);
+ }
+
+ public boolean getBooleanAttribute(String name, boolean defaultValue) {
+ if (this.ignoreCase) {
+ name = name.toUpperCase();
+ }
+ Object value = this.attributes.get(name);
+ if (value == null) {
+ return defaultValue;
+ } else {
+ return ((Boolean)value).booleanValue();
+ }
+ }
+
+ public long getLongAttribute(String name, long defaultValue) {
+ if (this.ignoreCase) {
+ name = name.toUpperCase();
+ }
+ Object value = (Object) this.attributes.get(name);
+ if (value == null) {
+ return defaultValue;
+ } else {
+ if (value instanceof Double) {
+ Double d=(Double)value;
+ return d.longValue();
+ }
+ return ((Long)value).longValue();
+ }
+ }
+
+ public long getLongAttribute(String name) {
+ return getLongAttribute(name, -1L);
+ }
+
+ public float getFloatAttribute(String name, float defaultValue) {
+ if (this.ignoreCase) {
+ name = name.toUpperCase();
+ }
+ Object value = this.attributes.get(name);
+ if (value == null) {
+ return defaultValue;
+ } else {
+ //it's ok to convert a long or double to float
+ if (value instanceof Long) {
+ Long l=(Long)value;
+ return l.floatValue();
+ }
+ return ((Double)value).floatValue();
+ }
+ }
+
+ public float getFloatAttribute(String name) {
+ return getFloatAttribute(name, -1.0f);
+ }
+
+ /**
+ * Returns an attribute of the element. If the attribute doesn't exist, <code>defaultValue</code> is returned.
+ *
+ * @param name
+ * The name of the attribute.
+ * @param defaultValue
+ * Key to use if the attribute is missing.
+ *
+ * </dl>
+ * <dl>
+ * <dt><b>Preconditions:</b></dt>
+ * <dd>
+ * <ul>
+ * <li><code>name != null</code>
+ * <li><code>name</code> is a valid XML identifier
+ * </ul>
+ * </dd>
+ * </dl>
+ * <dl>
+ *
+ * @see nanoxml.XMLElement#setAttribute(java.lang.String, java.lang.Object) setAttribute(String, Object)
+ * @see nanoxml.XMLElement#removeAttribute(java.lang.String) removeAttribute(String)
+ * @see nanoxml.XMLElement#enumerateAttributeNames()
+ * @see nanoxml.XMLElement#getStringAttribute(java.lang.String) getStringAttribute(String)
+ * @see nanoxml.XMLElement#getStringAttribute(java.lang.String, java.util.Hashtable, java.lang.String, boolean) getStringAttribute(String, Hashtable, String, boolean)
+ */
+ public String getStringAttribute(String name, String defaultValue) {
+ Object raw = this.getAttribute(name);
+ //have to do this check first!
+ if (raw==null) {
+ return defaultValue;
+ }
+ if (raw instanceof Double) {
+ Double d = (Double)raw;
+ return unencode(enUSFormat.format(d.doubleValue()));
+ } else {
+ return unencode(raw.toString());
+ }
+ //return unencode((String) this.getAttribute(name, defaultValue));
+ }
+
+ /**
+ * Returns an attribute by looking up a key in a hashtable. If the attribute doesn't exist, the value corresponding to defaultKey is returned.
+ * <P>
+ * As an example, if valueSet contains the mapping <code>"one" =>
+ * "1"</code> and the element contains the attribute <code>attr="one"</code>, then <code>getAttribute("attr", mapping, defaultKey, false)</code> returns <code>"1"</code>.
+ *
+ * @param name
+ * The name of the attribute.
+ * @param valueSet
+ * Hashtable mapping keys to values.
+ * @param defaultKey
+ * Key to use if the attribute is missing.
+ * @param allowLiterals
+ * <code>true</code> if literals are valid.
+ *
+ * </dl>
+ * <dl>
+ * <dt><b>Preconditions:</b></dt>
+ * <dd>
+ * <ul>
+ * <li><code>name != null</code>
+ * <li><code>name</code> is a valid XML identifier
+ * <li><code>valueSet</code> != null
+ * <li>the keys of <code>valueSet</code> are strings
+ * <li>the values of <code>valueSet</code> are strings
+ * </ul>
+ * </dd>
+ * </dl>
+ * <dl>
+ *
+ * @see nanoxml.XMLElement#setAttribute(java.lang.String, java.lang.Object) setAttribute(String, Object)
+ * @see nanoxml.XMLElement#removeAttribute(java.lang.String) removeAttribute(String)
+ * @see nanoxml.XMLElement#enumerateAttributeNames()
+ * @see nanoxml.XMLElement#getStringAttribute(java.lang.String) getStringAttribute(String)
+ * @see nanoxml.XMLElement#getStringAttribute(java.lang.String, java.lang.String) getStringAttribute(String, String)
+ */
+ public String getStringAttribute(String name, Hashtable valueSet, String defaultKey, boolean allowLiterals) {
+ return (String) this.getAttribute(name, valueSet, defaultKey, allowLiterals);
+ }
+
+ /**
+ * Returns an attribute of the element. If the attribute doesn't exist, <code>0</code> is returned.
+ *
+ * @param name
+ * The name of the attribute.
+ *
+ * </dl>
+ * <dl>
+ * <dt><b>Preconditions:</b></dt>
+ * <dd>
+ * <ul>
+ * <li><code>name != null</code>
+ * <li><code>name</code> is a valid XML identifier
+ * </ul>
+ * </dd>
+ * </dl>
+ * <dl>
+ *
+ * @see nanoxml.XMLElement#setIntAttribute(java.lang.String, int) setIntAttribute(String, int)
+ * @see nanoxml.XMLElement#enumerateAttributeNames()
+ * @see nanoxml.XMLElement#getIntAttribute(java.lang.String, int) getIntAttribute(String, int)
+ * @see nanoxml.XMLElement#getIntAttribute(java.lang.String, java.util.Hashtable, java.lang.String, boolean) getIntAttribute(String, Hashtable, String, boolean)
+ */
+ public int getIntAttribute(String name) {
+ return this.getIntAttribute(name, 0);
+ }
+
+ /**
+ * Returns an attribute of the element. If the attribute doesn't exist, <code>defaultValue</code> is returned.
+ *
+ * @param name
+ * The name of the attribute.
+ * @param defaultValue
+ * Key to use if the attribute is missing.
+ *
+ * </dl>
+ * <dl>
+ * <dt><b>Preconditions:</b></dt>
+ * <dd>
+ * <ul>
+ * <li><code>name != null</code>
+ * <li><code>name</code> is a valid XML identifier
+ * </ul>
+ * </dd>
+ * </dl>
+ * <dl>
+ *
+ * @see nanoxml.XMLElement#setIntAttribute(java.lang.String, int) setIntAttribute(String, int)
+ * @see nanoxml.XMLElement#enumerateAttributeNames()
+ * @see nanoxml.XMLElement#getIntAttribute(java.lang.String) getIntAttribute(String)
+ * @see nanoxml.XMLElement#getIntAttribute(java.lang.String, java.util.Hashtable, java.lang.String, boolean) getIntAttribute(String, Hashtable, String, boolean)
+ */
+ public int getIntAttribute(String name, int defaultValue) {
+ if (this.ignoreCase) {
+ name = name.toUpperCase();
+ }
+ Object value = this.attributes.get(name);
+ if (value == null) {
+ return defaultValue;
+ } else {
+ if (value instanceof Long) {
+ Long l=(Long)value;
+ if ((l.longValue()>Integer.MAX_VALUE) || (l.longValue()<Integer.MIN_VALUE)) {
+ throw new IllegalArgumentException("Trying to convert a long that is too big (" + l+") to an int!");
+ }
+ return l.intValue();
+ }
+ if (value instanceof Double) {
+ Double d=(Double)value;
+ return d.intValue();
+ }
+ return ((Integer)value).intValue();
+ }
+ }
+
+ /**
+ * Returns an attribute by looking up a key in a hashtable. If the attribute doesn't exist, the value corresponding to defaultKey is returned.
+ * <P>
+ * As an example, if valueSet contains the mapping <code>"one" => 1</code> and the element contains the attribute <code>attr="one"</code>, then <code>getIntAttribute("attr", mapping, defaultKey, false)</code> returns <code>1</code>.
+ *
+ * @param name
+ * The name of the attribute.
+ * @param valueSet
+ * Hashtable mapping keys to values.
+ * @param defaultKey
+ * Key to use if the attribute is missing.
+ * @param allowLiteralNumbers
+ * <code>true</code> if literal numbers are valid.
+ *
+ * </dl>
+ * <dl>
+ * <dt><b>Preconditions:</b></dt>
+ * <dd>
+ * <ul>
+ * <li><code>name != null</code>
+ * <li><code>name</code> is a valid XML identifier
+ * <li><code>valueSet</code> != null
+ * <li>the keys of <code>valueSet</code> are strings
+ * <li>the values of <code>valueSet</code> are Integer objects
+ * <li><code>defaultKey</code> is either <code>null</code>, a key in <code>valueSet</code> or an integer.
+ * </ul>
+ * </dd>
+ * </dl>
+ * <dl>
+ *
+ * @see nanoxml.XMLElement#setIntAttribute(java.lang.String, int) setIntAttribute(String, int)
+ * @see nanoxml.XMLElement#enumerateAttributeNames()
+ * @see nanoxml.XMLElement#getIntAttribute(java.lang.String) getIntAttribute(String)
+ * @see nanoxml.XMLElement#getIntAttribute(java.lang.String, int) getIntAttribute(String, int)
+ */
+ public int getIntAttribute(String name, Hashtable valueSet, String defaultKey, boolean allowLiteralNumbers) {
+ if (this.ignoreCase) {
+ name = name.toUpperCase();
+ }
+ Object key = this.attributes.get(name);
+ Integer result;
+ if (key == null) {
+ key = defaultKey;
+ }
+ try {
+ result = (Integer) valueSet.get(key);
+ } catch (ClassCastException e) {
+ throw this.invalidValueSet(name);
+ }
+ if (result == null) {
+ if (!allowLiteralNumbers) {
+ throw this.invalidValue(name, (String) key);
+ }
+ try {
+ result = Integer.valueOf((String) key);
+ } catch (NumberFormatException e) {
+ throw this.invalidValue(name, (String) key);
+ }
+ }
+ return result.intValue();
+ }
+
+ /**
+ * Returns an attribute of the element. If the attribute doesn't exist, <code>0.0</code> is returned.
+ *
+ * @param name
+ * The name of the attribute.
+ *
+ * </dl>
+ * <dl>
+ * <dt><b>Preconditions:</b></dt>
+ * <dd>
+ * <ul>
+ * <li><code>name != null</code>
+ * <li><code>name</code> is a valid XML identifier
+ * </ul>
+ * </dd>
+ * </dl>
+ * <dl>
+ *
+ * @see nanoxml.XMLElement#setDoubleAttribute(java.lang.String, double) setDoubleAttribute(String, double)
+ * @see nanoxml.XMLElement#enumerateAttributeNames()
+ * @see nanoxml.XMLElement#getDoubleAttribute(java.lang.String, double) getDoubleAttribute(String, double)
+ * @see nanoxml.XMLElement#getDoubleAttribute(java.lang.String, java.util.Hashtable, java.lang.String, boolean) getDoubleAttribute(String, Hashtable, String, boolean)
+ */
+ public double getDoubleAttribute(String name) {
+ return this.getDoubleAttribute(name, -1);
+ }
+
+ /**
+ * Returns an attribute of the element. If the attribute doesn't exist, <code>defaultValue</code> is returned.
+ *
+ * @param name
+ * The name of the attribute.
+ * @param defaultValue
+ * Key to use if the attribute is missing.
+ *
+ * </dl>
+ * <dl>
+ * <dt><b>Preconditions:</b></dt>
+ * <dd>
+ * <ul>
+ * <li><code>name != null</code>
+ * <li><code>name</code> is a valid XML identifier
+ * </ul>
+ * </dd>
+ * </dl>
+ * <dl>
+ *
+ * @see nanoxml.XMLElement#setDoubleAttribute(java.lang.String, double) setDoubleAttribute(String, double)
+ * @see nanoxml.XMLElement#enumerateAttributeNames()
+ * @see nanoxml.XMLElement#getDoubleAttribute(java.lang.String) getDoubleAttribute(String)
+ * @see nanoxml.XMLElement#getDoubleAttribute(java.lang.String, java.util.Hashtable, java.lang.String, boolean) getDoubleAttribute(String, Hashtable, String, boolean)
+ */
+ public double getDoubleAttribute(String name, double defaultValue) {
+ if (this.ignoreCase) {
+ name = name.toUpperCase();
+ }
+ Object value = this.attributes.get(name);
+ if (value == null) {
+ return defaultValue;
+ } else {
+ //some conversions are ok
+ if (value instanceof Long) {
+ Long l=(Long)value;
+ return l.doubleValue();
+ }
+ return ((Double)value).doubleValue();
+ }
+ }
+
+ /**
+ * Returns an attribute by looking up a key in a hashtable. If the attribute doesn't exist, the value corresponding to defaultKey is returned.
+ * <P>
+ * As an example, if valueSet contains the mapping <code>"one" =>
+ * 1.0</code> and the element contains the attribute <code>attr="one"</code>, then <code>getDoubleAttribute("attr", mapping, defaultKey, false)</code> returns <code>1.0</code>.
+ *
+ * @param name
+ * The name of the attribute.
+ * @param valueSet
+ * Hashtable mapping keys to values.
+ * @param defaultKey
+ * Key to use if the attribute is missing.
+ * @param allowLiteralNumbers
+ * <code>true</code> if literal numbers are valid.
+ *
+ * </dl>
+ * <dl>
+ * <dt><b>Preconditions:</b></dt>
+ * <dd>
+ * <ul>
+ * <li><code>name != null</code>
+ * <li><code>name</code> is a valid XML identifier
+ * <li><code>valueSet != null</code>
+ * <li>the keys of <code>valueSet</code> are strings
+ * <li>the values of <code>valueSet</code> are Double objects
+ * <li><code>defaultKey</code> is either <code>null</code>, a key in <code>valueSet</code> or a double.
+ * </ul>
+ * </dd>
+ * </dl>
+ * <dl>
+ *
+ * @see nanoxml.XMLElement#setDoubleAttribute(java.lang.String, double) setDoubleAttribute(String, double)
+ * @see nanoxml.XMLElement#enumerateAttributeNames()
+ * @see nanoxml.XMLElement#getDoubleAttribute(java.lang.String) getDoubleAttribute(String)
+ * @see nanoxml.XMLElement#getDoubleAttribute(java.lang.String, double) getDoubleAttribute(String, double)
+ */
+ public double getDoubleAttribute(String name, Hashtable valueSet, String defaultKey, boolean allowLiteralNumbers) {
+ if (this.ignoreCase) {
+ name = name.toUpperCase();
+ }
+ Object key = this.attributes.get(name);
+ Double result;
+ if (key == null) {
+ key = defaultKey;
+ }
+ try {
+ result = (Double) valueSet.get(key);
+ } catch (ClassCastException e) {
+ throw this.invalidValueSet(name);
+ }
+ if (result == null) {
+ if (!allowLiteralNumbers) {
+ throw this.invalidValue(name, (String) key);
+ }
+ try {
+ result = Double.valueOf((String) key);
+ } catch (NumberFormatException e) {
+ throw this.invalidValue(name, (String) key);
+ }
+ }
+ return result.doubleValue();
+ }
+
+ /**
+ * Returns an attribute of the element. If the attribute doesn't exist, <code>defaultValue</code> is returned. If the value of the attribute is equal to <code>trueValue</code>, <code>true</code> is returned. If the value of the attribute is equal to <code>falseValue</code>, <code>false</code> is returned. If the value doesn't match <code>trueValue</code> or
+ * <code>falseValue</code>, an exception is thrown.
+ *
+ * @param name
+ * The name of the attribute.
+ * @param trueValue
+ * The value associated with <code>true</code>.
+ * @param falseValue
+ * The value associated with <code>true</code>.
+ * @param defaultValue
+ * Value to use if the attribute is missing.
+ *
+ * </dl>
+ * <dl>
+ * <dt><b>Preconditions:</b></dt>
+ * <dd>
+ * <ul>
+ * <li><code>name != null</code>
+ * <li><code>name</code> is a valid XML identifier
+ * <li><code>trueValue</code> and <code>falseValue</code> are different strings.
+ * </ul>
+ * </dd>
+ * </dl>
+ * <dl>
+ *
+ * @see nanoxml.XMLElement#setAttribute(java.lang.String, java.lang.Object) setAttribute(String, Object)
+ * @see nanoxml.XMLElement#removeAttribute(java.lang.String) removeAttribute(String)
+ * @see nanoxml.XMLElement#enumerateAttributeNames()
+ */
+ public boolean getBooleanAttribute(String name, String trueValue, String falseValue, boolean defaultValue) {
+ if (this.ignoreCase) {
+ name = name.toUpperCase();
+ }
+ Object value = this.attributes.get(name);
+ if (value == null) {
+ return defaultValue;
+ } else if (value.equals(trueValue)) {
+ return true;
+ } else if (value.equals(falseValue)) {
+ return false;
+ } else {
+ throw this.invalidValue(name, (String) value);
+ }
+ }
+
+ /**
+ * Returns the name of the element.
+ *
+ * @see nanoxml.XMLElement#setName(java.lang.String) setName(String)
+ */
+ public String getName() {
+ return this.name;
+ }
+
+ /**
+ * Reads one XML element from a java.io.Reader and parses it.
+ *
+ * @param reader
+ * The reader from which to retrieve the XML data.
+ *
+ * </dl>
+ * <dl>
+ * <dt><b>Preconditions:</b></dt>
+ * <dd>
+ * <ul>
+ * <li><code>reader != null</code>
+ * <li><code>reader</code> is not closed
+ * </ul>
+ * </dd...
[truncated message content] |