Use XML classes from utils.
authorDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Sun, 26 Aug 2012 20:30:59 +0000 (22:30 +0200)
committerDavid ‘Bombe’ Roden <bombe@freenetproject.org>
Sun, 26 Aug 2012 20:30:59 +0000 (22:30 +0200)
src/main/java/de/todesbaum/jsite/main/Configuration.java
src/main/java/de/todesbaum/util/xml/SimpleXML.java [deleted file]
src/main/java/de/todesbaum/util/xml/XML.java [deleted file]

index f5001b0..12eb398 100644 (file)
@@ -35,6 +35,8 @@ import java.util.Map.Entry;
 import java.util.logging.Level;
 import java.util.logging.Logger;
 
+import net.pterodactylus.util.xml.SimpleXML;
+import net.pterodactylus.util.xml.XML;
 import de.todesbaum.jsite.application.FileOption;
 import de.todesbaum.jsite.application.Node;
 import de.todesbaum.jsite.application.Project;
@@ -43,8 +45,6 @@ import de.todesbaum.util.freenet.fcp2.ClientPutDir.ManifestPutter;
 import de.todesbaum.util.freenet.fcp2.PriorityClass;
 import de.todesbaum.util.io.Closer;
 import de.todesbaum.util.io.StreamCopier;
-import de.todesbaum.util.xml.SimpleXML;
-import de.todesbaum.util.xml.XML;
 
 /**
  * The configuration.
@@ -316,24 +316,24 @@ public class Configuration {
                                try {
                                        Project project = new Project();
                                        projects.add(project);
-                                       project.setDescription(projectNode.getNode("description").getValue(""));
-                                       String indexFile = projectNode.getNode("index-file").getValue("");
+                                       project.setDescription(projectNode.getValue("description", ""));
+                                       String indexFile = projectNode.getValue("index-file", "");
                                        if (indexFile.indexOf('/') > -1) {
                                                indexFile = "";
                                        }
                                        project.setIndexFile(indexFile);
-                                       project.setLastInsertionTime(Long.parseLong(projectNode.getNode("last-insertion-time").getValue("0")));
-                                       project.setLocalPath(projectNode.getNode("local-path").getValue(""));
-                                       project.setName(projectNode.getNode("name").getValue(""));
-                                       project.setPath(projectNode.getNode("path").getValue(""));
+                                       project.setLastInsertionTime(Long.parseLong(projectNode.getValue("last-insertion-time", "0")));
+                                       project.setLocalPath(projectNode.getValue("local-path", ""));
+                                       project.setName(projectNode.getValue("name", ""));
+                                       project.setPath(projectNode.getValue("path", ""));
                                        if ((project.getPath() != null) && (project.getPath().indexOf("/") != -1)) {
                                                project.setPath(project.getPath().replaceAll("/", ""));
                                        }
-                                       project.setEdition(Integer.parseInt(projectNode.getNode("edition").getValue("0")));
-                                       project.setInsertURI(projectNode.getNode("insert-uri").getValue(""));
-                                       project.setRequestURI(projectNode.getNode("request-uri").getValue(""));
+                                       project.setEdition(Integer.parseInt(projectNode.getValue("edition", "0")));
+                                       project.setInsertURI(projectNode.getValue("insert-uri", ""));
+                                       project.setRequestURI(projectNode.getValue("request-uri", ""));
                                        if (projectNode.getNode("ignore-hidden-files") != null) {
-                                               project.setIgnoreHiddenFiles(Boolean.parseBoolean(projectNode.getNode("ignore-hidden-files").getValue("true")));
+                                               project.setIgnoreHiddenFiles(Boolean.parseBoolean(projectNode.getValue("ignore-hidden-files", "true")));
                                        } else {
                                                project.setIgnoreHiddenFiles(true);
                                        }
@@ -366,11 +366,11 @@ public class Configuration {
                                                        if (fileOptionNode.getNode("insert-redirect") != null) {
                                                                fileOption.setInsertRedirect(Boolean.parseBoolean(fileOptionNode.getNode("insert-redirect").getValue()));
                                                        }
-                                                       fileOption.setCustomKey(fileOptionNode.getNode("custom-key").getValue(""));
+                                                       fileOption.setCustomKey(fileOptionNode.getValue("custom-key", ""));
                                                        if (fileOptionNode.getNode("changed-name") != null) {
                                                                fileOption.setChangedName(fileOptionNode.getNode("changed-name").getValue());
                                                        }
-                                                       fileOption.setMimeType(fileOptionNode.getNode("mime-type").getValue(""));
+                                                       fileOption.setMimeType(fileOptionNode.getValue("mime-type", ""));
                                                        fileOptions.put(filename, fileOption);
                                                }
                                        }
diff --git a/src/main/java/de/todesbaum/util/xml/SimpleXML.java b/src/main/java/de/todesbaum/util/xml/SimpleXML.java
deleted file mode 100644 (file)
index 8351581..0000000
+++ /dev/null
@@ -1,338 +0,0 @@
-/*
- * 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
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
- */
-
-package de.todesbaum.util.xml;
-
-import java.util.ArrayList;
-import java.util.List;
-
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.parsers.ParserConfigurationException;
-
-import org.w3c.dom.Document;
-import org.w3c.dom.Element;
-import org.w3c.dom.Node;
-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)}.
- *
- * @author David Roden &lt;droden@gmail.com&gt;
- * @version $Id:SimpleXML.java 221 2006-03-06 14:46:49Z bombe $
- */
-public class SimpleXML {
-
-       /**
-        * A {@link List} containing all child nodes of this node.
-        */
-       private List<SimpleXML> children = new ArrayList<SimpleXML>();
-
-       /**
-        * The name of this node.
-        */
-       private String name = null;
-
-       /**
-        * The value of this node.
-        */
-       private String value = null;
-
-       /**
-        * Constructs a new XML node without a name.
-        */
-       public SimpleXML() {
-               super();
-       }
-
-       /**
-        * Constructs a new XML node with the specified name.
-        *
-        * @param name
-        *            The name of the new node
-        */
-       public SimpleXML(String name) {
-               this.name = name;
-       }
-
-       /**
-        * 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
-        */
-       public SimpleXML getNode(String nodeName) {
-               for (int index = 0, count = children.size(); index < count; index++) {
-                       if (children.get(index).name.equals(nodeName)) {
-                               return children.get(index);
-                       }
-               }
-               return null;
-       }
-
-       /**
-        * 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
-        */
-       public SimpleXML getNode(String[] nodeNames) {
-               SimpleXML node = this;
-               for (String nodeName : nodeNames) {
-                       node = node.getNode(nodeName);
-               }
-               return node;
-       }
-
-       /**
-        * Returns all child nodes of this node.
-        *
-        * @return All child nodes of this node
-        */
-       public SimpleXML[] getNodes() {
-               return getNodes(null);
-       }
-
-       /**
-        * 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
-        * @return All child nodes with the specified name
-        */
-       public SimpleXML[] getNodes(String nodeName) {
-               List<SimpleXML> resultList = new ArrayList<SimpleXML>();
-               for (SimpleXML child : children) {
-                       if ((nodeName == null) || child.name.equals(nodeName)) {
-                               resultList.add(child);
-                       }
-               }
-               return resultList.toArray(new SimpleXML[resultList.size()]);
-       }
-
-       /**
-        * 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
-        */
-       public SimpleXML append(String nodeName) {
-               return append(new SimpleXML(nodeName));
-       }
-
-       /**
-        * 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
-        *            The value of the node to append
-        * @return The newly appended node
-        */
-       public SimpleXML append(String nodeName, String nodeValue) {
-               return append(nodeName).setValue(nodeValue);
-       }
-
-       /**
-        * 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
-        */
-       public SimpleXML append(SimpleXML newChild) {
-               children.add(newChild);
-               return newChild;
-       }
-
-       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
-        */
-       public SimpleXML setValue(String nodeValue) {
-               value = nodeValue;
-               return this;
-       }
-
-       /**
-        * Returns the name of this node.
-        *
-        * @return The name of this node
-        */
-       public String getName() {
-               return name;
-       }
-
-       /**
-        * Returns the value of this node.
-        *
-        * @return The value of this node
-        */
-       public String getValue() {
-               return value;
-       }
-
-       /**
-        * 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() {
-               DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
-               try {
-                       DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
-                       Document document = documentBuilder.newDocument();
-                       Element rootElement = document.createElement(name);
-                       document.appendChild(rootElement);
-                       addChildren(rootElement);
-                       return document;
-               } catch (ParserConfigurationException e) {
-               }
-               return null;
-       }
-
-       /**
-        * 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) {
-                       Element childElement = rootElement.getOwnerDocument().createElement(child.name);
-                       rootElement.appendChild(childElement);
-                       if (child.value != null) {
-                               Text childText = rootElement.getOwnerDocument().createTextNode(child.value);
-                               childElement.appendChild(childText);
-                       } else {
-                               child.addChildren(childElement);
-                       }
-               }
-       }
-
-       /**
-        * 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
-        */
-       public static SimpleXML fromDocument(Document document) {
-               SimpleXML xmlDocument = new SimpleXML(document.getFirstChild().getNodeName());
-               document.normalizeDocument();
-               return addDocumentChildren(xmlDocument, document.getFirstChild());
-       }
-
-       /**
-        * 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
-        *            The document whose child nodes to append
-        * @return The SimpleXML node the child nodes were appended to
-        */
-       private static SimpleXML addDocumentChildren(SimpleXML xmlDocument, Node document) {
-               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"))) {
-                               xmlDocument.append(childNode.getNodeName(), childNode.getFirstChild().getNodeValue());
-                       } else {
-                               if (!childNode.getNodeName().equals("#text") || (childNode.getChildNodes().getLength() != 0)) {
-                                       SimpleXML newXML = xmlDocument.append(childNode.getNodeName());
-                                       addDocumentChildren(newXML, childNode);
-                               }
-                       }
-               }
-               return xmlDocument;
-       }
-
-}
diff --git a/src/main/java/de/todesbaum/util/xml/XML.java b/src/main/java/de/todesbaum/util/xml/XML.java
deleted file mode 100644 (file)
index 8b6b259..0000000
+++ /dev/null
@@ -1,179 +0,0 @@
-/*
- * jSite - XML.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 (at your option) any later
- * version.
- *
- * This program is distributed in the hope that it will be useful, but WITHOUT
- * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
- * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
- * details.
- *
- * You should have received a copy of the GNU General Public License along with
- * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
- * Place - Suite 330, Boston, MA 02111-1307, USA.
- */
-
-package de.todesbaum.util.xml;
-
-import java.io.ByteArrayInputStream;
-import java.io.ByteArrayOutputStream;
-import java.io.IOException;
-import java.io.InputStreamReader;
-import java.io.OutputStreamWriter;
-import java.nio.charset.Charset;
-
-import javax.xml.parsers.DocumentBuilder;
-import javax.xml.parsers.DocumentBuilderFactory;
-import javax.xml.parsers.ParserConfigurationException;
-import javax.xml.transform.Result;
-import javax.xml.transform.Source;
-import javax.xml.transform.Transformer;
-import javax.xml.transform.TransformerConfigurationException;
-import javax.xml.transform.TransformerException;
-import javax.xml.transform.TransformerFactory;
-import javax.xml.transform.dom.DOMResult;
-import javax.xml.transform.dom.DOMSource;
-import javax.xml.transform.stream.StreamResult;
-import javax.xml.transform.stream.StreamSource;
-
-import org.w3c.dom.Document;
-
-
-/**
- * Contains method to transform DOM XML trees to byte arrays and vice versa.
- *
- * @author David Roden &lt;droden@gmail.com&gt;
- * @version $Id:XML.java 221 2006-03-06 14:46:49Z bombe $
- */
-public class XML {
-
-       /** Cached document builder factory. */
-       private static DocumentBuilderFactory documentBuilderFactory = null;
-
-       /** Cached document builder. */
-       private static DocumentBuilder documentBuilder = null;
-
-       /** Cached transformer factory. */
-       private static TransformerFactory transformerFactory = null;
-
-       /** Does nothing. */
-       private XML() {
-       }
-
-       /**
-        * Returns a document builder factory. If possible the cached instance will be returned.
-        *
-        * @return A document builder factory
-        */
-       private static DocumentBuilderFactory getDocumentBuilderFactory() {
-               if (documentBuilderFactory != null) {
-                       return documentBuilderFactory;
-               }
-               documentBuilderFactory = DocumentBuilderFactory.newInstance();
-               return documentBuilderFactory;
-       }
-
-       /**
-        * Returns a document builder. If possible the cached instance will be returned.
-        *
-        * @return A document builder
-        */
-       private static DocumentBuilder getDocumentBuilder() {
-               if (documentBuilder != null) {
-                       return documentBuilder;
-               }
-               try {
-                       documentBuilder = getDocumentBuilderFactory().newDocumentBuilder();
-               } catch (ParserConfigurationException e) {
-               }
-               return documentBuilder;
-       }
-
-       /**
-        * Returns a transformer factory. If possible the cached instance will be returned.
-        *
-        * @return A transformer factory
-        */
-       private static TransformerFactory getTransformerFactory() {
-               if (transformerFactory != null) {
-                       return transformerFactory;
-               }
-               transformerFactory = TransformerFactory.newInstance();
-               return transformerFactory;
-       }
-
-       /**
-        * Creates a new XML document.
-        *
-        * @return A new XML document
-        */
-       public static Document createDocument() {
-               return getDocumentBuilder().newDocument();
-       }
-
-       /**
-        * Transforms the DOM XML document into a byte array.
-        *
-        * @param document
-        *            The document to transform
-        * @return The byte array containing the XML representation
-        */
-       public static byte[] transformToByteArray(Document document) {
-               ByteArrayOutputStream byteOutput = new ByteArrayOutputStream();
-               OutputStreamWriter converter = new OutputStreamWriter(byteOutput, Charset.forName("UTF-8"));
-               Result transformResult = new StreamResult(converter);
-               Source documentSource = new DOMSource(document);
-               try {
-                       Transformer transformer = getTransformerFactory().newTransformer();
-                       transformer.transform(documentSource, transformResult);
-                       byteOutput.close();
-                       return byteOutput.toByteArray();
-               } catch (IOException ioe1) {
-               } catch (TransformerConfigurationException tce1) {
-               } catch (TransformerException te1) {
-               } finally {
-                       try {
-                               byteOutput.close();
-                       } catch (IOException ioe1) {
-                       }
-               }
-               return null;
-       }
-
-       /**
-        * Transforms the byte array into a DOM XML document.
-        *
-        * @param data
-        *            The byte array to parse
-        * @return The DOM XML document
-        */
-       public static Document transformToDocument(byte[] data) {
-               ByteArrayInputStream byteInput = new ByteArrayInputStream(data);
-               InputStreamReader converter = new InputStreamReader(byteInput, Charset.forName("UTF-8"));
-               Source xmlSource = new StreamSource(converter);
-               Result xmlResult = new DOMResult();
-               try {
-                       Transformer transformer = getTransformerFactory().newTransformer();
-                       transformer.transform(xmlSource, xmlResult);
-                       return (Document) ((DOMResult) xmlResult).getNode();
-               } catch (TransformerConfigurationException tce1) {
-               } catch (TransformerException te1) {
-               } finally {
-                       if (byteInput != null)
-                               try {
-                                       byteInput.close();
-                               } catch (IOException ioe1) {
-                               }
-                       if (converter != null)
-                               try {
-                                       converter.close();
-                               } catch (IOException ioe1) {
-                               }
-               }
-               return null;
-       }
-
-}