1 57 58 package org.apache.soap.util.xml; 59 60 import java.io.*; 61 import java.util.*; 62 import org.w3c.dom.*; 63 import org.apache.soap.util.*; 64 import org.apache.soap.Constants; 65 import org.apache.soap.rpc.SOAPContext; 66 import org.apache.soap.encoding.soapenc.MimePartSerializer; 67 import org.apache.soap.encoding.SOAPMappingRegistry; 68 69 76 public class XMLJavaMappingRegistry 77 { 78 private Hashtable sReg = new Hashtable(); 79 private Hashtable dsReg = new Hashtable(); 80 private Hashtable xml2JavaReg = new Hashtable(); 81 private Hashtable java2XMLReg = new Hashtable(); 82 private String defaultEncodingStyle = null; 83 84 88 public void setDefaultEncodingStyle(String defEncStyle) 89 { 90 defaultEncodingStyle = defEncStyle; 91 } 92 93 public void mapTypes(String encodingStyleURI, QName elementType, 95 Class javaType, Serializer s, Deserializer ds) 96 { 97 String java2XMLKey = getKey(javaType, encodingStyleURI); 98 String xml2JavaKey = getKey(elementType, encodingStyleURI); 99 100 if (s != null) 101 { 102 sReg.put(java2XMLKey, s); 103 } 104 105 if (ds != null) 106 { 107 dsReg.put(xml2JavaKey, ds); 108 } 109 110 if (elementType != null && javaType != null) 112 { 113 java2XMLReg.put(java2XMLKey, elementType); 114 xml2JavaReg.put(xml2JavaKey, javaType); 115 } 116 } 117 118 123 protected Serializer querySerializer_(Class javaType, 124 String encodingStyleURI) 125 { 126 if (encodingStyleURI == null) { 127 encodingStyleURI = defaultEncodingStyle; 128 } 129 String java2XMLKey = getKey(javaType, encodingStyleURI); 130 Serializer s = (Serializer)sReg.get(java2XMLKey); 131 132 if (s != null) 133 { 134 return s; 135 } 136 else 137 { 138 java2XMLKey = getKey(null, encodingStyleURI); 139 return (Serializer)sReg.get(java2XMLKey); 140 } 141 } 142 143 147 public Serializer querySerializer(Class javaType, String encodingStyleURI) 148 throws IllegalArgumentException 149 { 150 Serializer s = querySerializer_(javaType, encodingStyleURI); 151 if (s != null) 152 { 153 return s; 154 } 155 else 156 { 157 throw new IllegalArgumentException ("No Serializer found to " + 158 "serialize a '" + 159 getClassName(javaType) + 160 "' using encoding style '" + 161 encodingStyleURI + "'."); 162 } 163 } 164 165 170 protected Deserializer queryDeserializer_(QName elementType, 171 String encodingStyleURI) 172 { 173 if (encodingStyleURI == null) { 174 encodingStyleURI = defaultEncodingStyle; 175 } 176 177 String xml2JavaKey = getKey(elementType, encodingStyleURI); 178 Deserializer ds = (Deserializer)dsReg.get(xml2JavaKey); 179 180 if (ds != null) 181 { 182 return ds; 183 } 184 else 185 { 186 xml2JavaKey = getKey(null, encodingStyleURI); 187 return (Deserializer)dsReg.get(xml2JavaKey); 188 } 189 } 190 191 195 public Deserializer queryDeserializer(QName elementType, 196 String encodingStyleURI) 197 throws IllegalArgumentException 198 { 199 Deserializer ds = queryDeserializer_(elementType, encodingStyleURI); 200 if (ds != null) 201 { 202 return ds; 203 } 204 else 205 { 206 throw new IllegalArgumentException ("No Deserializer found to " + 207 "deserialize a '" + elementType + 208 "' using encoding style '" + 209 encodingStyleURI + "'."); 210 } 211 } 212 213 218 protected QName queryElementType_(Class javaType, String encodingStyleURI) 219 { 220 if (encodingStyleURI == null) { 221 encodingStyleURI = defaultEncodingStyle; 222 } 223 224 String java2XMLkey = getKey(javaType, encodingStyleURI); 225 return (QName)java2XMLReg.get(java2XMLkey); 226 } 227 228 232 public QName queryElementType(Class javaType, String encodingStyleURI) 233 throws IllegalArgumentException 234 { 235 QName elementType = queryElementType_(javaType, encodingStyleURI); 236 if (elementType != null) 237 { 238 return elementType; 239 } 240 else 241 { 242 throw new IllegalArgumentException ("No mapping found for '" + 243 getClassName(javaType) + 244 "' using encoding style '" + 245 encodingStyleURI + "'."); 246 } 247 } 248 249 254 protected Class queryJavaType_(QName elementType, String encodingStyleURI) 255 { 256 if (encodingStyleURI == null) { 257 encodingStyleURI = defaultEncodingStyle; 258 } 259 260 String xml2JavaKey = getKey(elementType, encodingStyleURI); 261 return (Class )xml2JavaReg.get(xml2JavaKey); 262 } 263 264 268 public Class queryJavaType(QName elementType, String encodingStyleURI) 269 throws IllegalArgumentException 270 { 271 Class javaType = queryJavaType_(elementType, encodingStyleURI); 272 if (javaType != null) 273 { 274 return javaType; 275 } 276 else 277 { 278 throw new IllegalArgumentException ("No mapping found for '" + 279 elementType + 280 "' using encoding style '" + 281 encodingStyleURI + "'."); 282 } 283 } 284 285 public void marshall(String inScopeEncStyle, Class javaType, Object src, 286 Object context, Writer sink, NSStack nsStack, 287 SOAPContext ctx) 288 throws IllegalArgumentException , IOException 289 { 290 Serializer s = (Serializer)querySerializer(javaType, inScopeEncStyle); 291 292 s.marshall(inScopeEncStyle, javaType, src, context, 293 sink, nsStack, this, ctx); 294 } 295 296 public Bean unmarshall(String inScopeEncStyle, QName elementType, 297 Node src, SOAPContext ctx) 298 throws IllegalArgumentException 299 { 300 Deserializer ds = null; 301 try { 302 ds = (Deserializer)queryDeserializer(elementType, 303 inScopeEncStyle); 304 } catch(IllegalArgumentException iae) { 305 String href = ((Element)src).getAttribute(Constants.ATTR_REFERENCE); 308 if (href != null && !href.equals("")) 309 ds = SOAPMappingRegistry.partSer; 310 else 311 throw iae; 312 } 313 314 return ds.unmarshall(inScopeEncStyle, elementType, src, this, ctx); 315 } 316 317 private static String getKey(Object type, String encodingStyleURI) 318 { 319 return type + " + " + encodingStyleURI; 320 } 321 322 protected static String getClassName(Class javaType) 323 { 324 return javaType != null ? StringUtils.getClassName(javaType) : "null"; 325 } 326 } 327 | Popular Tags |