A massive organize imports and formatting of the sources using default Eclipse code...
[phpeclipse.git] / net.sourceforge.phpeclipse.phphelp / src / net / sourceforge / phpdt / httpquery / config / XMLMemento.java
index dc1954d..d9d80df 100644 (file)
@@ -4,7 +4,7 @@
  * are made available under the terms of the Common Public License v1.0
  * which accompanies this distribution, and is available at
  * http://www.eclipse.org/legal/cpl-v10.html
- *
+ �*
  * Contributors:
  *     IBM Corporation - Initial API and implementation
  **********************************************************************/
@@ -43,30 +43,31 @@ import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
 import org.xml.sax.InputSource;
 import org.xml.sax.SAXException;
+
 /**
- * A Memento is a class independent container for persistence
- * info.  It is a reflection of 3 storage requirements.
- *
- * 1)   We need the ability to persist an object and restore it.
- * 2)   The class for an object may be absent.  If so we would
- *      like to skip the object and keep reading.
- * 3)   The class for an object may change.  If so the new class
- *      should be able to read the old persistence info.
- *
- * We could ask the objects to serialize themselves into an
- * ObjectOutputStream, DataOutputStream, or Hashtable.  However
- * all of these approaches fail to meet the second requirement.
- *
+ * A Memento is a class independent container for persistence info. It is a
+ * reflection of 3 storage requirements.
+ * 
+ * 1) We need the ability to persist an object and restore it. 2) The class for
+ * an object may be absent. If so we would like to skip the object and keep
+ * reading. 3) The class for an object may change. If so the new class should be
+ * able to read the old persistence info.
+ * 
+ * We could ask the objects to serialize themselves into an ObjectOutputStream,
+ * DataOutputStream, or Hashtable. However all of these approaches fail to meet
+ * the second requirement.
+ * 
  * Memento supports binary persistance with a version ID.
  */
 public final class XMLMemento implements IMemento {
        private Document factory;
+
        private Element element;
 
        /**
-        * Answer a memento for the document and element.  For simplicity
-        * you should use createReadRoot and createWriteRoot to create the initial
-        * mementos on a document.
+        * Answer a memento for the document and element. For simplicity you should
+        * use createReadRoot and createWriteRoot to create the initial mementos on
+        * a document.
         */
        public XMLMemento(Document doc, Element el) {
                factory = doc;
@@ -93,13 +94,14 @@ public final class XMLMemento implements IMemento {
        }
 
        /**
-        * Create a Document from a Reader and answer a root memento for reading
-        * a document.
+        * Create a Document from a Reader and answer a root memento for reading a
+        * document.
         */
        protected static XMLMemento createReadRoot(Reader reader) {
                Document document = null;
                try {
-                       DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+                       DocumentBuilderFactory factory = DocumentBuilderFactory
+                                       .newInstance();
                        DocumentBuilder parser = factory.newDocumentBuilder();
                        document = parser.parse(new InputSource(reader));
                        Node node = document.getFirstChild();
@@ -111,7 +113,8 @@ public final class XMLMemento implements IMemento {
                } finally {
                        try {
                                reader.close();
-                       } catch (Exception e) { }
+                       } catch (Exception e) {
+                       }
                }
                return null;
        }
@@ -122,7 +125,8 @@ public final class XMLMemento implements IMemento {
        public static XMLMemento createWriteRoot(String type) {
                Document document;
                try {
-                       document = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
+                       document = DocumentBuilderFactory.newInstance()
+                                       .newDocumentBuilder().newDocument();
                        Element element = document.createElement(type);
                        document.appendChild(element);
                        return new XMLMemento(document, element);
@@ -142,10 +146,10 @@ public final class XMLMemento implements IMemento {
                        return null;
 
                // Find the first node which is a child of this node.
-               for (int nX = 0; nX < size; nX ++) {
+               for (int nX = 0; nX < size; nX++) {
                        Node node = nodes.item(nX);
                        if (node instanceof Element) {
-                               Element element2 = (Element)node;
+                               Element element2 = (Element) node;
                                if (element2.getNodeName().equals(type))
                                        return new XMLMemento(factory, element2);
                        }
@@ -158,7 +162,7 @@ public final class XMLMemento implements IMemento {
        /**
         * @see IMemento.
         */
-       public IMemento [] getChildren(String type) {
+       public IMemento[] getChildren(String type) {
                // Get the nodes.
                NodeList nodes = element.getChildNodes();
                int size = nodes.getLength();
@@ -167,10 +171,10 @@ public final class XMLMemento implements IMemento {
 
                // Extract each node with given fType.
                ArrayList list = new ArrayList(size);
-               for (int nX = 0; nX < size; nX ++) {
+               for (int nX = 0; nX < size; nX++) {
                        Node node = nodes.item(nX);
                        if (node instanceof Element) {
-                               Element element2 = (Element)node;
+                               Element element2 = (Element) node;
                                if (element2.getNodeName().equals(type))
                                        list.add(element2);
                        }
@@ -178,16 +182,16 @@ public final class XMLMemento implements IMemento {
 
                // Create a memento for each node.
                size = list.size();
-               IMemento [] results = new IMemento[size];
-               for (int x = 0; x < size; x ++) {
-                       results[x] = new XMLMemento(factory, (Element)list.get(x));
+               IMemento[] results = new IMemento[size];
+               for (int x = 0; x < size; x++) {
+                       results[x] = new XMLMemento(factory, (Element) list.get(x));
                }
                return results;
        }
 
        /**
         * Return the contents of this memento as a byte array.
-        *
+        * 
         * @return byte[]
         */
        public byte[] getContents() throws IOException {
@@ -198,7 +202,7 @@ public final class XMLMemento implements IMemento {
 
        /**
         * Returns an input stream for writing to the disk with a local locale.
-        *
+        * 
         * @return java.io.InputStream
         */
        public InputStream getInputStream() throws IOException {
@@ -275,8 +279,9 @@ public final class XMLMemento implements IMemento {
 
        /**
         * Loads a memento from the given filename.
-        *
-        * @param in java.io.InputStream
+        * 
+        * @param in
+        *            java.io.InputStream
         * @return org.eclipse.ui.IMemento
         * @exception java.io.IOException
         */
@@ -286,15 +291,17 @@ public final class XMLMemento implements IMemento {
 
        /**
         * Loads a memento from the given filename.
-        *
-        * @param in java.io.InputStream
+        * 
+        * @param in
+        *            java.io.InputStream
         * @return org.eclipse.ui.IMemento
         * @exception java.io.IOException
         */
        public static IMemento loadCorruptMemento(InputStream in) {
                Document document = null;
                try {
-                       DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
+                       DocumentBuilderFactory factory = DocumentBuilderFactory
+                                       .newInstance();
                        DocumentBuilder parser = factory.newDocumentBuilder();
                        document = parser.parse(in);
                        Node node = document.getFirstChild();
@@ -306,15 +313,17 @@ public final class XMLMemento implements IMemento {
                } finally {
                        try {
                                in.close();
-                       } catch (Exception e) { }
+                       } catch (Exception e) {
+                       }
                }
                return null;
        }
 
        /**
         * Loads a memento from the given filename.
-        *
-        * @param filename java.lang.String
+        * 
+        * @param filename
+        *            java.lang.String
         * @return org.eclipse.ui.IMemento
         * @exception java.io.IOException
         */
@@ -324,13 +333,15 @@ public final class XMLMemento implements IMemento {
 
        /**
         * Loads a memento from the given filename.
-        *
-        * @param url java.net.URL
+        * 
+        * @param url
+        *            java.net.URL
         * @return org.eclipse.ui.IMemento
         * @exception java.io.IOException
         */
        public static IMemento loadMemento(URL url) throws IOException {
-               return XMLMemento.createReadRoot(new InputStreamReader(url.openStream()));
+               return XMLMemento
+                               .createReadRoot(new InputStreamReader(url.openStream()));
        }
 
        /**
@@ -339,18 +350,18 @@ public final class XMLMemento implements IMemento {
        private void putElement(Element element2) {
                NamedNodeMap nodeMap = element2.getAttributes();
                int size = nodeMap.getLength();
-               for (int i = 0; i < size; i++){
-                       Attr attr = (Attr)nodeMap.item(i);
-                       putString(attr.getName(),attr.getValue());
+               for (int i = 0; i < size; i++) {
+                       Attr attr = (Attr) nodeMap.item(i);
+                       putString(attr.getName(), attr.getValue());
                }
 
                NodeList nodes = element2.getChildNodes();
                size = nodes.getLength();
-               for (int i = 0; i < size; i ++) {
+               for (int i = 0; i < size; i++) {
                        Node node = nodes.item(i);
                        if (node instanceof Element) {
-                               XMLMemento child = (XMLMemento)createChild(node.getNodeName());
-                               child.putElement((Element)node);
+                               XMLMemento child = (XMLMemento) createChild(node.getNodeName());
+                               child.putElement((Element) node);
                        }
                }
        }
@@ -393,7 +404,8 @@ public final class XMLMemento implements IMemento {
                Result result = new StreamResult(writer);
                Source source = new DOMSource(factory);
                try {
-                       Transformer transformer = TransformerFactory.newInstance().newTransformer();
+                       Transformer transformer = TransformerFactory.newInstance()
+                                       .newTransformer();
                        transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
                        transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$
                        transformer.transform(source, result);
@@ -409,7 +421,8 @@ public final class XMLMemento implements IMemento {
                Result result = new StreamResult(os);
                Source source = new DOMSource(factory);
                try {
-                       Transformer transformer = TransformerFactory.newInstance().newTransformer();
+                       Transformer transformer = TransformerFactory.newInstance()
+                                       .newTransformer();
                        transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
                        transformer.setOutputProperty(OutputKeys.METHOD, "xml"); //$NON-NLS-1$
                        transformer.transform(source, result);
@@ -420,8 +433,9 @@ public final class XMLMemento implements IMemento {
 
        /**
         * Saves the memento to the given file.
-        *
-        * @param filename java.lang.String
+        * 
+        * @param filename
+        *            java.lang.String
         * @exception java.io.IOException
         */
        public void saveToFile(String filename) throws IOException {
@@ -437,7 +451,8 @@ public final class XMLMemento implements IMemento {
                        if (w != null) {
                                try {
                                        w.close();
-                               } catch (Exception e) { }
+                               } catch (Exception e) {
+                               }
                        }
                }
        }