KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > wsif > compiler > util > Utils


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.compiler.util;
59
60 import java.io.BufferedReader JavaDoc;
61 import java.io.IOException JavaDoc;
62 import java.io.Reader JavaDoc;
63 import java.io.StringWriter JavaDoc;
64 import java.util.Hashtable JavaDoc;
65 import java.util.Iterator JavaDoc;
66 import java.util.List JavaDoc;
67 import java.util.Map JavaDoc;
68 import java.util.Vector JavaDoc;
69
70 import javax.wsdl.Definition;
71 import javax.wsdl.Import;
72 import javax.wsdl.Types;
73 import javax.wsdl.extensions.UnknownExtensibilityElement;
74 import javax.xml.namespace.QName JavaDoc;
75
76 import org.apache.wsif.WSIFConstants;
77 import org.apache.wsif.WSIFException;
78
79 import com.ibm.wsdl.util.StringUtils;
80
81 /**
82  * @author Matthew J. Duftler
83  */

84 public class Utils {
85     
86     public static void addAllTypesElements(Definition def, List JavaDoc toList) {
87         Types types = def.getTypes();
88         if (types != null) {
89             Iterator JavaDoc extEleIt = types.getExtensibilityElements().iterator();
90             while (extEleIt.hasNext()) {
91                 // the unknown extensibility element are wrappers for DOM elements
92
UnknownExtensibilityElement typesElement =
93                     (UnknownExtensibilityElement) extEleIt.next();
94                 toList.add(typesElement);
95             }
96         }
97
98         Map JavaDoc imports = def.getImports();
99
100         if (imports != null) {
101             Iterator JavaDoc valueIterator = imports.values().iterator();
102
103             while (valueIterator.hasNext()) {
104                 List JavaDoc importList = (List JavaDoc) valueIterator.next();
105
106                 if (importList != null) {
107                     Iterator JavaDoc importIterator = importList.iterator();
108
109                     while (importIterator.hasNext()) {
110                         Import tempImport = (Import) importIterator.next();
111
112                         if (tempImport != null) {
113                             Definition importedDef = tempImport.getDefinition();
114
115                             if (importedDef != null) {
116                                 addAllTypesElements(importedDef, toList);
117                             }
118                         }
119                     }
120                 }
121             }
122         }
123     }
124
125     public static List JavaDoc getAllTypesElements(Definition def) {
126         List JavaDoc ret = new Vector JavaDoc();
127
128         addAllTypesElements(def, ret);
129
130         return ret;
131     }
132
133     public static String JavaDoc getPackageName(String JavaDoc fqClassName) {
134         String JavaDoc packageName = "";
135
136         if (fqClassName != null) {
137             int index = fqClassName.lastIndexOf('.');
138
139             if (index != -1) {
140                 packageName = fqClassName.substring(0, index);
141             }
142         }
143
144         return packageName;
145     }
146
147     public static String JavaDoc getClassName(String JavaDoc fqClassName) {
148         if (fqClassName != null) {
149             int index = fqClassName.lastIndexOf('.');
150
151             if (index != -1) {
152                 fqClassName = fqClassName.substring(index + 1);
153             }
154         }
155
156         return fqClassName;
157     }
158
159     public static String JavaDoc queryJavaTypeName(
160         QName JavaDoc type,
161         String JavaDoc encodingStyleURI,
162         Hashtable JavaDoc typeReg)
163         throws IllegalArgumentException JavaDoc {
164         TypeMapping tm = (TypeMapping) typeReg.get(type);
165
166         if (tm != null) {
167             return tm.javaType;
168         } else if (
169             encodingStyleURI != null
170                 && encodingStyleURI.equals(WSIFConstants.NS_URI_LITERAL_XML)) {
171             return "org.w3c.dom.Element";
172         } else {
173             throw new IllegalArgumentException JavaDoc("No mapping was found for '" + type + "'.");
174         }
175     }
176
177     public static String JavaDoc getQuotedString(Reader JavaDoc source, int indent)
178         throws WSIFException {
179         String JavaDoc indentStr = StringUtils.getChars(indent, ' ');
180
181         try {
182             BufferedReader JavaDoc br = new BufferedReader JavaDoc(source);
183             StringWriter JavaDoc sw = new StringWriter JavaDoc();
184             int count = 0;
185             String JavaDoc tempLine = null;
186
187             while ((tempLine = br.readLine()) != null) {
188                 sw.write(
189                     (count > 0
190                         ? " + \"" + StringUtils.lineSeparatorStr + "\" +" + StringUtils.lineSeparator
191                         : "")
192                         + indentStr
193                         + '\"'
194                         + StringUtils.cleanString(tempLine)
195                         + '\"');
196
197                 count++;
198             }
199
200             return sw.toString();
201         } catch (IOException JavaDoc e) {
202             throw new WSIFException("Problem writing strings.", e);
203         }
204     }
205
206     public static String JavaDoc convertToObject(String JavaDoc sourceClassName, String JavaDoc expr)
207         throws WSIFException {
208         return convertClass(sourceClassName, expr, "java.lang.Object");
209     }
210
211     public static String JavaDoc convertFromObject(String JavaDoc expr, String JavaDoc targetClassName)
212         throws WSIFException {
213         return convertClass("java.lang.Object", expr, targetClassName);
214     }
215
216     private static String JavaDoc convertClass(
217         String JavaDoc sourceClassName,
218         String JavaDoc expr,
219         String JavaDoc targetClassName)
220         throws WSIFException {
221         if (sourceClassName == null || targetClassName == null) {
222             throw new WSIFException(
223                 "I was unable to convert an object from "
224                     + sourceClassName
225                     + " to "
226                     + targetClassName
227                     + ".");
228         }
229
230         String JavaDoc shortTargetClassName = getShortName(targetClassName);
231
232         if (sourceClassName.equals("java.lang.Object")) {
233             if (isPrimitive(targetClassName)) {
234                 return "(("
235                     + getWrapperClassName(targetClassName)
236                     + ")"
237                     + expr
238                     + ")."
239                     + shortTargetClassName
240                     + "Value()";
241             } else {
242                 return "(" + targetClassName + ")" + expr;
243             }
244         } else if (
245             isPrimitive(sourceClassName) && targetClassName.equals("java.lang.Object")) {
246             return "new " + getWrapperClassName(sourceClassName) + "(" + expr + ")";
247         } else {
248             // Target class must be "assignableFrom" source class.
249
return expr;
250         }
251     }
252
253     private static String JavaDoc getShortName(String JavaDoc className) {
254         if (className.startsWith("java.lang."))
255             return className.substring(10);
256         else
257             return className;
258     }
259
260     private static String JavaDoc getWrapperClassName(String JavaDoc primitiveClassName) {
261         if (primitiveClassName.equals("int"))
262             return "Integer";
263         else if (primitiveClassName.equals("char"))
264             return "Character";
265         else
266             return getCapitalized(primitiveClassName);
267     }
268
269     private static String JavaDoc getCapitalized(String JavaDoc className) {
270         return Character.toUpperCase(className.charAt(0)) + className.substring(1);
271     }
272
273     private static boolean isPrimitive(String JavaDoc className) {
274         String JavaDoc[] primNames =
275             {
276                 "boolean",
277                 "byte",
278                 "char",
279                 "short",
280                 "int",
281                 "long",
282                 "float",
283                 "double",
284                 "void" };
285
286         for (int i = 0; i < primNames.length; i++) {
287             if (primNames[i].equals(className)) {
288                 return true;
289             }
290         }
291         return false;
292     }
293 }
Popular Tags