You are viewing a plain text version of this content. The canonical link for it is here.
Posted to soap-dev@xml.apache.org by sn...@apache.org on 2002/11/18 21:19:26 UTC
cvs commit: xml-soap/java/src/org/apache/soap/util/xml XMLJavaMappingRegistry.java
snichol 2002/11/18 12:19:26
Modified: java/src/org/apache/soap/util/xml
XMLJavaMappingRegistry.java
Log:
Submitted by: Pavel Ausianik <Pa...@epam.com>
XMLJavaMappingRegistry uses separated maps per encoding - and avoids
creating String keys which is expensive. Class and QName used
as a keys.
Revision Changes Path
1.12 +82 -103 xml-soap/java/src/org/apache/soap/util/xml/XMLJavaMappingRegistry.java
Index: XMLJavaMappingRegistry.java
===================================================================
RCS file: /home/cvs/xml-soap/java/src/org/apache/soap/util/xml/XMLJavaMappingRegistry.java,v
retrieving revision 1.11
retrieving revision 1.12
diff -u -r1.11 -r1.12
--- XMLJavaMappingRegistry.java 30 Oct 2002 14:33:02 -0000 1.11
+++ XMLJavaMappingRegistry.java 18 Nov 2002 20:19:26 -0000 1.12
@@ -79,20 +79,28 @@
* @author Sanjiva Weerawarana (sanjiva@watson.ibm.com)
* @author Francisco Curbera (curbera@us.ibm.com)
* @author Scott Nichol (snichol@computer.org)
+ * @author Pavel Ausianik <Pavel_Ausianik@epam.com>
*/
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();
+ /** Maps for a particular encoding */
+ 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 = null;
-
+ private String defaultEncodingStyle = "";
+
/**
* Sets the default encoding style. If the query*() calls
* are invoked with a null encodingStyleURI parameter, we'll
@@ -102,10 +110,27 @@
*/
public void setDefaultEncodingStyle(String defEncStyle)
{
+ if (defEncStyle != null)
defaultEncodingStyle = defEncStyle;
}
/**
+ * Gets the maps for an encoding style, creating new maps
+ * if necessary.
+ */
+ 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;
+ }
+
+ /**
* Adds type and serializer mappings. If a serializer is specified,
* a mapping from the Java type to the serializer is added. If a
* deserializer is specifed, a mapping from the XML type to the
@@ -117,7 +142,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 +152,35 @@
public void mapTypes(String encodingStyleURI, QName elementType,
Class javaType, Serializer s, Deserializer ds)
{
- String java2XMLKey = null;
- String xml2JavaKey = null;
-
- if (s != null)
- {
- java2XMLKey = getKey(javaType, encodingStyleURI);
- sReg.put(java2XMLKey, s);
+ Maps maps = getMapsForEncoding(encodingStyleURI);
+ Object java2XMLKey = "";
+ Object xml2JavaKey = "";
+
+ if (s != null) {
+ if (javaType != null)
+ java2XMLKey = javaType;
+ maps.sReg.put(java2XMLKey, s);
}
- if (ds != null)
- {
- xml2JavaKey = getKey(elementType, encodingStyleURI);
- dsReg.put(xml2JavaKey, ds);
+ if (ds != null) {
+ if (elementType != null)
+ xml2JavaKey = elementType;
+ maps.dsReg.put(xml2JavaKey , ds);
}
// Only map types if both types are provided.
- if (elementType != null && javaType != null)
- {
+ 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 +192,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 +230,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 +242,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 +279,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 +290,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 +325,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 +336,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 +406,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();
}
/**
--
To unsubscribe, e-mail: <ma...@xml.apache.org>
For additional commands, e-mail: <ma...@xml.apache.org>