1 55 56 package org.jboss.axis.encoding.ser; 57 58 import org.jboss.axis.AxisFault; 59 import org.jboss.axis.Constants; 60 import org.jboss.axis.description.FieldDesc; 61 import org.jboss.axis.description.TypeDesc; 62 import org.jboss.axis.encoding.SerializationContext; 63 import org.jboss.axis.encoding.SimpleType; 64 import org.jboss.axis.encoding.SimpleValueSerializer; 65 import org.jboss.axis.utils.BeanPropertyDescriptor; 66 import org.jboss.axis.utils.BeanUtils; 67 import org.jboss.axis.utils.Messages; 68 import org.jboss.axis.wsdl.fromJava.Types; 69 import org.w3c.dom.Element ; 70 import org.xml.sax.Attributes ; 71 import org.xml.sax.helpers.AttributesImpl ; 72 73 import javax.xml.namespace.QName ; 74 import java.io.IOException ; 75 76 81 public class SimpleSerializer implements SimpleValueSerializer 82 { 83 public QName xmlType; 84 public Class javaType; 85 86 private BeanPropertyDescriptor[] propertyDescriptor = null; 87 private TypeDesc typeDesc = null; 88 89 public SimpleSerializer(Class javaType, QName xmlType) 90 { 91 this.xmlType = xmlType; 92 this.javaType = javaType; 93 init(); 94 } 95 96 public SimpleSerializer(Class javaType, QName xmlType, TypeDesc typeDesc) 97 { 98 this.xmlType = xmlType; 99 this.javaType = javaType; 100 this.typeDesc = typeDesc; 101 init(); 102 } 103 104 107 private void init() 108 { 109 if (SimpleType.class.isAssignableFrom(javaType)) 112 { 113 if (typeDesc == null) 115 { 116 typeDesc = TypeDesc.getTypeDescForClass(javaType); 117 } 118 if (typeDesc != null) 121 { 122 propertyDescriptor = typeDesc.getPropertyDescriptors(); 123 } 124 else 125 { 126 propertyDescriptor = BeanUtils.getPd(javaType, null); 127 } 128 } 129 } 130 131 138 public void serialize(QName name, Attributes attributes, 139 Object value, SerializationContext context) 140 throws IOException 141 { 142 if (value != null && value.getClass() == java.lang.Object .class) 143 { 144 throw new IOException (Messages.getMessage("cantSerialize02")); 145 } 146 147 if (value instanceof SimpleType) 149 attributes = getObjectAttributes(value, attributes, context); 150 151 152 if (name != null) 153 context.startElement(name, attributes); 154 155 if (value != null) 156 context.writeSafeString(getValueAsString(value, context)); 157 158 if (name != null) 159 context.endElement(); 160 } 161 162 public String getValueAsString(Object value, SerializationContext context) 163 { 164 if (value instanceof Float || 168 value instanceof Double ) 169 { 170 double data = 0.0; 171 if (value instanceof Float ) 172 { 173 data = ((Float )value).doubleValue(); 174 } 175 else 176 { 177 data = ((Double )value).doubleValue(); 178 } 179 if (Double.isNaN(data)) 180 { 181 return "NaN"; 182 } 183 else if (data == Double.POSITIVE_INFINITY) 184 { 185 return "INF"; 186 } 187 else if (data == Double.NEGATIVE_INFINITY) 188 { 189 return "-INF"; 190 } 191 } 192 193 return value.toString(); 194 } 195 196 private Attributes getObjectAttributes(Object value, 197 Attributes attributes, 198 SerializationContext context) 199 { 200 if (typeDesc == null || !typeDesc.hasAttributes()) 201 return attributes; 202 203 AttributesImpl attrs; 204 if (attributes == null) 205 { 206 attrs = new AttributesImpl (); 207 } 208 else if (attributes instanceof AttributesImpl ) 209 { 210 attrs = (AttributesImpl )attributes; 211 } 212 else 213 { 214 attrs = new AttributesImpl (attributes); 215 } 216 217 try 218 { 219 for (int i = 0; i < propertyDescriptor.length; i++) 222 { 223 String propName = propertyDescriptor[i].getName(); 224 if (propName.equals("class")) 225 continue; 226 227 FieldDesc field = typeDesc.getFieldByName(propName); 228 if (field == null || field.isElement()) 230 continue; 231 232 QName qname = field.getXmlName(); 233 if (qname == null) 234 { 235 qname = new QName ("", propName); 236 } 237 238 if (propertyDescriptor[i].isReadable() && 239 !propertyDescriptor[i].isIndexed()) 240 { 241 Object propValue = propertyDescriptor[i].get(value); 243 if (propValue != null) 248 { 249 String propString = getValueAsString(propValue, context); 250 251 String namespace = qname.getNamespaceURI(); 252 String localName = qname.getLocalPart(); 253 254 attrs.addAttribute(namespace, 255 localName, 256 context.qName2String(qname), 257 "CDATA", 258 propString); 259 } 260 } 261 } 262 } 263 catch (Exception e) 264 { 265 return attrs; 267 } 268 269 return attrs; 270 } 271 272 public String getMechanismType() 273 { 274 return Constants.AXIS_SAX; 275 } 276 277 288 public Element writeSchema(Class javaType, Types types) throws Exception 289 { 290 if (!SimpleType.class.isAssignableFrom(javaType)) 292 return null; 293 294 Element complexType = types.createElement("complexType"); 296 types.writeSchemaElement(xmlType, complexType); 297 complexType.setAttribute("name", xmlType.getLocalPart()); 298 299 Element simpleContent = types.createElement("simpleContent"); 301 complexType.appendChild(simpleContent); 302 Element extension = types.createElement("extension"); 303 simpleContent.appendChild(extension); 304 305 String base = "string"; 307 for (int i = 0; i < propertyDescriptor.length; i++) 308 { 309 String propName = propertyDescriptor[i].getName(); 310 if (!propName.equals("value")) 311 { 312 if (typeDesc != null) 313 { 314 FieldDesc field = typeDesc.getFieldByName(propName); 315 if (field != null) 316 { 317 if (field.isElement()) 318 { 319 } 321 QName qname = field.getXmlName(); 322 if (qname == null) 323 { 324 qname = new QName ("", propName); 326 } 327 328 Class fieldType = propertyDescriptor[i].getType(); 330 331 if (!types.isAcceptableAsAttribute(fieldType)) 333 { 334 throw new AxisFault(Messages.getMessage("AttrNotSimpleType00", 335 propName, 336 fieldType.getName())); 337 } 338 339 Element elem = types.createAttributeElement(propName, 342 fieldType, 343 field.getXmlType(), 344 false, 345 extension.getOwnerDocument()); 346 extension.appendChild(elem); 347 } 348 } 349 continue; 350 } 351 352 BeanPropertyDescriptor bpd = propertyDescriptor[i]; 353 Class type = bpd.getType(); 354 if (!types.isAcceptableAsAttribute(type)) 356 { 357 throw new AxisFault(Messages.getMessage("AttrNotSimpleType01", 358 type.getName())); 359 } 360 base = types.writeType(type); 361 extension.setAttribute("base", base); 362 } 363 364 return complexType; 366 367 } 368 } 369 | Popular Tags |