Sorry,

this time with a patch

Pavel

> -----Original Message-----
> From: Pavel Ausianik [mailto:Pavel_Ausianik@;epam.com]
> Sent: Friday, November 15, 2002 3:34 PM
> To: '[EMAIL PROTECTED]'
> Subject: [PATCH] XMLJavaMappingRegistry QName DOMUtils
> 
> 
> Hello
> 
> Please find patches for XMLJavaMappingRegistry QName DOMUtils
> 
> XMLJavaMappingRegistry uses separated maps per encoding - and avoids
> creating String keys which is expencive. Class and QName used 
> as a keys
> QName changed so it not uses intern() in equals() because the 
> number of
> calls per  object instance was small
> DOMUtils  - avoid creating strings were not necessary (2 cases)
> 
> Best regards,
> Pavel
> 
> 
> 
> 
> --
> To unsubscribe, e-mail:   <mailto:soap-dev-unsubscribe@;xml.apache.org>
> For additional commands, e-mail: <mailto:soap-dev-help@;xml.apache.org>
> 

Index: QName.java
===================================================================
RCS file: /home/cvspublic/xml-soap/java/src/org/apache/soap/util/xml/QName.java,v
retrieving revision 1.8
diff -u -r1.8 QName.java
--- QName.java  30 Oct 2002 14:33:02 -0000      1.8
+++ QName.java  15 Nov 2002 14:40:09 -0000
@@ -2,7 +2,7 @@
  * The Apache Software License, Version 1.1
  *
  *
- * Copyright (c) 2000 The Apache Software Foundation.  All rights 
+ * Copyright (c) 2000 The Apache Software Foundation.  All rights
  * reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -10,7 +10,7 @@
  * are met:
  *
  * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer. 
+ *    notice, this list of conditions and the following disclaimer.
  *
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in
@@ -18,7 +18,7 @@
  *    distribution.
  *
  * 3. The end-user documentation included with the redistribution,
- *    if any, must include the following acknowledgment:  
+ *    if any, must include the following acknowledgment:
  *       "This product includes software developed by the
  *        Apache Software Foundation (http://www.apache.org/)."
  *    Alternately, this acknowledgment may appear in the software itself,
@@ -26,7 +26,7 @@
  *
  * 4. The names "SOAP" and "Apache Software Foundation" must
  *    not be used to endorse or promote products derived from this
- *    software without prior written permission. For written 
+ *    software without prior written permission. For written
  *    permission, please contact [EMAIL PROTECTED]
  *
  * 5. Products derived from this software may not be called "Apache",
@@ -93,19 +93,19 @@
                                          "must not be null.");
     }
 
-    setNamespaceURI(namespaceURI);
-    setLocalPart(localPart);
+    this.namespaceURI = namespaceURI;
+    this.localPart = localPart ;
   }
 
   public QName(String namespaceURI, String localPart)
   {
-    setNamespaceURI(namespaceURI);
-    setLocalPart(localPart);
+      setNamespaceURI(namespaceURI);
+      setLocalPart(localPart);
   }
-  
+
   public void setNamespaceURI(String namespaceURI)
   {
-    this.namespaceURI = (namespaceURI == null ? "" : namespaceURI).intern();
+    this.namespaceURI = (namespaceURI == null ? "" : namespaceURI);
   }
 
   public String getNamespaceURI()
@@ -115,7 +115,7 @@
 
   public void setLocalPart(String localPart)
   {
-    this.localPart = (localPart == null ? "" : localPart).intern();
+    this.localPart = (localPart == null ? "" : localPart);
   }
 
   public String getLocalPart()
@@ -131,8 +131,8 @@
   public boolean equals(Object obj)
   {
     return (obj != null
-            && namespaceURI == ((QName)obj).getNamespaceURI()
-            && localPart == ((QName)obj).getLocalPart());
+            && namespaceURI.equals(((QName)obj).namespaceURI)
+            && localPart.equals(((QName)obj).localPart));
   }
 
   public boolean matches(Node node)
Index: XMLJavaMappingRegistry.java
===================================================================
RCS file: 
/home/cvspublic/xml-soap/java/src/org/apache/soap/util/xml/XMLJavaMappingRegistry.java,v
retrieving revision 1.11
diff -u -r1.11 XMLJavaMappingRegistry.java
--- XMLJavaMappingRegistry.java 30 Oct 2002 14:33:02 -0000      1.11
+++ XMLJavaMappingRegistry.java 15 Nov 2002 14:40:21 -0000
@@ -78,21 +78,27 @@
  * @author Matthew J. Duftler ([EMAIL PROTECTED])
  * @author Sanjiva Weerawarana ([EMAIL PROTECTED])
  * @author Francisco Curbera ([EMAIL PROTECTED])
- * @author Scott Nichol ([EMAIL PROTECTED])
+ * @author Pavel Ausianik &lt;[EMAIL PROTECTED]&gt;
  */
 public class XMLJavaMappingRegistry
 {
-  /** Java type to Serializer */
-  private Hashtable sReg        = new Hashtable();
-  /** XML type to Deserializer */
-  private Hashtable dsReg       = new Hashtable();
-  /** XML type to Java type */
-  private Hashtable xml2JavaReg = new Hashtable();
-  /** Java type to XML type */
-  private Hashtable java2XMLReg = new Hashtable();
-  /** Encoding to use if none specified */
-  private String defaultEncodingStyle = null;
-  
+    private static final class Maps {
+        /** Java type to Serializer */
+        Map sReg        = new HashMap();
+        /** XML type to Deserializer */
+        Map dsReg       = new HashMap();
+        /** XML type to Java type */
+        Map xml2JavaReg = new HashMap();
+        /** Java type to XML type */
+        Map java2XMLReg = new HashMap();
+    };
+
+    /** Encoding to maps */
+    Map encToMaps = new HashMap();
+
+    /** Encoding to use if none specified */
+    private String defaultEncodingStyle = "";
+
   /**
    * Sets the default encoding style.  If the query*() calls
    * are invoked with a null encodingStyleURI parameter, we'll
@@ -102,7 +108,20 @@
    */
   public void setDefaultEncodingStyle(String defEncStyle)
   {
-      defaultEncodingStyle = defEncStyle;
+      if (defEncStyle != null)
+          defaultEncodingStyle = defEncStyle;
+  }
+
+  private Maps getMapsForEncoding(String encodingStyleURI)
+  {
+      if (encodingStyleURI == null)
+          encodingStyleURI = defaultEncodingStyle;
+      Maps maps = (Maps)encToMaps.get(encodingStyleURI);
+      if (maps == null) {
+          maps = new Maps();
+          encToMaps.put(encodingStyleURI, maps);
+      }
+      return  maps;
   }
 
   /**
@@ -117,7 +136,7 @@
    * To register a default serializer and/or deserializer, set the
    * corresponding type to null.
    *
-   * @param encodingStyleURI The encoding style for the map.  The 
+   * @param encodingStyleURI The encoding style for the map.  The
    *        encoding style qualifies the types.
    * @param elementType The XML type.
    * @param javaType The Java type.
@@ -127,35 +146,36 @@
   public void mapTypes(String encodingStyleURI, QName elementType,
                        Class javaType, Serializer s, Deserializer ds)
   {
-    String java2XMLKey = null;
-    String xml2JavaKey = null;
+    Maps maps =  getMapsForEncoding(encodingStyleURI);
+
+    Object java2XMLKey = "";
+    Object xml2JavaKey = "";
+
+    if (javaType != null)
+        java2XMLKey = javaType;
+    if (elementType != null)
+        xml2JavaKey = elementType;
 
     if (s != null)
-    {
-      java2XMLKey = getKey(javaType, encodingStyleURI);
-      sReg.put(java2XMLKey, s);
-    }
+        maps.sReg.put(java2XMLKey, s);
 
     if (ds != null)
-    {
-      xml2JavaKey = getKey(elementType, encodingStyleURI);
-      dsReg.put(xml2JavaKey, ds);
-    }
+        maps.dsReg.put( xml2JavaKey , ds);
 
     // Only map types if both types are provided.
     if (elementType != null && javaType != null)
     {
       // Only map Java to XML if a serializer was provided
       if (s != null)
-        java2XMLReg.put(java2XMLKey, elementType);
+        maps.java2XMLReg.put(java2XMLKey, elementType);
       // Only map XML to Java if a deserializer was provided
       if (ds != null)
-        xml2JavaReg.put(xml2JavaKey, javaType);
+        maps.xml2JavaReg.put(xml2JavaKey, javaType);
     }
   }
 
   /**
-   * This version returns null if the serializer is not found. It is 
+   * This version returns null if the serializer is not found. It is
    * intended for internal usage (its used for chaining registries,
    * for example).
    *
@@ -167,26 +187,19 @@
   protected Serializer querySerializer_(Class javaType,
                                         String encodingStyleURI)
   {
-    if (encodingStyleURI == null) {
-      encodingStyleURI = defaultEncodingStyle;
-    }
-    String java2XMLKey = getKey(javaType, encodingStyleURI);
-    Serializer s = (Serializer)sReg.get(java2XMLKey);
+    Maps maps =  getMapsForEncoding(encodingStyleURI);
+    Object java2XMLKey = "";
+    if (javaType != null)
+        java2XMLKey = javaType;
 
-    if (s != null)
-    {
-      return s;
-    }
-    else
-    {
-      java2XMLKey = getKey((Class) null, encodingStyleURI);
-      return (Serializer)sReg.get(java2XMLKey);
-    }
+    Serializer s = (Serializer)maps.sReg.get(java2XMLKey);
+
+    return s != null ? s : (Serializer)maps.sReg.get("");
   }
 
   /**
    * This version calls the protected method to do the work and if it's
-   * not found throws an exception. 
+   * not found throws an exception.
    *
    * @param javaType The Java type.
    * @param encodingStyleURI The encoding style.
@@ -212,7 +225,7 @@
   }
 
   /**
-   * This version returns null if the deserializer is not found. It is 
+   * This version returns null if the deserializer is not found. It is
    * intended for internal usage (its used for chaining registries,
    * for example).
    *
@@ -224,27 +237,18 @@
   protected Deserializer queryDeserializer_(QName elementType,
                                             String encodingStyleURI)
   {
-    if (encodingStyleURI == null) {
-      encodingStyleURI = defaultEncodingStyle;
-    }
-
-    String xml2JavaKey = getKey(elementType, encodingStyleURI);
-    Deserializer ds = (Deserializer)dsReg.get(xml2JavaKey);
+    Maps maps =  getMapsForEncoding(encodingStyleURI);
+    Object xml2JavaKey = "";
+    if (elementType != null)
+        xml2JavaKey = elementType;
 
-    if (ds != null)
-    {
-      return ds;
-    }
-    else
-    {
-      xml2JavaKey = getKey((QName) null, encodingStyleURI);
-      return (Deserializer)dsReg.get(xml2JavaKey);
-    }
+    Deserializer ds = (Deserializer)maps.dsReg.get(xml2JavaKey);
+    return ds != null ? ds : (Deserializer)maps.dsReg.get("");
   }
 
   /**
    * This version calls the protected method to do the work and if its
-   * not found throws an exception. 
+   * not found throws an exception.
    *
    * @param elementType The XML type.
    * @param encodingStyleURI The encoding style.
@@ -270,7 +274,7 @@
   }
 
   /**
-   * This version returns null if the element type is not found. It is 
+   * This version returns null if the element type is not found. It is
    * intended for internal usage (its used for chaining registries,
    * for example).
    *
@@ -281,17 +285,17 @@
    */
   protected QName queryElementType_(Class javaType, String encodingStyleURI)
   {
-    if (encodingStyleURI == null) {
-      encodingStyleURI = defaultEncodingStyle;
-    }
+    Maps maps =  getMapsForEncoding(encodingStyleURI);
+    Object java2XMLKey = "";
+    if (javaType != null)
+        java2XMLKey = javaType;
 
-    String java2XMLkey = getKey(javaType, encodingStyleURI);
-    return (QName)java2XMLReg.get(java2XMLkey);
+    return (QName)maps.java2XMLReg.get(java2XMLKey);
   }
 
   /**
    * This version calls the protected method to do the work and if its
-   * not found throws an exception. 
+   * not found throws an exception.
    *
    * @param javaType The Java type.
    * @param encodingStyleURI The encoding style.
@@ -316,7 +320,7 @@
   }
 
   /**
-   * This version returns null if the Java type is not found. It is 
+   * This version returns null if the Java type is not found. It is
    * intended for internal usage (its used for chaining registries,
    * for example).
    *
@@ -327,17 +331,17 @@
    */
   protected Class queryJavaType_(QName elementType, String encodingStyleURI)
   {
-    if (encodingStyleURI == null) {
-      encodingStyleURI = defaultEncodingStyle;
-    }
+    Maps maps =  getMapsForEncoding(encodingStyleURI);
+    Object xml2JavaKey = "";
+    if (elementType != null)
+        xml2JavaKey = elementType;
 
-    String xml2JavaKey = getKey(elementType, encodingStyleURI);
-    return (Class)xml2JavaReg.get(xml2JavaKey);
+    return (Class)maps.xml2JavaReg.get(xml2JavaKey);
   }
 
   /**
    * This version calls the protected method to do the work and if its
-   * not found throws an exception. 
+   * not found throws an exception.
    *
    * @param elementType The XML type.
    * @param encodingStyleURI The encoding style.
@@ -397,36 +401,6 @@
       }
 
     return ds.unmarshall(inScopeEncStyle, elementType, src, this, ctx);
-  }
-
-  /**
-   * Creates a key for the registry Hashtables.
-   *
-   * @param type The XML type.
-   * @param encodingStyleURI The encoding style.
-   * @return The key.
-   */
-  private static String getKey(QName type, String encodingStyleURI)
-  {
-    if (type == null)
-      return encodingStyleURI;
-    String strObj = type.toString();
-    return new StringBuffer(strObj.length() + 3 + 
encodingStyleURI.length()).append(strObj).append(" + 
").append(encodingStyleURI).toString();
-  }
-
-  /**
-   * Creates a key for the registry Hashtables.
-   *
-   * @param type The Java type.
-   * @param encodingStyleURI The encoding style.
-   * @return The key.
-   */
-  private static String getKey(Class type, String encodingStyleURI)
-  {
-    if (type == null)
-      return encodingStyleURI;
-    String strObj = type.getName();
-    return new StringBuffer(strObj.length() + 3 + 
encodingStyleURI.length()).append(strObj).append(" + 
").append(encodingStyleURI).toString();
   }
 
   /**
Index: DOMUtils.java
===================================================================
RCS file: /home/cvspublic/xml-soap/java/src/org/apache/soap/util/xml/DOMUtils.java,v
retrieving revision 1.8
diff -u -r1.8 DOMUtils.java
--- DOMUtils.java       6 Sep 2002 17:50:27 -0000       1.8
+++ DOMUtils.java       15 Nov 2002 14:41:38 -0000
@@ -2,7 +2,7 @@
  * The Apache Software License, Version 1.1
  *
  *
- * Copyright (c) 2000 The Apache Software Foundation.  All rights 
+ * Copyright (c) 2000 The Apache Software Foundation.  All rights
  * reserved.
  *
  * Redistribution and use in source and binary forms, with or without
@@ -10,7 +10,7 @@
  * are met:
  *
  * 1. Redistributions of source code must retain the above copyright
- *    notice, this list of conditions and the following disclaimer. 
+ *    notice, this list of conditions and the following disclaimer.
  *
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in
@@ -18,7 +18,7 @@
  *    distribution.
  *
  * 3. The end-user documentation included with the redistribution,
- *    if any, must include the following acknowledgment:  
+ *    if any, must include the following acknowledgment:
  *       "This product includes software developed by the
  *        Apache Software Foundation (http://www.apache.org/)."
  *    Alternately, this acknowledgment may appear in the software itself,
@@ -26,7 +26,7 @@
  *
  * 4. The names "SOAP" and "Apache Software Foundation" must
  *    not be used to endorse or promote products derived from this
- *    software without prior written permission. For written 
+ *    software without prior written permission. For written
  *    permission, please contact [EMAIL PROTECTED]
  *
  * 5. Products derived from this software may not be called "Apache",
@@ -78,7 +78,7 @@
    * returns "" if an attrib is not found).
    *
    * @param el       Element whose attrib is looked for
-   * @param attrName name of attribute to look for 
+   * @param attrName name of attribute to look for
    * @return the attribute value
    */
   static public String getAttribute (Element el, String attrName) {
@@ -97,8 +97,8 @@
    * returns "" if an attrib is not found).
    *
    * @param el       Element whose attrib is looked for
-   * @param namespaceURI namespace URI of attribute to look for 
-   * @param localPart local part of attribute to look for 
+   * @param namespaceURI namespace URI of attribute to look for
+   * @param localPart local part of attribute to look for
    * @return the attribute value
    */
   static public String getAttributeNS (Element el,
@@ -122,24 +122,39 @@
    *                 be combined.
    * @return the concatanated string.
    */
+  static int num = 0;
   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;
-      }
-      tempNode = tempNode.getNextSibling();
+    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 "";
+        }
+        // Otherwise collect in StringBuffer
+        StringBuffer strBuf =  new StringBuffer(64);
+        do {
+            switch (tempNode.getNodeType()) {
+                case Node.TEXT_NODE :
+                case Node.CDATA_SECTION_NODE :
+                    strBuf.append(((CharacterData)tempNode).getData());
+                    break;
+            }
+            tempNode = nextNode;
+            if ((tempNode = nextNode) != null)
+                nextNode = nextNode.getNextSibling();
+        } while (tempNode != null);
+        return strBuf.toString();
     }
-    return strBuf.toString();
+    return "";
   }
 
   /**
@@ -184,7 +199,7 @@
    *
    * @return the first matching child element.
    */
-  public static Element findChildElementWithAttribute (Element elem, 
+  public static Element findChildElementWithAttribute (Element elem,
                    String attrName,
                    String attrValue) {
     for (Node n = elem.getFirstChild (); n != null; n = n.getNextSibling ()) {
@@ -197,7 +212,7 @@
     return  null;
   }
 
-  /** 
+  /**
    * Count number of children of a certain type of the given element.
    *
    * @param elem the element whose kids are to be counted
@@ -250,18 +265,21 @@
       }
     }
 
+    // Do not concatenate strings in the loop
+    String longPrefix = prefix == null ? "" : "xmlns:" + prefix;
+
     while (tempNode != null && tempNode.getNodeType () == Node.ELEMENT_NODE)
     {
       Element tempEl = (Element) tempNode;
       String namespaceURI;
-      
+
       if (prefix == null) {
         namespaceURI = getAttribute(tempEl, "xmlns");
       } else {
         namespaceURI = getAttributeNS(tempEl, NS_URI_XMLNS, prefix);
         if (namespaceURI == null) {
           // SAX parser (maybe others?) need this
-          namespaceURI = getAttribute(tempEl, "xmlns:" + prefix);
+          namespaceURI = getAttribute(tempEl, longPrefix);
         }
       }
 
@@ -285,7 +303,7 @@
          String thisId = el.getAttribute("id");
          if (id.equals(thisId))
                  return el;
-         
+
          NodeList list = el.getChildNodes();
          for (int i = 0; i < list.getLength(); i++) {
                  Node node = list.item(i);
@@ -295,7 +313,7 @@
                                  return ret;
                  }
          }
-         
+
          return null;
   }
 

--
To unsubscribe, e-mail:   <mailto:soap-dev-unsubscribe@;xml.apache.org>
For additional commands, e-mail: <mailto:soap-dev-help@;xml.apache.org>

Reply via email to