Source code: org/apache/axis/encoding/ser/SimpleListSerializer.java
1 /*
2 * Copyright 2001,2004 The Apache Software Foundation.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
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 /**
38 * Serializer for
39 * <xsd:simpleType ...>
40 * <xsd:list itemType="...">
41 * </xsd:simpleType>
42 * based on SimpleSerializer
43 *
44 * @author Ias <iasandcb@tmax.co.kr>
45 */
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 /**
65 * Serialize a list of primitives or simple values.
66 */
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 // get any attributes
76 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 // We could have separate serializers/deserializers to take
92 // care of Float/Double cases, but it makes more sence to
93 // put them here with the rest of the java lang primitives.
94
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 // Find each property that is an attribute
148 // and add it to our attribute list
149 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 // skip it if its not an attribute
156 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 // add to our attributes
167 Object propValue = propertyDescriptor[i].get(value);
168 // If the property value does not exist, don't serialize
169 // the attribute. In the future, the decision to serializer
170 // the attribute may be more sophisticated. For example, don't
171 // serialize if the attribute matches the default value.
172 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 // no attributes
188 return attrs;
189 }
190
191 return attrs;
192 }
193
194 public String getMechanismType() { return Constants.AXIS_SAX; }
195
196 /**
197 * Return XML schema for the specified type, suitable for insertion into
198 * the <types> element of a WSDL document, or underneath an
199 * <element> or <attribute> declaration.
200 *
201 * @param javaType the Java Class we're writing out schema for
202 * @param types the Java2WSDL Types object which holds the context
203 * for the WSDL being generated.
204 * @return a type element containing a schema simpleType/complexType
205 * @see org.apache.axis.wsdl.fromJava.Types
206 */
207 public Element writeSchema(Class javaType, Types types) throws Exception {
208 // Let the caller generate WSDL if this is not a SimpleType
209 if (!SimpleType.class.isAssignableFrom(javaType))
210 return null;
211
212 // ComplexType representation of SimpleType bean class
213 Element complexType = types.createElement("complexType");
214 types.writeSchemaElementDecl(xmlType, complexType);
215 complexType.setAttribute("name", xmlType.getLocalPart());
216
217 // Produce simpleContent extending base type.
218 Element simpleContent = types.createElement("simpleContent");
219 complexType.appendChild(simpleContent);
220 Element extension = types.createElement("extension");
221 simpleContent.appendChild(extension);
222
223 // Get the base type from the "value" element of the bean
224 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 // throw?
233 }
234 QName qname = field.getXmlName();
235 if (qname == null) {
236 // Use the default...
237 qname = new QName("", propName);
238 }
239
240 // write attribute element
241 Class fieldType = propertyDescriptor[i].getType();
242
243 // Attribute must be a simple type, enum or SimpleType
244 if (!types.isAcceptableAsAttribute(fieldType)) {
245 throw new AxisFault(Messages.getMessage("AttrNotSimpleType00",
246 propName,
247 fieldType.getName()));
248 }
249
250 // write attribute element
251 // TODO the attribute name needs to be preserved from the XML
252 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 // Attribute must extend a simple type, enum or SimpleType
266 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 // done
275 return complexType;
276
277 }
278 }