1 28 29 package org.jibx.extras; 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 95 96 public class HashMapperStringToComplex 97 implements IMarshaller, IUnmarshaller, IAliasable { 98 99 private static final int DEFAULT_SIZE = 10; 100 101 private String m_uri; 102 private int m_index; 103 private String m_name; 104 105 110 public HashMapperStringToComplex() { 111 m_uri = null; 112 m_index = 0; 113 m_name = "hashmap"; 114 } 115 116 127 public HashMapperStringToComplex(String uri, int index, String name) { 128 m_uri = uri; 129 m_index = index; 130 m_name = name; 131 } 132 133 140 protected String getSizeAttributeName() { 141 return "size"; 142 } 143 144 148 protected String getEntryElementName() { 149 return "entry"; 150 } 151 152 156 protected String getKeyAttributeName() { 157 return "key"; 158 } 159 160 163 public boolean isExtension(int index) { 164 return false; 165 } 166 167 171 public void marshal(Object obj, IMarshallingContext ictx) 172 throws JiBXException { 173 174 if (!(obj instanceof Map )) { 176 throw new JiBXException("Invalid object type for marshaller"); 177 } else if (!(ictx instanceof MarshallingContext)) { 178 throw new JiBXException("Invalid object type for marshaller"); 179 } else { 180 181 MarshallingContext ctx = (MarshallingContext)ictx; 183 Map map = (Map )obj; 184 ctx.startTagAttributes(m_index, m_name). 185 attribute(m_index, getSizeAttributeName(), map.size()). 186 closeStartContent(); 187 188 Iterator iter = map.entrySet().iterator(); 190 while (iter.hasNext()) { 191 Map.Entry entry = (Map.Entry )iter.next(); 192 ctx.startTagAttributes(m_index, getEntryElementName()); 193 ctx.attribute(m_index, getKeyAttributeName(), 194 entry.getKey().toString()); 195 ctx.closeStartContent(); 196 if (entry.getValue() instanceof IMarshallable) { 197 ((IMarshallable)entry.getValue()).marshal(ctx); 198 ctx.endTag(m_index, getEntryElementName()); 199 } else { 200 throw new JiBXException("Mapped value is not marshallable"); 201 } 202 } 203 204 ctx.endTag(m_index, m_name); 206 } 207 } 208 209 212 public boolean isPresent(IUnmarshallingContext ctx) throws JiBXException { 213 return ctx.isAt(m_uri, m_name); 214 } 215 216 220 public Object unmarshal(Object obj, IUnmarshallingContext ictx) 221 throws JiBXException { 222 223 UnmarshallingContext ctx = (UnmarshallingContext)ictx; 225 if (!ctx.isAt(m_uri, m_name)) { 226 ctx.throwStartTagNameError(m_uri, m_name); 227 } 228 229 int size = ctx.attributeInt(m_uri, 231 getSizeAttributeName(), DEFAULT_SIZE); 232 Map map = (Map )obj; 233 if (map == null) { 234 map = new HashMap (size); 235 } 236 237 ctx.parsePastStartTag(m_uri, m_name); 239 while (ctx.isAt(m_uri, getEntryElementName())) { 240 Object key = ctx.attributeText(m_uri, getKeyAttributeName(), null); 241 ctx.parsePastStartTag(m_uri, getEntryElementName()); 242 Object value = ctx.unmarshalElement(); 243 map.put(key, value); 244 ctx.parsePastEndTag(m_uri, getEntryElementName()); 245 } 246 ctx.parsePastEndTag(m_uri, m_name); 247 return map; 248 } 249 } | Popular Tags |