001: package org.objectweb.celtix.tools;
002:
003: import java.io.File;
004: import java.util.HashSet;
005: import java.util.Iterator;
006: import java.util.Properties;
007: import java.util.Set;
008:
009: import org.objectweb.celtix.common.i18n.Message;
010: import org.objectweb.celtix.tools.common.ProcessorEnvironment;
011: import org.objectweb.celtix.tools.common.ToolConstants;
012: import org.objectweb.celtix.tools.common.ToolException;
013: import org.objectweb.celtix.tools.common.toolspec.ToolRunner;
014: import org.objectweb.celtix.tools.common.toolspec.ToolSpec;
015: import org.objectweb.celtix.tools.common.toolspec.parser.BadUsageException;
016: import org.objectweb.celtix.tools.common.toolspec.parser.CommandDocument;
017: import org.objectweb.celtix.tools.common.toolspec.parser.ErrorVisitor;
018: import org.objectweb.celtix.tools.processors.wsdl2.WSDLToJavaProcessor;
019:
020: public class WSDLToJava extends AbstractCeltixToolContainer {
021:
022: private static final String TOOL_NAME = "wsdl2java";
023: private static String[] args;
024:
025: public WSDLToJava(ToolSpec toolspec) throws Exception {
026: super (TOOL_NAME, toolspec);
027: }
028:
029: private Set getArrayKeys() {
030: Set<String> set = new HashSet<String>();
031: set.add(ToolConstants.CFG_BINDING);
032: set.add(ToolConstants.CFG_PACKAGENAME);
033: set.add(ToolConstants.CFG_NEXCLUDE);
034: return set;
035: }
036:
037: public void execute(boolean exitOnFinish) {
038: WSDLToJavaProcessor processor = new WSDLToJavaProcessor();
039: try {
040: super .execute(exitOnFinish);
041: if (!hasInfoOption()) {
042: ProcessorEnvironment env = new ProcessorEnvironment();
043: env.setParameters(getParametersMap(getArrayKeys()));
044: if (env.get(ToolConstants.CFG_OUTPUTDIR) == null) {
045: env.put(ToolConstants.CFG_OUTPUTDIR, ".");
046: }
047:
048: if (env.containsKey(ToolConstants.CFG_ANT)) {
049: setAntProperties(env);
050: setLibraryReferences(env);
051: }
052:
053: if (isVerboseOn()) {
054: env.put(ToolConstants.CFG_VERBOSE, Boolean.TRUE);
055: }
056: env.put(ToolConstants.CFG_CMD_ARG, args);
057:
058: validate(env);
059: loadDefaultNSPackageMapping(env);
060: setPackageAndNamespaces(env);
061: setExcludePackageAndNamespaces(env);
062:
063: processor.setEnvironment(env);
064: processor.process();
065: }
066: } catch (ToolException ex) {
067: System.err.println("Error : " + ex.getMessage());
068: if (ex.getCause() instanceof BadUsageException) {
069: getInstance().printUsageException(TOOL_NAME,
070: (BadUsageException) ex.getCause());
071: }
072: System.err.println();
073: if (isVerboseOn()) {
074: ex.printStackTrace();
075: }
076: } catch (Exception ex) {
077: System.err.println("Error : " + ex.getMessage());
078: System.err.println();
079: if (isVerboseOn()) {
080: ex.printStackTrace();
081: }
082: }
083: }
084:
085: private void loadDefaultNSPackageMapping(ProcessorEnvironment env) {
086: if (env.getBooleanValue(ToolConstants.CFG_DEFAULT_NS, "true")) {
087: env.loadDefaultNS2Pck();
088: }
089: if (env.getBooleanValue(ToolConstants.CFG_DEFAULT_EX, "true")) {
090: env.loadDefaultExcludes();
091: }
092: }
093:
094: private void setExcludePackageAndNamespaces(ProcessorEnvironment env) {
095: if (env.get(ToolConstants.CFG_NEXCLUDE) != null) {
096: String[] pns = (String[]) env
097: .get(ToolConstants.CFG_NEXCLUDE);
098: for (int j = 0; j < pns.length; j++) {
099: int pos = pns[j].indexOf("=");
100: String excludePackagename = pns[j];
101: if (pos != -1) {
102: String ns = pns[j].substring(0, pos);
103: excludePackagename = pns[j].substring(pos + 1);
104: env.addExcludeNamespacePackageMap(ns,
105: excludePackagename);
106: } else {
107: env.addExcludeNamespacePackageMap(pns[j], null);
108: }
109: }
110: }
111: }
112:
113: private void setPackageAndNamespaces(ProcessorEnvironment env) {
114: if (env.get(ToolConstants.CFG_PACKAGENAME) != null) {
115: String[] pns = (String[]) env
116: .get(ToolConstants.CFG_PACKAGENAME);
117: for (int j = 0; j < pns.length; j++) {
118: int pos = pns[j].indexOf("=");
119: String packagename = pns[j];
120: if (pos != -1) {
121: String ns = pns[j].substring(0, pos);
122: packagename = pns[j].substring(pos + 1);
123: env.addNamespacePackageMap(ns, packagename);
124: } else {
125: env.setPackageName(packagename);
126: }
127: }
128: }
129: }
130:
131: private void validate(ProcessorEnvironment env)
132: throws ToolException {
133: String outdir = (String) env.get(ToolConstants.CFG_OUTPUTDIR);
134: if (outdir != null) {
135: File dir = new File(outdir);
136: if (!dir.exists()) {
137: Message msg = new Message("DIRECTORY_NOT_EXIST", LOG,
138: outdir);
139: throw new ToolException(msg);
140: }
141: if (!dir.isDirectory()) {
142: Message msg = new Message("NOT_A_DIRECTORY", LOG,
143: outdir);
144: throw new ToolException(msg);
145: }
146: }
147:
148: if (env.containsKey(ToolConstants.CFG_BINDING)) {
149: String[] bindings = (String[]) env
150: .get(ToolConstants.CFG_BINDING);
151: for (int i = 0; i < bindings.length; i++) {
152: File binding = new File(bindings[i]);
153: if (!binding.exists()) {
154: Message msg = new Message("FILE_NOT_EXIST", LOG,
155: binding);
156: throw new ToolException(msg);
157: } else if (binding.isDirectory()) {
158: Message msg = new Message("NOT_A_FILE", LOG,
159: binding);
160: throw new ToolException(msg);
161: }
162: }
163: }
164:
165: if (!env.optionSet(ToolConstants.CFG_EXTRA_SOAPHEADER)) {
166: env.put(ToolConstants.CFG_EXTRA_SOAPHEADER, "false");
167: }
168:
169: if (env.optionSet(ToolConstants.CFG_COMPILE)) {
170: String clsdir = (String) env
171: .get(ToolConstants.CFG_CLASSDIR);
172: if (clsdir != null) {
173: File dir = new File(clsdir);
174: if (!dir.exists()) {
175: Message msg = new Message("DIRECTORY_NOT_EXIST",
176: LOG, clsdir);
177: throw new ToolException(msg);
178: }
179: }
180: }
181: }
182:
183: protected void setAntProperties(ProcessorEnvironment env) {
184: String installDir = System.getProperty("install.dir");
185: if (installDir != null) {
186: env.put(ToolConstants.CFG_INSTALL_DIR, installDir);
187: } else {
188: env.put(ToolConstants.CFG_INSTALL_DIR, ".");
189: }
190: }
191:
192: protected void setLibraryReferences(ProcessorEnvironment env) {
193: Properties props = loadProperties("wsdltojavalib.properties");
194: if (props != null) {
195: for (Iterator keys = props.keySet().iterator(); keys
196: .hasNext();) {
197: String key = (String) keys.next();
198: env.put(key, props.get(key));
199: }
200: }
201: env.put(ToolConstants.CFG_ANT_PROP, props);
202: }
203:
204: public static void main(String[] pargs) {
205: args = pargs;
206:
207: try {
208: ToolRunner.runTool(WSDLToJava.class, WSDLToJava.class
209: .getResourceAsStream(ToolConstants.TOOLSPECS_BASE
210: + "wsdl2java.xml"), false, args);
211: } catch (BadUsageException ex) {
212: getInstance().printUsageException(TOOL_NAME, ex);
213: } catch (Exception ex) {
214: System.err.println("Error : " + ex.getMessage());
215: System.err.println();
216: ex.printStackTrace();
217: }
218: }
219:
220: public void checkParams(ErrorVisitor errors) throws ToolException {
221: CommandDocument doc = super .getCommandDocument();
222:
223: if (!doc.hasParameter("wsdlurl")) {
224: errors.add(new ErrorVisitor.UserError(
225: "WSDL/SCHEMA URL has to be specified"));
226: }
227: if (errors.getErrors().size() > 0) {
228: Message msg = new Message("PARAMETER_MISSING", LOG);
229: throw new ToolException(msg, new BadUsageException(
230: getUsage(), errors));
231: }
232: }
233: }
|