snichol 2002/11/18 13:16:50 Modified: java/src/org/apache/soap/util/xml DOMUtils.java Log: Submitted by: Pavel Ausianik <[EMAIL PROTECTED]> Avoid creating strings where not necessary (2 cases) Scott Nichol added javadoc and use of Constants. Revision Changes Path 1.9 +53 -24 xml-soap/java/src/org/apache/soap/util/xml/DOMUtils.java Index: DOMUtils.java =================================================================== RCS file: /home/cvs/xml-soap/java/src/org/apache/soap/util/xml/DOMUtils.java,v retrieving revision 1.8 retrieving revision 1.9 diff -u -r1.8 -r1.9 --- DOMUtils.java 6 Sep 2002 17:50:27 -0000 1.8 +++ DOMUtils.java 18 Nov 2002 21:16:50 -0000 1.9 @@ -57,7 +57,13 @@ package org.apache.soap.util.xml; -import org.w3c.dom.*; +import org.apache.soap.Constants; + +import org.w3c.dom.Attr; +import org.w3c.dom.CharacterData; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; /** * Common operations on DOM structures. @@ -65,14 +71,10 @@ * @author Matthew J. Duftler * @author Sanjiva Weerawarana * @author Scott Nichol ([EMAIL PROTECTED]) + * @author Pavel Ausianik ([EMAIL PROTECTED]) */ public class DOMUtils { /** - * The namespaceURI represented by the prefix <code>xmlns</code>. - */ - private static String NS_URI_XMLNS = "http://www.w3.org/2000/xmlns/"; - - /** * Returns the value of an attribute of an element. Returns null * if the attribute is not found (whereas Element.getAttribute * returns "" if an attrib is not found). @@ -125,21 +127,35 @@ static public String getChildCharacterData (Element parentEl) { if (parentEl == null) { return null; - } - Node tempNode = parentEl.getFirstChild(); - StringBuffer strBuf = new StringBuffer(64); - CharacterData charData; - - while (tempNode != null) { - switch (tempNode.getNodeType()) { - case Node.TEXT_NODE : - case Node.CDATA_SECTION_NODE : charData = (CharacterData)tempNode; - strBuf.append(charData.getData()); - break; + } + Node tempNode = parentEl.getFirstChild(); + if (tempNode != null) { + Node nextNode = tempNode.getNextSibling(); + // If only one Child element - return its directly + if (nextNode == null) { + switch (tempNode.getNodeType()) { + case Node.TEXT_NODE : + case Node.CDATA_SECTION_NODE : + return ((CharacterData)tempNode).getData(); + } + return ""; } - tempNode = tempNode.getNextSibling(); + // Otherwise collect in StringBuffer + StringBuffer strBuf = new StringBuffer(256); + do { + switch (tempNode.getNodeType()) { + case Node.TEXT_NODE : + case Node.CDATA_SECTION_NODE : + strBuf.append(((CharacterData)tempNode).getData()); + break; + } + tempNode = nextNode; + if (tempNode != null) + nextNode = tempNode.getNextSibling(); + } while (tempNode != null); + return strBuf.toString(); } - return strBuf.toString(); + return ""; } /** @@ -250,18 +266,21 @@ } } + // Do not concatenate strings in the loop + String longPrefix = prefix == null ? "" : Constants.NS_PRE_XMLNS + ":" + prefix; + while (tempNode != null && tempNode.getNodeType () == Node.ELEMENT_NODE) { Element tempEl = (Element) tempNode; String namespaceURI; - + if (prefix == null) { - namespaceURI = getAttribute(tempEl, "xmlns"); + namespaceURI = getAttribute(tempEl, Constants.NS_PRE_XMLNS); } else { - namespaceURI = getAttributeNS(tempEl, NS_URI_XMLNS, prefix); + namespaceURI = getAttributeNS(tempEl, Constants.NS_URI_XMLNS, prefix); if (namespaceURI == null) { // SAX parser (maybe others?) need this - namespaceURI = getAttribute(tempEl, "xmlns:" + prefix); + namespaceURI = getAttribute(tempEl, longPrefix); } } @@ -278,11 +297,18 @@ return null; } + /** + * Gets an Element within a fragment by ID. + * + * @param el The root of the fragment to search. + * @param id The id for which to search. + * @return The element with the ID or null if no match was found. + */ public static Element getElementByID(Element el, String id) { if (el == null) return null; - String thisId = el.getAttribute("id"); + String thisId = el.getAttribute(Constants.ATTR_ID); if (id.equals(thisId)) return el; @@ -302,6 +328,9 @@ /** * Gets an attribute value as a QName. The value must have a prefix * that correctly maps to a namespace URI. + * + * @param el The DOM Element to which the attribute applies. + * @param attrName The name of the attribute. */ public static QName getQualifiedAttributeValue(Element el, String attrName)
-- To unsubscribe, e-mail: <mailto:[EMAIL PROTECTED]> For additional commands, e-mail: <mailto:[EMAIL PROTECTED]>