KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > soap > server > TypeMappingSerializer


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2000 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "SOAP" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 2000, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package org.apache.soap.server;
59
60 import java.util.*;
61 import java.io.*;
62 import org.w3c.dom.*;
63 import org.apache.soap.util.Bean;
64 import org.apache.soap.util.StringUtils;
65 import org.apache.soap.util.xml.*;
66 import org.apache.soap.Constants;
67 import org.apache.soap.rpc.SOAPContext;
68 import org.apache.soap.encoding.*;
69 import org.apache.soap.encoding.soapenc.SoapEncUtils;
70
71 /**
72  * Serialize and deserialize type mappings according to SOAP-Enc.
73  *
74  * @author Sanjiva Weerawarana (sanjiva@watson.ibm.com)
75  * @author Matthew J. Duftler (duftler@us.ibm.com)
76  */

77 public class TypeMappingSerializer implements Serializer, Deserializer {
78   /**
79    * This is the Serializer interface .. called to serialize an instance
80    * of a TypeMapping (the src arg)
81    * @param inScopeEncStyle the encoding style currently in scope
82    * @param javaType should be org.apache.soap.server.TypeMapping.class
83    * @param src instance to serialize as XML
84    * @param context accessor name
85    * @param sink the writer to write XML into
86    * @param nsStack namespace stack
87    * @param xjmr unused
88    */

89   public void marshall (String JavaDoc inScopeEncStyle, Class JavaDoc javaType, Object JavaDoc src,
90                         Object JavaDoc context, Writer sink, NSStack nsStack,
91                         XMLJavaMappingRegistry xjmr, SOAPContext ctx)
92        throws IllegalArgumentException JavaDoc, IOException {
93     TypeMapping tm = (TypeMapping) src;
94
95     nsStack.pushScope ();
96     SoapEncUtils.generateStructureHeader (inScopeEncStyle, javaType, context,
97                                           sink, nsStack, xjmr);
98     sink.write (StringUtils.lineSeparator);
99
100     // these namespaces being defined by the envelope stuff
101
String JavaDoc xsiPrefix = nsStack.getPrefixFromURI (Constants.NS_URI_CURRENT_SCHEMA_XSI);
102     String JavaDoc xsdPrefix = nsStack.getPrefixFromURI (Constants.NS_URI_CURRENT_SCHEMA_XSD);
103     if ((xsiPrefix == null) || (xsdPrefix == null)) {
104       throw new IllegalArgumentException JavaDoc ("required namespace names '" +
105                                           Constants.NS_URI_CURRENT_SCHEMA_XSI +
106                                           "' and/or '" +
107                                           Constants.NS_URI_CURRENT_SCHEMA_XSD +
108                                           "' is not defined.");
109     }
110
111     if (tm.encodingStyle != null) {
112       sink.write ("<encodingStyle " + xsiPrefix + ":type=\"" + xsdPrefix +
113                   ":string\">" + tm.encodingStyle + "</encodingStyle>");
114       sink.write (StringUtils.lineSeparator);
115     }
116
117     if (tm.elementType != null) {
118       sink.write ("<elementType-ns " + xsiPrefix + ":type=\"" + xsdPrefix +
119                   ":string\">" + tm.elementType.getNamespaceURI () +
120                   "</elementType-ns>");
121       sink.write (StringUtils.lineSeparator);
122       
123       sink.write ("<elementType-lp " + xsiPrefix + ":type=\"" + xsdPrefix +
124                   ":string\">" + tm.elementType.getLocalPart () +
125                   "</elementType-lp>");
126       sink.write (StringUtils.lineSeparator);
127     }
128
129     if (tm.javaType != null) {
130       sink.write ("<javaType " + xsiPrefix + ":type=\"" + xsdPrefix +
131                   ":string\">" + tm.javaType + "</javaType>");
132       sink.write (StringUtils.lineSeparator);
133     }
134
135     if (tm.xml2JavaClassName != null) {
136       sink.write ("<xml2JavaClassName " + xsiPrefix + ":type=\"" + xsdPrefix +
137                   ":string\">" + tm.xml2JavaClassName +
138                   "</xml2JavaClassName>");
139       sink.write (StringUtils.lineSeparator);
140     }
141
142     if (tm.java2XMLClassName != null) {
143       sink.write ("<java2XMLClassName " + xsiPrefix + ":type=\"" + xsdPrefix +
144                   ":string\">" + tm.java2XMLClassName +
145                   "</java2XMLClassName>");
146       sink.write (StringUtils.lineSeparator);
147     }
148
149     sink.write ("</" + context + '>');
150     nsStack.popScope ();
151   }
152
153
154   /**
155    * The deserializer interface.
156    */

157   public Bean unmarshall (String JavaDoc inScopeEncStyle, QName elementType, Node src,
158                           XMLJavaMappingRegistry xjmr, SOAPContext ctx)
159        throws IllegalArgumentException JavaDoc {
160     NodeList nl = src.getChildNodes ();
161     int nKids = nl.getLength ();
162
163     // info for the type mapping object
164
String JavaDoc encodingStyle = null;
165     String JavaDoc elTypeNS = null;
166     String JavaDoc elTypeLP = null;
167     QName qname = null;
168     String JavaDoc javaType = null;
169     String JavaDoc java2XMLClassName = null;
170     String JavaDoc xml2JavaClassName = null;
171
172     for (int i = 0; i < nKids; i++) {
173       Node n = nl.item (i);
174       if (n.getNodeType () != Node.ELEMENT_NODE) {
175         continue;
176       }
177       Element e = (Element) n;
178       String JavaDoc tagName = e.getTagName ();
179       String JavaDoc elData = DOMUtils.getChildCharacterData (e);
180       if (tagName.equals ("encodingStyle")) {
181         encodingStyle = elData;
182       } else if (tagName.equals ("elementType-ns")) {
183         elTypeNS = elData;
184       } else if (tagName.equals ("elementType-lp")) {
185         elTypeLP = elData;
186       } else if (tagName.equals ("javaType")) {
187         javaType = elData;
188       } else if (tagName.equals ("java2XMLClassName")) {
189         java2XMLClassName = elData;
190       } else if (tagName.equals ("xml2JavaClassName")) {
191         xml2JavaClassName = elData;
192       } else {
193         throw new IllegalArgumentException JavaDoc ("unknown element '" +
194                                             tagName + "' while " +
195                                             "unmarshalling a TypeMapping");
196       }
197     }
198
199     if (elTypeNS != null && elTypeLP != null) {
200       qname = new QName (elTypeNS, elTypeLP);
201     }
202
203     return new Bean (TypeMapping.class,
204                      new TypeMapping (encodingStyle,
205                                       qname,
206                                       javaType,
207                                       java2XMLClassName, xml2JavaClassName));
208   }
209 }
210
Popular Tags