KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > soap > encoding > soapenc > QNameSerializer


1 package org.apache.soap.encoding.soapenc;
2
3 import org.apache.soap.Constants;
4 import org.apache.soap.util.Bean;
5 import org.apache.soap.util.xml.*;
6 import org.apache.soap.rpc.SOAPContext;
7 import org.w3c.dom.*;
8 import java.io.*;
9
10 /** A QNameSerializer serializes a QName as follows:
11  * <elementName xmlns:ns="QNameURIPart">ns:QNameLocalPart</elementName>
12  */

13 public class QNameSerializer implements Serializer, Deserializer
14 {
15     public void marshall(String JavaDoc inScopeEncStyle, Class JavaDoc javaType, Object JavaDoc src,
16                              Object JavaDoc context, Writer sink, NSStack nsStack,
17                              XMLJavaMappingRegistry xjmr, SOAPContext ctx)
18         throws IllegalArgumentException JavaDoc, IOException
19     {
20         sink.write('<' + context.toString());
21         
22         QName qName = (QName)src;
23         
24         // Make sure our namespace has a valid prefix in this doc.
25
String JavaDoc prefix = nsStack.getPrefixFromURI(qName.getNamespaceURI(), sink);
26         
27         QName elementType = xjmr.queryElementType(javaType,
28                                               Constants.NS_URI_SOAP_ENC);
29         
30         String JavaDoc xsiNSPrefix =
31                 nsStack.getPrefixFromURI(Constants.NS_URI_CURRENT_SCHEMA_XSI, sink);
32         String JavaDoc elementTypeNSPrefix =
33                 nsStack.getPrefixFromURI(elementType.getNamespaceURI(), sink);
34
35         sink.write(' ' + xsiNSPrefix + ':' + Constants.ATTR_TYPE + "=\"" +
36                    elementTypeNSPrefix + ':' +
37                    elementType.getLocalPart() + '\"');
38
39         sink.write('>' + prefix + ':' + qName.getLocalPart());
40         sink.write("</" + context.toString() + '>');
41     }
42   
43     public Bean unmarshall(String JavaDoc inScopeEncStyle, QName elementType, Node src,
44                                XMLJavaMappingRegistry xjmr,
45                                SOAPContext ctx)
46         throws IllegalArgumentException JavaDoc
47     {
48         Element root = (Element)src;
49         String JavaDoc value = DOMUtils.getChildCharacterData(root);
50         
51         int idx = value.indexOf(":");
52         if (idx <= 0)
53             throw new IllegalArgumentException JavaDoc("No NamespaceURI while deserializing QName");
54         
55         String JavaDoc prefix = value.substring(0,idx);
56         String JavaDoc uri = DOMUtils.getNamespaceURIFromPrefix(src, prefix);
57         
58         QName qName = new QName(uri, value.substring(idx + 1));
59
60         return new Bean(QName.class, qName);
61     }
62 }
63
Popular Tags