1 28 29 package example14; 30 31 import java.util.HashMap ; 32 import java.util.Iterator ; 33 import java.util.Map ; 34 35 import org.jibx.runtime.IAliasable; 36 import org.jibx.runtime.IMarshallable; 37 import org.jibx.runtime.IMarshaller; 38 import org.jibx.runtime.IMarshallingContext; 39 import org.jibx.runtime.IUnmarshaller; 40 import org.jibx.runtime.IUnmarshallingContext; 41 import org.jibx.runtime.JiBXException; 42 import org.jibx.runtime.impl.MarshallingContext; 43 import org.jibx.runtime.impl.UnmarshallingContext; 44 45 public class HashMapper implements IMarshaller, IUnmarshaller, IAliasable 46 { 47 private static final String SIZE_ATTRIBUTE_NAME = "size"; 48 private static final String ENTRY_ELEMENT_NAME = "entry"; 49 private static final String KEY_ATTRIBUTE_NAME = "key"; 50 private static final int DEFAULT_SIZE = 10; 51 52 private String m_uri; 53 private int m_index; 54 private String m_name; 55 56 public HashMapper() { 57 m_uri = null; 58 m_index = 0; 59 m_name = "hashmap"; 60 } 61 62 public HashMapper(String uri, int index, String name) { 63 m_uri = uri; 64 m_index = index; 65 m_name = name; 66 } 67 68 71 72 public boolean isExtension(int index) { 73 return false; 74 } 75 76 80 81 public void marshal(Object obj, IMarshallingContext ictx) 82 throws JiBXException { 83 84 if (!(obj instanceof HashMap )) { 86 throw new JiBXException("Invalid object type for marshaller"); 87 } else if (!(ictx instanceof MarshallingContext)) { 88 throw new JiBXException("Invalid object type for marshaller"); 89 } else { 90 91 MarshallingContext ctx = (MarshallingContext)ictx; 93 HashMap map = (HashMap )obj; 94 ctx.startTagAttributes(m_index, m_name). 95 attribute(m_index, SIZE_ATTRIBUTE_NAME, map.size()). 96 closeStartContent(); 97 98 Iterator iter = map.entrySet().iterator(); 100 while (iter.hasNext()) { 101 Map.Entry entry = (Map.Entry )iter.next(); 102 ctx.startTagAttributes(m_index, ENTRY_ELEMENT_NAME); 103 if (entry.getKey() != null) { 104 ctx.attribute(m_index, KEY_ATTRIBUTE_NAME, 105 entry.getKey().toString()); 106 } 107 ctx.closeStartContent(); 108 if (entry.getValue() instanceof IMarshallable) { 109 ((IMarshallable)entry.getValue()).marshal(ctx); 110 ctx.endTag(m_index, ENTRY_ELEMENT_NAME); 111 } else { 112 throw new JiBXException("Mapped value is not marshallable"); 113 } 114 } 115 116 ctx.endTag(m_index, m_name); 118 } 119 } 120 121 124 125 public boolean isPresent(IUnmarshallingContext ctx) throws JiBXException { 126 return ctx.isAt(m_uri, m_name); 127 } 128 129 133 134 public Object unmarshal(Object obj, IUnmarshallingContext ictx) 135 throws JiBXException { 136 137 UnmarshallingContext ctx = (UnmarshallingContext)ictx; 139 if (!ctx.isAt(m_uri, m_name)) { 140 ctx.throwStartTagNameError(m_uri, m_name); 141 } 142 143 int size = ctx.attributeInt(m_uri, SIZE_ATTRIBUTE_NAME, DEFAULT_SIZE); 145 HashMap map = (HashMap )obj; 146 if (map == null) { 147 map = new HashMap (size); 148 } 149 150 ctx.parsePastStartTag(m_uri, m_name); 152 while (ctx.isAt(m_uri, ENTRY_ELEMENT_NAME)) { 153 Object key = ctx.attributeText(m_uri, KEY_ATTRIBUTE_NAME, null); 154 ctx.parsePastStartTag(m_uri, ENTRY_ELEMENT_NAME); 155 Object value = ctx.unmarshalElement(); 156 map.put(key, value); 157 ctx.parsePastEndTag(m_uri, ENTRY_ELEMENT_NAME); 158 } 159 ctx.parsePastEndTag(m_uri, m_name); 160 return map; 161 } 162 } | Popular Tags |