1 28 29 package org.jibx.extras; 30 31 import java.lang.reflect.Array ; 32 import java.util.ArrayList ; 33 34 import org.jibx.runtime.IAliasable; 35 import org.jibx.runtime.IMarshallable; 36 import org.jibx.runtime.IMarshaller; 37 import org.jibx.runtime.IMarshallingContext; 38 import org.jibx.runtime.IUnmarshaller; 39 import org.jibx.runtime.IUnmarshallingContext; 40 import org.jibx.runtime.JiBXException; 41 import org.jibx.runtime.impl.MarshallingContext; 42 import org.jibx.runtime.impl.UnmarshallingContext; 43 44 55 56 public class TypedArrayMapper 57 implements IMarshaller, IUnmarshaller, IAliasable { 58 59 private static final Object [] DUMMY_ARRAY = {}; 60 61 private String m_uri; 62 private int m_index; 63 private String m_name; 64 private Object [] m_baseArray; 65 private ArrayList m_holder; 66 67 78 79 public TypedArrayMapper(String uri, int index, String name, String type) { 80 81 m_uri = uri; 83 m_index = index; 84 m_name = name; 85 86 int dimen = 0; 88 while (type.endsWith("[]")) { 89 type = type.substring(0, type.length()-2); 90 dimen++; 91 } 92 93 try { 95 96 Class clas = null; 98 ClassLoader loader = 99 Thread.currentThread().getContextClassLoader(); 100 if (loader != null) { 101 try { 102 clas = loader.loadClass(type); 103 } catch (ClassNotFoundException e) { } 104 } 105 if (clas == null) { 106 107 clas = UnmarshallingContext.class.getClassLoader(). 109 loadClass(type); 110 } 111 112 int[] dimens = new int[dimen]; 114 m_baseArray = (Object [])Array.newInstance(clas, dimens); 115 116 } catch (ClassNotFoundException e) { 117 throw new IllegalArgumentException 118 ("Error loading array item class " + type + ": " + 119 e.getMessage()); 120 } 121 } 122 123 131 132 public TypedArrayMapper(String type) { 133 this(null, 0, null, type); 134 } 135 136 139 140 public boolean isExtension(int index) { 141 return false; 142 } 143 144 148 149 public void marshal(Object obj, IMarshallingContext ictx) 150 throws JiBXException { 151 152 if (obj == null) { 154 if (m_name == null) { 155 throw new JiBXException 156 ("null array not allowed without wrapper"); 157 } 158 } else if (!(ictx instanceof MarshallingContext)) { 159 throw new JiBXException("Marshalling context not of expected type"); 160 } else { 161 162 Class clas = obj.getClass(); 164 if (!clas.isArray() || !IMarshallable.class. 165 isAssignableFrom(clas.getComponentType())) { 166 throw new JiBXException("Invalid object type for marshaller"); 167 } else { 168 169 MarshallingContext ctx = (MarshallingContext)ictx; 171 Object [] array = (Object [])obj; 172 if (m_name != null) { 173 ctx.startTag(m_index, m_name); 174 } 175 176 for (int i = 0; i < array.length; i++) { 178 Object item = array[i]; 179 if (item == null) { 180 throw new JiBXException("Null value at offset " + i + 181 " not supported"); 182 } else { 183 ((IMarshallable)item).marshal(ctx); 184 } 185 } 186 187 if (m_name != null) { 189 ctx.endTag(m_index, m_name); 190 } 191 192 } 193 } 194 } 195 196 199 200 public boolean isPresent(IUnmarshallingContext ctx) throws JiBXException { 201 return ctx.isAt(m_uri, m_name); 202 } 203 204 208 209 public Object unmarshal(Object obj, IUnmarshallingContext ictx) 210 throws JiBXException { 211 212 UnmarshallingContext ctx = (UnmarshallingContext)ictx; 214 if (m_name != null) { 215 if (ctx.isAt(m_uri, m_name)) { 216 ctx.parsePastStartTag(m_uri, m_name); 217 } else { 218 return null; 219 } 220 } 221 222 if (m_holder == null) { 224 m_holder = new ArrayList (); 225 } 226 227 while (!ctx.isEnd()) { 229 Object item = ctx.unmarshalElement(); 230 m_holder.add(item); 231 } 232 233 if (m_name != null) { 235 ctx.parsePastEndTag(m_uri, m_name); 236 } 237 238 Object [] result = m_holder.toArray(m_baseArray); 240 m_holder.clear(); 241 return result; 242 } 243 } | Popular Tags |