001: /*
002: * The Apache Software License, Version 1.1
003: *
004: *
005: * Copyright (c) 2002 The Apache Software Foundation. All rights
006: * reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * 1. Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * 2. Redistributions in binary form must reproduce the above copyright
016: * notice, this list of conditions and the following disclaimer in
017: * the documentation and/or other materials provided with the
018: * distribution.
019: *
020: * 3. The end-user documentation included with the redistribution,
021: * if any, must include the following acknowledgment:
022: * "This product includes software developed by the
023: * Apache Software Foundation (http://www.apache.org/)."
024: * Alternately, this acknowledgment may appear in the software itself,
025: * if and wherever such third-party acknowledgments normally appear.
026: *
027: * 4. The names "WSIF" and "Apache Software Foundation" must
028: * not be used to endorse or promote products derived from this
029: * software without prior written permission. For written
030: * permission, please contact apache@apache.org.
031: *
032: * 5. Products derived from this software may not be called "Apache",
033: * nor may "Apache" appear in their name, without prior written
034: * permission of the Apache Software Foundation.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
040: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: * ====================================================================
049: *
050: * This software consists of voluntary contributions made by many
051: * individuals on behalf of the Apache Software Foundation and was
052: * originally based on software copyright (c) 2001, 2002, International
053: * Business Machines, Inc., http://www.apache.org. For more
054: * information on the Apache Software Foundation, please see
055: * <http://www.apache.org/>.
056: */
057:
058: package org.apache.wsif.compiler.schema.tools;
059:
060: import java.io.File;
061: import java.util.Hashtable;
062: import java.util.StringTokenizer;
063:
064: import com.ibm.wsdl.util.xml.DOMUtils;
065: import org.w3c.dom.Element;
066: import org.w3c.dom.Node;
067: import org.w3c.dom.NodeList;
068:
069: /**
070: * Contain some static methods for mapping conventions.
071: * Creation date: (6/20/00 5:05:21 PM)
072: * @author:Tian Zhao (tzhao@cs.purdue.edu)
073: * @author:Sanjiva Weerawarana (sanjiva@us.ibm.com)
074: * @author:Matthew J. Duftler (duftler@us.ibm.com)
075: */
076: public class Conventions {
077:
078: private static Hashtable postfixTable = init();
079: private static boolean verbose = true;
080:
081: /**
082: * Insert the method's description here.
083: * Creation date: (6/20/00 6:20:11 PM)
084: */
085: private static Hashtable init() {
086:
087: Hashtable postfixTable = new Hashtable(11);
088:
089: postfixTable.put("simpleType", ""); // These two lines cannot be changed.
090: postfixTable.put("complexType", ""); // The code will break otherwise.
091: postfixTable.put("group", "_Group");
092: postfixTable.put("attributeGroup", "_AttrGp");
093: postfixTable.put("element", "");
094: postfixTable.put("attribute", "_Attr");
095: /* postfixTable.put ("any", "_Any");
096: postfixTable.put ("sequence", "_Seqn");
097: postfixTable.put ("choice", "_choice");
098: postfixTable.put ("anyAttribute", "_AnyAttr");
099: 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 namespaceURI2JavaPath(String namespaceURI)
112: throws IllegalArgumentException {
113:
114: if (namespaceURI == null)
115: throw new IllegalArgumentException("Argument to "
116: + "'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
126: .lastIndexOf("/"));
127:
128: StringTokenizer tokens = new StringTokenizer(namespaceURI, "/",
129: false);
130:
131: String javaPath = tokens.nextToken();
132: while (tokens.hasMoreTokens()) {
133: javaPath = tokens.nextToken() + "." + javaPath;
134: }
135:
136: tokens = new StringTokenizer(javaPath, ".", false);
137: javaPath = tokens.nextToken();
138:
139: while (tokens.hasMoreTokens()) {
140: javaPath = tokens.nextToken() + "." + javaPath;
141: }
142:
143: javaPath = javaPath.replace(':', '_');
144: javaPath = javaPath.replace('-', '_');
145:
146: return javaPath;
147: }
148:
149: /**
150: * Map top-level schema type names to corresponding java class names.
151: * and map schema component names to java field names.
152: * If schema name is intended to map to a Java class name then set the
153: * boolean variable <code> isClass </code> to true. If the schema name
154: * is mapped to a java field name, then set <code> isClass </code> to false.
155: * Creation date: (6/20/00 5:13:45 PM)
156: * @return java.lang.String
157: * @param schemaType java.lang.String
158: * @param schemaName java.lang.String
159: * @param isClass boolean
160: */
161: public static String schema2JavaName(String schemaType,
162: String schemaName, boolean isClass)
163: throws IllegalArgumentException {
164: if (schemaType == null)
165: throw new IllegalArgumentException("Illegal arguments to "
166: + "'schema2JavaName'.");
167:
168: if (schemaType.compareTo("any") == 0)
169: return "any";
170: else if (schemaType.compareTo("simpleType") == 0) {
171: // schemaName should not be null in this case.
172: if (schemaName == null)
173: throw new IllegalArgumentException(
174: "Illegal arguments to " + "'schema2JavaName'.");
175: schemaName = schemaName.replace('-', '_');
176: return schemaName;
177: } else if (schemaType.compareTo("attribute") == 0) {
178: if (schemaName == null)
179: throw new IllegalArgumentException(
180: "Illegal arguments to " + "'schema2JavaName'.");
181: schemaName = schemaName.replace('-', '_');
182: return schemaName + postfixTable.get("attribute");
183: } else if (schemaType.compareTo("anyAttribute") == 0)
184: return "anyAttribute";
185: else if (schemaType.compareTo("all") == 0)
186: schemaName = "all";
187: else if (schemaType.compareTo("choice") == 0)
188: schemaName = "choice";
189: else if (schemaType.compareTo("sequence") == 0)
190: schemaName = "sequence";
191:
192: if (schemaName == null)
193: throw new IllegalArgumentException("Illegal arguments to "
194: + "'schema2JavaName'.");
195:
196: schemaName = schemaName.replace('-', '_');
197: // some of the id's in Schema is illegal in Java.
198:
199: String postfix = (String) postfixTable.get(schemaType);
200: if (postfix == null)
201: postfix = "";
202:
203: String javaName = schemaName;
204: if (isClass)
205: javaName = Character.toUpperCase(schemaName.charAt(0))
206: + schemaName.substring(1);
207: return javaName + postfix;
208:
209: }
210:
211: /**
212: * This method takes an Element type and a target namespace uri and
213: * return a fully qualified java class name.
214: * The targetURI is translated to java package name genericly.
215: * If the targetURI is mapped specifically to a java package name, then
216: * this method should NOT be used.
217: * Creation date: (6/21/00 2:25:18 PM)
218: * @return java.lang.String
219: */
220: public static String schema2JavaName(Node node, String targetURI) {
221: if (node == null || targetURI == null)
222: throw new IllegalArgumentException("Illegal arguments to "
223: + "'schema2JavaName'.");
224:
225: String targetNSPrefix = namespaceURI2JavaPath(targetURI) + ".";
226: String name = DOMUtils.getAttribute((Element) node, "name");
227: String type = node.getLocalName();
228:
229: if (name == null || type == null)
230: return null;
231: else {
232: name = schema2JavaName(type, name, true);
233: return targetNSPrefix + name;
234: }
235:
236: }
237:
238: public static String schema2JavaName(NodeList nl, NodeList targetURI) {
239: if (nl.getLength() == 0) {
240: throw new IllegalArgumentException(
241: "No type name found for serializer " + "class.");
242: }
243: Node node = nl.item(0);
244:
245: if (targetURI.getLength() == 0) {
246: throw new IllegalArgumentException(
247: "No type name found for serializer " + "class.");
248: }
249: Node tnode = targetURI.item(0);
250: String targetNS = tnode.getNodeValue();
251:
252: // now produce the fully qualified name
253:
254: return schema2JavaName(node, targetNS);
255:
256: }
257:
258: public static String schema2NonQualifiedJavaName(NodeList nl,
259: NodeList targetURI) {
260:
261: String fullname = schema2JavaName(nl, targetURI);
262: StringTokenizer st = new StringTokenizer(fullname, ".");
263: String name = null;
264: while (st.hasMoreTokens()) {
265: name = st.nextToken();
266: }
267:
268: return name;
269: }
270:
271: public static String getJavaPathName(String targetDirectoryName,
272: String packageName) {
273: if (packageName != null && !packageName.equals("")) {
274: targetDirectoryName += File.separatorChar
275: + packageName.replace('.', File.separatorChar);
276: }
277:
278: return targetDirectoryName;
279: }
280:
281: public static String getJavaFileName(NodeList nl,
282: NodeList targetURI, String javaFileSuffix) {
283: String javaFileName = schema2NonQualifiedJavaName(nl, targetURI)
284: + javaFileSuffix + ".java";
285:
286: return javaFileName;
287: }
288:
289: public static boolean JDKcompile(String fileName,
290: String workingDirectory) throws IllegalArgumentException {
291: String classPath = System.getProperty("java.class.path");
292:
293: if (workingDirectory != null && !workingDirectory.equals("")) {
294: classPath += System.getProperty("path.separator")
295: + workingDirectory;
296: }
297:
298: String args[] = { "-classpath", classPath, fileName };
299:
300: try {
301: return new sun.tools.javac.Main(System.err, "javac")
302: .compile(args);
303: } catch (Throwable th) {
304: System.err.println("Unable to load JDK compiler.");
305:
306: return false;
307: }
308: }
309:
310: public static void setVerbose(boolean ver) {
311: verbose = ver;
312: }
313:
314: public static boolean getVerbose() {
315: return verbose;
316: }
317: }
|