Fix up all file headers.
[jSite.git] / src / de / todesbaum / util / xml / SimpleXML.java
index d400ed8..8351581 100644 (file)
@@ -1,4 +1,6 @@
 /*
+ * jSite - SimpleXML.java - Copyright © 2006–2012 David Roden
+ *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
  * the Free Software Foundation; either version 2 of the License, or
@@ -30,9 +32,10 @@ import org.w3c.dom.NodeList;
 import org.w3c.dom.Text;
 
 /**
- * SimpleXML is a helper class to construct XML trees in a fast and simple way. Construct a new XML tree by calling {@link #SimpleXML(String)} and
- * append new nodes by calling {@link #append(String)}.
- * 
+ * SimpleXML is a helper class to construct XML trees in a fast and simple way.
+ * Construct a new XML tree by calling {@link #SimpleXML(String)} and append new
+ * nodes by calling {@link #append(String)}.
+ *
  * @author David Roden <droden@gmail.com>
  * @version $Id:SimpleXML.java 221 2006-03-06 14:46:49Z bombe $
  */
@@ -62,7 +65,7 @@ public class SimpleXML {
 
        /**
         * Constructs a new XML node with the specified name.
-        * 
+        *
         * @param name
         *            The name of the new node
         */
@@ -71,12 +74,14 @@ public class SimpleXML {
        }
 
        /**
-        * Returns the child node of this node with the specified name. If there are several child nodes with the specified name only the first node is
+        * Returns the child node of this node with the specified name. If there are
+        * several child nodes with the specified name only the first node is
         * returned.
-        * 
+        *
         * @param nodeName
         *            The name of the child node
-        * @return The child node, or <code>null</code> if there is no child node with the specified name
+        * @return The child node, or <code>null</code> if there is no child node
+        *         with the specified name
         */
        public SimpleXML getNode(String nodeName) {
                for (int index = 0, count = children.size(); index < count; index++) {
@@ -88,20 +93,24 @@ public class SimpleXML {
        }
 
        /**
-        * Returns the child node that is specified by the names. The first element of <code>nodeNames</code> is the name of the child node of this
-        * node, the second element of <code>nodeNames</code> is the name of a child node's child node, and so on. By using this method you can descend
-        * into an XML tree pretty fast.
-        * 
+        * Returns the child node that is specified by the names. The first element
+        * of <code>nodeNames</code> is the name of the child node of this node, the
+        * second element of <code>nodeNames</code> is the name of a child node's
+        * child node, and so on. By using this method you can descend into an XML
+        * tree pretty fast.
+        *
         * <pre>
+        *
         * SimpleXML deepNode = topNode.getNodes(new String[] { &quot;person&quot;, &quot;address&quot;, &quot;number&quot; });
         * </pre>
-        * 
+        *
         * @param nodeNames
-        * @return A node that is a deep child of this node, or <code>null</code> if the specified node does not eixst
+        * @return A node that is a deep child of this node, or <code>null</code> if
+        *         the specified node does not eixst
         */
        public SimpleXML getNode(String[] nodeNames) {
                SimpleXML node = this;
-               for (String nodeName: nodeNames) {
+               for (String nodeName : nodeNames) {
                        node = node.getNode(nodeName);
                }
                return node;
@@ -109,7 +118,7 @@ public class SimpleXML {
 
        /**
         * Returns all child nodes of this node.
-        * 
+        *
         * @return All child nodes of this node
         */
        public SimpleXML[] getNodes() {
@@ -117,15 +126,17 @@ public class SimpleXML {
        }
 
        /**
-        * Returns all child nodes of this node with the specified name. If there are no child nodes with the specified name an empty array is returned.
-        * 
+        * Returns all child nodes of this node with the specified name. If there
+        * are no child nodes with the specified name an empty array is returned.
+        *
         * @param nodeName
-        *            The name of the nodes to retrieve, or <code>null</code> to retrieve all nodes
+        *            The name of the nodes to retrieve, or <code>null</code> to
+        *            retrieve all nodes
         * @return All child nodes with the specified name
         */
        public SimpleXML[] getNodes(String nodeName) {
                List<SimpleXML> resultList = new ArrayList<SimpleXML>();
-               for (SimpleXML child: children) {
+               for (SimpleXML child : children) {
                        if ((nodeName == null) || child.name.equals(nodeName)) {
                                resultList.add(child);
                        }
@@ -134,12 +145,14 @@ public class SimpleXML {
        }
 
        /**
-        * Appends a new XML node with the specified name and returns the new node. With this method you can create deep structures very fast.
-        * 
+        * Appends a new XML node with the specified name and returns the new node.
+        * With this method you can create deep structures very fast.
+        *
         * <pre>
+        *
         * SimpleXML mouseNode = topNode.append(&quot;computer&quot;).append(&quot;bus&quot;).append(&quot;usb&quot;).append(&quot;mouse&quot;);
         * </pre>
-        * 
+        *
         * @param nodeName
         *            The name of the node to append as a child to this node
         * @return The new node
@@ -149,8 +162,9 @@ public class SimpleXML {
        }
 
        /**
-        * Appends a new XML node with the specified name and value and returns the new node.
-        * 
+        * Appends a new XML node with the specified name and value and returns the
+        * new node.
+        *
         * @param nodeName
         *            The name of the node to append
         * @param nodeValue
@@ -162,8 +176,9 @@ public class SimpleXML {
        }
 
        /**
-        * Appends the node with all its child nodes to this node and returns the child node.
-        * 
+        * Appends the node with all its child nodes to this node and returns the
+        * child node.
+        *
         * @param newChild
         *            The node to append as a child
         * @return The child node that was appended
@@ -176,31 +191,31 @@ public class SimpleXML {
        public void remove(SimpleXML child) {
                children.remove(child);
        }
-       
+
        public void remove(String childName) {
                SimpleXML child = getNode(childName);
                if (child != null) {
                        remove(child);
                }
        }
-       
+
        public void replace(String childName, String value) {
                remove(childName);
                append(childName, value);
        }
-       
+
        public void replace(SimpleXML childNode) {
                remove(childNode.getName());
                append(childNode);
        }
-       
+
        public void removeAll() {
                children.clear();
        }
 
        /**
         * Sets the value of this node.
-        * 
+        *
         * @param nodeValue
         *            The new value of this node
         * @return This node
@@ -212,7 +227,7 @@ public class SimpleXML {
 
        /**
         * Returns the name of this node.
-        * 
+        *
         * @return The name of this node
         */
        public String getName() {
@@ -221,7 +236,7 @@ public class SimpleXML {
 
        /**
         * Returns the value of this node.
-        * 
+        *
         * @return The value of this node
         */
        public String getValue() {
@@ -229,8 +244,20 @@ public class SimpleXML {
        }
 
        /**
+        * Returns the value of this node. If the node does not have a value, the
+        * given default value is returned.
+        *
+        *@param defaultValue
+        *            The default value to return if the node does not have a value
+        * @return The value of this node
+        */
+       public String getValue(String defaultValue) {
+               return (value == null) ? defaultValue : value;
+       }
+
+       /**
         * Creates a {@link Document} from this node and all its child nodes.
-        * 
+        *
         * @return The {@link Document} created from this node
         */
        public Document getDocument() {
@@ -248,14 +275,15 @@ public class SimpleXML {
        }
 
        /**
-        * Appends all children of this node to the specified {@link Element}. If a node has a value that is not <code>null</code> the value is
-        * appended as a text node.
-        * 
+        * Appends all children of this node to the specified {@link Element}. If a
+        * node has a value that is not <code>null</code> the value is appended as a
+        * text node.
+        *
         * @param rootElement
         *            The element to attach this node's children to
         */
        private void addChildren(Element rootElement) {
-               for (SimpleXML child: children) {
+               for (SimpleXML child : children) {
                        Element childElement = rootElement.getOwnerDocument().createElement(child.name);
                        rootElement.appendChild(childElement);
                        if (child.value != null) {
@@ -268,8 +296,9 @@ public class SimpleXML {
        }
 
        /**
-        * Creates a SimpleXML node from the specified {@link Document}. The SimpleXML node of the document's top-level node is returned.
-        * 
+        * Creates a SimpleXML node from the specified {@link Document}. The
+        * SimpleXML node of the document's top-level node is returned.
+        *
         * @param document
         *            The {@link Document} to create a SimpleXML node from
         * @return The SimpleXML node created from the document's top-level node
@@ -281,8 +310,9 @@ public class SimpleXML {
        }
 
        /**
-        * Appends the child nodes of the specified {@link Document} to this node. Text nodes are converted into a node's value.
-        * 
+        * Appends the child nodes of the specified {@link Document} to this node.
+        * Text nodes are converted into a node's value.
+        *
         * @param xmlDocument
         *            The SimpleXML node to append the child nodes to
         * @param document
@@ -293,7 +323,7 @@ public class SimpleXML {
                NodeList childNodes = document.getChildNodes();
                for (int childIndex = 0, childCount = childNodes.getLength(); childIndex < childCount; childIndex++) {
                        Node childNode = childNodes.item(childIndex);
-                       if ((childNode.getChildNodes().getLength() == 1) && (childNode.getFirstChild().getNodeName().equals("#text")) /*&& (childNode.getFirstChild().getNodeValue().trim().length() != 0)*/) {
+                       if ((childNode.getChildNodes().getLength() == 1) && (childNode.getFirstChild().getNodeName().equals("#text"))) {
                                xmlDocument.append(childNode.getNodeName(), childNode.getFirstChild().getNodeValue());
                        } else {
                                if (!childNode.getNodeName().equals("#text") || (childNode.getChildNodes().getLength() != 0)) {