KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > wsif > wsdl > extensions > format > FormatBindingSerializer


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2002 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 "WSIF" 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) 2001, 2002, 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.wsif.wsdl.extensions.format;
59
60 import java.io.Serializable JavaDoc;
61 import java.util.Iterator JavaDoc;
62 import java.util.List JavaDoc;
63
64 import javax.wsdl.extensions.ExtensionRegistry;
65 import javax.wsdl.factory.WSDLFactory;
66 import javax.wsdl.xml.WSDLReader;
67 import javax.xml.namespace.QName JavaDoc;
68
69 import org.apache.wsif.logging.Trc;
70 import org.w3c.dom.Element JavaDoc;
71
72 import com.ibm.wsdl.Constants;
73 import com.ibm.wsdl.util.xml.DOMUtils;
74 import com.ibm.wsdl.util.xml.QNameUtils;
75
76 /**
77  * @author Owen Burroughs <owenb@apache.org>
78  * @author Ant Elder <antelder@apache.org>
79  * @author Jeremy Hughes <hughesj@apache.org>
80  * @author Mark Whitlock <whitlock@apache.org>
81  */

82 public class FormatBindingSerializer
83     implements
84         javax.wsdl.extensions.ExtensionDeserializer,
85         javax.wsdl.extensions.ExtensionSerializer,
86         Serializable JavaDoc {
87     private static final long serialVersionUID = 1L;
88     /**
89      * ConnectorBindingSerializer constructor comment.
90      */

91     public FormatBindingSerializer() {
92         super();
93         Trc.entry(this);
94         Trc.exit();
95     }
96
97     /**
98       *
99       */

100     public void marshall(
101         Class JavaDoc parentType,
102         QName JavaDoc elementType,
103         javax.wsdl.extensions.ExtensibilityElement extension,
104         java.io.PrintWriter JavaDoc pw,
105         javax.wsdl.Definition def,
106         javax.wsdl.extensions.ExtensionRegistry extReg)
107         throws javax.wsdl.WSDLException {
108         Trc.entry(this, parentType, elementType, extension, pw, def, extReg);
109
110         if (extension == null) {
111             Trc.exit();
112             return;
113         }
114
115         if (extension instanceof TypeMapping) {
116             TypeMapping typeMapping = (TypeMapping) extension;
117             pw.print(" <format:typeMapping");
118
119             String JavaDoc style = typeMapping.getStyle();
120             if (style != null)
121                 DOMUtils.printAttribute("style", style, pw);
122             String JavaDoc encoding = typeMapping.getEncoding();
123             if (encoding != null)
124                 DOMUtils.printAttribute("encoding", encoding, pw);
125             pw.println(">");
126
127             List JavaDoc maps = typeMapping.getMaps();
128             Iterator JavaDoc iterator = maps.iterator();
129             while (iterator.hasNext()) {
130                 TypeMap typeMap = (TypeMap) iterator.next();
131                 pw.print(" <format:typeMap");
132
133                 // Need definition to resolve namespace to prefix to do it properly
134
QName JavaDoc elementName = typeMap.getElementName();
135                 if (elementName != null) {
136                     String JavaDoc prefix =
137                         def.getPrefix(elementName.getNamespaceURI());
138                     DOMUtils.printAttribute(
139                         "elementName",
140                         prefix + ":" + elementName.getLocalPart(),
141                         pw);
142                 }
143                 QName JavaDoc typeName = typeMap.getTypeName();
144                 if (typeName != null) {
145                     String JavaDoc prefix = def.getPrefix(typeName.getNamespaceURI());
146                     DOMUtils.printAttribute(
147                         "typeName",
148                         prefix + ":" + typeName.getLocalPart(),
149                         pw);
150                 }
151                 String JavaDoc formatType = typeMap.getFormatType();
152                 if (formatType != null)
153                     DOMUtils.printAttribute("formatType", formatType, pw);
154                 pw.println("/>");
155                 Boolean JavaDoc required = extension.getRequired();
156
157                 if (required != null) {
158                     DOMUtils.printQualifiedAttribute(
159                         Constants.Q_ATTR_REQUIRED,
160                         required.toString(),
161                         def,
162                         pw);
163                 }
164
165             }
166             pw.println(" </format:typeMapping>");
167
168         }
169         Trc.exit();
170     }
171
172     /**
173      * Registers the serializer.
174      */

175     public void registerSerializer(ExtensionRegistry registry) {
176         Trc.entry(this, registry);
177
178         // Binding
179
registry.registerSerializer(
180             javax.wsdl.Binding.class,
181             FormatBindingConstants.Q_ELEM_FORMAT_BINDING,
182             this);
183
184         registry.registerDeserializer(
185             javax.wsdl.Binding.class,
186             FormatBindingConstants.Q_ELEM_FORMAT_BINDING,
187             this);
188         Trc.exit();
189     }
190
191     /**
192      * unmarshall method comment.
193      */

194     public javax.wsdl.extensions.ExtensibilityElement unmarshall(
195         Class JavaDoc parentPart,
196         javax.xml.namespace.QName JavaDoc elementPart,
197         org.w3c.dom.Element JavaDoc el,
198         javax.wsdl.Definition def,
199         javax.wsdl.extensions.ExtensionRegistry extReg)
200         throws javax.wsdl.WSDLException {
201         Trc.entry(this, parentPart, elementPart, el, def, extReg);
202
203         javax.wsdl.extensions.ExtensibilityElement returnValue = null;
204
205         if (FormatBindingConstants.Q_ELEM_FORMAT_BINDING.equals(elementPart)) {
206             TypeMapping typeMapping = new TypeMapping();
207
208             String JavaDoc style = DOMUtils.getAttribute(el, "style");
209             String JavaDoc encoding = DOMUtils.getAttribute(el, "encoding");
210             String JavaDoc requiredStr =
211                 DOMUtils.getAttributeNS(
212                     el,
213                     Constants.NS_URI_WSDL,
214                     Constants.ATTR_REQUIRED);
215
216             if (style != null) {
217                 typeMapping.setStyle(style);
218             }
219             if (encoding != null) {
220                 typeMapping.setEncoding(encoding);
221             }
222
223             Element JavaDoc tempEl = DOMUtils.getFirstChildElement(el);
224             while (tempEl != null) {
225                 if (QNameUtils
226                     .matches(
227                         FormatBindingConstants.Q_ELEM_FORMAT_BINDING_MAP,
228                         tempEl)) {
229                     TypeMap typeMap = new TypeMap();
230
231                     QName JavaDoc qElementName =
232                         DOMUtils.getQualifiedAttributeValue(
233                             tempEl,
234                             "elementName",
235                             "typeMap",
236                             false);
237                     QName JavaDoc qTypeName =
238                         DOMUtils.getQualifiedAttributeValue(
239                             tempEl,
240                             "typeName",
241                             "typeMap",
242                             false);
243
244                     String JavaDoc formatType =
245                         DOMUtils.getAttribute(tempEl, "formatType");
246                     if (qElementName != null) {
247                         typeMap.setElementName(qElementName);
248                     }
249                     if (qTypeName != null) {
250                         typeMap.setTypeName(qTypeName);
251                     }
252                     if (formatType != null) {
253                         typeMap.setFormatType(formatType);
254                     }
255                     typeMapping.addMap(typeMap);
256                 }
257                 tempEl = DOMUtils.getNextSiblingElement(tempEl);
258             }
259             Trc.exit(typeMapping);
260             return typeMapping;
261         }
262         Trc.exit(returnValue);
263         return returnValue;
264     }
265 }
Popular Tags