1 55 56 package org.jboss.axis.encoding.ser; 57 58 import org.jboss.axis.encoding.DeserializationContext; 59 import org.jboss.axis.encoding.DeserializerImpl; 60 import org.jboss.logging.Logger; 61 import org.xml.sax.Attributes ; 62 import org.xml.sax.SAXException ; 63 64 import javax.xml.namespace.QName ; 65 import java.lang.reflect.Array ; 66 import java.lang.reflect.Constructor ; 67 import java.util.ArrayList ; 68 import java.util.List ; 69 import java.util.StringTokenizer ; 70 71 76 public class ListDeserializer extends DeserializerImpl 77 { 78 private static Logger log = Logger.getLogger(ListDeserializer.class.getName()); 79 80 private List valueList = new ArrayList (); 81 82 private Class javaType; 83 private QName xmlType; 84 85 public ListDeserializer(Class javaType, QName xmlType) 86 { 87 this.javaType = javaType; 88 this.xmlType = xmlType; 89 } 90 91 103 public void onStartElement(String namespace, String localName, 104 String prefix, Attributes attributes, 105 DeserializationContext context) 106 throws SAXException 107 { 108 109 if (log.isDebugEnabled()) 110 { 111 log.debug("Enter: ListDeserializer::startElement()"); 112 } 113 114 if (context.isNil(attributes)) 115 { 116 return; 117 } 118 119 if (log.isDebugEnabled()) 120 { 121 log.debug("Exit: ListDeserializer::startElement()"); 122 } 123 } 124 125 133 public void onEndElement(String namespace, String localName, DeserializationContext context) throws SAXException 134 { 135 136 if (log.isDebugEnabled()) 137 { 138 log.debug("Enter: ListDeserializer::endElement()"); 139 } 140 141 Class componentType = javaType.getComponentType(); 142 143 Constructor ctor = null; 144 try 145 { 146 ctor = componentType.getConstructor(new Class []{String .class}); 148 } 149 catch (NoSuchMethodException e) 150 { 151 throw new IllegalStateException ("Cannot obtain string constructor for: " + componentType); 152 } 153 154 Object [] objArr = (Object [])Array.newInstance(componentType, valueList.size()); 156 157 String strValue = null; 159 try 160 { 161 for (int i = 0; i < valueList.size(); i++) 162 { 163 strValue = (String )valueList.get(i); 164 Object obj = ctor.newInstance(new Object []{strValue}); 165 objArr[i] = obj; 166 } 167 168 value = objArr; 169 } 170 catch (Exception e) 171 { 172 throw new IllegalArgumentException ("Cannot construct object with: " + strValue); 173 } 174 175 176 if (log.isDebugEnabled()) 177 { 178 log.debug("Exit: ListDeserializer::endElement()"); 179 } 180 } 181 182 185 public void characters(char[] p1, int p2, int p3) throws SAXException 186 { 187 super.characters(p1, p2, p3); 188 189 String strContent = new String (p1, p2, p3); 190 191 StringTokenizer st = new StringTokenizer (strContent); 192 while (st.hasMoreTokens()) 193 { 194 String nextToken = st.nextToken(); 195 Object item = getValueAsObject(nextToken); 196 valueList.add(item); 197 } 198 } 199 200 203 private Object getValueAsObject(String token) 204 { 205 return token; 206 } 207 } 208 | Popular Tags |