1 16 17 package org.apache.axis.encoding.ser; 18 import java.io.IOException ; 19 import java.lang.reflect.Array ; 20 21 import javax.xml.namespace.QName ; 22 23 import org.apache.axis.AxisFault; 24 import org.apache.axis.Constants; 25 import org.apache.axis.description.FieldDesc; 26 import org.apache.axis.description.TypeDesc; 27 import org.apache.axis.encoding.SerializationContext; 28 import org.apache.axis.encoding.SimpleType; 29 import org.apache.axis.encoding.SimpleValueSerializer; 30 import org.apache.axis.utils.BeanPropertyDescriptor; 31 import org.apache.axis.utils.Messages; 32 import org.apache.axis.wsdl.fromJava.Types; 33 import org.w3c.dom.Element ; 34 import org.xml.sax.Attributes ; 35 import org.xml.sax.helpers.AttributesImpl ; 36 37 46 public class SimpleListSerializer implements SimpleValueSerializer { 47 public QName xmlType; 48 public Class javaType; 49 50 private BeanPropertyDescriptor[] propertyDescriptor = null; 51 private TypeDesc typeDesc = null; 52 53 public SimpleListSerializer(Class javaType, QName xmlType) { 54 this.xmlType = xmlType; 55 this.javaType = javaType; 56 } 57 public SimpleListSerializer(Class javaType, QName xmlType, TypeDesc typeDesc) { 58 this.xmlType = xmlType; 59 this.javaType = javaType; 60 this.typeDesc = typeDesc; 61 } 62 63 64 67 public void serialize(QName name, Attributes attributes, 68 Object value, SerializationContext context) 69 throws IOException 70 { 71 if (value != null && value.getClass() == java.lang.Object .class) { 72 throw new IOException (Messages.getMessage("cantSerialize02")); 73 } 74 75 if (value instanceof SimpleType) 77 attributes = getObjectAttributes(value, attributes, context); 78 79 String strValue = null; 80 if (value != null) { 81 strValue = getValueAsString(value, context); 82 } 83 context.startElement(name, attributes); 84 if (strValue != null) { 85 context.writeSafeString(strValue); 86 } 87 context.endElement(); 88 } 89 90 public String getValueAsString(Object value, SerializationContext context) { 91 95 int length = Array.getLength(value); 96 StringBuffer result = new StringBuffer (); 97 for (int i = 0; i < length; i++) { 98 Object object = Array.get(value, i); 99 if (object instanceof Float || 100 object instanceof Double ) { 101 double data = 0.0; 102 if (object instanceof Float ) { 103 data = ((Float ) object).doubleValue(); 104 } else { 105 data = ((Double ) object).doubleValue(); 106 } 107 if (Double.isNaN(data)) { 108 result.append("NaN"); 109 } else if (data == Double.POSITIVE_INFINITY) { 110 result.append("INF"); 111 } else if (data == Double.NEGATIVE_INFINITY) { 112 result.append("-INF"); 113 } 114 else { 115 result.append(object.toString()); 116 } 117 } 118 else if (object instanceof QName ) { 119 result.append(QNameSerializer.qName2String((QName )object, context)); 120 } 121 else { 122 result.append(object.toString()); 123 } 124 if (i < length - 1) { 125 result.append(' '); 126 } 127 } 128 return result.toString(); 129 } 130 131 private Attributes getObjectAttributes(Object value, 132 Attributes attributes, 133 SerializationContext context) { 134 if (typeDesc == null || !typeDesc.hasAttributes()) 135 return attributes; 136 137 AttributesImpl attrs; 138 if (attributes == null) { 139 attrs = new AttributesImpl (); 140 } else if (attributes instanceof AttributesImpl ) { 141 attrs = (AttributesImpl )attributes; 142 } else { 143 attrs = new AttributesImpl (attributes); 144 } 145 146 try { 147 for (int i=0; i<propertyDescriptor.length; i++) { 150 String propName = propertyDescriptor[i].getName(); 151 if (propName.equals("class")) 152 continue; 153 154 FieldDesc field = typeDesc.getFieldByName(propName); 155 if (field == null || field.isElement()) 157 continue; 158 159 QName qname = field.getXmlName(); 160 if (qname == null) { 161 qname = new QName ("", propName); 162 } 163 164 if (propertyDescriptor[i].isReadable() && 165 !propertyDescriptor[i].isIndexed()) { 166 Object propValue = propertyDescriptor[i].get(value); 168 if (propValue != null) { 173 String propString = getValueAsString(propValue, context); 174 175 String namespace = qname.getNamespaceURI(); 176 String localName = qname.getLocalPart(); 177 178 attrs.addAttribute(namespace, 179 localName, 180 context.qName2String(qname), 181 "CDATA", 182 propString); 183 } 184 } 185 } 186 } catch (Exception e) { 187 return attrs; 189 } 190 191 return attrs; 192 } 193 194 public String getMechanismType() { return Constants.AXIS_SAX; } 195 196 207 public Element writeSchema(Class javaType, Types types) throws Exception { 208 if (!SimpleType.class.isAssignableFrom(javaType)) 210 return null; 211 212 Element complexType = types.createElement("complexType"); 214 types.writeSchemaElementDecl(xmlType, complexType); 215 complexType.setAttribute("name", xmlType.getLocalPart()); 216 217 Element simpleContent = types.createElement("simpleContent"); 219 complexType.appendChild(simpleContent); 220 Element extension = types.createElement("extension"); 221 simpleContent.appendChild(extension); 222 223 String base = "string"; 225 for (int i=0; i<propertyDescriptor.length; i++) { 226 String propName = propertyDescriptor[i].getName(); 227 if (!propName.equals("value")) { 228 if (typeDesc != null) { 229 FieldDesc field = typeDesc.getFieldByName(propName); 230 if (field != null) { 231 if (field.isElement()) { 232 } 234 QName qname = field.getXmlName(); 235 if (qname == null) { 236 qname = new QName ("", propName); 238 } 239 240 Class fieldType = propertyDescriptor[i].getType(); 242 243 if (!types.isAcceptableAsAttribute(fieldType)) { 245 throw new AxisFault(Messages.getMessage("AttrNotSimpleType00", 246 propName, 247 fieldType.getName())); 248 } 249 250 Element elem = types.createAttributeElement(propName, 253 fieldType, 254 field.getXmlType(), 255 false, 256 extension.getOwnerDocument()); 257 extension.appendChild(elem); 258 } 259 } 260 continue; 261 } 262 263 BeanPropertyDescriptor bpd = propertyDescriptor[i]; 264 Class type = bpd.getType(); 265 if (!types.isAcceptableAsAttribute(type)) { 267 throw new AxisFault(Messages.getMessage("AttrNotSimpleType01", 268 type.getName())); 269 } 270 base = types.writeType(type); 271 extension.setAttribute("base", base); 272 } 273 274 return complexType; 276 277 } 278 } 279 | Popular Tags |