You are viewing a plain text version of this content. The canonical link for it is here.
Posted to cvs@cocoon.apache.org by an...@apache.org on 2004/03/29 01:57:41 UTC

cvs commit: cocoon-2.1/src/java/org/apache/cocoon/xml/dom DOMUtil.java DOMStreamer.java

antonio     2004/03/28 15:57:41

  Modified:    src/java/org/apache/cocoon/xml/dom DOMUtil.java
                        DOMStreamer.java
  Log:
  Using Apache.commons.lang
  
  Revision  Changes    Path
  1.10      +54 -79    cocoon-2.1/src/java/org/apache/cocoon/xml/dom/DOMUtil.java
  
  Index: DOMUtil.java
  ===================================================================
  RCS file: /home/cvs//cocoon-2.1/src/java/org/apache/cocoon/xml/dom/DOMUtil.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- DOMUtil.java	5 Mar 2004 13:03:02 -0000	1.9
  +++ DOMUtil.java	28 Mar 2004 23:57:41 -0000	1.10
  @@ -34,6 +34,8 @@
   import org.apache.cocoon.ProcessingException;
   import org.apache.cocoon.xml.IncludeXMLConsumer;
   import org.apache.cocoon.xml.XMLUtils;
  +import org.apache.commons.lang.BooleanUtils;
  +import org.apache.commons.lang.StringUtils;
   import org.apache.excalibur.source.SourceParameters;
   import org.apache.excalibur.xml.sax.SAXParser;
   import org.apache.excalibur.xml.sax.XMLizable;
  @@ -90,19 +92,14 @@
           if (path == null) {
               throw new ProcessingException("Not a valid XPath: " + path);
           }
  -        if (root == null)
  -            return null;
  -        if (path.startsWith("/") == true)
  -            path = path.substring(1); // remove leading "/"
  -        if (path.endsWith("/") == true) { // remove ending "/" for root node
  -            path = path.substring(0, path.length() - 1);
  -        }
  -
  -        Node node = XPathUtil.searchSingleNode(processor, root, path);
  -        if (node != null) {
  -            return getValueOfNode(node);
  +        if (root != null) {
  +            path = StringUtils.strip(path, "/");
  +            Node node = XPathUtil.searchSingleNode(processor, root, path);
  +            if (node != null) {
  +                return getValueOfNode(node);
  +            }
           }
  -        return null;
  +       return null;
       }
   
       /**
  @@ -166,10 +163,10 @@
                                                     boolean defaultValue)
       throws ProcessingException {
           String value = getValueOfNode(processor, root, path);
  -        if (value == null) {
  -            return defaultValue;
  +        if (value != null) {
  +            return BooleanUtils.toBoolean(value);
           }
  -        return Boolean.valueOf(value).booleanValue();
  +        return defaultValue;
       }
   
       /**
  @@ -178,25 +175,24 @@
        * If the node has no text nodes, <code>null</code> is returned.
        */
       public static String getValueOfNode(Node node) {
  -        if (node == null)
  -            return null;
  -        if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
  -            return node.getNodeValue();
  -        } else {
  -            String value = null;
  -            node.normalize();
  -            NodeList childs = node.getChildNodes();
  -            int i, l;
  -            i = 0;
  -            l = childs.getLength();
  -            while (i < l && value == null) {
  -                if (childs.item(i).getNodeType() == Node.TEXT_NODE)
  -                    value = childs.item(i).getNodeValue().trim();
  -                else
  -                    i++;
  +        if (node != null) {
  +            if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
  +                return node.getNodeValue();
  +            } else {
  +                node.normalize();
  +                NodeList childs = node.getChildNodes();
  +                int i = 0;
  +                int length = childs.getLength();
  +                while (i < length) {
  +                    if (childs.item(i).getNodeType() == Node.TEXT_NODE) {
  +                        return childs.item(i).getNodeValue().trim();
  +                    } else {
  +                        i++;
  +                    }
  +                }
               }
  -            return value;
           }
  +        return null;
       }
   
       /**
  @@ -206,10 +202,7 @@
        * returned.
        */
       public static String getValueOfNode(Node node, String defaultValue) {
  -        String value = getValueOfNode(node);
  -        if (value == null)
  -            value = defaultValue;
  -        return value;
  +        return StringUtils.defaultString(getValueOfNode(node), defaultValue);
       }
   
       /**
  @@ -302,13 +295,11 @@
                       frag.appendChild(child);
                   }
               }
  -
           } catch (SAXException sax) {
               throw new ProcessingException("SAXException: " + sax, sax);
           } catch (IOException ioe) {
               throw new ProcessingException("IOException: " + ioe, ioe);
           }
  -
           return frag;
       }
   
  @@ -684,12 +675,8 @@
                       + local,
                   local);
           }
  -
  -        if (path.startsWith("/") == true)
  -            path = path.substring(1); // remove leading "/"
  -        if (path.endsWith("/") == true) { // remove ending "/" for root node
  -            path = path.substring(0, path.length() - 1);
  -        }
  +        // Remove leading "/" on both ends
  +        path = StringUtils.strip(path, "/");
   
           // now step through the nodes!
           Node parent = rootNode;
  @@ -856,11 +843,8 @@
                   local);
           }
   
  -        if (path.startsWith("/") == true)
  -            path = path.substring(1); // remove leading "/"
  -        if (path.endsWith("/") == true) { // remove ending "/" for root node
  -            path = path.substring(0, path.length() - 1);
  -        }
  +        // remove leading "/" oon both ends
  +        path = StringUtils.strip(path, "/");
   
           // now step through the nodes!
           Node parent = rootNode;
  @@ -991,11 +975,7 @@
           }
           if (root == null)
               return null;
  -        if (path.startsWith("/") == true)
  -            path = path.substring(1); // remove leading "/"
  -        if (path.endsWith("/") == true) { // remove ending "/" for root node
  -            path = path.substring(0, path.length() - 1);
  -        }
  +        path = StringUtils.strip(path, "/");
   
           try {
               Node node = getSingleNode(root, path);
  @@ -1027,11 +1007,7 @@
           }
           if (root == null)
               return null;
  -        if (path.startsWith("/") == true)
  -            path = path.substring(1); // remove leading "/"
  -        if (path.endsWith("/") == true) { // remove ending "/" for root node
  -            path = path.substring(0, path.length() - 1);
  -        }
  +        path = StringUtils.strip(path, "/");
   
           try {
               Node node = getSingleNode(root, path, processor);
  @@ -1060,9 +1036,9 @@
       public static String getValueOf(Node root, String path, String defaultValue)
           throws ProcessingException {
           String value = getValueOf(root, path);
  -        if (value == null)
  +        if (value == null) {
               value = defaultValue;
  -
  +        }
           return value;
       }
   
  @@ -1081,9 +1057,9 @@
                                       XPathProcessor processor)
       throws ProcessingException {
           String value = getValueOf(root, path, processor);
  -        if (value == null)
  +        if (value == null) {
               value = defaultValue;
  -
  +        }
           return value;
       }
   
  @@ -1101,10 +1077,11 @@
        */
       public static boolean getValueAsBooleanOf(Node root, String path) throws ProcessingException {
           String value = getValueOf(root, path);
  -        if (value == null) {
  +        if (value != null) {
  +            return Boolean.valueOf(value).booleanValue();
  +        } else {
               throw new ProcessingException("No such node: " + path);
           }
  -        return Boolean.valueOf(value).booleanValue();
       }
   
       /**
  @@ -1145,10 +1122,10 @@
       public static boolean getValueAsBooleanOf(Node root, String path, boolean defaultValue)
           throws ProcessingException {
           String value = getValueOf(root, path);
  -        if (value == null) {
  -            return defaultValue;
  +        if (value != null) {
  +            return Boolean.valueOf(value).booleanValue();
           }
  -        return Boolean.valueOf(value).booleanValue();
  +        return defaultValue;
       }
   
       /**
  @@ -1168,10 +1145,10 @@
                                                 XPathProcessor processor)
       throws ProcessingException {
           String value = getValueOf(root, path, processor);
  -        if (value == null) {
  -            return defaultValue;
  +        if (value != null) {
  +            return Boolean.valueOf(value).booleanValue();
           }
  -        return Boolean.valueOf(value).booleanValue();
  +        return defaultValue;        
       }
   
       /**
  @@ -1203,11 +1180,10 @@
       public static NodeList selectNodeList(Node contextNode, String str)
           throws TransformerException {
           String[] pathComponents = buildPathArray(str);
  -        if (pathComponents == null) {
  -            return XPathAPI.selectNodeList(contextNode, str);
  -        } else {
  +        if (pathComponents != null) {
               return getNodeListFromPath(contextNode, pathComponents);
           }
  +        return XPathAPI.selectNodeList(contextNode, str);
       }
   
       /**
  @@ -1224,11 +1200,10 @@
       public static NodeList selectNodeList(Node contextNode, String str, XPathProcessor processor)
       throws TransformerException {
           String[] pathComponents = buildPathArray(str);
  -        if (pathComponents == null) {
  -            return processor.selectNodeList(contextNode, str); 
  -        } else {
  +        if (pathComponents != null) {
               return getNodeListFromPath(contextNode, pathComponents);
           }
  +       return processor.selectNodeList(contextNode, str); 
       }
   
       /**
  @@ -1479,6 +1454,7 @@
           try {
               return XMLUtils.serializeNodeToXML(node);
           } catch (ProcessingException e) {
  +            // Empty
           }
           return "";
       }
  @@ -1529,5 +1505,4 @@
       public static StringBuffer node2StringBuffer(Node node, boolean pretty, String indent) {
           return new StringBuffer(node2String(node, pretty));
       }
  -
   }
  
  
  
  1.15      +22 -18    cocoon-2.1/src/java/org/apache/cocoon/xml/dom/DOMStreamer.java
  
  Index: DOMStreamer.java
  ===================================================================
  RCS file: /home/cvs//cocoon-2.1/src/java/org/apache/cocoon/xml/dom/DOMStreamer.java,v
  retrieving revision 1.14
  retrieving revision 1.15
  diff -u -r1.14 -r1.15
  --- DOMStreamer.java	5 Mar 2004 13:03:02 -0000	1.14
  +++ DOMStreamer.java	28 Mar 2004 23:57:41 -0000	1.15
  @@ -30,6 +30,7 @@
   import org.apache.cocoon.xml.EmbeddedXMLPipe;
   import org.apache.cocoon.xml.XMLConsumer;
   import org.apache.cocoon.xml.XMLProducer;
  +import org.apache.commons.lang.StringUtils;
   
   import org.w3c.dom.Attr;
   import org.w3c.dom.Comment;
  @@ -240,9 +241,9 @@
                           pos = pos.getParentNode();
   
                           if ((null == pos) || (top.equals(pos))) {
  -                            if (null != pos)
  +                            if (null != pos) {
                                   endNode(pos);
  -
  +                            }
                               nextNode = null;
   
                               break;
  @@ -320,12 +321,12 @@
   
                       if (namespaceURI != null) {
                           // no prefix means: make this the default namespace
  -                        if (prefix == null)
  +                        if (prefix == null) {
                               prefix = "";
  -
  +                        }
                           // check that is declared
                           String uri = currentElementInfo.findNamespaceURI(prefix);
  -                        if (uri != null && uri.equals(namespaceURI)) {
  +                        if (StringUtils.equals(uri, namespaceURI)) {
                               // System.out.println("namespace is declared");
                               // prefix is declared correctly, do nothing
                           } else if (uri != null) {
  @@ -340,7 +341,7 @@
                           // element has no namespace
                           // check if there is a default namespace, if so undeclare it
                           String uri = currentElementInfo.findNamespaceURI("");
  -                        if (uri != null && !uri.equals("")) {
  +                        if (StringUtils.isNotEmpty(uri)) {
                               // System.out.println("undeclaring default namespace");
                               currentElementInfo.put("", "");
                           }
  @@ -351,10 +352,11 @@
                           namespaceURI = "";
   
                       String qName;
  -                    if (prefix != null && prefix.length() > 0)
  +                    if (StringUtils.isNotEmpty(prefix)) {
                           qName = prefix + ":" + localName;
  -                    else
  +                    } else {
                           qName = localName;
  +                    }
   
                       // make the attributes
                       AttributesImpl newAttrs = new AttributesImpl();
  @@ -416,10 +418,11 @@
   
                               String assignedAttrNsURI = attrNsURI != null ? attrNsURI : "";
                               String attrQName;
  -                            if (assignedAttrPrefix != null)
  +                            if (assignedAttrPrefix != null) {
                                   attrQName = assignedAttrPrefix + ":" + attrLocalName;
  -                            else
  +                            } else {
                                   attrQName = attrLocalName;
  +                            }
                               newAttrs.addAttribute(assignedAttrNsURI, attrLocalName, attrQName, "CDATA", attr.getNodeValue());
                           }
                       }
  @@ -522,16 +525,14 @@
                          || (type == Node.ENTITY_REFERENCE_NODE))) {
                       if (type == Node.ELEMENT_NODE) {
                           Attr attr=((Element)parent).getAttributeNode(declname);
  -                        if(attr!=null) {
  +                        if (attr != null) {
                               namespace = attr.getNodeValue();
                               break;
                           }
                       }
  -
                       parent = parent.getParentNode();
                   }
               }
  -
               return namespace;
           }
   
  @@ -651,13 +652,15 @@
               public String findPrefix(String namespaceURI) {
                   if (namespaceDeclarations != null && namespaceDeclarations.size() != 0) {
                       String prefix = getPrefix(namespaceURI);
  -                    if (prefix != null)
  +                    if (prefix != null) {
                           return prefix;
  +                    }
                   }
  -                if (parent != null)
  +                if (parent != null) {
                       return parent.findPrefix(namespaceURI);
  -                else
  +                } else {
                       return null;
  +                }
               }
   
               /**
  @@ -666,8 +669,9 @@
               public String findNamespaceURI(String prefix) {
                   if (namespaceDeclarations != null && namespaceDeclarations.size() != 0) {
                       String uri = (String) namespaceDeclarations.get(prefix);
  -                    if (uri != null)
  +                    if (uri != null) {
                           return uri;
  +                    }
                   }
                   if (parent != null)
                       return parent.findNamespaceURI(prefix);