1 16 17 package org.apache.axis.encoding.ser; 18 19 import org.apache.axis.AxisFault; 20 import org.apache.axis.Constants; 21 import org.apache.axis.description.FieldDesc; 22 import org.apache.axis.description.TypeDesc; 23 import org.apache.axis.encoding.SerializationContext; 24 import org.apache.axis.encoding.SimpleValueSerializer; 25 import org.apache.axis.encoding.SimpleType; 26 import org.apache.axis.utils.BeanPropertyDescriptor; 27 import org.apache.axis.utils.BeanUtils; 28 import org.apache.axis.utils.Messages; 29 import org.apache.axis.utils.JavaUtils; 30 import org.apache.axis.wsdl.fromJava.Types; 31 import org.w3c.dom.Element ; 32 import org.xml.sax.Attributes ; 33 import org.xml.sax.helpers.AttributesImpl ; 34 35 import javax.xml.namespace.QName ; 36 import java.io.IOException ; 37 38 43 public class SimpleSerializer implements SimpleValueSerializer { 44 public QName xmlType; 45 public Class javaType; 46 47 private BeanPropertyDescriptor[] propertyDescriptor = null; 48 private TypeDesc typeDesc = null; 49 public static final String VALUE_PROPERTY = "_value"; 50 51 public SimpleSerializer(Class javaType, QName xmlType) { 52 this.xmlType = xmlType; 53 this.javaType = javaType; 54 init(); 55 } 56 public SimpleSerializer(Class javaType, QName xmlType, TypeDesc typeDesc) { 57 this.xmlType = xmlType; 58 this.javaType = javaType; 59 this.typeDesc = typeDesc; 60 init(); 61 } 62 63 66 private void init() { 67 if (typeDesc == null) { 69 typeDesc = TypeDesc.getTypeDescForClass(javaType); 70 } 71 if (typeDesc != null) { 74 propertyDescriptor = typeDesc.getPropertyDescriptors(); 75 } else if (!JavaUtils.isBasic(javaType)) { 76 propertyDescriptor = BeanUtils.getPd(javaType, null); 77 } 78 } 79 80 87 public void serialize(QName name, Attributes attributes, 88 Object value, SerializationContext context) 89 throws IOException 90 { 91 if (value != null && value.getClass() == java.lang.Object .class) { 92 throw new IOException (Messages.getMessage("cantSerialize02")); 93 } 94 95 attributes = getObjectAttributes(value, attributes, context); 97 98 String valueStr = null; 99 if (value != null) { 100 valueStr = getValueAsString(value, context); 101 } 102 context.startElement(name, attributes); 103 if (valueStr != null) { 104 context.writeSafeString(valueStr); 105 } 106 context.endElement(); 107 } 108 109 public String getValueAsString(Object value, SerializationContext context) { 110 if (value instanceof Float || 114 value instanceof Double ) { 115 double data = 0.0; 116 if (value instanceof Float ) { 117 data = ((Float ) value).doubleValue(); 118 } else { 119 data = ((Double ) value).doubleValue(); 120 } 121 if (Double.isNaN(data)) { 122 return "NaN"; 123 } else if (data == Double.POSITIVE_INFINITY) { 124 return "INF"; 125 } else if (data == Double.NEGATIVE_INFINITY) { 126 return "-INF"; 127 } 128 } else if (value instanceof QName ) { 129 return context.qName2String((QName )value); 130 } 131 132 if (propertyDescriptor != null && !(value instanceof SimpleType)) { 133 BeanPropertyDescriptor pd = BeanUtils.getSpecificPD(propertyDescriptor, "_value"); 134 if(pd != null) { 135 try { 136 return pd.get(value).toString(); 137 } catch (Exception e) { 138 } 139 } 140 } 141 return value.toString(); 142 } 143 144 private Attributes getObjectAttributes(Object value, 145 Attributes attributes, 146 SerializationContext context) { 147 if (typeDesc != null && !typeDesc.hasAttributes()) 148 return attributes; 149 150 AttributesImpl attrs; 151 if (attributes == null) { 152 attrs = new AttributesImpl (); 153 } else if (attributes instanceof AttributesImpl ) { 154 attrs = (AttributesImpl )attributes; 155 } else { 156 attrs = new AttributesImpl (attributes); 157 } 158 159 try { 160 for (int i = 0; 163 propertyDescriptor != null && i < propertyDescriptor.length; 164 i++) { 165 String propName = propertyDescriptor[i].getName(); 166 if (propName.equals("class")) 167 continue; 168 169 QName qname = null; 170 if(typeDesc != null) { 171 FieldDesc field = typeDesc.getFieldByName(propName); 172 if (field == null || field.isElement()) 174 continue; 175 qname = field.getXmlName(); 176 } else { 177 if(propName.equals(VALUE_PROPERTY)) 178 continue; 179 } 180 if (qname == null) { 181 qname = new QName ("", propName); 182 } 183 184 if (propertyDescriptor[i].isReadable() && 185 !propertyDescriptor[i].isIndexed()) { 186 Object propValue = propertyDescriptor[i].get(value); 188 if (propValue != null) { 193 String propString = getValueAsString(propValue, context); 194 195 String namespace = qname.getNamespaceURI(); 196 String localName = qname.getLocalPart(); 197 198 attrs.addAttribute(namespace, 199 localName, 200 context.qName2String(qname), 201 "CDATA", 202 propString); 203 } 204 } 205 } 206 } catch (Exception e) { 207 return attrs; 209 } 210 211 return attrs; 212 } 213 214 public String getMechanismType() { return Constants.AXIS_SAX; } 215 216 227 public Element writeSchema(Class javaType, Types types) throws Exception { 228 Element complexType = types.createElement("complexType"); 230 types.writeSchemaTypeDecl(xmlType, complexType); 231 complexType.setAttribute("name", xmlType.getLocalPart()); 232 233 Element simpleContent = types.createElement("simpleContent"); 235 complexType.appendChild(simpleContent); 236 Element extension = types.createElement("extension"); 237 simpleContent.appendChild(extension); 238 239 String base = "string"; 241 for (int i=0; propertyDescriptor != null && i<propertyDescriptor.length; i++) { 242 String propName = propertyDescriptor[i].getName(); 243 if (!propName.equals("value")) { 244 if (typeDesc != null) { 245 FieldDesc field = typeDesc.getFieldByName(propName); 246 if (field != null) { 247 if (field.isElement()) { 248 } 250 QName qname = field.getXmlName(); 251 if (qname == null) { 252 qname = new QName ("", propName); 254 } 255 256 Class fieldType = propertyDescriptor[i].getType(); 258 259 if (!types.isAcceptableAsAttribute(fieldType)) { 261 throw new AxisFault(Messages.getMessage("AttrNotSimpleType00", 262 propName, 263 fieldType.getName())); 264 } 265 266 Element elem = types.createAttributeElement(propName, 269 fieldType, 270 field.getXmlType(), 271 false, 272 extension.getOwnerDocument()); 273 extension.appendChild(elem); 274 } 275 } 276 continue; 277 } 278 279 BeanPropertyDescriptor bpd = propertyDescriptor[i]; 280 Class type = bpd.getType(); 281 if (!types.isAcceptableAsAttribute(type)) { 283 throw new AxisFault(Messages.getMessage("AttrNotSimpleType01", 284 type.getName())); 285 } 286 base = types.writeType(type); 287 extension.setAttribute("base", base); 288 } 289 290 return complexType; 292 293 } 294 } 295 | Popular Tags |