You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lenya.apache.org by so...@apache.org on 2008/01/31 08:44:36 UTC

svn commit: r617035 [21/22] - in /lenya/branches/revolution/1.3.x: ./ src/java/org/apache/lenya/ac/ src/java/org/apache/lenya/ac/cache/ src/java/org/apache/lenya/ac/cifs/ src/java/org/apache/lenya/ac/file/ src/java/org/apache/lenya/ac/impl/ src/java/or...

Modified: lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/xml/Configuration.java
URL: http://svn.apache.org/viewvc/lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/xml/Configuration.java?rev=617035&r1=617034&r2=617035&view=diff
==============================================================================
--- lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/xml/Configuration.java (original)
+++ lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/xml/Configuration.java Wed Jan 30 23:44:03 2008
@@ -14,142 +14,118 @@
  *  limitations under the License.
  *
  */
-
 /* $Id$  */
-
 package org.apache.lenya.xml;
-
 import java.net.URL;
 import java.util.Properties;
-
 import org.apache.log4j.Category;
-
-
 /**
  * Reads xpsconf.properties
+ * 
  * @deprecated replaced by config/ directory
  */
 public class Configuration {
-    static Category log = Category.getInstance(Configuration.class);
-    public static final String DEFAULT_CONFIGURATION_FILE = "org/apache/lenya/xml/xpsconf.properties";
-    public static final String DEFAULT_CONFIGURATION_KEY = "xps.configuration";
-    public static final String OVERRIDE_DEFAULT_CONFIGURATION_KEY = "override.xps.configuration";
-    public String cacheFolder = null;
-    public boolean cacheHTTP = false;
-    public String INCLUDE = null;
-    public String JAVA_ZONE = null;
-    public String proxyHost = null;
-    public String proxyPort = null;
-
-    /**
-     * Creates a new Configuration object.
-     */
-    public Configuration() {
-        getProperties(load());
-    }
-
-    /**
-     * http://www.artima.com/java/answers/Mar2001/messages/164.html export
-     * CLASSPATH=/home/lenya/src/xps/build/properties:... java
-     * -Doverride.xps.configuration=org/apache/lenya/xps/altconf.properties org.apache.lenya.xps.Configuration
-     *
-     * @param args DOCUMENT ME!
-     */
-    public static void main(String[] args) {
-        Configuration conf = new Configuration();
-
-        System.out.println("Caching directory: " + conf.cacheFolder);
-        System.out.println("Cache xml from http connections: " + conf.cacheHTTP);
-
-        if ((conf.proxyHost != null) && (conf.proxyHost != null)) {
-            System.out.println("Proxy set:");
-            System.out.println(conf.proxyHost);
-            System.out.println(conf.proxyPort);
-        } else {
-            System.out.println("No proxy set.");
-        }
-    }
-
-    /**
-     * DOCUMENT ME!
-     *
-     * @return DOCUMENT ME!
-     */
-    public static Properties load() {
-        String resourcePathRelativeToClasspath = System.getProperty(OVERRIDE_DEFAULT_CONFIGURATION_KEY);
-
-        if (resourcePathRelativeToClasspath == null) {
-            resourcePathRelativeToClasspath = System.getProperty(DEFAULT_CONFIGURATION_KEY,
-                    DEFAULT_CONFIGURATION_FILE);
-            log.debug(DEFAULT_CONFIGURATION_KEY + "=" + resourcePathRelativeToClasspath);
-        } else {
-            log.debug(OVERRIDE_DEFAULT_CONFIGURATION_KEY + "=" + resourcePathRelativeToClasspath);
-        }
-
-        ClassLoader cl = ClassLoader.getSystemClassLoader();
-
-        // FIXME:
-        URL url = org.apache.log4j.helpers.Loader.getResource("hallo");
-
-        if (url == null) {
-            //return null;
-        }
-
-        log.debug(url);
-
-        Properties properties = new Properties();
-
-        try {
-            properties.load(Configuration.class.getResourceAsStream("xpsconf.properties"));
-        } catch (Exception e) {
-            log.error(".load(): " + e);
-        }
-
-        return properties;
-    }
-
-    /**
-     * DOCUMENT ME!
-     *
-     * @param properties DOCUMENT ME!
-     */
-    public void getProperties(Properties properties) {
-        if (properties != null) {
-            cacheFolder = getProperty(properties,
-                    "org.apache.lenya.xps.XLinkInterpreter.cacheFolder");
-            cacheHTTP = false;
-            INCLUDE = getProperty(properties, "Include");
-            JAVA_ZONE = getProperty(properties, "JavaZone");
-            proxyHost = null;
-            proxyPort = null;
-        }
-    }
-
-    /**
-     * DOCUMENT ME!
-     *
-     * @param properties DOCUMENT ME!
-     * @param key DOCUMENT ME!
-     *
-     * @return DOCUMENT ME!
-     */
-    public String getProperty(Properties properties, String key) {
-        String value = properties.getProperty(key);
-
-        if (value != null) {
-            log.debug(key + "=" + value);
-
-            return value;
-        } else {
-            log.debug(".getProperty(): No such property: " + key);
-        }
-
-        return null;
-    }
-
-    /**
-     * DOCUMENT ME!
-     */
-    public static void register() {
-    }
+   static Category log = Category.getInstance(Configuration.class);
+   public static final String DEFAULT_CONFIGURATION_FILE = "org/apache/lenya/xml/xpsconf.properties";
+   public static final String DEFAULT_CONFIGURATION_KEY = "xps.configuration";
+   public static final String OVERRIDE_DEFAULT_CONFIGURATION_KEY = "override.xps.configuration";
+   public String cacheFolder = null;
+   public boolean cacheHTTP = false;
+   public String INCLUDE = null;
+   public String JAVA_ZONE = null;
+   public String proxyHost = null;
+   public String proxyPort = null;
+   /**
+    * Creates a new Configuration object.
+    */
+   public Configuration() {
+      getProperties(load());
+   }
+   /**
+    * http://www.artima.com/java/answers/Mar2001/messages/164.html export CLASSPATH=/home/lenya/src/xps/build/properties:... java -Doverride.xps.configuration=org/apache/lenya/xps/altconf.properties org.apache.lenya.xps.Configuration
+    * 
+    * @param args
+    *           DOCUMENT ME!
+    */
+   public static void main(String[] args) {
+      Configuration conf = new Configuration();
+      System.out.println("Caching directory: " + conf.cacheFolder);
+      System.out.println("Cache xml from http connections: " + conf.cacheHTTP);
+      if((conf.proxyHost != null) && (conf.proxyHost != null)){
+         System.out.println("Proxy set:");
+         System.out.println(conf.proxyHost);
+         System.out.println(conf.proxyPort);
+      }else{
+         System.out.println("No proxy set.");
+      }
+   }
+   /**
+    * DOCUMENT ME!
+    * 
+    * @return DOCUMENT ME!
+    */
+   public static Properties load() {
+      String resourcePathRelativeToClasspath = System.getProperty(OVERRIDE_DEFAULT_CONFIGURATION_KEY);
+      if(resourcePathRelativeToClasspath == null){
+         resourcePathRelativeToClasspath = System.getProperty(DEFAULT_CONFIGURATION_KEY, DEFAULT_CONFIGURATION_FILE);
+         log.debug(DEFAULT_CONFIGURATION_KEY + "=" + resourcePathRelativeToClasspath);
+      }else{
+         log.debug(OVERRIDE_DEFAULT_CONFIGURATION_KEY + "=" + resourcePathRelativeToClasspath);
+      }
+      // ClassLoader cl = ClassLoader.getSystemClassLoader();
+      // FIXME:
+      URL url = org.apache.log4j.helpers.Loader.getResource("hallo");
+      if(url == null){
+         // return null;
+      }
+      log.debug(url);
+      Properties properties = new Properties();
+      try{
+         properties.load(Configuration.class.getResourceAsStream("xpsconf.properties"));
+      }catch(Exception e){
+         log.error(".load(): " + e);
+      }
+      return properties;
+   }
+   /**
+    * DOCUMENT ME!
+    * 
+    * @param properties
+    *           DOCUMENT ME!
+    */
+   public void getProperties(Properties properties) {
+      if(properties != null){
+         cacheFolder = getProperty(properties, "org.apache.lenya.xps.XLinkInterpreter.cacheFolder");
+         cacheHTTP = false;
+         INCLUDE = getProperty(properties, "Include");
+         JAVA_ZONE = getProperty(properties, "JavaZone");
+         proxyHost = null;
+         proxyPort = null;
+      }
+   }
+   /**
+    * DOCUMENT ME!
+    * 
+    * @param properties
+    *           DOCUMENT ME!
+    * @param key
+    *           DOCUMENT ME!
+    * 
+    * @return DOCUMENT ME!
+    */
+   public String getProperty(Properties properties, String key) {
+      String value = properties.getProperty(key);
+      if(value != null){
+         log.debug(key + "=" + value);
+         return value;
+      }else{
+         log.debug(".getProperty(): No such property: " + key);
+      }
+      return null;
+   }
+   /**
+    * DOCUMENT ME!
+    */
+   public static void register() {
+   }
 }

Modified: lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/xml/DOMParserFactory.java
URL: http://svn.apache.org/viewvc/lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/xml/DOMParserFactory.java?rev=617035&r1=617034&r2=617035&view=diff
==============================================================================
--- lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/xml/DOMParserFactory.java (original)
+++ lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/xml/DOMParserFactory.java Wed Jan 30 23:44:03 2008
@@ -14,17 +14,13 @@
  *  limitations under the License.
  *
  */
-
 /* $Id$  */
-
 package org.apache.lenya.xml;
-
 import java.io.File;
 import java.io.FileNotFoundException;
 import java.io.InputStream;
 import java.io.Reader;
 import java.util.Properties;
-
 import org.apache.lenya.xml.parser.Parser;
 import org.apache.log4j.Category;
 import org.w3c.dom.CDATASection;
@@ -36,283 +32,261 @@
 import org.w3c.dom.NodeList;
 import org.w3c.dom.Text;
 import org.xml.sax.SAXException;
-
-
 /**
  * Utility class for creating DOM documents
+ * 
  * @deprecated replaced by DocumentHelper
  */
 public class DOMParserFactory {
-    static Category log = Category.getInstance(DOMParserFactory.class);
-    public Parser parser = null;
-
-    /**
-     * Reads the properties and gets the parser
-     */
-    public DOMParserFactory() {
-        Properties properties = new Properties();
-        String propertiesFileName = "conf.properties";
-
-        try {
-            properties.load(DOMParserFactory.class.getResourceAsStream(propertiesFileName));
-        } catch (Exception e) {
-            log.fatal(": Failed to load properties from resource: " + propertiesFileName);
-        }
-
-        String parserName = properties.getProperty("Parser");
-
-        if (parserName == null) {
-            log.fatal(": No Parser specified in " + propertiesFileName);
-        }
-
-        try {
-            Class parserClass = Class.forName(parserName);
-            parser = (Parser) parserClass.newInstance();
-        } catch (Exception e) {
-            log.fatal(": " + e);
-        }
-    }
-
-    /**
-     * DOCUMENT ME!
-     *
-     * @param args DOCUMENT ME!
-     */
-    public static void main(String[] args) {
-        DOMParserFactory dpf = new DOMParserFactory();
-
-        if (args.length != 1) {
-            System.out.println("Usage: java " + dpf.getClass().getName() + " example.xml");
-
-            return;
-        }
-
-        Document doc = null;
-
-        try {
-            doc = dpf.getDocument(args[0]);
-        } catch (FileNotFoundException e) {
-            System.err.println("No such file or directory: " + e.getMessage());
-
-            return;
-        } catch (SAXException e) {
-            System.err.println(e);
-
-            return;
-        } catch (Exception e) {
-            System.err.println(e.getMessage());
-
-            return;
-        }
-    }
-
-    /**
-     * DOCUMENT ME!
-     *
-     * @param filename DOCUMENT ME!
-     *
-     * @return DOCUMENT ME!
-     *
-     * @throws FileNotFoundException DOCUMENT ME!
-     * @throws Exception DOCUMENT ME!
-     */
-    public Document getDocument(String filename) throws FileNotFoundException, Exception {
-        File file = new File(filename);
-
-        if (!file.exists()) {
-            log.error("No such file or directory: " + filename);
-            throw new FileNotFoundException(filename);
-        }
-
-        return parser.getDocument(filename);
-    }
-
-    /**
-     * DOCUMENT ME!
-     *
-     * @param inputStream DOCUMENT ME!
-     *
-     * @return DOCUMENT ME!
-     *
-     * @throws Exception DOCUMENT ME!
-     */
-    public Document getDocument(InputStream inputStream)
-        throws Exception {
-        return parser.getDocument(inputStream);
-    }
-
-    /**
-     * Create a document from a reader.
-     *
-     * @param inputStream DOCUMENT ME!
-     * @return DOCUMENT ME!
-     * @throws Exception DOCUMENT ME!
-     */
-    public Document getDocument(Reader reader) throws Exception {
-        return parser.getDocument(reader);
-    }
-
-    /**
-     * DOCUMENT ME!
-     *
-     * @return DOCUMENT ME!
-     */
-    public Document getDocument() {
-        return parser.getDocument();
-    }
-
-    /**
-     * DOCUMENT ME!
-     *
-     * @param document DOCUMENT ME!
-     * @param name DOCUMENT ME!
-     *
-     * @return DOCUMENT ME!
-     */
-    public Element newElementNode(Document document, String name) {
-        return parser.newElementNode(document, name);
-    }
-
-    /**
-     * Creates an element with namespace support.
-     *
-     * @param document The owner document.
-     * @param namespaceUri The namespace URI of the element.
-     * @param qualifiedName The qualified name of the element.
-     *
-     * @return An element.
-     */
-    public Element newElementNSNode(Document document, String namespaceUri, String qualifiedName) {
-        return parser.newElementNSNode(document, namespaceUri, qualifiedName);
-    }
-        
-    /**
-     * DOCUMENT ME!
-     *
-     * @param document DOCUMENT ME!
-     * @param data DOCUMENT ME!
-     *
-     * @return DOCUMENT ME!
-     */
-    public Text newTextNode(Document document, String data) {
-        return parser.newTextNode(document, data);
-    }
-
-    /**
-     * CDATA
-     *
-     * @param document DOM document
-     * @param data Text
-     *
-     * @return CDATASection
-     */
-    public CDATASection newCDATASection(Document document, String data) {
-        return parser.newCDATASection(document, data);
-    }
-
-    /**
-     * DOCUMENT ME!
-     *
-     * @param document DOCUMENT ME!
-     * @param data DOCUMENT ME!
-     *
-     * @return DOCUMENT ME!
-     */
-    public Comment newCommentNode(Document document, String data) {
-        return parser.newCommentNode(document, data);
-    }
-
-    /**
-     * Clone node, which means copy a node into another document
-     *
-     * @param document New document where original nodes shall be attached to
-     * @param original Original node from original document
-     * @param deep true means clone also all children
-     *
-     * @return New node, which is clone of original node
-     */
-    public Node cloneNode(Document document, Node original, boolean deep) {
-        Node node = null;
-        short nodeType = original.getNodeType();
-
-        switch (nodeType) {
-        case Node.ELEMENT_NODE: {
+   static Category log = Category.getInstance(DOMParserFactory.class);
+   public Parser parser = null;
+   /**
+    * Reads the properties and gets the parser
+    */
+   public DOMParserFactory() {
+      Properties properties = new Properties();
+      String propertiesFileName = "conf.properties";
+      try{
+         properties.load(DOMParserFactory.class.getResourceAsStream(propertiesFileName));
+      }catch(Exception e){
+         log.fatal(": Failed to load properties from resource: " + propertiesFileName);
+      }
+      String parserName = properties.getProperty("Parser");
+      if(parserName == null){
+         log.fatal(": No Parser specified in " + propertiesFileName);
+      }
+      try{
+         Class parserClass = Class.forName(parserName);
+         parser = (Parser) parserClass.newInstance();
+      }catch(Exception e){
+         log.fatal(": " + e);
+      }
+   }
+   /**
+    * DOCUMENT ME!
+    * 
+    * @param args
+    *           DOCUMENT ME!
+    */
+   public static void main(String[] args) {
+      DOMParserFactory dpf = new DOMParserFactory();
+      if(args.length != 1){
+         System.out.println("Usage: java " + dpf.getClass().getName() + " example.xml");
+         return;
+      }
+      // Document doc = null;
+      try{
+         // doc =
+         dpf.getDocument(args[0]);
+      }catch(FileNotFoundException e){
+         System.err.println("No such file or directory: " + e.getMessage());
+         return;
+      }catch(SAXException e){
+         System.err.println(e);
+         return;
+      }catch(Exception e){
+         System.err.println(e.getMessage());
+         return;
+      }
+   }
+   /**
+    * DOCUMENT ME!
+    * 
+    * @param filename
+    *           DOCUMENT ME!
+    * 
+    * @return DOCUMENT ME!
+    * 
+    * @throws FileNotFoundException
+    *            DOCUMENT ME!
+    * @throws Exception
+    *            DOCUMENT ME!
+    */
+   public Document getDocument(String filename) throws FileNotFoundException, Exception {
+      File file = new File(filename);
+      if(!file.exists()){
+         log.error("No such file or directory: " + filename);
+         throw new FileNotFoundException(filename);
+      }
+      return parser.getDocument(filename);
+   }
+   /**
+    * DOCUMENT ME!
+    * 
+    * @param inputStream
+    *           DOCUMENT ME!
+    * 
+    * @return DOCUMENT ME!
+    * 
+    * @throws Exception
+    *            DOCUMENT ME!
+    */
+   public Document getDocument(InputStream inputStream) throws Exception {
+      return parser.getDocument(inputStream);
+   }
+   /**
+    * Create a document from a reader.
+    * 
+    * @param inputStream
+    *           DOCUMENT ME!
+    * @return DOCUMENT ME!
+    * @throws Exception
+    *            DOCUMENT ME!
+    */
+   public Document getDocument(Reader reader) throws Exception {
+      return parser.getDocument(reader);
+   }
+   /**
+    * DOCUMENT ME!
+    * 
+    * @return DOCUMENT ME!
+    */
+   public Document getDocument() {
+      return parser.getDocument();
+   }
+   /**
+    * DOCUMENT ME!
+    * 
+    * @param document
+    *           DOCUMENT ME!
+    * @param name
+    *           DOCUMENT ME!
+    * 
+    * @return DOCUMENT ME!
+    */
+   public Element newElementNode(Document document, String name) {
+      return parser.newElementNode(document, name);
+   }
+   /**
+    * Creates an element with namespace support.
+    * 
+    * @param document
+    *           The owner document.
+    * @param namespaceUri
+    *           The namespace URI of the element.
+    * @param qualifiedName
+    *           The qualified name of the element.
+    * 
+    * @return An element.
+    */
+   public Element newElementNSNode(Document document, String namespaceUri, String qualifiedName) {
+      return parser.newElementNSNode(document, namespaceUri, qualifiedName);
+   }
+   /**
+    * DOCUMENT ME!
+    * 
+    * @param document
+    *           DOCUMENT ME!
+    * @param data
+    *           DOCUMENT ME!
+    * 
+    * @return DOCUMENT ME!
+    */
+   public Text newTextNode(Document document, String data) {
+      return parser.newTextNode(document, data);
+   }
+   /**
+    * CDATA
+    * 
+    * @param document
+    *           DOM document
+    * @param data
+    *           Text
+    * 
+    * @return CDATASection
+    */
+   public CDATASection newCDATASection(Document document, String data) {
+      return parser.newCDATASection(document, data);
+   }
+   /**
+    * DOCUMENT ME!
+    * 
+    * @param document
+    *           DOCUMENT ME!
+    * @param data
+    *           DOCUMENT ME!
+    * 
+    * @return DOCUMENT ME!
+    */
+   public Comment newCommentNode(Document document, String data) {
+      return parser.newCommentNode(document, data);
+   }
+   /**
+    * Clone node, which means copy a node into another document
+    * 
+    * @param document
+    *           New document where original nodes shall be attached to
+    * @param original
+    *           Original node from original document
+    * @param deep
+    *           true means clone also all children
+    * 
+    * @return New node, which is clone of original node
+    */
+   public Node cloneNode(Document document, Node original, boolean deep) {
+      Node node = null;
+      short nodeType = original.getNodeType();
+      switch(nodeType){
+         case Node.ELEMENT_NODE: {
             Element element = newElementNSNode(document, original.getNamespaceURI(), original.getNodeName());
             log.debug(".cloneNode(): Clone element: " + original.getNodeName());
             NamedNodeMap attributes = original.getAttributes();
-
-            for (int i = 0; i < attributes.getLength(); i++) {
-                Node attribute = attributes.item(i);
-                log.debug(".cloneNode(): LocalName: " + attribute.getLocalName() + ", Prefix: " + attribute.getPrefix() + ", NamespaceURI: " + attribute.getNamespaceURI());
-                element.setAttributeNS(attribute.getNamespaceURI(), attribute.getNodeName(), attribute.getNodeValue());
+            for(int i = 0; i < attributes.getLength(); i++){
+               Node attribute = attributes.item(i);
+               log.debug(".cloneNode(): LocalName: " + attribute.getLocalName() + ", Prefix: " + attribute.getPrefix() + ", NamespaceURI: " + attribute.getNamespaceURI());
+               element.setAttributeNS(attribute.getNamespaceURI(), attribute.getNodeName(), attribute.getNodeValue());
             }
-
             node = element;
-
             break;
-        }
-
-        case Node.TEXT_NODE: {
+         }
+         case Node.TEXT_NODE: {
             Text text = newTextNode(document, original.getNodeValue());
-
             node = text;
-
             break;
-        }
-
-        case Node.CDATA_SECTION_NODE: {
+         }
+         case Node.CDATA_SECTION_NODE: {
             CDATASection cdata = newCDATASection(document, original.getNodeValue());
-
             node = cdata;
-
             break;
-        }
-
-        case Node.COMMENT_NODE: {
+         }
+         case Node.COMMENT_NODE: {
             Comment comment = newCommentNode(document, original.getNodeValue());
-
             node = comment;
-
             break;
-        }
-
-        default:
+         }
+         default:
             log.warn(".cloneNode(): Node type not implemented: " + nodeType);
             break;
-        }
-
-        if (deep && original.hasChildNodes()) {
-            NodeList nl = original.getChildNodes();
-
-            for (int i = 0; i < nl.getLength(); i++) {
-                node.appendChild(cloneNode(document, nl.item(i), deep));
-            }
-        }
-
-        return node;
-    }
-
-    /**
-     * DOCUMENT ME!
-     *
-     * @param document DOCUMENT ME!
-     * @param element DOCUMENT ME!
-     * @param value DOCUMENT ME!
-     */
-    public void setElementValue(Document document, Element element, String value) {
-        // remove all child nodes
-        NodeList nl = element.getChildNodes();
-
-        for (int i = 0; i < nl.getLength(); i++) {
-            try {
-                element.removeChild(nl.item(i));
-            } catch (Exception e) {
-                System.err.println("EXCEPTION: " + this.getClass().getName() +
-                    ".setElementValue(): " + e);
-            }
-        }
-
-        // add a new TextNode for storing the new value
-        element.appendChild(newTextNode(document, value));
-    }
+      }
+      if(deep && original.hasChildNodes()){
+         NodeList nl = original.getChildNodes();
+         for(int i = 0; i < nl.getLength(); i++){
+            node.appendChild(cloneNode(document, nl.item(i), deep));
+         }
+      }
+      return node;
+   }
+   /**
+    * DOCUMENT ME!
+    * 
+    * @param document
+    *           DOCUMENT ME!
+    * @param element
+    *           DOCUMENT ME!
+    * @param value
+    *           DOCUMENT ME!
+    */
+   public void setElementValue(Document document, Element element, String value) {
+      // remove all child nodes
+      NodeList nl = element.getChildNodes();
+      for(int i = 0; i < nl.getLength(); i++){
+         try{
+            element.removeChild(nl.item(i));
+         }catch(Exception e){
+            System.err.println("EXCEPTION: " + this.getClass().getName() + ".setElementValue(): " + e);
+         }
+      }
+      // add a new TextNode for storing the new value
+      element.appendChild(newTextNode(document, value));
+   }
 }

Modified: lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/xml/DOMUtil.java
URL: http://svn.apache.org/viewvc/lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/xml/DOMUtil.java?rev=617035&r1=617034&r2=617035&view=diff
==============================================================================
--- lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/xml/DOMUtil.java (original)
+++ lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/xml/DOMUtil.java Wed Jan 30 23:44:03 2008
@@ -14,501 +14,447 @@
  *  limitations under the License.
  *
  */
-
 /* $Id$  */
-
 package org.apache.lenya.xml;
-
 import java.io.StringReader;
 import java.util.Vector;
-
-import org.apache.log4j.Category;
+import org.apache.log4j.Logger;
 import org.w3c.dom.Attr;
 import org.w3c.dom.Document;
 import org.w3c.dom.Element;
 import org.w3c.dom.Node;
 import org.w3c.dom.NodeList;
-
 /**
- * This class is a utility class for miscellaneous DOM functions, similar to
- * org.apache.cocoon.xml.dom.DOMUtil FIXME: Merge classes or extend functionality
+ * This class is a utility class for miscellaneous DOM functions, similar to org.apache.cocoon.xml.dom.DOMUtil FIXME: Merge classes or extend functionality
  */
 public class DOMUtil {
-    static Category log = Category.getInstance(DOMUtil.class);
-
-    DOMParserFactory dpf = null;
-
-    XPointerFactory xpf = null;
-
-    /**
-     * Ctor.
-     */
-    public DOMUtil() {
-        dpf = new DOMParserFactory();
-        xpf = new XPointerFactory();
-    }
-
-    /**
-     * Main method, used to test the class.
-     * @param args The command line arguments.
-     */
-    public static void main(String[] args) {
-        try {
-            DOMUtil du = new DOMUtil();
-            Document document = du
-                    .create("<?xml version=\"1.0\"?><Artikel><Datum><Monat Name=\"Juli\"/><Tag>23</Tag></Datum><Content/></Artikel>");
-            new DOMWriter(System.out).printWithoutFormatting(document);
-            du.setElementValue(document, "/Artikel/Datum/Tag", "25");
-            du.setElementValue(document, "/Artikel/Datum/Monat", "7");
-            du.setElementValue(document, "/Artikel/Datum/Monat", "9");
-            du.setElementValue(document, "/Artikel/Datm/Mont", "13");
-            du.setAttributeValue(document, "/Artikel/Datum/Monat/@Name", "Oktober");
-            du.setAttributeValue(document, "/Artikel/Datu/Monat/@Nam", "August");
-            du.setElementValue(document, "/Artikel/Datu/Monat", "8");
-            du.addElement(document, "/Artikel/Datum/Tag", "26");
-            du.setElementValue(document, "/Artikel/Datum/Tag", "24");
-
-            new DOMWriter(System.out).printWithoutFormatting(document);
-            System.out.print("\n");
-            System.out.print("\n");
-
-            String[] elements = du.getAllElementValues(document, new XPath("/Artikel/Datum/Monat"));
-
-            for (int i = 0; i < elements.length; i++) {
-                System.out.println("Elements=" + elements[i]);
+   private static Logger log = Logger.getLogger(DOMUtil.class);
+   DOMParserFactory dpf = null;
+   XPointerFactory xpf = null;
+   /**
+    * Ctor.
+    */
+   public DOMUtil() {
+      dpf = new DOMParserFactory();
+      xpf = new XPointerFactory();
+   }
+   /**
+    * Main method, used to test the class.
+    * 
+    * @param args
+    *           The command line arguments.
+    */
+   public static void main(String[] args) {
+      try{
+         DOMUtil du = new DOMUtil();
+         Document document = du.create("<?xml version=\"1.0\"?><Artikel><Datum><Monat Name=\"Juli\"/><Tag>23</Tag></Datum><Content/></Artikel>");
+         new DOMWriter(System.out).printWithoutFormatting(document);
+         du.setElementValue(document, "/Artikel/Datum/Tag", "25");
+         du.setElementValue(document, "/Artikel/Datum/Monat", "7");
+         du.setElementValue(document, "/Artikel/Datum/Monat", "9");
+         du.setElementValue(document, "/Artikel/Datm/Mont", "13");
+         du.setAttributeValue(document, "/Artikel/Datum/Monat/@Name", "Oktober");
+         du.setAttributeValue(document, "/Artikel/Datu/Monat/@Nam", "August");
+         du.setElementValue(document, "/Artikel/Datu/Monat", "8");
+         du.addElement(document, "/Artikel/Datum/Tag", "26");
+         du.setElementValue(document, "/Artikel/Datum/Tag", "24");
+         new DOMWriter(System.out).printWithoutFormatting(document);
+         System.out.print("\n");
+         System.out.print("\n");
+         String[] elements = du.getAllElementValues(document, new XPath("/Artikel/Datum/Monat"));
+         for(int i = 0; i < elements.length; i++){
+            System.out.println("Elements=" + elements[i]);
+         }
+         System.out.print("\n");
+         System.out.println("Datum/Monat=" + du.getElementValue(document.getDocumentElement(), new XPath("Datum/Monat")));
+         System.out.println("Datm=" + du.getElementValue(document.getDocumentElement(), new XPath("Datm")));
+         System.out.println("Datum/Monat/@Name=" + du.getAttributeValue(document.getDocumentElement(), new XPath("Datum/Monat/@Name")));
+      }catch(Exception e){
+         System.err.println(e);
+      }
+   }
+   /**
+    * Creates a DOM document from a string.
+    * 
+    * @param xml
+    *           The string.
+    * @return A DOM document.
+    * @throws Exception
+    *            if an error occurs.
+    * 
+    * @deprecated Use {@link DocumentHelper#readDocument(java.lang.String)} instead.
+    */
+   public Document create(String xml) throws Exception {
+      return dpf.getDocument(new StringReader(xml));
+   }
+   /**
+    * Selects an array of elements using an XPath.
+    * 
+    * @param document
+    *           The document.
+    * @param xpath
+    *           The XPath.
+    * @return An array of elements.
+    * @throws Exception
+    *            if the XPath does not return a <code>NodeList</code> consisting of elements.
+    * 
+    * @deprecated Use {@link org.apache.xpath.XPathAPI#selectNodeList(org.w3c.dom.Node, java.lang.String)} instead.
+    */
+   public Element[] select(Document document, String xpath) throws Exception {
+      log.debug(".select(): " + xpath);
+      Vector nodes = xpf.select(document, "xpointer(" + xpath + ")");
+      Element[] elements = new Element[nodes.size()];
+      for(int i = 0; i < nodes.size(); i++){
+         elements[i] = (Element) nodes.elementAt(i);
+      }
+      return elements;
+   }
+   /**
+    * <p>
+    * This method removes all child nodes from an element and inserts a text node instead.
+    * </p>
+    * <p>
+    * Caution: Child elements are removed as well!
+    * </p>
+    * 
+    * @param element
+    *           The element.
+    * @param text
+    *           The string to insert as a text node.
+    */
+   public void replaceText(Element element, String text) {
+      NodeList nl = element.getChildNodes();
+      for(int i = nl.getLength() - 1; i >= 0; i--){
+         element.removeChild(nl.item(i));
+      }
+      element.appendChild(dpf.newTextNode(element.getOwnerDocument(), text));
+   }
+   /**
+    * Returns the concatenation string of all text nodes which are children of an element. The XPath is resolved against the document element.
+    * 
+    * @param document
+    *           The document.
+    * @param xpath
+    *           The XPath of the element to resolve.
+    * @return A string.
+    * @throws Exception
+    *            if an error occurs.
+    * 
+    * @deprecated Use {@link DocumentHelper#getSimpleElementText(org.w3c.dom.Element) instead.}
+    */
+   public String getElementValue(Document document, XPath xpath) throws Exception {
+      return getElementValue(document.getDocumentElement(), xpath);
+   }
+   /**
+    * Returns the concatenation string of all text nodes which are children of an element. The XPath is resolved against a certain element.
+    * 
+    * @param element
+    *           The element to resolve the XPath against.
+    * @param xpath
+    *           The XPath of the element to resolve.
+    * @return A string.
+    * @throws Exception
+    *            if an error occurs.
+    * 
+    * @deprecated Use {@link DocumentHelper#getSimpleElementText(org.w3c.dom.Element) instead.}
+    */
+   public String getElementValue(Element element, XPath xpath) throws Exception {
+      String value = "";
+      NodeList nl = getElement(element, xpath).getChildNodes();
+      for(int i = 0; i < nl.getLength(); i++){
+         short nodeType = nl.item(i).getNodeType();
+         if(nodeType == Node.TEXT_NODE){
+            value = value + nl.item(i).getNodeValue();
+         }else{
+            log.warn("XPath " + xpath + " contains node types other than just TEXT_NODE");
+         }
+      }
+      return value;
+   }
+   /**
+    * Check if elements exists This method just checks the root element! TODO: Implementation is not really finished, or is it!
+    * 
+    * Replacement code:
+    * 
+    * <code>
+    * Node node = XPathAPI.selectSingleNode(element, xPath);
+    * if (node != null && node instanceof Element) {
+    *     exists = true;
+    * }
+    * </code>
+    * 
+    * @deprecated See replacement code.
+    */
+   public boolean elementExists(Element element, XPath xpath) throws Exception {
+      log.debug(xpath);
+      if(xpath.parts.length > 0){
+         NodeList nl = element.getElementsByTagName(xpath.parts[0]);
+         if(nl.getLength() == 0){
+            return false;
+         }else if(nl.getLength() == 1){
+            return true;
+         }
+      }
+      return false;
+   }
+   /**
+    * Get element via XPath.
+    * 
+    * @deprecated Use {@link org.apache.xpath.XPathAPI#selectSingleNode(org.w3c.dom.Node, java.lang.String)} instead.
+    */
+   public Element getElement(Element element, XPath xpath) throws Exception {
+      log.debug(xpath);
+      if(xpath.parts.length > 0){
+         NodeList nl = element.getElementsByTagName(xpath.parts[0]);
+         if(nl.getLength() == 0){
+            throw new Exception("There are no elements with Name \"" + xpath.parts[0] + "\".");
+         }else if(nl.getLength() == 1){
+            log.debug("There is one element with Name \"" + xpath.parts[0] + "\" (" + xpath.parts.length + ").");
+            if(xpath.parts.length == 1){
+               return (Element) nl.item(0);
+            }else{
+               String newXPathString = xpath.parts[1];
+               for(int i = 2; i < xpath.parts.length; i++){
+                  newXPathString = newXPathString + "/" + xpath.parts[i];
+               }
+               return getElement((Element) nl.item(0), new XPath(newXPathString));
             }
-
-            System.out.print("\n");
-            System.out.println("Datum/Monat="
-                    + du.getElementValue(document.getDocumentElement(), new XPath("Datum/Monat")));
-            System.out.println("Datm="
-                    + du.getElementValue(document.getDocumentElement(), new XPath("Datm")));
-
-            System.out.println("Datum/Monat/@Name="
-                    + du.getAttributeValue(document.getDocumentElement(), new XPath(
-                            "Datum/Monat/@Name")));
-        } catch (Exception e) {
-            System.err.println(e);
-        }
-    }
-
-    /**
-     * Creates a DOM document from a string.
-     * 
-     * @param xml The string.
-     * @return A DOM document.
-     * @throws Exception if an error occurs.
-     * 
-     * @deprecated Use {@link DocumentHelper#readDocument(java.lang.String)} instead.
-     */
-    public Document create(String xml) throws Exception {
-        return dpf.getDocument(new StringReader(xml));
-    }
-
-    /**
-     * Selects an array of elements using an XPath.
-     * 
-     * @param document The document.
-     * @param xpath The XPath.
-     * @return An array of elements.
-     * @throws Exception if the XPath does not return a <code>NodeList</code> consisting of elements.
-     * 
-     * @deprecated Use
-     *             {@link org.apache.xpath.XPathAPI#selectNodeList(org.w3c.dom.Node, java.lang.String)}
-     *             instead.
-     */
-    public Element[] select(Document document, String xpath) throws Exception {
-        log.debug(".select(): " + xpath);
-
-        Vector nodes = xpf.select(document, "xpointer(" + xpath + ")");
-        Element[] elements = new Element[nodes.size()];
-
-        for (int i = 0; i < nodes.size(); i++) {
-            elements[i] = (Element) nodes.elementAt(i);
-        }
-
-        return elements;
-    }
-
-    /**
-     * <p>
-     * This method removes all child nodes from an element and inserts a text node instead.
-     * </p>
-     * <p>
-     * Caution: Child elements are removed as well!
-     * </p>
-     * @param element The element.
-     * @param text The string to insert as a text node.
-     */
-    public void replaceText(Element element, String text) {
-        NodeList nl = element.getChildNodes();
-
-        for (int i = nl.getLength() - 1; i >= 0; i--) {
-            element.removeChild(nl.item(i));
-        }
-
-        element.appendChild(dpf.newTextNode(element.getOwnerDocument(), text));
-    }
-
-    /**
-     * Returns the concatenation string of all text nodes which are children of an element.
-     * The XPath is resolved against the document element.
-     * 
-     * @param document The document.
-     * @param xpath The XPath of the element to resolve.
-     * @return A string.
-     * @throws Exception if an error occurs.
-     * 
-     * @deprecated Use {@link DocumentHelper#getSimpleElementText(org.w3c.dom.Element) instead.}
-     */
-    public String getElementValue(Document document, XPath xpath) throws Exception {
-        return getElementValue(document.getDocumentElement(), xpath);
-    }
-
-    /**
-     * Returns the concatenation string of all text nodes which are children of an element.
-     * The XPath is resolved against a certain element.
-     * 
-     * @param element The element to resolve the XPath against.
-     * @param xpath The XPath of the element to resolve.
-     * @return A string.
-     * @throws Exception if an error occurs.
-     * 
-     * @deprecated Use {@link DocumentHelper#getSimpleElementText(org.w3c.dom.Element) instead.}
-     */
-    public String getElementValue(Element element, XPath xpath) throws Exception {
-        String value = "";
-        NodeList nl = getElement(element, xpath).getChildNodes();
-
-        for (int i = 0; i < nl.getLength(); i++) {
-            short nodeType = nl.item(i).getNodeType();
-
-            if (nodeType == Node.TEXT_NODE) {
-                value = value + nl.item(i).getNodeValue();
-            } else {
-                log.warn("XPath " + xpath + " contains node types other than just TEXT_NODE");
-            }
-        }
-
-        return value;
-    }
-
-    /**
-     * Check if elements exists This method just checks the root element! TODO: Implementation is
-     * not really finished, or is it!
-     * 
-     * Replacement code:
-     * 
-     * <code>
-     * Node node = XPathAPI.selectSingleNode(element, xPath);
-     * if (node != null && node instanceof Element) {
-     *     exists = true;
-     * }
-     * </code>
-     * 
-     * @deprecated See replacement code.
-     */
-    public boolean elementExists(Element element, XPath xpath) throws Exception {
-        log.debug(xpath);
-
-        if (xpath.parts.length > 0) {
-            NodeList nl = element.getElementsByTagName(xpath.parts[0]);
-
-            if (nl.getLength() == 0) {
-                return false;
-            } else if (nl.getLength() == 1) {
-                return true;
-            }
-        }
-        return false;
-    }
-
-    /**
-     * Get element via XPath.
-     * 
-     * @deprecated Use
-     *             {@link org.apache.xpath.XPathAPI#selectSingleNode(org.w3c.dom.Node, java.lang.String)}
-     *             instead.
-     */
-    public Element getElement(Element element, XPath xpath) throws Exception {
-        log.debug(xpath);
-
-        if (xpath.parts.length > 0) {
-            NodeList nl = element.getElementsByTagName(xpath.parts[0]);
-
-            if (nl.getLength() == 0) {
-                throw new Exception("There are no elements with Name \"" + xpath.parts[0] + "\".");
-            } else if (nl.getLength() == 1) {
-                log.debug("There is one element with Name \"" + xpath.parts[0] + "\" ("
-                        + xpath.parts.length + ").");
-
-                if (xpath.parts.length == 1) {
-                    return (Element) nl.item(0);
-                } else {
-                    String newXPathString = xpath.parts[1];
-
-                    for (int i = 2; i < xpath.parts.length; i++) {
-                        newXPathString = newXPathString + "/" + xpath.parts[i];
-                    }
-
-                    return getElement((Element) nl.item(0), new XPath(newXPathString));
-                }
-            } else {
-                throw new Exception("There are more elements than one with Name \""
-                        + xpath.parts[0] + "\".");
-            }
-        }
-
-        return null;
-    }
-
-    /**
-     * get all elements with |xpath|, xpath has to start with the root node
-     * 
-     * @param document a value of type 'Document'
-     * @param xpath a value of type 'XPath'
-     * 
-     * @return a value of type 'Element[]'
-     * 
-     * @exception Exception if an error occurs
-     * 
-     * @deprecated Use
-     *             {@link org.apache.xpath.XPathAPI#selectNodeList(org.w3c.dom.Node, java.lang.String)}
-     *             instead.
-     */
-    public Element[] getAllElements(Document document, XPath xpath) throws Exception {
-        Vector nodes = xpf.select(document.getDocumentElement(), "xpointer(" + xpath.toString()
-                + ")");
-        Element[] elements = new Element[nodes.size()];
-
-        for (int i = 0; i < nodes.size(); i++) {
-            elements[i] = (Element) nodes.elementAt(i);
-        }
-
-        return elements;
-    }
-
-    /**
-     * get all elements values from |xpath|, xpath has to start with the root node
-     * 
-     * @param document a value of type 'Document'
-     * @param xpath a value of type 'XPath'
-     * 
-     * @return a value of type 'String[]'
-     * 
-     * @exception Exception if an error occurs
-     */
-    public String[] getAllElementValues(Document document, XPath xpath) throws Exception {
-        Vector nodes = xpf.select(document.getDocumentElement(), "xpointer(" + xpath.toString()
-                + ")");
-        log.debug("n elements " + nodes.size());
-
-        String[] values = new String[nodes.size()];
-
-        for (int i = 0; i < nodes.size(); i++) {
-            values[i] = getElementValue((Element) nodes.elementAt(i));
-        }
-
-        return values;
-    }
-
-    /**
-     * Returns the concatenation string of all text nodes which are children of an element.
-     * 
-     * @param element The element.
-     * @return A string.
-     * @throws Exception if an error occurs.
-     * 
-     * Replacement code:
-     * 
-     * <code>
-     * Element element = (Element) XPathAPI.selectSingleNode(document, xPath);
-     * String value = DocumentHelper.getSimpleElementText(element, "...");
-     * </code>
-     * 
-     * @deprecated See replacement code.
-     */
-    public String getElementValue(Element element) throws Exception {
-        String value = "";
-        NodeList nl = element.getChildNodes();
-
-        for (int i = 0; i < nl.getLength(); i++) {
-            short nodeType = nl.item(i).getNodeType();
-
-            if (nodeType == Node.TEXT_NODE) {
-                value = value + nl.item(i).getNodeValue();
-            } else {
-                log.warn("Element " + element.getNodeName()
-                        + " contains node types other than just TEXT_NODE");
-            }
-        }
-
-        return value;
-    }
-
-    /**
-     * Return the value of an attribte named e.g. "this/myelement/(at)myattribute"
-     * 
-     * @param element a value of type 'Element'
-     * @param xpath a value of type 'XPath'
-     * 
-     * @return a value of type 'String'
-     * 
-     * Replacement code:
-     * 
-     * <code>
-     * Element element = (Element) XPathAPI.selectSingleNode(document, xPath);
-     * String value = element.getAttribute("...");
-     * </code>
-     * 
-     * @deprecated See replacement code.
-     */
-    public String getAttributeValue(Element element, XPath xpath) throws Exception {
-        Element el = getElement(element, new XPath(xpath.getElementName()));
-
-        return el.getAttribute(xpath.getName());
-    }
-
-    /**
-     * Describe 'setElementValue' method here.
-     * 
-     * @param document a value of type 'Document'
-     * @param xpath a value of type 'String'
-     * @param value a value of type 'String'
-     * 
-     * @exception Exception if an error occurs
-     * 
-     * Replacement code:
-     * 
-     * <code>
-     * Element element = (Element) XPathAPI.selectSingleNode(document, xPath);
-     * DocumentHelper.setSimpleElementText(element, "...");
-     * </code>
-     * 
-     * @deprecated See replacement code.
-     */
-    public void setElementValue(Document document, String xpath, String value) throws Exception {
-        Element[] elements = select(document, xpath);
-
-        if (elements.length >= 1) {
-            if (elements.length > 1) {
-                log.warn("There are more elements than one with XPath \"" + xpath
-                        + "\". The value of the first element will be replaced");
-            }
-
-            replaceText(elements[0], value);
-        } else {
-            XPath xp = new XPath(xpath);
-            log.warn("XPath does not exist, but will be created: " + xp);
-
-            Element element = (Element) createNode(document, xp);
-            replaceText(element, value);
-        }
-    }
-
-    /**
-     * Replacement code:
-     * 
-     * <code>
-     * Element parent = (Element) XPathAPI.selectSingleNode(document, xPath);
-     * Element child = NamespaceHelper.createElement("...", "...");
-     * parent.appendChild(child);
-     * </code>
-     * 
-     * @deprecated See replacement code.
-     */
-    public void addElement(Document document, String xpath, String value) throws Exception {
-        XPath xp = new XPath(xpath);
-        Node parent = createNode(document, xp.getParent());
-        Element element = dpf.newElementNode(document, xp.getName());
-        parent.appendChild(element);
-
-        if (value != null) {
-            element.appendChild(dpf.newTextNode(element.getOwnerDocument(), value));
-        }
-    }
-
-    /**
-     * Replacement code:
-     * 
-     * <code>
-     * Element element = (Element) XPathAPI.selectSingleNode(document, xPath);
-     * element.setAttribute("...", "...");
-     * </code>
-     * 
-     * @deprecated See replacement code.
-     */
-    public void setAttributeValue(Document document, String xpath, String value) throws Exception {
-        Vector nodes = xpf.select(document, "xpointer(" + xpath + ")");
-
-        if (nodes.size() >= 1) {
-            Attr attribute = (Attr) nodes.elementAt(0);
-            attribute.setValue(value);
-        } else {
-            XPath xp = new XPath(xpath);
-            log.debug("XPath does not exist, but will be created: " + xp);
-
-            Attr attribute = (Attr) createNode(document, xp);
-            attribute.setValue(value);
-        }
-    }
-
-    /**
-     * <ul>
-     * <li>If the XPath expression denotes an element, the child nodes of this element are replaced by a single
-     * text node.</li>
-     * <li>If the XPath expression denotes an attribute, the attribute value is set.</li>
-     * <li>Otherwise, an error is logged.</li>
-     * </ul>
-     * 
-     * @param document The document to resolve the XPath against.
-     * @param xpath The XPath.
-     * @param value A string.
-     * @throws Exception if an error occurs.
-     */
-    public void setValue(Document document, XPath xpath, String value) throws Exception {
-        short type = xpath.getType();
-
-        if (type == Node.ATTRIBUTE_NODE) {
-            setAttributeValue(document, xpath.toString(), value);
-        } else if (type == Node.ELEMENT_NODE) {
-            setElementValue(document, xpath.toString(), value);
-        } else {
-            log.error("No such type: " + type);
-        }
-    }
-
-    /**
-     * Replacement code:
-     * 
-     * <code>
-     * Element parent = XPathAPI.selectSingleNode(...);
-     * Element child = document.createElementNS("http://...", "...");
-     * parent.appendChild(child);
-     * </code>
-     * 
-     * @deprecated See replacement code.
-     */
-    public Node createNode(Document document, XPath xpath) throws Exception {
-        log.debug(xpath);
-
-        Node node = null;
-        Vector nodes = xpf.select(document, "xpointer(" + xpath + ")");
-
-        if (nodes.size() >= 1) {
-            node = (Node) nodes.elementAt(0);
-        } else {
-            Node parentNode = createNode(document, xpath.getParent());
-
-            if (xpath.getType() == Node.ATTRIBUTE_NODE) {
-                ((Element) parentNode).setAttribute(xpath.getNameWithoutPredicates(), "null");
-                node = ((Element) parentNode).getAttributeNode(xpath.getNameWithoutPredicates());
-            } else {
-                node = dpf.newElementNode(document, xpath.getNameWithoutPredicates());
-                parentNode.appendChild(node);
-            }
-        }
-
-        return node;
-    }
+         }else{
+            throw new Exception("There are more elements than one with Name \"" + xpath.parts[0] + "\".");
+         }
+      }
+      return null;
+   }
+   /**
+    * get all elements with |xpath|, xpath has to start with the root node
+    * 
+    * @param document
+    *           a value of type 'Document'
+    * @param xpath
+    *           a value of type 'XPath'
+    * 
+    * @return a value of type 'Element[]'
+    * 
+    * @exception Exception
+    *               if an error occurs
+    * 
+    * @deprecated Use {@link org.apache.xpath.XPathAPI#selectNodeList(org.w3c.dom.Node, java.lang.String)} instead.
+    */
+   public Element[] getAllElements(Document document, XPath xpath) throws Exception {
+      Vector nodes = xpf.select(document.getDocumentElement(), "xpointer(" + xpath.toString() + ")");
+      Element[] elements = new Element[nodes.size()];
+      for(int i = 0; i < nodes.size(); i++){
+         elements[i] = (Element) nodes.elementAt(i);
+      }
+      return elements;
+   }
+   /**
+    * get all elements values from |xpath|, xpath has to start with the root node
+    * 
+    * @param document
+    *           a value of type 'Document'
+    * @param xpath
+    *           a value of type 'XPath'
+    * 
+    * @return a value of type 'String[]'
+    * 
+    * @exception Exception
+    *               if an error occurs
+    */
+   public String[] getAllElementValues(Document document, XPath xpath) throws Exception {
+      Vector nodes = xpf.select(document.getDocumentElement(), "xpointer(" + xpath.toString() + ")");
+      log.debug("n elements " + nodes.size());
+      String[] values = new String[nodes.size()];
+      for(int i = 0; i < nodes.size(); i++){
+         values[i] = getElementValue((Element) nodes.elementAt(i));
+      }
+      return values;
+   }
+   /**
+    * Returns the concatenation string of all text nodes which are children of an element.
+    * 
+    * @param element
+    *           The element.
+    * @return A string.
+    * @throws Exception
+    *            if an error occurs.
+    * 
+    * Replacement code:
+    * 
+    * <code>
+    * Element element = (Element) XPathAPI.selectSingleNode(document, xPath);
+    * String value = DocumentHelper.getSimpleElementText(element, "...");
+    * </code>
+    * 
+    * @deprecated See replacement code.
+    */
+   public String getElementValue(Element element) throws Exception {
+      String value = "";
+      NodeList nl = element.getChildNodes();
+      for(int i = 0; i < nl.getLength(); i++){
+         short nodeType = nl.item(i).getNodeType();
+         if(nodeType == Node.TEXT_NODE){
+            value = value + nl.item(i).getNodeValue();
+         }else{
+            log.warn("Element " + element.getNodeName() + " contains node types other than just TEXT_NODE");
+         }
+      }
+      return value;
+   }
+   /**
+    * Return the value of an attribte named e.g. "this/myelement/(at)myattribute"
+    * 
+    * @param element
+    *           a value of type 'Element'
+    * @param xpath
+    *           a value of type 'XPath'
+    * 
+    * @return a value of type 'String'
+    * 
+    * Replacement code:
+    * 
+    * <code>
+    * Element element = (Element) XPathAPI.selectSingleNode(document, xPath);
+    * String value = element.getAttribute("...");
+    * </code>
+    * 
+    * @deprecated See replacement code.
+    */
+   public String getAttributeValue(Element element, XPath xpath) throws Exception {
+      Element el = getElement(element, new XPath(xpath.getElementName()));
+      return el.getAttribute(xpath.getName());
+   }
+   /**
+    * Describe 'setElementValue' method here.
+    * 
+    * @param document
+    *           a value of type 'Document'
+    * @param xpath
+    *           a value of type 'String'
+    * @param value
+    *           a value of type 'String'
+    * 
+    * @exception Exception
+    *               if an error occurs
+    * 
+    * Replacement code:
+    * 
+    * <code>
+    * Element element = (Element) XPathAPI.selectSingleNode(document, xPath);
+    * DocumentHelper.setSimpleElementText(element, "...");
+    * </code>
+    * 
+    * @deprecated See replacement code.
+    */
+   public void setElementValue(Document document, String xpath, String value) throws Exception {
+      Element[] elements = select(document, xpath);
+      if(elements.length >= 1){
+         if(elements.length > 1){
+            log.warn("There are more elements than one with XPath \"" + xpath + "\". The value of the first element will be replaced");
+         }
+         replaceText(elements[0], value);
+      }else{
+         XPath xp = new XPath(xpath);
+         log.warn("XPath does not exist, but will be created: " + xp);
+         Element element = (Element) createNode(document, xp);
+         replaceText(element, value);
+      }
+   }
+   /**
+    * Replacement code:
+    * 
+    * <code>
+    * Element parent = (Element) XPathAPI.selectSingleNode(document, xPath);
+    * Element child = NamespaceHelper.createElement("...", "...");
+    * parent.appendChild(child);
+    * </code>
+    * 
+    * @deprecated See replacement code.
+    */
+   public void addElement(Document document, String xpath, String value) throws Exception {
+      XPath xp = new XPath(xpath);
+      Node parent = createNode(document, xp.getParent());
+      Element element = dpf.newElementNode(document, xp.getName());
+      parent.appendChild(element);
+      if(value != null){
+         element.appendChild(dpf.newTextNode(element.getOwnerDocument(), value));
+      }
+   }
+   /**
+    * Replacement code:
+    * 
+    * <code>
+    * Element element = (Element) XPathAPI.selectSingleNode(document, xPath);
+    * element.setAttribute("...", "...");
+    * </code>
+    * 
+    * @deprecated See replacement code.
+    */
+   public void setAttributeValue(Document document, String xpath, String value) throws Exception {
+      Vector nodes = xpf.select(document, "xpointer(" + xpath + ")");
+      if(nodes.size() >= 1){
+         Attr attribute = (Attr) nodes.elementAt(0);
+         attribute.setValue(value);
+      }else{
+         XPath xp = new XPath(xpath);
+         log.debug("XPath does not exist, but will be created: " + xp);
+         Attr attribute = (Attr) createNode(document, xp);
+         attribute.setValue(value);
+      }
+   }
+   /**
+    * <ul>
+    * <li>If the XPath expression denotes an element, the child nodes of this element are replaced by a single text node.</li>
+    * <li>If the XPath expression denotes an attribute, the attribute value is set.</li>
+    * <li>Otherwise, an error is logged.</li>
+    * </ul>
+    * 
+    * @param document
+    *           The document to resolve the XPath against.
+    * @param xpath
+    *           The XPath.
+    * @param value
+    *           A string.
+    * @throws Exception
+    *            if an error occurs.
+    */
+   public void setValue(Document document, XPath xpath, String value) throws Exception {
+      short type = xpath.getType();
+      if(type == Node.ATTRIBUTE_NODE){
+         setAttributeValue(document, xpath.toString(), value);
+      }else if(type == Node.ELEMENT_NODE){
+         setElementValue(document, xpath.toString(), value);
+      }else{
+         log.error("No such type: " + type);
+      }
+   }
+   /**
+    * Replacement code:
+    * 
+    * <code>
+    * Element parent = XPathAPI.selectSingleNode(...);
+    * Element child = document.createElementNS("http://...", "...");
+    * parent.appendChild(child);
+    * </code>
+    * 
+    * @deprecated See replacement code.
+    */
+   public Node createNode(Document document, XPath xpath) throws Exception {
+      log.debug(xpath);
+      Node node = null;
+      Vector nodes = xpf.select(document, "xpointer(" + xpath + ")");
+      if(nodes.size() >= 1){
+         node = (Node) nodes.elementAt(0);
+      }else{
+         Node parentNode = createNode(document, xpath.getParent());
+         if(xpath.getType() == Node.ATTRIBUTE_NODE){
+            ((Element) parentNode).setAttribute(xpath.getNameWithoutPredicates(), "null");
+            node = ((Element) parentNode).getAttributeNode(xpath.getNameWithoutPredicates());
+         }else{
+            node = dpf.newElementNode(document, xpath.getNameWithoutPredicates());
+            parentNode.appendChild(node);
+         }
+      }
+      return node;
+   }
 }

Modified: lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/xml/MalformedXPointerException.java
URL: http://svn.apache.org/viewvc/lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/xml/MalformedXPointerException.java?rev=617035&r1=617034&r2=617035&view=diff
==============================================================================
--- lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/xml/MalformedXPointerException.java (original)
+++ lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/xml/MalformedXPointerException.java Wed Jan 30 23:44:03 2008
@@ -23,7 +23,9 @@
  * DOCUMENT ME!
  */
 public class MalformedXPointerException extends Exception {
-    /**
+    private static final long serialVersionUID = 1L;
+
+   /**
      * Creates a new MalformedXPointerException object.
      */
     public MalformedXPointerException() {

Modified: lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/xml/RelaxNG.java
URL: http://svn.apache.org/viewvc/lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/xml/RelaxNG.java?rev=617035&r1=617034&r2=617035&view=diff
==============================================================================
--- lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/xml/RelaxNG.java (original)
+++ lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/xml/RelaxNG.java Wed Jan 30 23:44:03 2008
@@ -14,75 +14,65 @@
  *  limitations under the License.
  *
  */
-
 /* $Id$  */
-
 package org.apache.lenya.xml;
-
 import java.io.BufferedWriter;
 import java.io.ByteArrayOutputStream;
 import java.io.File;
 import java.io.OutputStreamWriter;
-
+import org.apache.log4j.Logger;
 import org.xml.sax.InputSource;
-
 import com.thaiopensource.util.PropertyMapBuilder;
 import com.thaiopensource.validate.SchemaReader;
 import com.thaiopensource.validate.ValidateProperty;
 import com.thaiopensource.validate.ValidationDriver;
 import com.thaiopensource.validate.auto.AutoSchemaReader;
 import com.thaiopensource.xml.sax.ErrorHandlerImpl;
-
-import org.apache.log4j.Category;
-
 /**
  * Validate XML Document with RELAX NG Schema
  */
 public class RelaxNG {
-    static Category log = Category.getInstance(RelaxNG.class);
-
-    /**
-     *
-     */
-    public static void main(String[] args) {
-        if(args.length == 0) {
-            System.out.println("Usage: relaxng.rng sample.xml");
-            return;
-        }
-
-        try {
-            String message = RelaxNG.validate(new File(args[0]), new File(args[1]));
-            if (message == null) {
-                System.out.println("Document is valid");
-            } else {
-                System.out.println("Document not valid: " + message);
-            }
-        } catch (Exception e) {
-            System.err.println("" + e);
-        }
-    }
-
-    /**
-     *
-     */
-    public static String validate(File schema, File xml) throws Exception {
-        InputSource in = ValidationDriver.uriOrFileInputSource(schema.getAbsolutePath());
-        PropertyMapBuilder properties = new PropertyMapBuilder();
-	    ByteArrayOutputStream error = new ByteArrayOutputStream();
-        ErrorHandlerImpl eh = new ErrorHandlerImpl(new BufferedWriter(new OutputStreamWriter(error)));
-        ValidateProperty.ERROR_HANDLER.put(properties, eh);
-        SchemaReader schemaReader = new AutoSchemaReader();
-        ValidationDriver driver = new ValidationDriver(properties.toPropertyMap(), schemaReader);
-        if (driver.loadSchema(in)) {
-            if (driver.validate(ValidationDriver.uriOrFileInputSource(xml.getAbsolutePath()))) {
-                log.debug("" + error);
-                return null;
-            } else {
-                log.error("" + error);
-                return "" + error;
-            }
-        } else {
-            throw new Exception("Could not load schema!\n" + error);
-        }
-    }
+   private static Logger log = Logger.getLogger(RelaxNG.class);
+   /**
+    * 
+    */
+   public static void main(String[] args) {
+      if(args.length == 0){
+         System.out.println("Usage: relaxng.rng sample.xml");
+         return;
+      }
+      try{
+         String message = RelaxNG.validate(new File(args[0]), new File(args[1]));
+         if(message == null){
+            System.out.println("Document is valid");
+         }else{
+            System.out.println("Document not valid: " + message);
+         }
+      }catch(Exception e){
+         System.err.println("" + e);
+      }
+   }
+   /**
+    * 
+    */
+   public static String validate(File schema, File xml) throws Exception {
+      InputSource in = ValidationDriver.uriOrFileInputSource(schema.getAbsolutePath());
+      PropertyMapBuilder properties = new PropertyMapBuilder();
+      ByteArrayOutputStream error = new ByteArrayOutputStream();
+      ErrorHandlerImpl eh = new ErrorHandlerImpl(new BufferedWriter(new OutputStreamWriter(error)));
+      ValidateProperty.ERROR_HANDLER.put(properties, eh);
+      SchemaReader schemaReader = new AutoSchemaReader();
+      ValidationDriver driver = new ValidationDriver(properties.toPropertyMap(), schemaReader);
+      if(driver.loadSchema(in)){
+         if(driver.validate(ValidationDriver.uriOrFileInputSource(xml.getAbsolutePath()))){
+            log.debug("" + error);
+            return null;
+         }else{
+            log.error("" + error);
+            return "" + error;
+         }
+      }else{
+         throw new Exception("Could not load schema!\n" + error);
+      }
+   }
 }

Modified: lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/xml/XMLEncToJavaEnc.java
URL: http://svn.apache.org/viewvc/lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/xml/XMLEncToJavaEnc.java?rev=617035&r1=617034&r2=617035&view=diff
==============================================================================
--- lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/xml/XMLEncToJavaEnc.java (original)
+++ lenya/branches/revolution/1.3.x/src/java/org/apache/lenya/xml/XMLEncToJavaEnc.java Wed Jan 30 23:44:03 2008
@@ -14,92 +14,80 @@
  *  limitations under the License.
  *
  */
-
 /* $Id$  */
-
 package org.apache.lenya.xml;
-
 import java.util.Hashtable;
-
-
 /**
  * DOCUMENT ME!
- * @deprecated 
+ * 
+ * @deprecated
  */
 public class XMLEncToJavaEnc extends Hashtable {
-    private static String DEFAULT_ENCODING = "utf-8";
-
-    private XMLEncToJavaEnc() {
-        // JAVA supports a lot more...
-        // http://www.w3.org/International/O-charset-list.html
-        //
-        put("ascii", "ASCII");
-        put("iso-8859-1", "ISO8859_1"); // Latin 1
-        put("iso-8859-2", "ISO8859_2"); // Latin 2
-        put("iso-8859-3", "ISO8859_3");
-        put("iso-8859-4", "ISO8859_4");
-        put("iso-8859-5", "ISO8859_5");
-        put("iso-8859-6", "ISO8859_6");
-        put("iso-8859-7", "ISO8859_7");
-        put("iso-8859-8", "ISO8859_8");
-        put("iso-8859-9", "ISO8859_9");
-        put("big-5", "Big5"); // Traditional Chinese
-
-        put("cp-874", "Cp874"); // IBM Thai
-
-        //put("cp-932",???);
-        put("cp-950", "Cp950"); // PC Chinese (Hongkong, Taiwan)
-        put("cp-1250", "Cp1250"); // Windows Eastern Europe
-        put("cp-1251", "Cp1251"); // Windows Cyrillic
-        put("cp-1252", "Cp1252"); // Windows Latin 1
-        put("cp-1253", "Cp1253"); // Windows Greek
-        put("cp-1255", "Cp1255"); // Windows Hebrew
-        put("cp-1256", "Cp1256"); // Windows Arabic
-        put("cp-1257", "Cp1257"); // Windows Baltic
-        put("cp-1258", "Cp1258"); // Windows Vietnamese
-        put("euc-jp", "EUC_JP"); // JIS0201, 0208, 0212, EUC Encoding, Japanese
-        put("euc-kr", "EUC_KR"); // KS C 5601, EUC Encoding, Korean
-
-        put("iso-2022-jp", "ISO2022JP"); // JIS0201, 0208, 0212, ISO2022 Encoding, Japanese
-        put("iso-2022-kr", "ISO2022KR"); // ISO 2022 KR, Korean
-
-        put("koi8-r", "KOI8_R"); // KOI8-R, Russian
-        put("shift_jis", "SJIS"); // Shift-JIS, Japanese
-
-        put("utf-8", "UTF8");
-
-        put("euc-tw", "EUC_TW"); // CNS11643 (Plane 1-3), T. Chinese, EUC encoding
-        put("x-mac-roman", "MacRoman"); // Macintosh Roman
-        put("x-mac-ce", "MacCentralEurope"); // Macintosh Latin-2
-        put("x-mac-greek", "MacGreek"); // Macintosh Greek
-        put("x-mac-turkish", "MacTurkish"); // Macintosh Turkish
-        put("x-mac-cyrillic", "MacCyrillic"); // Macintosh Cyrillic
-    }
-
-    /**
-     * DOCUMENT ME!
-     *
-     * @param args DOCUMENT ME!
-     */
-    public static void main(String[] args) {
-        System.out.println(XMLEncToJavaEnc.getJava("utf-8"));
-    }
-
-    /**
-     * DOCUMENT ME!
-     *
-     * @param xmlencoding DOCUMENT ME!
-     *
-     * @return DOCUMENT ME!
-     */
-    public static String getJava(String xmlencoding) {
-
-        try {
-            return ((String) ((new XMLEncToJavaEnc()).get(xmlencoding.toLowerCase())));
-        } catch (Exception e) {
-            System.err.println("Unsupported Encoding; reverting to " + DEFAULT_ENCODING);
-
-            return DEFAULT_ENCODING;
-        }
-    }
+   private static final long serialVersionUID = -6977502564034148488L;
+   private static String DEFAULT_ENCODING = "utf-8";
+   private XMLEncToJavaEnc() {
+      // JAVA supports a lot more...
+      // http://www.w3.org/International/O-charset-list.html
+      //
+      put("ascii", "ASCII");
+      put("iso-8859-1", "ISO8859_1"); // Latin 1
+      put("iso-8859-2", "ISO8859_2"); // Latin 2
+      put("iso-8859-3", "ISO8859_3");
+      put("iso-8859-4", "ISO8859_4");
+      put("iso-8859-5", "ISO8859_5");
+      put("iso-8859-6", "ISO8859_6");
+      put("iso-8859-7", "ISO8859_7");
+      put("iso-8859-8", "ISO8859_8");
+      put("iso-8859-9", "ISO8859_9");
+      put("big-5", "Big5"); // Traditional Chinese
+      put("cp-874", "Cp874"); // IBM Thai
+      // put("cp-932",???);
+      put("cp-950", "Cp950"); // PC Chinese (Hongkong, Taiwan)
+      put("cp-1250", "Cp1250"); // Windows Eastern Europe
+      put("cp-1251", "Cp1251"); // Windows Cyrillic
+      put("cp-1252", "Cp1252"); // Windows Latin 1
+      put("cp-1253", "Cp1253"); // Windows Greek
+      put("cp-1255", "Cp1255"); // Windows Hebrew
+      put("cp-1256", "Cp1256"); // Windows Arabic
+      put("cp-1257", "Cp1257"); // Windows Baltic
+      put("cp-1258", "Cp1258"); // Windows Vietnamese
+      put("euc-jp", "EUC_JP"); // JIS0201, 0208, 0212, EUC Encoding, Japanese
+      put("euc-kr", "EUC_KR"); // KS C 5601, EUC Encoding, Korean
+      put("iso-2022-jp", "ISO2022JP"); // JIS0201, 0208, 0212, ISO2022 Encoding, Japanese
+      put("iso-2022-kr", "ISO2022KR"); // ISO 2022 KR, Korean
+      put("koi8-r", "KOI8_R"); // KOI8-R, Russian
+      put("shift_jis", "SJIS"); // Shift-JIS, Japanese
+      put("utf-8", "UTF8");
+      put("euc-tw", "EUC_TW"); // CNS11643 (Plane 1-3), T. Chinese, EUC encoding
+      put("x-mac-roman", "MacRoman"); // Macintosh Roman
+      put("x-mac-ce", "MacCentralEurope"); // Macintosh Latin-2
+      put("x-mac-greek", "MacGreek"); // Macintosh Greek
+      put("x-mac-turkish", "MacTurkish"); // Macintosh Turkish
+      put("x-mac-cyrillic", "MacCyrillic"); // Macintosh Cyrillic
+   }
+   /**
+    * DOCUMENT ME!
+    * 
+    * @param args
+    *           DOCUMENT ME!
+    */
+   public static void main(String[] args) {
+      System.out.println(XMLEncToJavaEnc.getJava("utf-8"));
+   }
+   /**
+    * DOCUMENT ME!
+    * 
+    * @param xmlencoding
+    *           DOCUMENT ME!
+    * 
+    * @return DOCUMENT ME!
+    */
+   public static String getJava(String xmlencoding) {
+      try{
+         return((String) ((new XMLEncToJavaEnc()).get(xmlencoding.toLowerCase())));
+      }catch(Exception e){
+         System.err.println("Unsupported Encoding; reverting to " + DEFAULT_ENCODING);
+         return DEFAULT_ENCODING;
+      }
+   }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@lenya.apache.org
For additional commands, e-mail: commits-help@lenya.apache.org