1 | /*
|
---|
2 | * Copyright 2002-2004 The Apache Software Foundation
|
---|
3 | *
|
---|
4 | * Licensed under the Apache License, Version 2.0 (the "License");
|
---|
5 | * you may not use this file except in compliance with the License.
|
---|
6 | * You may obtain a copy of the License at
|
---|
7 | *
|
---|
8 | * http://www.apache.org/licenses/LICENSE-2.0
|
---|
9 | *
|
---|
10 | * Unless required by applicable law or agreed to in writing, software
|
---|
11 | * distributed under the License is distributed on an "AS IS" BASIS,
|
---|
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
---|
13 | * See the License for the specific language governing permissions and
|
---|
14 | * limitations under the License.
|
---|
15 | *
|
---|
16 | */
|
---|
17 | package org.apache.tools.ant.taskdefs.optional.dotnet;
|
---|
18 |
|
---|
19 | import java.io.File;
|
---|
20 | import org.apache.tools.ant.BuildException;
|
---|
21 | import org.apache.tools.ant.Task;
|
---|
22 |
|
---|
23 | /**
|
---|
24 | * Converts a WSDL file or URL resource into a .NET language.
|
---|
25 | *
|
---|
26 | * Why add a wrapper to the MS WSDL tool?
|
---|
27 | * So that you can verify that your web services, be they written with Axis or
|
---|
28 | *anyone else's SOAP toolkit, work with .NET clients.
|
---|
29 | *
|
---|
30 | *This task is dependency aware when using a file as a source and destination;
|
---|
31 | *so if you <get> the file (with <code>usetimestamp="true"</code>) then
|
---|
32 | *you only rebuild stuff when the WSDL file is changed. Of course,
|
---|
33 | *if the server generates a new timestamp every time you ask for the WSDL,
|
---|
34 | *this is not enough...use the <filesmatch> <condition> to
|
---|
35 | *to byte for byte comparison against a cached WSDL file then make
|
---|
36 | *the target conditional on that test failing.
|
---|
37 |
|
---|
38 | * See "Creating an XML Web Service Proxy", "wsdl.exe" docs in
|
---|
39 | * the framework SDK documentation
|
---|
40 | * @version 0.5
|
---|
41 | * @ant.task category="dotnet"
|
---|
42 | * @since Ant 1.5
|
---|
43 | */
|
---|
44 |
|
---|
45 | public class WsdlToDotnet extends Task {
|
---|
46 |
|
---|
47 | /**
|
---|
48 | * name of output file (required)
|
---|
49 | */
|
---|
50 | private File destFile = null;
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * url to retrieve
|
---|
54 | */
|
---|
55 | private String url = null;
|
---|
56 |
|
---|
57 | /**
|
---|
58 | * name of source file
|
---|
59 | */
|
---|
60 | private File srcFile = null;
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * language; defaults to C#
|
---|
64 | */
|
---|
65 | private String language = "CS";
|
---|
66 |
|
---|
67 | /**
|
---|
68 | * flag set to true to generate server side skeleton
|
---|
69 | */
|
---|
70 | private boolean server = false;
|
---|
71 |
|
---|
72 | /**
|
---|
73 | * namespace
|
---|
74 | */
|
---|
75 | private String namespace = null;
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * flag to control action on execution trouble
|
---|
79 | */
|
---|
80 | private boolean failOnError = true;
|
---|
81 |
|
---|
82 | /**
|
---|
83 | * any extra command options?
|
---|
84 | */
|
---|
85 | protected String extraOptions = null;
|
---|
86 |
|
---|
87 | /**
|
---|
88 | * Name of the file to generate. Required
|
---|
89 | * @param destFile filename
|
---|
90 | */
|
---|
91 | public void setDestFile(File destFile) {
|
---|
92 | this.destFile = destFile;
|
---|
93 | }
|
---|
94 |
|
---|
95 | /**
|
---|
96 | * Sets the URL to fetch. Fetching is by wsdl.exe; Ant proxy settings
|
---|
97 | * are ignored; either url or srcFile is required.
|
---|
98 | * @param url url to save
|
---|
99 | */
|
---|
100 |
|
---|
101 | public void setUrl(String url) {
|
---|
102 | this.url = url;
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * The local WSDL file to parse; either url or srcFile is required.
|
---|
107 | * @param srcFile name of WSDL file
|
---|
108 | */
|
---|
109 | public void setSrcFile(File srcFile) {
|
---|
110 | this.srcFile = srcFile;
|
---|
111 | }
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * set the language; one of "CS", "JS", or "VB"
|
---|
115 | * optional, default is CS for C# source
|
---|
116 | * @param language language to generate
|
---|
117 | */
|
---|
118 | public void setLanguage(String language) {
|
---|
119 | this.language = language;
|
---|
120 | }
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * flag to enable server side code generation;
|
---|
124 | * optional, default=false
|
---|
125 | * @param server server-side flag
|
---|
126 | */
|
---|
127 |
|
---|
128 | public void setServer(boolean server) {
|
---|
129 | this.server = server;
|
---|
130 | }
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * namespace to place the source in.
|
---|
134 | * optional; default ""
|
---|
135 | * @param namespace new namespace
|
---|
136 | */
|
---|
137 | public void setNamespace(String namespace) {
|
---|
138 | this.namespace = namespace;
|
---|
139 | }
|
---|
140 |
|
---|
141 | /**
|
---|
142 | * Whether or not a failure should halt the build.
|
---|
143 | * Optional - default is <code>true</code>.
|
---|
144 | * @param failOnError new failure option
|
---|
145 | */
|
---|
146 | public void setFailOnError(boolean failOnError) {
|
---|
147 | this.failOnError = failOnError;
|
---|
148 | }
|
---|
149 |
|
---|
150 | /**
|
---|
151 | * Any extra WSDL.EXE options which aren't explicitly
|
---|
152 | * supported by the ant wrapper task; optional
|
---|
153 | *
|
---|
154 | *@param extraOptions The new ExtraOptions value
|
---|
155 | */
|
---|
156 | public void setExtraOptions(String extraOptions) {
|
---|
157 | this.extraOptions = extraOptions;
|
---|
158 | }
|
---|
159 |
|
---|
160 | /**
|
---|
161 | * validation code
|
---|
162 | * @throws BuildException if validation failed
|
---|
163 | */
|
---|
164 | protected void validate()
|
---|
165 | throws BuildException {
|
---|
166 | if (destFile == null) {
|
---|
167 | throw new BuildException("destination file must be specified");
|
---|
168 | }
|
---|
169 | if (destFile.isDirectory()) {
|
---|
170 | throw new BuildException(
|
---|
171 | "destination file is a directory");
|
---|
172 | }
|
---|
173 | if (url != null && srcFile != null) {
|
---|
174 | throw new BuildException(
|
---|
175 | "you can not specify both a source file and a URL");
|
---|
176 | }
|
---|
177 | if (url == null && srcFile == null) {
|
---|
178 | throw new BuildException(
|
---|
179 | "you must specify either a source file or a URL");
|
---|
180 | }
|
---|
181 | if (srcFile != null) {
|
---|
182 | if (!srcFile.exists()) {
|
---|
183 | throw new BuildException(
|
---|
184 | "source file does not exist");
|
---|
185 | }
|
---|
186 | if (srcFile.isDirectory()) {
|
---|
187 | throw new BuildException(
|
---|
188 | "source file is a directory");
|
---|
189 | }
|
---|
190 | }
|
---|
191 |
|
---|
192 | }
|
---|
193 |
|
---|
194 | /**
|
---|
195 | * do the work by building the command line and then calling it
|
---|
196 | *
|
---|
197 | *@throws BuildException if validation or execution failed
|
---|
198 | */
|
---|
199 | public void execute()
|
---|
200 | throws BuildException {
|
---|
201 | validate();
|
---|
202 | NetCommand command = new NetCommand(this, "WSDL", "wsdl");
|
---|
203 | command.setFailOnError(failOnError);
|
---|
204 | //fill in args
|
---|
205 | command.addArgument("/nologo");
|
---|
206 | command.addArgument("/out:" + destFile);
|
---|
207 | command.addArgument("/language:", language);
|
---|
208 | if (server) {
|
---|
209 | command.addArgument("/server");
|
---|
210 | }
|
---|
211 | command.addArgument("/namespace:", namespace);
|
---|
212 | command.addArgument(extraOptions);
|
---|
213 |
|
---|
214 | //set source and rebuild options
|
---|
215 | boolean rebuild = true;
|
---|
216 | if (srcFile != null) {
|
---|
217 | command.addArgument(srcFile.toString());
|
---|
218 | //rebuild unless the dest file is newer than the source file
|
---|
219 | if (srcFile.exists() && destFile.exists()
|
---|
220 | && srcFile.lastModified() <= destFile.lastModified()) {
|
---|
221 | rebuild = false;
|
---|
222 | }
|
---|
223 | } else {
|
---|
224 | //no source file? must be a url, which has no dependency
|
---|
225 | //handling
|
---|
226 | rebuild = true;
|
---|
227 | command.addArgument(url);
|
---|
228 | }
|
---|
229 | if (rebuild) {
|
---|
230 | command.runCommand();
|
---|
231 | }
|
---|
232 | }
|
---|
233 | }
|
---|
234 |
|
---|