001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.ws.java2wsdl;
020:
021: import org.apache.tools.ant.AntClassLoader;
022: import org.apache.tools.ant.BuildException;
023: import org.apache.tools.ant.Task;
024: import org.apache.tools.ant.types.Path;
025: import org.apache.tools.ant.types.Reference;
026: import org.apache.ws.java2wsdl.utils.Java2WSDLCommandLineOption;
027: import org.apache.axis2.description.java2wsdl.Java2WSDLConstants;
028:
029: import java.util.ArrayList;
030: import java.util.HashMap;
031: import java.util.Iterator;
032: import java.util.Map;
033:
034: public class Java2WSDLTask extends Task implements Java2WSDLConstants {
035: public static final String OPEN_BRACKET = "[";
036: public static final String CLOSE_BRACKET = "]";
037: public static final String COMMA = ",";
038:
039: private String className = null;
040: private String outputLocation = null;
041: private String targetNamespace = null;
042: private String targetNamespacePrefix = null;
043: private String schemaTargetNamespace = null;
044: private String schemaTargetNamespacePrefix = null;
045: private String serviceName = null;
046: private String outputFileName = null;
047: private Path classpath = null;
048: private String style = Java2WSDLConstants.DOCUMENT;
049: private String use = Java2WSDLConstants.LITERAL;
050: private String locationUri;
051: private String attrFormDefault = null;
052: private String elementFormDefault = null;
053: private String wsdlVersion = null;
054: private String docLitBare = null;
055:
056: //names of java types not used in the service defn. directly, but for which schema must be generated
057: private String[] extraClasses;
058:
059: //namespace generator classname
060: private String nsGenClassName = null;
061:
062: //package to namespace map
063: private HashMap namespaceMap = new HashMap();
064:
065: //names of java types not used in the service defn. directly, but for which schema must be generated
066: private ArrayList pkg2nsMappings = new ArrayList();
067:
068: private MappingSet mappings = new MappingSet();
069:
070: private String schemaGenClassName = null;
071:
072: public String getLocationUri() {
073: return locationUri;
074: }
075:
076: public void setLocationUri(String locationUri) {
077: this .locationUri = locationUri;
078: }
079:
080: public String getStyle() {
081: return style;
082: }
083:
084: public void setStyle(String style) {
085: this .style = style;
086: }
087:
088: public String getUse() {
089: return use;
090: }
091:
092: public void setUse(String use) {
093: this .use = use;
094: }
095:
096: /**
097: *
098: */
099: public Java2WSDLTask() {
100: super ();
101: }
102:
103: /**
104: * Fills the option map. This map is passed onto
105: * the code generation API to generate the code.
106: */
107: private Map fillOptionMap() {
108: Map optionMap = new HashMap();
109:
110: // Check that critical options exist
111: if (className == null) {
112: throw new BuildException("You must specify a classname");
113: }
114:
115: ////////////////////////////////////////////////////////////////
116:
117: // Classname
118: addToOptionMap(optionMap, Java2WSDLConstants.CLASSNAME_OPTION,
119: className);
120:
121: // Output location
122: addToOptionMap(optionMap,
123: Java2WSDLConstants.OUTPUT_LOCATION_OPTION,
124: outputLocation);
125:
126: // Target namespace
127: addToOptionMap(optionMap,
128: Java2WSDLConstants.TARGET_NAMESPACE_OPTION,
129: targetNamespace);
130:
131: // Target namespace prefix
132: addToOptionMap(optionMap,
133: Java2WSDLConstants.TARGET_NAMESPACE_PREFIX_OPTION,
134: targetNamespacePrefix);
135:
136: // Schema target namespace
137: addToOptionMap(optionMap,
138: Java2WSDLConstants.SCHEMA_TARGET_NAMESPACE_OPTION,
139: schemaTargetNamespace);
140:
141: // Schema target namespace prefix
142: addToOptionMap(
143: optionMap,
144: Java2WSDLConstants.SCHEMA_TARGET_NAMESPACE_PREFIX_OPTION,
145: schemaTargetNamespacePrefix);
146:
147: // Service name
148: addToOptionMap(optionMap,
149: Java2WSDLConstants.SERVICE_NAME_OPTION, serviceName);
150:
151: // Output file name
152: addToOptionMap(optionMap,
153: Java2WSDLConstants.OUTPUT_FILENAME_OPTION,
154: outputFileName);
155:
156: addToOptionMap(optionMap, Java2WSDLConstants.STYLE_OPTION,
157: getStyle());
158:
159: addToOptionMap(optionMap, Java2WSDLConstants.USE_OPTION,
160: getUse());
161:
162: addToOptionMap(optionMap, Java2WSDLConstants.LOCATION_OPTION,
163: getLocationUri());
164:
165: addToOptionMap(optionMap,
166: Java2WSDLConstants.ATTR_FORM_DEFAULT_OPTION,
167: getAttrFormDefault());
168:
169: addToOptionMap(optionMap,
170: Java2WSDLConstants.ELEMENT_FORM_DEFAULT_OPTION,
171: getElementFormDefault());
172:
173: addToOptionMap(optionMap,
174: Java2WSDLConstants.EXTRA_CLASSES_DEFAULT_OPTION,
175: getExtraClasses());
176:
177: addToOptionMap(optionMap,
178: Java2WSDLConstants.NAMESPACE_GENERATOR_OPTION,
179: getNsGenClassName());
180:
181: addToOptionMap(optionMap,
182: Java2WSDLConstants.SCHEMA_GENERATOR_OPTION,
183: getSchemaGenClassName());
184:
185: addToOptionMap(optionMap,
186: Java2WSDLConstants.WSDL_VERSION_OPTION,
187: getWSDLVersion());
188:
189: addToOptionMap(optionMap, Java2WSDLConstants.DOC_LIT_BARE,
190: getDocLitBare());
191:
192: loadPkg2NsMap();
193: addToOptionMap(optionMap,
194: Java2WSDLConstants.JAVA_PKG_2_NSMAP_OPTION,
195: getPkg2nsMappings());
196:
197: return optionMap;
198: }
199:
200: /**
201: * Utility method to convert a string into a single item string[]
202: *
203: * @param value
204: * @return Returns String[].
205: */
206: private String[] getStringArray(String value) {
207: String[] values = new String[1];
208: values[0] = value;
209: return values;
210: }
211:
212: /**
213: * Function to put arguments in the option map.
214: * This functions skips adding of options that have a null value.
215: *
216: * @param map The option map into which the option is to be added
217: * @param option The option name
218: * @param value The value of the option
219: */
220: private void addToOptionMap(Map map, String option, String value) {
221: if (value != null) {
222: map.put(option, new Java2WSDLCommandLineOption(option,
223: getStringArray(value)));
224: }
225: }
226:
227: private void addToOptionMap(Map map, String option, String[] values) {
228: if (values != null && values.length > 0) {
229: map.put(option, new Java2WSDLCommandLineOption(option,
230: values));
231: }
232: }
233:
234: private void addToOptionMap(Map map, String option, ArrayList values) {
235: if (values != null && !values.isEmpty()) {
236: map.put(option, new Java2WSDLCommandLineOption(option,
237: values));
238: }
239: }
240:
241: public void execute() throws BuildException {
242: try {
243:
244: Map commandLineOptions = this .fillOptionMap();
245: ClassLoader conextClassLoader = Thread.currentThread()
246: .getContextClassLoader();
247: AntClassLoader cl = new AntClassLoader(getClass()
248: .getClassLoader(), getProject(),
249: classpath == null ? createClasspath() : classpath,
250: false);
251:
252: commandLineOptions.put(Java2WSDLConstants.CLASSPATH_OPTION,
253: new Java2WSDLCommandLineOption(
254: Java2WSDLConstants.CLASSPATH_OPTION,
255: classpath.list()));
256:
257: Thread.currentThread().setContextClassLoader(cl);
258:
259: if (outputLocation != null)
260: cl.addPathElement(outputLocation);
261:
262: new Java2WSDLCodegenEngine(commandLineOptions).generate();
263: Thread.currentThread().setContextClassLoader(
264: conextClassLoader);
265: } catch (Throwable e) {
266: throw new BuildException(e);
267: }
268:
269: }
270:
271: public void setClassName(String className) {
272: this .className = className;
273: }
274:
275: public void setOutputLocation(String outputLocation) {
276: this .outputLocation = outputLocation;
277: }
278:
279: public void setTargetNamespace(String targetNamespace) {
280: this .targetNamespace = targetNamespace;
281: }
282:
283: public void setTargetNamespacePrefix(String targetNamespacePrefix) {
284: this .targetNamespacePrefix = targetNamespacePrefix;
285: }
286:
287: public void setSchemaTargetNamespace(String schemaTargetNamespace) {
288: this .schemaTargetNamespace = schemaTargetNamespace;
289: }
290:
291: public void setSchemaTargetNamespacePrefix(
292: String schemaTargetNamespacePrefix) {
293: this .schemaTargetNamespacePrefix = schemaTargetNamespacePrefix;
294: }
295:
296: public void setServiceName(String serviceName) {
297: this .serviceName = serviceName;
298: }
299:
300: public void setOutputFileName(String outputFileName) {
301: this .outputFileName = outputFileName;
302: }
303:
304: /**
305: * Set the optional classpath
306: *
307: * @param classpath the classpath to use when loading class
308: */
309: public void setClasspath(Path classpath) {
310: createClasspath().append(classpath);
311: }
312:
313: /**
314: * Set the optional classpath
315: *
316: * @return a path instance to be configured by the Ant core.
317: */
318: public Path createClasspath() {
319: if (classpath == null) {
320: classpath = new Path(getProject());
321: classpath = classpath.concatSystemClasspath();
322: }
323: return classpath.createPath();
324: }
325:
326: /**
327: * Set the reference to an optional classpath
328: *
329: * @param r the id of the Ant path instance to act as the classpath
330: */
331: public void setClasspathRef(Reference r) {
332: createClasspath().setRefid(r);
333: }
334:
335: public String getAttrFormDefault() {
336: return attrFormDefault;
337: }
338:
339: public void setAttrFormDefault(String attrFormDefault) {
340: this .attrFormDefault = attrFormDefault;
341: }
342:
343: public String getElementFormDefault() {
344: return elementFormDefault;
345: }
346:
347: public void setElementFormDefault(String elementFormDefault) {
348: this .elementFormDefault = elementFormDefault;
349: }
350:
351: public String[] getExtraClasses() {
352: return extraClasses;
353: }
354:
355: public void setExtraClasses(String[] extraClasses) {
356: this .extraClasses = extraClasses;
357: }
358:
359: public String getNsGenClassName() {
360: return nsGenClassName;
361: }
362:
363: public void setNsGenClassName(String nsGenClassName) {
364: this .nsGenClassName = nsGenClassName;
365: }
366:
367: public String getSchemaGenClassName() {
368: return schemaGenClassName;
369: }
370:
371: public void setSchemaGenClassName(String schemaGenClassName) {
372: this .schemaGenClassName = schemaGenClassName;
373: }
374:
375: public void loadPkg2NsMap() {
376: mappings.execute(namespaceMap, true);
377: Iterator packageNames = namespaceMap.keySet().iterator();
378: String packageName = null;
379: while (packageNames.hasNext()) {
380: packageName = (String) packageNames.next();
381: pkg2nsMappings.add(OPEN_BRACKET + packageName + COMMA
382: + namespaceMap.get(packageName) + CLOSE_BRACKET);
383: }
384: }
385:
386: public ArrayList getPkg2nsMappings() {
387: return pkg2nsMappings;
388: }
389:
390: public void setPkg2nsMappings(ArrayList pkg2nsMappings) {
391: this .pkg2nsMappings = pkg2nsMappings;
392: }
393:
394: /**
395: * add a mapping of namespaces to packages
396: */
397: public void addMapping(NamespaceMapping mapping) {
398: mappings.addMapping(mapping);
399: }
400:
401: /**
402: * add a mapping of namespaces to packages
403: */
404: public void addMappingSet(MappingSet mappingset) {
405: mappings.addMappingSet(mappingset);
406: }
407:
408: public String getDocLitBare() {
409: return docLitBare;
410: }
411:
412: public void setDocLitBare(String docLitBare) {
413: this .docLitBare = docLitBare;
414: }
415:
416: public String getWSDLVersion() {
417: return wsdlVersion;
418: }
419:
420: public void setWSDLVersion(String wsdlVersion) {
421: this.wsdlVersion = wsdlVersion;
422: }
423: }
|