1 16 package org.apache.axis.wsdl.gen; 17 18 import org.apache.axis.utils.CLArgsParser; 19 import org.apache.axis.utils.CLOption; 20 import org.apache.axis.utils.CLOptionDescriptor; 21 import org.apache.axis.utils.CLUtil; 22 import org.apache.axis.utils.DefaultAuthenticator; 23 import org.apache.axis.utils.Messages; 24 25 import java.net.Authenticator ; 26 import java.net.MalformedURLException ; 27 import java.net.URL ; 28 import java.util.List ; 29 30 35 public class WSDL2 { 36 37 38 protected static final int DEBUG_OPT = 'D'; 39 40 41 protected static final int HELP_OPT = 'h'; 42 43 44 protected static final int NETWORK_TIMEOUT_OPT = 'O'; 45 46 47 protected static final int NOIMPORTS_OPT = 'n'; 48 49 50 protected static final int VERBOSE_OPT = 'v'; 51 52 53 protected static final int NOWRAP_OPT = 'W'; 54 55 56 protected static final int QUIET_OPT = 'q'; 57 58 59 protected CLOptionDescriptor[] options = new CLOptionDescriptor[]{ 60 new CLOptionDescriptor("help", CLOptionDescriptor.ARGUMENT_DISALLOWED, 61 HELP_OPT, Messages.getMessage("optionHelp00")), 62 new CLOptionDescriptor("verbose", 63 CLOptionDescriptor.ARGUMENT_DISALLOWED, 64 VERBOSE_OPT, 65 Messages.getMessage("optionVerbose00")), 66 new CLOptionDescriptor("noImports", 67 CLOptionDescriptor.ARGUMENT_DISALLOWED, 68 NOIMPORTS_OPT, 69 Messages.getMessage("optionImport00")), 70 new CLOptionDescriptor("timeout", CLOptionDescriptor.ARGUMENT_REQUIRED, 71 NETWORK_TIMEOUT_OPT, 72 Messages.getMessage("optionTimeout00")), 73 new CLOptionDescriptor("Debug", CLOptionDescriptor.ARGUMENT_DISALLOWED, 74 DEBUG_OPT, Messages.getMessage("optionDebug00")), 75 new CLOptionDescriptor("noWrapped", 76 CLOptionDescriptor.ARGUMENT_DISALLOWED, 77 NOWRAP_OPT, 78 Messages.getMessage("optionNoWrap00")), 79 new CLOptionDescriptor("quiet", 80 CLOptionDescriptor.ARGUMENT_DISALLOWED, 81 QUIET_OPT, 82 Messages.getMessage("optionQuiet")) 83 }; 84 85 86 protected String wsdlURI = null; 87 88 89 protected Parser parser; 90 91 95 protected WSDL2() { 96 parser = createParser(); 97 } 99 105 protected Parser createParser() { 106 return new Parser(); 107 } 109 115 protected Parser getParser() { 116 return parser; 117 } 119 125 protected void addOptions(CLOptionDescriptor[] newOptions) { 126 127 if ((newOptions != null) && (newOptions.length > 0)) { 128 CLOptionDescriptor[] allOptions = 129 new CLOptionDescriptor[options.length + newOptions.length]; 130 131 System.arraycopy(options, 0, allOptions, 0, options.length); 132 System.arraycopy(newOptions, 0, allOptions, options.length, 133 newOptions.length); 134 135 options = allOptions; 136 } 137 } 139 145 protected void removeOption(String name) { 146 147 int foundOptionIndex = -1; 148 149 for (int i = 0; i < options.length; i++) { 150 if (options[i].getName().equals(name)) { 151 foundOptionIndex = i; 152 153 break; 154 } 155 } 156 157 if (foundOptionIndex != -1) { 158 CLOptionDescriptor[] newOptions = 159 new CLOptionDescriptor[options.length - 1]; 160 161 System.arraycopy(options, 0, newOptions, 0, foundOptionIndex); 162 163 if (foundOptionIndex < newOptions.length) { 164 System.arraycopy(options, foundOptionIndex + 1, newOptions, 165 foundOptionIndex, 166 newOptions.length - foundOptionIndex); 167 } 168 169 options = newOptions; 170 } 171 } 173 178 protected void parseOption(CLOption option) { 179 180 switch (option.getId()) { 181 182 case CLOption.TEXT_ARGUMENT: 183 if (wsdlURI != null) { 184 System.out.println( 185 Messages.getMessage( 186 "w2jDuplicateWSDLURI00", wsdlURI, 187 option.getArgument())); 188 printUsage(); 189 } 190 191 wsdlURI = option.getArgument(); 192 break; 193 194 case HELP_OPT: 195 printUsage(); 196 break; 197 198 case NOIMPORTS_OPT: 199 parser.setImports(false); 200 break; 201 202 case NETWORK_TIMEOUT_OPT: 203 String timeoutValue = option.getArgument(); 204 long timeout = Long.parseLong(timeoutValue); 205 206 if (timeout > 0) { 208 timeout = timeout * 1000; 209 } 210 211 parser.setTimeout(timeout); 212 break; 213 214 case VERBOSE_OPT: 215 parser.setVerbose(true); 216 break; 217 218 case DEBUG_OPT: 219 parser.setDebug(true); 220 break; 221 222 case QUIET_OPT: 223 parser.setQuiet(true); 224 break; 225 226 case NOWRAP_OPT: 227 parser.setNowrap(true); 228 break; 229 } 230 } 232 237 protected void validateOptions() { 238 239 if (wsdlURI == null) { 240 System.out.println(Messages.getMessage("w2jMissingWSDLURI00")); 241 printUsage(); 242 } 243 244 if (parser.isQuiet()) { 245 if (parser.isVerbose()) { 246 System.out.println(Messages.getMessage("exclusiveQuietVerbose")); 247 printUsage(); 248 } 249 if (parser.isDebug()) { 250 System.out.println(Messages.getMessage("exclusiveQuietDebug")); 251 printUsage(); 252 } 253 } 254 255 checkForAuthInfo(wsdlURI); 257 Authenticator.setDefault(new DefaultAuthenticator(parser.getUsername(), 258 parser.getPassword())); 259 } 261 267 private void checkForAuthInfo(String uri) { 268 269 URL url = null; 270 271 try { 272 url = new URL (uri); 273 } catch (MalformedURLException e) { 274 275 return; 277 } 278 279 String userInfo = url.getUserInfo(); 280 281 if (userInfo != null) { 282 int i = userInfo.indexOf(':'); 283 284 if (i >= 0) { 285 parser.setUsername(userInfo.substring(0, i)); 286 parser.setPassword(userInfo.substring(i + 1)); 287 } else { 288 parser.setUsername(userInfo); 289 } 290 } 291 } 292 293 297 protected void printUsage() { 298 299 String lSep = System.getProperty("line.separator"); 300 StringBuffer msg = new StringBuffer (); 301 302 msg.append(Messages.getMessage("usage00", 303 "java " + getClass().getName() 304 + " [options] WSDL-URI")).append(lSep); 305 msg.append(Messages.getMessage("options00")).append(lSep); 306 msg.append(CLUtil.describeOptions(options).toString()); 307 System.out.println(msg.toString()); 308 System.exit(1); 309 } 311 317 protected void run(String [] args) { 318 319 CLArgsParser argsParser = new CLArgsParser(args, options); 321 322 if (null != argsParser.getErrorString()) { 324 System.err.println( 325 Messages.getMessage("error01", argsParser.getErrorString())); 326 printUsage(); 327 } 328 329 List clOptions = argsParser.getArguments(); 331 int size = clOptions.size(); 332 333 try { 334 335 for (int i = 0; i < size; i++) { 337 parseOption((CLOption) clOptions.get(i)); 338 } 339 340 validateOptions(); 343 parser.run(wsdlURI); 344 345 System.exit(0); 347 } catch (Throwable t) { 348 t.printStackTrace(); 349 System.exit(1); 350 } 351 } 353 359 public static void main(String [] args) { 360 361 WSDL2 wsdl2 = new WSDL2(); 362 363 wsdl2.run(args); 364 } } | Popular Tags |