KickJava   Java API By Example, From Geeks To Geeks.

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


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.SerializationContext;
21 import org.apache.axis.encoding.SimpleValueSerializer;
22 import org.apache.axis.wsdl.fromJava.Types;
23 import org.w3c.dom.Element JavaDoc;
24 import org.xml.sax.Attributes JavaDoc;
25
26 import javax.xml.namespace.QName JavaDoc;
27 import java.io.IOException JavaDoc;
28
29 /**
30  * Serializer for QNames.
31  */

32 public class QNameSerializer implements SimpleValueSerializer {
33
34     /**
35      * Serialize a QName.
36      */

37     public void serialize(QName JavaDoc name, Attributes JavaDoc attributes,
38                           Object JavaDoc value, SerializationContext context)
39         throws IOException JavaDoc
40     {
41         // NOTE: getValueAsString has the side-effect of priming the context
42
// with the QName's namespace, so it must be called BEFORE context.startElement.
43
String JavaDoc qnameString = getValueAsString(value, context);
44         context.startElement(name, attributes);
45         context.writeString(qnameString);
46         context.endElement();
47     }
48
49     public static String JavaDoc qName2String(QName JavaDoc qname,
50                                       SerializationContext context) {
51         String JavaDoc str = context.qName2String(qname);
52         // work around for default namespace
53
if (str == qname.getLocalPart()) {
54             String JavaDoc namespace = qname.getNamespaceURI();
55             if (namespace != null && namespace.length() > 0) {
56                 String JavaDoc prefix =
57                     context.getPrefixForURI(qname.getNamespaceURI(),
58                                             null, true);
59                 return prefix + ":" + str;
60             }
61         }
62         return str;
63     }
64
65     public String JavaDoc getValueAsString(Object JavaDoc value, SerializationContext context) {
66         return qName2String((QName JavaDoc)value, context);
67     }
68     
69     public String JavaDoc getMechanismType() { return Constants.AXIS_SAX; }
70
71     /**
72      * Return XML schema for the specified type, suitable for insertion into
73      * the <types> element of a WSDL document, or underneath an
74      * <element> or <attribute> declaration.
75      *
76      * @param javaType the Java Class we're writing out schema for
77      * @param types the Java2WSDL Types object which holds the context
78      * for the WSDL being generated.
79      * @return a type element containing a schema simpleType/complexType
80      * @see org.apache.axis.wsdl.fromJava.Types
81      */

82     public Element JavaDoc writeSchema(Class JavaDoc javaType, Types types) throws Exception JavaDoc {
83         return null;
84     }
85 }
86
Popular Tags