KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > axis > encoding > ser > SimpleListSerializer


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 JavaDoc;
19 import java.lang.reflect.Array JavaDoc;
20
21 import javax.xml.namespace.QName JavaDoc;
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 JavaDoc;
34 import org.xml.sax.Attributes JavaDoc;
35 import org.xml.sax.helpers.AttributesImpl JavaDoc;
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 JavaDoc xmlType;
48     public Class JavaDoc javaType;
49
50     private BeanPropertyDescriptor[] propertyDescriptor = null;
51     private TypeDesc typeDesc = null;
52
53     public SimpleListSerializer(Class JavaDoc javaType, QName JavaDoc xmlType) {
54         this.xmlType = xmlType;
55         this.javaType = javaType;
56     }
57     public SimpleListSerializer(Class JavaDoc javaType, QName JavaDoc 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 JavaDoc name, Attributes JavaDoc attributes,
68                           Object JavaDoc value, SerializationContext context)
69         throws IOException JavaDoc
70     {
71         if (value != null && value.getClass() == java.lang.Object JavaDoc.class) {
72             throw new IOException JavaDoc(Messages.getMessage("cantSerialize02"));
73         }
74
75         // get any attributes
76
if (value instanceof SimpleType)
77             attributes = getObjectAttributes(value, attributes, context);
78
79         String JavaDoc 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 JavaDoc getValueAsString(Object JavaDoc 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 JavaDoc result = new StringBuffer JavaDoc();
97       for (int i = 0; i < length; i++) {
98         Object JavaDoc object = Array.get(value, i);
99         if (object instanceof Float JavaDoc ||
100             object instanceof Double JavaDoc) {
101           double data = 0.0;
102           if (object instanceof Float JavaDoc) {
103               data = ((Float JavaDoc) object).doubleValue();
104           } else {
105               data = ((Double JavaDoc) 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 JavaDoc) {
119           result.append(QNameSerializer.qName2String((QName JavaDoc)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 JavaDoc getObjectAttributes(Object JavaDoc value,
132                                            Attributes JavaDoc attributes,
133                                            SerializationContext context) {
134         if (typeDesc == null || !typeDesc.hasAttributes())
135             return attributes;
136
137         AttributesImpl JavaDoc attrs;
138         if (attributes == null) {
139             attrs = new AttributesImpl JavaDoc();
140         } else if (attributes instanceof AttributesImpl JavaDoc) {
141             attrs = (AttributesImpl JavaDoc)attributes;
142         } else {
143             attrs = new AttributesImpl JavaDoc(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 JavaDoc 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 JavaDoc qname = field.getXmlName();
160                 if (qname == null) {
161                     qname = new QName JavaDoc("", propName);
162                 }
163
164                 if (propertyDescriptor[i].isReadable() &&
165                     !propertyDescriptor[i].isIndexed()) {
166                     // add to our attributes
167
Object JavaDoc 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 JavaDoc propString = getValueAsString(propValue, context);
174
175                         String JavaDoc namespace = qname.getNamespaceURI();
176                         String JavaDoc localName = qname.getLocalPart();
177
178                         attrs.addAttribute(namespace,
179                                            localName,
180                                            context.qName2String(qname),
181                                            "CDATA",
182                                            propString);
183                     }
184                 }
185             }
186         } catch (Exception JavaDoc e) {
187             // no attributes
188
return attrs;
189         }
190
191         return attrs;
192     }
193
194     public String JavaDoc getMechanismType() { return Constants.AXIS_SAX; }
195
196     /**
197      * Return XML schema for the specified type, suitable for insertion into
198      * the &lt;types&gt; element of a WSDL document, or underneath an
199      * &lt;element&gt; or &lt;attribute&gt; 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 JavaDoc writeSchema(Class JavaDoc javaType, Types types) throws Exception JavaDoc {
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 JavaDoc complexType = types.createElement("complexType");
214         types.writeSchemaElementDecl(xmlType, complexType);
215         complexType.setAttribute("name", xmlType.getLocalPart());
216
217         // Produce simpleContent extending base type.
218
Element JavaDoc simpleContent = types.createElement("simpleContent");
219         complexType.appendChild(simpleContent);
220         Element JavaDoc extension = types.createElement("extension");
221         simpleContent.appendChild(extension);
222
223         // Get the base type from the "value" element of the bean
224
String JavaDoc base = "string";
225         for (int i=0; i<propertyDescriptor.length; i++) {
226             String JavaDoc 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 JavaDoc qname = field.getXmlName();
235                         if (qname == null) {
236                             // Use the default...
237
qname = new QName JavaDoc("", propName);
238                         }
239
240                         // write attribute element
241
Class JavaDoc 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 JavaDoc 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 JavaDoc 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 }
279
Popular Tags