1 55 package org.jboss.axis.wsdl.gen; 56 57 import org.jboss.axis.utils.CLArgsParser; 58 import org.jboss.axis.utils.CLOption; 59 import org.jboss.axis.utils.CLOptionDescriptor; 60 import org.jboss.axis.utils.CLUtil; 61 import org.jboss.axis.utils.DefaultAuthenticator; 62 import org.jboss.axis.utils.Messages; 63 64 import java.net.Authenticator ; 65 import java.net.MalformedURLException ; 66 import java.net.URL ; 67 import java.util.List ; 68 69 public class WSDL2 70 { 71 72 protected static final int DEBUG_OPT = 'D'; 73 protected static final int HELP_OPT = 'h'; 74 protected static final int NETWORK_TIMEOUT_OPT = 'O'; 75 protected static final int NOIMPORTS_OPT = 'n'; 76 protected static final int VERBOSE_OPT = 'v'; 77 protected static final int NOWRAP_OPT = 'W'; 78 79 protected CLOptionDescriptor[] options = new CLOptionDescriptor[]{ 80 new CLOptionDescriptor("help", 81 CLOptionDescriptor.ARGUMENT_DISALLOWED, 82 HELP_OPT, 83 Messages.getMessage("optionHelp00")), 84 new CLOptionDescriptor("verbose", 85 CLOptionDescriptor.ARGUMENT_DISALLOWED, 86 VERBOSE_OPT, 87 Messages.getMessage("optionVerbose00")), 88 new CLOptionDescriptor("noImports", 89 CLOptionDescriptor.ARGUMENT_DISALLOWED, 90 NOIMPORTS_OPT, 91 Messages.getMessage("optionImport00")), 92 new CLOptionDescriptor("timeout", 93 CLOptionDescriptor.ARGUMENT_REQUIRED, 94 NETWORK_TIMEOUT_OPT, 95 Messages.getMessage("optionTimeout00")), 96 new CLOptionDescriptor("Debug", 97 CLOptionDescriptor.ARGUMENT_DISALLOWED, 98 DEBUG_OPT, 99 Messages.getMessage("optionDebug00")), 100 new CLOptionDescriptor("noWrapped", 101 CLOptionDescriptor.ARGUMENT_DISALLOWED, 102 NOWRAP_OPT, 103 Messages.getMessage("optionNoWrap00")) 104 }; 105 106 protected String wsdlURI = null; 107 protected Parser parser; 108 109 113 protected WSDL2() 114 { 115 parser = createParser(); 116 } 118 122 protected Parser createParser() 123 { 124 return new Parser(); 125 } 127 131 protected Parser getParser() 132 { 133 return parser; 134 } 136 142 protected void addOptions(CLOptionDescriptor[] newOptions) 143 { 144 if (newOptions != null && newOptions.length > 0) 145 { 146 CLOptionDescriptor[] allOptions = new CLOptionDescriptor[ 147 options.length + newOptions.length]; 148 System.arraycopy(options, 0, allOptions, 0, options.length); 149 System.arraycopy(newOptions, 0, allOptions, options.length, newOptions.length); 150 options = allOptions; 151 } 152 } 154 160 protected void removeOption(String name) 161 { 162 int foundOptionIndex = -1; 163 for (int i = 0; i < options.length; i++) 164 { 165 if (options[i].getName().equals(name)) 166 { 167 foundOptionIndex = i; 168 break; 169 } 170 } 171 if (foundOptionIndex != -1) 172 { 173 CLOptionDescriptor[] newOptions = 174 new CLOptionDescriptor[options.length - 1]; 175 System.arraycopy(options, 0, newOptions, 0, foundOptionIndex); 176 if (foundOptionIndex < newOptions.length) 177 { 178 System.arraycopy(options, 179 foundOptionIndex + 1, 180 newOptions, 181 foundOptionIndex, 182 newOptions.length - foundOptionIndex); 183 } 184 options = newOptions; 185 } 186 } 188 193 protected void parseOption(CLOption option) 194 { 195 switch (option.getId()) 196 { 197 case CLOption.TEXT_ARGUMENT: 198 if (wsdlURI != null) 199 { 200 System.out.println(Messages.getMessage("w2jDuplicateWSDLURI00", 201 wsdlURI, 202 option.getArgument())); 203 printUsage(); 204 } 205 wsdlURI = option.getArgument(); 206 break; 207 208 case HELP_OPT: 209 printUsage(); 210 break; 211 212 case NOIMPORTS_OPT: 213 parser.setImports(false); 214 break; 215 216 case NETWORK_TIMEOUT_OPT: 217 String timeoutValue = option.getArgument(); 218 long timeout = Long.parseLong(timeoutValue); 219 if (timeout > 0) 221 timeout = timeout * 1000; 222 parser.setTimeout(timeout); 223 break; 224 225 case VERBOSE_OPT: 226 parser.setVerbose(true); 227 break; 228 229 case DEBUG_OPT: 230 parser.setDebug(true); 231 break; 232 233 case NOWRAP_OPT: 234 parser.setNowrap(true); 235 break; 236 } 237 } 239 244 protected void validateOptions() 245 { 246 if (wsdlURI == null) 247 { 248 System.out.println(Messages.getMessage("w2jMissingWSDLURI00")); 249 printUsage(); 250 } 251 252 checkForAuthInfo(wsdlURI); 254 Authenticator.setDefault(new DefaultAuthenticator(parser.getUsername(), parser.getPassword())); 255 } 257 258 262 private void checkForAuthInfo(String uri) 263 { 264 URL url = null; 265 try 266 { 267 url = new URL (uri); 268 } 269 catch (MalformedURLException e) 270 { 271 return; 273 } 274 String userInfo = url.getUserInfo(); 275 if (userInfo != null) 276 { 277 int i = userInfo.indexOf(':'); 278 if (i >= 0) 279 { 280 parser.setUsername(userInfo.substring(0, i)); 281 parser.setPassword(userInfo.substring(i + 1)); 282 } 283 else 284 { 285 parser.setUsername(userInfo); 286 } 287 } 288 } 289 290 294 protected void printUsage() 295 { 296 String lSep = System.getProperty("line.separator"); 297 StringBuffer msg = new StringBuffer (); 298 msg.append(Messages.getMessage("usage00", 299 "java " + getClass().getName() + " [options] WSDL-URI")) 300 .append(lSep); 301 msg.append(Messages.getMessage("options00")).append(lSep); 302 msg.append(CLUtil.describeOptions(options).toString()); 303 System.out.println(msg.toString()); 304 System.exit(1); 305 } 307 313 protected void run(String [] args) 314 { 315 CLArgsParser argsParser = new CLArgsParser(args, options); 317 318 if (null != argsParser.getErrorString()) 320 { 321 System.err.println(Messages.getMessage("error01", argsParser.getErrorString())); 322 printUsage(); 323 } 324 325 List clOptions = argsParser.getArguments(); 327 int size = clOptions.size(); 328 329 try 330 { 331 for (int i = 0; i < size; i++) 333 { 334 parseOption((CLOption)clOptions.get(i)); 335 } 336 337 validateOptions(); 340 341 parser.run(wsdlURI); 342 343 System.exit(0); 345 } 346 catch (Throwable t) 347 { 348 t.printStackTrace(); 349 System.exit(1); 350 } 351 } 353 359 public static void main(String [] args) 360 { 361 WSDL2 wsdl2 = new WSDL2(); 362 wsdl2.run(args); 363 } } | Popular Tags |