1 28 29 package org.jibx.extras; 30 31 import java.util.ArrayList ; 32 33 import org.jibx.runtime.IAliasable; 34 import org.jibx.runtime.IMarshallable; 35 import org.jibx.runtime.IMarshaller; 36 import org.jibx.runtime.IMarshallingContext; 37 import org.jibx.runtime.IUnmarshaller; 38 import org.jibx.runtime.IUnmarshallingContext; 39 import org.jibx.runtime.JiBXException; 40 import org.jibx.runtime.impl.MarshallingContext; 41 import org.jibx.runtime.impl.UnmarshallingContext; 42 43 54 55 public class ObjectArrayMapper 56 implements IMarshaller, IUnmarshaller, IAliasable { 57 58 private static final Object [] DUMMY_ARRAY = {}; 59 60 private String m_uri; 61 private int m_index; 62 private String m_name; 63 private ArrayList m_holder; 64 65 71 72 public ObjectArrayMapper() { 73 m_uri = null; 74 m_index = 0; 75 m_name = null; 76 } 77 78 88 89 public ObjectArrayMapper(String uri, int index, String name) { 90 m_uri = uri; 91 m_index = index; 92 m_name = name; 93 } 94 95 98 99 public boolean isExtension(int index) { 100 return false; 101 } 102 103 107 108 public void marshal(Object obj, IMarshallingContext ictx) 109 throws JiBXException { 110 111 if (obj == null) { 113 if (m_name == null) { 114 throw new JiBXException 115 ("null array not allowed without wrapper"); 116 } 117 } else if (!(DUMMY_ARRAY.getClass().isInstance(obj))) { 118 throw new JiBXException("Invalid object type for marshaller"); 119 } else if (!(ictx instanceof MarshallingContext)) { 120 throw new JiBXException("Invalid object type for marshaller"); 121 } else { 122 123 MarshallingContext ctx = (MarshallingContext)ictx; 125 Object [] array = (Object [])obj; 126 if (m_name != null) { 127 ctx.startTag(m_index, m_name); 128 } 129 130 for (int i = 0; i < array.length; i++) { 132 Object item = array[i]; 133 if (item instanceof IMarshallable) { 134 ((IMarshallable)item).marshal(ctx); 135 } else if (item == null) { 136 throw new JiBXException("Null value at offset " + i + 137 " not supported"); 138 } else { 139 throw new JiBXException("Array value of type " + 140 item.getClass().getName() + " at offset " + i + 141 " is not marshallable"); 142 } 143 } 144 145 if (m_name != null) { 147 ctx.endTag(m_index, m_name); 148 } 149 150 } 151 } 152 153 156 157 public boolean isPresent(IUnmarshallingContext ctx) throws JiBXException { 158 return ctx.isAt(m_uri, m_name); 159 } 160 161 165 166 public Object unmarshal(Object obj, IUnmarshallingContext ictx) 167 throws JiBXException { 168 169 UnmarshallingContext ctx = (UnmarshallingContext)ictx; 171 if (m_name != null) { 172 if (ctx.isAt(m_uri, m_name)) { 173 ctx.parsePastStartTag(m_uri, m_name); 174 } else { 175 return null; 176 } 177 } 178 179 if (m_holder == null) { 181 m_holder = new ArrayList (); 182 } 183 184 while (!ctx.isEnd()) { 186 Object item = ctx.unmarshalElement(); 187 m_holder.add(item); 188 } 189 190 if (m_name != null) { 192 ctx.parsePastEndTag(m_uri, m_name); 193 } 194 195 Object [] result = m_holder.toArray(); 197 m_holder.clear(); 198 return result; 199 } 200 } | Popular Tags |