KickJava   Java API By Example, From Geeks To Geeks.

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


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
19 import org.apache.axis.Constants;
20 import org.apache.axis.encoding.Base64;
21 import org.apache.axis.encoding.SerializationContext;
22 import org.apache.axis.encoding.SimpleValueSerializer;
23 import org.apache.axis.wsdl.fromJava.Types;
24 import org.w3c.dom.Element JavaDoc;
25 import org.xml.sax.Attributes JavaDoc;
26
27 import javax.xml.namespace.QName JavaDoc;
28 import java.io.IOException JavaDoc;
29
30 /**
31  * Serializer for Base64
32  *
33  * @author Sam Ruby <rubys@us.ibm.com>
34  * Modified by @author Rich Scheuerle <scheu@us.ibm.com>
35  * @see <a HREF="http://www.w3.org/TR/xmlschema-2/#base64Binary">XML Schema 3.2.16</a>
36  */

37 public class Base64Serializer implements SimpleValueSerializer {
38
39     public QName JavaDoc xmlType;
40     public Class JavaDoc javaType;
41     public Base64Serializer(Class JavaDoc javaType, QName JavaDoc xmlType) {
42         this.xmlType = xmlType;
43         this.javaType = javaType;
44     }
45
46     /**
47      * Serialize a base64 quantity.
48      */

49     public void serialize(QName JavaDoc name, Attributes JavaDoc attributes,
50                           Object JavaDoc value, SerializationContext context)
51         throws IOException JavaDoc
52     {
53         context.startElement(name, attributes);
54         context.writeString(getValueAsString(value, context));
55         context.endElement();
56     }
57
58     public String JavaDoc getValueAsString(Object JavaDoc value, SerializationContext context) {
59         byte[] data = null;
60         if (javaType == byte[].class) {
61             data = (byte[]) value;
62         } else {
63             data = new byte[ ((Byte JavaDoc[]) value).length ];
64             for (int i=0; i<data.length; i++) {
65                 Byte JavaDoc b = ((Byte JavaDoc[]) value)[i];
66                 if (b != null)
67                     data[i] = b.byteValue();
68             }
69         }
70
71         return Base64.encode(data, 0, data.length);
72     }
73
74     public String JavaDoc getMechanismType() { return Constants.AXIS_SAX; }
75
76     /**
77      * Return XML schema for the specified type, suitable for insertion into
78      * the &lt;types&gt; element of a WSDL document, or underneath an
79      * &lt;element&gt; or &lt;attribute&gt; declaration.
80      *
81      * @param javaType the Java Class we're writing out schema for
82      * @param types the Java2WSDL Types object which holds the context
83      * for the WSDL being generated.
84      * @return a type element containing a schema simpleType/complexType
85      * @see org.apache.axis.wsdl.fromJava.Types
86      */

87     public Element JavaDoc writeSchema(Class JavaDoc javaType, Types types) throws Exception JavaDoc {
88         return null;
89     }
90 }
91
Popular Tags