KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > wsif > compiler > schema > tools > Conventions


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.schema.tools;
59
60 import java.io.File JavaDoc;
61 import java.util.Hashtable JavaDoc;
62 import java.util.StringTokenizer JavaDoc;
63
64 import com.ibm.wsdl.util.xml.DOMUtils;
65 import org.w3c.dom.Element JavaDoc;
66 import org.w3c.dom.Node JavaDoc;
67 import org.w3c.dom.NodeList JavaDoc;
68
69 /**
70  * Contain some static methods for mapping conventions.
71  * Creation date: (6/20/00 5:05:21 PM)
72  * @author:Tian Zhao (tzhao@cs.purdue.edu)
73  * @author:Sanjiva Weerawarana (sanjiva@us.ibm.com)
74  * @author:Matthew J. Duftler (duftler@us.ibm.com)
75  */

76 public class Conventions {
77
78     private static Hashtable JavaDoc postfixTable = init();
79     private static boolean verbose = true;
80
81     /**
82      * Insert the method's description here.
83      * Creation date: (6/20/00 6:20:11 PM)
84      */

85     private static Hashtable JavaDoc init() {
86
87         Hashtable JavaDoc postfixTable = new Hashtable JavaDoc(11);
88
89         postfixTable.put("simpleType", ""); // These two lines cannot be changed.
90
postfixTable.put("complexType", ""); // The code will break otherwise.
91
postfixTable.put("group", "_Group");
92         postfixTable.put("attributeGroup", "_AttrGp");
93         postfixTable.put("element", "");
94         postfixTable.put("attribute", "_Attr");
95         /* postfixTable.put ("any", "_Any");
96             postfixTable.put ("sequence", "_Seqn");
97             postfixTable.put ("choice", "_choice");
98             postfixTable.put ("anyAttribute", "_AnyAttr");
99             postfixTable.put ("all", "_All");
100         */

101         return postfixTable;
102
103     }
104     
105     /**
106      * Convert name space url into a directory name and a package name with '.' as delimiter.
107      * Creation date: (5/30/00 9:56:31 PM)
108      * @return java.lang.String
109      * @param nameSpace java.lang.String
110      */

111     public static String JavaDoc namespaceURI2JavaPath(String JavaDoc namespaceURI)
112         throws IllegalArgumentException JavaDoc {
113
114         if (namespaceURI == null)
115             throw new IllegalArgumentException JavaDoc(
116                 "Argument to " + "'namespaceURI2JavaPath' cannot " + "be null.");
117
118         if (namespaceURI.startsWith("http://"))
119             namespaceURI = namespaceURI.substring(7);
120
121         if (namespaceURI.compareTo("") == 0)
122             return namespaceURI;
123
124         if (namespaceURI.endsWith("/"))
125             namespaceURI = namespaceURI.substring(0, namespaceURI.lastIndexOf("/"));
126
127         StringTokenizer JavaDoc tokens = new StringTokenizer JavaDoc(namespaceURI, "/", false);
128
129         String JavaDoc javaPath = tokens.nextToken();
130         while (tokens.hasMoreTokens()) {
131             javaPath = tokens.nextToken() + "." + javaPath;
132         }
133
134         tokens = new StringTokenizer JavaDoc(javaPath, ".", false);
135         javaPath = tokens.nextToken();
136
137         while (tokens.hasMoreTokens()) {
138             javaPath = tokens.nextToken() + "." + javaPath;
139         }
140
141         javaPath = javaPath.replace(':', '_');
142         javaPath = javaPath.replace('-', '_');
143
144         return javaPath;
145     }
146     
147     /**
148      * Map top-level schema type names to corresponding java class names.
149      * and map schema component names to java field names.
150      * If schema name is intended to map to a Java class name then set the
151      * boolean variable <code> isClass </code> to true. If the schema name
152      * is mapped to a java field name, then set <code> isClass </code> to false.
153      * Creation date: (6/20/00 5:13:45 PM)
154      * @return java.lang.String
155      * @param schemaType java.lang.String
156      * @param schemaName java.lang.String
157      * @param isClass boolean
158      */

159     public static String JavaDoc schema2JavaName(
160         String JavaDoc schemaType,
161         String JavaDoc schemaName,
162         boolean isClass)
163         throws IllegalArgumentException JavaDoc {
164         if (schemaType == null)
165             throw new IllegalArgumentException JavaDoc(
166                 "Illegal arguments to " + "'schema2JavaName'.");
167
168         if (schemaType.compareTo("any") == 0)
169             return "any";
170         else if (
171             schemaType.compareTo("simpleType") == 0) {
172             // schemaName should not be null in this case.
173
if (schemaName == null)
174                 throw new IllegalArgumentException JavaDoc(
175                     "Illegal arguments to " + "'schema2JavaName'.");
176             schemaName = schemaName.replace('-', '_');
177             return schemaName;
178         } else if (schemaType.compareTo("attribute") == 0) {
179             if (schemaName == null)
180                 throw new IllegalArgumentException JavaDoc(
181                     "Illegal arguments to " + "'schema2JavaName'.");
182             schemaName = schemaName.replace('-', '_');
183             return schemaName + postfixTable.get("attribute");
184         } else if (schemaType.compareTo("anyAttribute") == 0)
185             return "anyAttribute";
186         else if (schemaType.compareTo("all") == 0)
187             schemaName = "all";
188         else if (schemaType.compareTo("choice") == 0)
189             schemaName = "choice";
190         else if (schemaType.compareTo("sequence") == 0)
191             schemaName = "sequence";
192
193         if (schemaName == null)
194             throw new IllegalArgumentException JavaDoc(
195                 "Illegal arguments to " + "'schema2JavaName'.");
196
197         schemaName = schemaName.replace('-', '_');
198         // some of the id's in Schema is illegal in Java.
199

200         String JavaDoc postfix = (String JavaDoc) postfixTable.get(schemaType);
201         if (postfix == null)
202             postfix = "";
203
204         String JavaDoc javaName = schemaName;
205         if (isClass)
206             javaName =
207                 Character.toUpperCase(schemaName.charAt(0)) + schemaName.substring(1);
208         return javaName + postfix;
209
210     }
211     
212     /**
213      * This method takes an Element type and a target namespace uri and
214      * return a fully qualified java class name.
215      * The targetURI is translated to java package name genericly.
216      * If the targetURI is mapped specifically to a java package name, then
217      * this method should NOT be used.
218      * Creation date: (6/21/00 2:25:18 PM)
219      * @return java.lang.String
220      */

221     public static String JavaDoc schema2JavaName(Node JavaDoc node, String JavaDoc targetURI) {
222         if (node == null || targetURI == null)
223             throw new IllegalArgumentException JavaDoc(
224                 "Illegal arguments to " + "'schema2JavaName'.");
225
226         String JavaDoc targetNSPrefix = namespaceURI2JavaPath(targetURI) + ".";
227         String JavaDoc name = DOMUtils.getAttribute((Element JavaDoc) node, "name");
228         String JavaDoc type = node.getLocalName();
229
230         if (name == null || type == null)
231             return null;
232         else {
233             name = schema2JavaName(type, name, true);
234             return targetNSPrefix + name;
235         }
236
237     }
238     
239     public static String JavaDoc schema2JavaName(NodeList JavaDoc nl, NodeList JavaDoc targetURI) {
240         if (nl.getLength() == 0) {
241             throw new IllegalArgumentException JavaDoc(
242                 "No type name found for serializer " + "class.");
243         }
244         Node JavaDoc node = nl.item(0);
245
246         if (targetURI.getLength() == 0) {
247             throw new IllegalArgumentException JavaDoc(
248                 "No type name found for serializer " + "class.");
249         }
250         Node JavaDoc tnode = targetURI.item(0);
251         String JavaDoc targetNS = tnode.getNodeValue();
252
253         // now produce the fully qualified name
254

255         return schema2JavaName(node, targetNS);
256
257     }
258
259     public static String JavaDoc schema2NonQualifiedJavaName(
260         NodeList JavaDoc nl,
261         NodeList JavaDoc targetURI) {
262
263         String JavaDoc fullname = schema2JavaName(nl, targetURI);
264         StringTokenizer JavaDoc st = new StringTokenizer JavaDoc(fullname, ".");
265         String JavaDoc name = null;
266         while (st.hasMoreTokens()) {
267             name = st.nextToken();
268         }
269
270         return name;
271     }
272
273     public static String JavaDoc getJavaPathName(
274         String JavaDoc targetDirectoryName,
275         String JavaDoc packageName) {
276         if (packageName != null && !packageName.equals("")) {
277             targetDirectoryName += File.separatorChar
278                 + packageName.replace('.', File.separatorChar);
279         }
280
281         return targetDirectoryName;
282     }
283
284     public static String JavaDoc getJavaFileName(
285         NodeList JavaDoc nl,
286         NodeList JavaDoc targetURI,
287         String JavaDoc javaFileSuffix) {
288         String JavaDoc javaFileName =
289             schema2NonQualifiedJavaName(nl, targetURI) + javaFileSuffix + ".java";
290
291         return javaFileName;
292     }
293
294     public static boolean JDKcompile(String JavaDoc fileName, String JavaDoc workingDirectory)
295         throws IllegalArgumentException JavaDoc {
296         String JavaDoc classPath = System.getProperty("java.class.path");
297
298         if (workingDirectory != null && !workingDirectory.equals("")) {
299             classPath += System.getProperty("path.separator") + workingDirectory;
300         }
301
302         String JavaDoc args[] = { "-classpath", classPath, fileName };
303
304         try {
305             return new sun.tools.javac.Main(System.err, "javac").compile(args);
306         } catch (Throwable JavaDoc th) {
307             System.err.println("Unable to load JDK compiler.");
308
309             return false;
310         }
311     }
312
313     public static void setVerbose(boolean ver) {
314         verbose = ver;
315     }
316
317     public static boolean getVerbose() {
318         return verbose;
319     }
320 }
Popular Tags