1 28 29 package org.jibx.extras; 30 31 import java.math.BigDecimal ; 32 import java.math.BigInteger ; 33 import java.util.Date ; 34 import java.util.HashMap ; 35 import java.util.Iterator ; 36 import java.util.Map ; 37 38 import org.jibx.runtime.EnumSet; 39 import org.jibx.runtime.IAliasable; 40 import org.jibx.runtime.IMarshaller; 41 import org.jibx.runtime.IMarshallingContext; 42 import org.jibx.runtime.IUnmarshaller; 43 import org.jibx.runtime.IUnmarshallingContext; 44 import org.jibx.runtime.IXMLWriter; 45 import org.jibx.runtime.JiBXException; 46 import org.jibx.runtime.Utility; 47 import org.jibx.runtime.impl.MarshallingContext; 48 import org.jibx.runtime.impl.UnmarshallingContext; 49 50 95 96 public class HashMapperStringToSchemaType 97 implements IMarshaller, IUnmarshaller, IAliasable { 98 99 102 private static final String SIZE_ATTRIBUTE_NAME = "size"; 103 private static final String ENTRY_ELEMENT_NAME = "entry"; 104 private static final String KEY_ATTRIBUTE_NAME = "key"; 105 private static final String TYPE_ATTRIBUTE_NAME = "type"; 106 private static final String XSI_NAMESPACE_URI = 107 "http://www.w3.org/2001/XMLSchema-instance"; 108 private static final String XSD_NAMESPACE_URI = 109 "http://www.w3.org/2001/XMLSchema"; 110 private static final String [] SCHEMA_NAMESPACE_URIS = 111 { 112 XSI_NAMESPACE_URI, XSD_NAMESPACE_URI 113 }; 114 private static final String XSI_NAMESPACE_PREFIX = "xsi"; 115 private static final String XSD_NAMESPACE_PREFIX = "xsd"; 116 private static final String [] SCHEMA_NAMESPACE_PREFIXES = 117 { 118 XSI_NAMESPACE_PREFIX, XSD_NAMESPACE_PREFIX 119 }; 120 private static final String XSD_PREFIX_LEAD = "xsd:"; 121 private static final int DEFAULT_SIZE = 10; 122 123 126 public static final int BOOLEAN_TYPE = 0; 128 public static final int BYTE_TYPE = 1; 129 public static final int DOUBLE_TYPE = 2; 130 public static final int FLOAT_TYPE = 3; 131 public static final int INT_TYPE = 4; 132 public static final int LONG_TYPE = 5; 133 public static final int SHORT_TYPE = 6; 134 public static final int DATETIME_TYPE = 7; 135 public static final int DATE_TYPE = 8; 136 public static final int TIME_TYPE = 9; 137 public static final int DECIMAL_TYPE = 10; 138 public static final int INTEGER_TYPE = 11; 139 public static final int BYTERRAY_TYPE = 12; 140 public static final int STRING_TYPE = 13; 141 142 private static final EnumSet s_javaTypesEnum = new EnumSet(BOOLEAN_TYPE, 144 new String [] { "java.lang.Boolean", "java.lang.Byte", 145 "java.lang.Double", "java.lang.Float", "java.lang.Integer", 146 "java.lang.Long", "java.lang.Short", "java.util.Date", "java.sql.Date", 147 "java.sql.Time", "java.math.BigDecimal", "java.math.BigInteger", 148 "byte[]", "java.lang.String"} ); 149 150 private static final EnumSet s_schemaTypesEnum = new EnumSet(BOOLEAN_TYPE, 152 new String [] { "boolean", "byte", "double", "float", "int", "long", 153 "short", "dateTime", "date", "time", "decimal", "integer", 154 "base64Binary", "string"} ); 155 156 159 private String m_uri; 160 private int m_index; 161 private String m_name; 162 163 168 169 public HashMapperStringToSchemaType() { 170 m_uri = null; 171 m_index = 0; 172 m_name = "hashmap"; 173 } 174 175 186 187 public HashMapperStringToSchemaType(String uri, int index, String name) { 188 m_uri = uri; 189 m_index = index; 190 m_name = name; 191 } 192 193 196 197 public boolean isExtension(int index) { 198 return false; 199 } 200 201 205 206 public void marshal(Object obj, IMarshallingContext ictx) 207 throws JiBXException { 208 209 if (!(obj instanceof Map )) { 211 throw new JiBXException("Invalid object type for marshaller"); 212 } else if (!(ictx instanceof MarshallingContext)) { 213 throw new JiBXException("Invalid object type for marshaller"); 214 } else { 215 216 MarshallingContext ctx = (MarshallingContext)ictx; 218 IXMLWriter xwrite = ctx.getXmlWriter(); 219 int ixsi = xwrite.getNamespaces().length; 220 String [][] extens = xwrite.getExtensionNamespaces(); 221 if (extens != null) { 222 for (int i = 0; i < extens.length; i++) { 223 ixsi += extens[i].length; 224 } 225 } 226 xwrite.pushExtensionNamespaces(SCHEMA_NAMESPACE_URIS); 227 228 Map map = (Map )obj; 230 ctx.startTagNamespaces(m_index, m_name, new int[] { ixsi, ixsi+1 }, 231 SCHEMA_NAMESPACE_PREFIXES). 232 attribute(m_index, SIZE_ATTRIBUTE_NAME, map.size()). 233 closeStartContent(); 234 235 Iterator iter = map.entrySet().iterator(); 237 while (iter.hasNext()) { 238 239 Map.Entry entry = (Map.Entry )iter.next(); 241 Object value = entry.getValue(); 242 if (value != null) { 243 244 ctx.startTagAttributes(m_index, ENTRY_ELEMENT_NAME); 246 ctx.attribute(m_index, KEY_ATTRIBUTE_NAME, 247 entry.getKey().toString()); 248 249 String tname = value.getClass().getName(); 251 int type = s_javaTypesEnum.getValue(tname); 252 if (type < 0) { 253 throw new JiBXException("Value of type " + tname + 254 " with key " + entry.getKey() + 255 " is not a supported type"); 256 } 257 258 ctx.attribute(ixsi, TYPE_ATTRIBUTE_NAME, 260 XSD_PREFIX_LEAD + s_schemaTypesEnum.getName(type)); 261 ctx.closeStartContent(); 262 263 switch (type) { 265 266 case BOOLEAN_TYPE: 267 ctx.content(Utility.serializeBoolean 268 (((Boolean )value).booleanValue())); 269 break; 270 271 case BYTE_TYPE: 272 ctx.content(Utility. 273 serializeByte(((Byte )value).byteValue())); 274 break; 275 276 case DOUBLE_TYPE: 277 ctx.content(Utility. 278 serializeDouble(((Double )value).doubleValue())); 279 break; 280 281 case FLOAT_TYPE: 282 ctx.content(Utility. 283 serializeFloat(((Float )value).floatValue())); 284 break; 285 286 case INT_TYPE: 287 ctx.content(((Integer )value).intValue()); 288 break; 289 290 case LONG_TYPE: 291 ctx.content(Utility. 292 serializeLong(((Long )value).longValue())); 293 break; 294 295 case SHORT_TYPE: 296 ctx.content(Utility. 297 serializeShort(((Short )value).shortValue())); 298 break; 299 300 case DATETIME_TYPE: 301 ctx.content(Utility.serializeDateTime((Date )value)); 302 break; 303 304 case DATE_TYPE: 305 ctx.content(Utility. 306 serializeSqlDate((java.sql.Date )value)); 307 break; 308 309 case TIME_TYPE: 310 ctx.content(Utility. 311 serializeSqlTime((java.sql.Time )value)); 312 break; 313 314 case BYTERRAY_TYPE: 315 ctx.content(Utility.serializeBase64((byte[])value)); 316 break; 317 318 case DECIMAL_TYPE: 319 case INTEGER_TYPE: 320 case STRING_TYPE: 321 ctx.content(value.toString()); 322 break; 323 } 324 325 ctx.endTag(m_index, ENTRY_ELEMENT_NAME); 327 } 328 } 329 330 ctx.endTag(m_index, m_name); 332 xwrite.popExtensionNamespaces(); 333 } 334 } 335 336 339 340 public boolean isPresent(IUnmarshallingContext ctx) throws JiBXException { 341 return ctx.isAt(m_uri, m_name); 342 } 343 344 348 349 public Object unmarshal(Object obj, IUnmarshallingContext ictx) 350 throws JiBXException { 351 352 UnmarshallingContext ctx = (UnmarshallingContext)ictx; 354 if (!ctx.isAt(m_uri, m_name)) { 355 ctx.throwStartTagNameError(m_uri, m_name); 356 } 357 358 int nscnt = ctx.getActiveNamespaceCount(); 360 String xsdlead = null; 361 for (int i = nscnt-1; i >= 0; i--) { 362 String uri = ctx.getActiveNamespaceUri(i); 363 if (XSD_NAMESPACE_URI.equals(uri)) { 364 String prefix = ctx.getActiveNamespacePrefix(i); 365 if (!"".equals(prefix)) { 366 xsdlead = prefix + ':'; 367 break; 368 } 369 } 370 } 371 if (xsdlead == null) { 372 throw new JiBXException 373 ("Missing required schema namespace declaration"); 374 } 375 376 int size = ctx.attributeInt(m_uri, SIZE_ATTRIBUTE_NAME, DEFAULT_SIZE); 378 Map map = (Map )obj; 379 if (map == null) { 380 map = new HashMap (size); 381 } 382 383 ctx.parsePastStartTag(m_uri, m_name); 385 String tdflt = xsdlead + "string"; 386 while (ctx.isAt(m_uri, ENTRY_ELEMENT_NAME)) { 387 388 Object key = ctx.attributeText(m_uri, KEY_ATTRIBUTE_NAME, null); 390 String tname = ctx.attributeText(XSI_NAMESPACE_URI, 391 TYPE_ATTRIBUTE_NAME, tdflt); 392 393 int type = -1; 395 if (tname.startsWith(xsdlead)) { 396 type = s_schemaTypesEnum. 397 getValue(tname.substring(xsdlead.length())); 398 } 399 if (type < 0) { 400 throw new JiBXException("Value of type " + tname + 401 " with key " + key + " is not a supported type"); 402 } 403 404 String text = ctx.parseElementText(m_uri, ENTRY_ELEMENT_NAME); 406 Object value = null; 407 switch (type) { 408 409 case BOOLEAN_TYPE: 410 value = Utility.parseBoolean(text) ? 411 Boolean.TRUE : Boolean.FALSE; 412 break; 413 414 case BYTE_TYPE: 415 value = new Byte (Utility.parseByte(text)); 416 break; 417 418 case DOUBLE_TYPE: 419 value = new Double (Utility.parseDouble(text)); 420 break; 421 422 case FLOAT_TYPE: 423 value = new Float (Utility.parseFloat(text)); 424 break; 425 426 case INT_TYPE: 427 value = new Integer (Utility.parseInt(text)); 428 break; 429 430 case LONG_TYPE: 431 value = new Long (Utility.parseLong(text)); 432 break; 433 434 case SHORT_TYPE: 435 value = new Short (Utility.parseShort(text)); 436 break; 437 438 case DATETIME_TYPE: 439 value = Utility.deserializeDateTime(text); 440 break; 441 442 case DATE_TYPE: 443 value = Utility.deserializeSqlDate(text); 444 break; 445 446 case TIME_TYPE: 447 value = Utility.deserializeSqlTime(text); 448 break; 449 450 case BYTERRAY_TYPE: 451 value = Utility.deserializeBase64(text); 452 break; 453 454 case DECIMAL_TYPE: 455 value = new BigDecimal (text); 456 break; 457 458 case INTEGER_TYPE: 459 value = new BigInteger (text); 460 break; 461 462 case STRING_TYPE: 463 value = text; 464 break; 465 } 466 467 map.put(key, value); 469 } 470 471 ctx.parsePastEndTag(m_uri, m_name); 473 return map; 474 } 475 } | Popular Tags |