1 package org.objectweb.celtix.tools.processors.wsdl2; 2 3 import java.io.File ; 4 import java.io.IOException ; 5 import java.io.Writer ; 6 import java.util.ArrayList ; 7 import java.util.Collection ; 8 import java.util.HashMap ; 9 import java.util.Iterator ; 10 import java.util.List ; 11 import java.util.Map ; 12 import java.util.Properties ; 13 import java.util.logging.Level ; 14 import java.util.logging.Logger ; 15 16 import javax.wsdl.Binding; 17 import javax.wsdl.Definition; 18 import javax.wsdl.Import; 19 import javax.wsdl.Message; 20 21 import javax.wsdl.PortType; 22 import javax.wsdl.Service; 23 import javax.wsdl.Types; 24 import javax.wsdl.WSDLException; 25 26 import javax.wsdl.extensions.schema.Schema; 27 import javax.wsdl.extensions.schema.SchemaImport; 28 import javax.wsdl.factory.WSDLFactory; 29 import javax.wsdl.xml.WSDLReader; 30 31 import org.w3c.dom.Element ; 32 import org.w3c.dom.Node ; 33 import org.w3c.dom.NodeList ; 34 35 import org.xml.sax.InputSource ; 36 37 import com.sun.tools.xjc.api.S2JJAXBModel; 38 import com.sun.tools.xjc.api.SchemaCompiler; 39 import com.sun.tools.xjc.api.XJC; 40 41 import org.apache.velocity.app.Velocity; 42 import org.objectweb.celtix.common.logging.LogUtils; 43 import org.objectweb.celtix.common.util.StringUtils; 44 import org.objectweb.celtix.tools.common.Processor; 45 import org.objectweb.celtix.tools.common.ProcessorEnvironment; 46 import org.objectweb.celtix.tools.common.ToolConstants; 47 import org.objectweb.celtix.tools.common.ToolException; 48 import org.objectweb.celtix.tools.extensions.jaxws.CustomizationParser; 49 import org.objectweb.celtix.tools.generators.AbstractGenerator; 50 import org.objectweb.celtix.tools.processors.wsdl2.internal.ClassCollector; 51 import org.objectweb.celtix.tools.processors.wsdl2.internal.ClassNameAllocatorImpl; 52 import org.objectweb.celtix.tools.processors.wsdl2.validators.WSDL11Validator; 53 import org.objectweb.celtix.tools.utils.FileWriterUtil; 54 import org.objectweb.celtix.tools.utils.JAXBUtils; 55 import org.objectweb.celtix.tools.utils.WSDLExtensionRegister; 56 57 public class WSDLToProcessor implements Processor, com.sun.tools.xjc.api.ErrorListener { 58 protected static final Logger LOG = LogUtils.getL7dLogger(WSDLToProcessor.class); 59 protected static final String WSDL_FILE_NAME_EXT = ".wsdl"; 60 61 protected Definition wsdlDefinition; 62 protected ProcessorEnvironment env; 63 protected WSDLFactory wsdlFactory; 64 protected WSDLReader wsdlReader; 65 protected S2JJAXBModel rawJaxbModel; 66 protected S2JJAXBModel rawJaxbModelGenCode; 67 68 protected ClassCollector classColletor; 69 List <Schema> schemaList = new ArrayList <Schema>(); 70 private final Map <String , AbstractGenerator> generators = new HashMap <String , AbstractGenerator>(); 71 private List <Definition> importedDefinitions = new ArrayList <Definition>(); 72 private List <String > schemaTargetNamespaces = new ArrayList <String >(); 73 74 protected Writer getOutputWriter(String newNameExt) throws ToolException { 75 Writer writer = null; 76 String newName = null; 77 String outputDir; 78 79 if (env.get(ToolConstants.CFG_OUTPUTFILE) != null) { 80 newName = (String )env.get(ToolConstants.CFG_OUTPUTFILE); 81 } else { 82 String oldName = (String )env.get(ToolConstants.CFG_WSDLURL); 83 int position = oldName.lastIndexOf("/"); 84 if (position < 0) { 85 position = oldName.lastIndexOf("\\"); 86 } 87 if (position >= 0) { 88 oldName = oldName.substring(position + 1, oldName.length()); 89 } 90 if (oldName.toLowerCase().indexOf(WSDL_FILE_NAME_EXT) >= 0) { 91 newName = oldName.substring(0, oldName.length() - 5) + newNameExt + WSDL_FILE_NAME_EXT; 92 } else { 93 newName = oldName + newNameExt; 94 } 95 } 96 if (env.get(ToolConstants.CFG_OUTPUTDIR) != null) { 97 outputDir = (String )env.get(ToolConstants.CFG_OUTPUTDIR); 98 if (!("/".equals(outputDir.substring(outputDir.length() - 1)) || "\\".equals(outputDir 99 .substring(outputDir.length() - 1)))) { 100 outputDir = outputDir + "/"; 101 } 102 } else { 103 outputDir = "./"; 104 } 105 FileWriterUtil fw = new FileWriterUtil(outputDir); 106 try { 107 writer = fw.getWriter("", newName); 108 } catch (IOException ioe) { 109 org.objectweb.celtix.common.i18n.Message msg = 110 new org.objectweb.celtix.common.i18n.Message("FAIL_TO_WRITE_FILE", 111 LOG, 112 env.get(ToolConstants.CFG_OUTPUTDIR) 113 + System.getProperty("file.seperator") 114 + newName); 115 throw new ToolException(msg, ioe); 116 } 117 return writer; 118 } 119 120 protected void parseWSDL(String wsdlURL) throws ToolException { 121 try { 122 wsdlFactory = WSDLFactory.newInstance(); 123 wsdlReader = wsdlFactory.newWSDLReader(); 124 wsdlReader.setFeature("javax.wsdl.verbose", false); 125 WSDLExtensionRegister register = new WSDLExtensionRegister(wsdlFactory, wsdlReader); 126 register.registerExtenstions(); 127 wsdlDefinition = wsdlReader.readWSDL(wsdlURL); 128 parseImports(wsdlDefinition); 129 buildWSDLDefinition(); 130 } catch (WSDLException we) { 131 org.objectweb.celtix.common.i18n.Message msg = 132 new org.objectweb.celtix.common.i18n.Message("FAIL_TO_CREATE_WSDL_DEFINITION", LOG); 133 throw new ToolException(msg, we); 134 } 135 136 } 137 138 private void buildWSDLDefinition() { 139 for (Definition def : importedDefinitions) { 140 this.wsdlDefinition.addNamespace(def.getPrefix(def.getTargetNamespace()), def 141 .getTargetNamespace()); 142 Object [] services = def.getServices().values().toArray(); 143 for (int i = 0; i < services.length; i++) { 144 this.wsdlDefinition.addService((Service)services[i]); 145 } 146 147 Object [] messages = def.getMessages().values().toArray(); 148 for (int i = 0; i < messages.length; i++) { 149 this.wsdlDefinition.addMessage((Message)messages[i]); 150 } 151 152 Object [] bindings = def.getBindings().values().toArray(); 153 for (int i = 0; i < bindings.length; i++) { 154 this.wsdlDefinition.addBinding((Binding)bindings[i]); 155 } 156 157 Object [] portTypes = def.getPortTypes().values().toArray(); 158 for (int i = 0; i < portTypes.length; i++) { 159 this.wsdlDefinition.addPortType((PortType)portTypes[i]); 160 } 161 } 162 } 163 164 @SuppressWarnings ("unchecked") 165 private void parseImports(Definition def) { 166 List <Import> importList = new ArrayList <Import>(); 167 Map imports = def.getImports(); 168 for (Iterator iter = imports.keySet().iterator(); iter.hasNext();) { 169 String uri = (String )iter.next(); 170 importList.addAll((List <Import>)imports.get(uri)); 171 } 172 for (Import impt : importList) { 173 parseImports(impt.getDefinition()); 174 importedDefinitions.add(impt.getDefinition()); 175 } 176 } 177 178 private String getVelocityLogFile(String logfile) { 179 String logdir = System.getProperty("user.home"); 180 if (logdir == null || logdir.length() == 0) { 181 logdir = System.getProperty("user.dir"); 182 } 183 return logdir + File.separator + logfile; 184 } 185 186 private void initVelocity() throws ToolException { 187 try { 188 Properties props = new Properties (); 189 String clzName = "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader"; 190 props.put("resource.loader", "class"); 191 props.put("class.resource.loader.class", clzName); 192 props.put("runtime.log", getVelocityLogFile("velocity.log")); 193 194 Velocity.init(props); 195 } catch (Exception e) { 196 org.objectweb.celtix.common.i18n.Message msg = 197 new org.objectweb.celtix.common.i18n.Message("FAIL_TO_INITIALIZE_VELOCITY_ENGINE", 198 LOG); 199 LOG.log(Level.SEVERE, msg.toString()); 200 throw new ToolException(msg, e); 201 } 202 } 203 204 private void extractSchema(Definition def) { 205 Types typesElement = def.getTypes(); 206 if (typesElement != null) { 207 Iterator ite = typesElement.getExtensibilityElements().iterator(); 208 while (ite.hasNext()) { 209 Object obj = ite.next(); 210 if (obj instanceof Schema) { 211 Schema schema = (Schema)obj; 212 addSchema(schema); 213 } 214 } 215 } 216 } 217 218 private void initJAXBModel() { 219 schemaTargetNamespaces.clear(); 220 extractSchema(wsdlDefinition); 221 for (Definition def : importedDefinitions) { 222 extractSchema(def); 223 } 224 225 if (schemaList.size() == 0) { 226 if (env.isVerbose()) { 227 System.err.println("No schema provided in the wsdl file"); 228 } 229 return; 230 } 231 232 schemaTargetNamespaces.clear(); 233 234 buildJaxbModel(); 235 } 236 237 @SuppressWarnings ("unchecked") 238 private void buildJaxbModel() { 239 SchemaCompiler schemaCompiler = XJC.createSchemaCompiler(); 240 ClassNameAllocatorImpl allocator = new ClassNameAllocatorImpl(classColletor); 241 242 allocator.setPortTypes(wsdlDefinition.getPortTypes().values(), env.mapPackageName(this.wsdlDefinition 243 .getTargetNamespace())); 244 schemaCompiler.setClassNameAllocator(allocator); 245 schemaCompiler.setErrorListener(this); 246 247 SchemaCompiler schemaCompilerGenCode = schemaCompiler; 248 if (env.isExcludeNamespaceEnabled()) { 249 schemaCompilerGenCode = XJC.createSchemaCompiler(); 250 schemaCompilerGenCode.setClassNameAllocator(allocator); 251 schemaCompilerGenCode.setErrorListener(this); 252 } 253 for (Schema schema : schemaList) { 254 boolean skipGenCode = false; 255 256 Element schemaElement = schema.getElement(); 257 String targetNamespace = schemaElement.getAttribute("targetNamespace"); 258 if (StringUtils.isEmpty(targetNamespace)) { 259 continue; 260 } 261 262 if (env.hasExcludeNamespace(targetNamespace) 263 && env.getExcludePackageName(targetNamespace) == null) { 264 skipGenCode = true; 265 } 266 customizeSchema(schemaElement, targetNamespace); 267 String systemid = schema.getDocumentBaseURI(); 268 schemaCompiler.parseSchema(systemid, schemaElement); 269 if (env.isExcludeNamespaceEnabled() && !skipGenCode) { 270 schemaCompilerGenCode.parseSchema(systemid, schemaElement); 271 } 272 } 273 Collection <InputSource > jaxbBindingFiles = env.getJaxbBindingFile().values(); 274 for (InputSource bindingFile : jaxbBindingFiles) { 275 schemaCompiler.parseSchema(bindingFile); 276 if (env.isExcludeNamespaceEnabled()) { 277 schemaCompilerGenCode.parseSchema(bindingFile); 278 } 279 } 280 rawJaxbModel = schemaCompiler.bind(); 281 if (env.isExcludeNamespaceEnabled()) { 282 rawJaxbModelGenCode = schemaCompilerGenCode.bind(); 283 } else { 284 rawJaxbModelGenCode = rawJaxbModel; 285 } 286 } 287 288 private boolean isSchemaParsed(String targetNamespace) { 289 if (!schemaTargetNamespaces.contains(targetNamespace)) { 290 schemaTargetNamespaces.add(targetNamespace); 291 return false; 292 } else { 293 return true; 294 } 295 } 296 297 private void customizeSchema(Element schema, String targetNamespace) { 298 String userPackage = env.mapPackageName(targetNamespace); 299 if (env.hasExcludeNamespace(targetNamespace) && env.getExcludePackageName(targetNamespace) != null) { 300 userPackage = env.getExcludePackageName(targetNamespace); 303 } 304 if (!isSchemaParsed(targetNamespace) && !StringUtils.isEmpty(userPackage)) { 305 Node jaxbBindings = JAXBUtils.innerJaxbPackageBinding(schema, userPackage); 306 schema.appendChild(jaxbBindings); 307 } 308 309 int nodeListLen = schema.getElementsByTagNameNS(ToolConstants.SCHEMA_URI, "import").getLength(); 310 for (int i = 0; i < nodeListLen; i++) { 311 removeImportElement(schema); 312 } 313 } 314 315 private void removeImportElement(Element element) { 316 NodeList nodeList = element.getElementsByTagNameNS(ToolConstants.SCHEMA_URI, "import"); 317 if (nodeList.getLength() > 0) { 318 Node importNode = nodeList.item(0); 319 Node schemaNode = importNode.getParentNode(); 320 schemaNode.removeChild(importNode); 321 } 322 } 323 324 private boolean isSchemaImported(Schema schema) { 325 return schemaList.contains(schema); 326 } 327 328 @SuppressWarnings ("unchecked") 329 private void addSchema(Schema schema) { 330 Map <String , List > imports = schema.getImports(); 331 if (imports != null && imports.size() > 0) { 332 Collection <String > importKeys = imports.keySet(); 333 for (String importNamespace : importKeys) { 334 if (!isSchemaParsed(importNamespace + "?file=" + schema.getDocumentBaseURI())) { 335 List <SchemaImport> schemaImports = imports.get(importNamespace); 336 for (SchemaImport schemaImport : schemaImports) { 337 Schema tempImport = schemaImport.getReferencedSchema(); 338 if (tempImport != null && !isSchemaImported(tempImport)) { 339 addSchema(tempImport); 340 } 341 } 342 } 343 } 344 } 345 if (!isSchemaImported(schema)) { 346 schemaList.add(schema); 347 } 348 } 349 350 private void parseCustomization() { 351 CustomizationParser customizationParser = CustomizationParser.getInstance(); 352 customizationParser.clean(); 353 if (!env.optionSet(ToolConstants.CFG_BINDING)) { 354 return; 355 } 356 customizationParser.parse(env, wsdlDefinition); 357 } 358 359 protected void init() throws ToolException { 360 parseWSDL((String )env.get(ToolConstants.CFG_WSDLURL)); 361 parseCustomization(); 362 initVelocity(); 363 classColletor = new ClassCollector(); 364 env.put(ToolConstants.GENERATED_CLASS_COLLECTOR, classColletor); 365 366 initJAXBModel(); 367 368 } 369 370 public S2JJAXBModel getRawJaxbModel() { 371 return this.rawJaxbModel; 372 } 373 374 public Definition getWSDLDefinition() { 375 return this.wsdlDefinition; 376 } 377 378 public void addGenerator(String name, AbstractGenerator gen) { 379 generators.put(name, gen); 380 } 381 382 public void process() throws ToolException { 383 } 384 385 public void validateWSDL() throws ToolException { 386 if (env.validateWSDL()) { 387 WSDL11Validator validator = new WSDL11Validator(this.wsdlDefinition, this.env); 388 validator.isValid(); 389 } 390 } 391 392 protected void doGeneration() throws ToolException { 393 for (String genName : generators.keySet()) { 394 AbstractGenerator gen = generators.get(genName); 395 gen.generate(); 396 } 397 } 398 399 public void setEnvironment(ProcessorEnvironment penv) { 400 this.env = penv; 401 } 402 403 public ProcessorEnvironment getEnvironment() { 404 return this.env; 405 } 406 407 public void error(org.xml.sax.SAXParseException exception) { 408 if (this.env.isVerbose()) { 409 exception.printStackTrace(); 410 } else { 411 System.err.println("Parsing schema error: \n" + exception.toString()); 412 } 413 } 414 415 public void fatalError(org.xml.sax.SAXParseException exception) { 416 if (this.env.isVerbose()) { 417 exception.printStackTrace(); 418 } else { 419 System.err.println("Parsing schema fatal error: \n" + exception.toString()); 420 } 421 } 422 423 public void info(org.xml.sax.SAXParseException exception) { 424 if (this.env.isVerbose()) { 425 System.err.println("Parsing schema info: " + exception.toString()); 426 } 427 } 428 429 public void warning(org.xml.sax.SAXParseException exception) { 430 if (this.env.isVerbose()) { 431 System.err.println("Parsing schema warning " + exception.toString()); 432 } 433 } 434 435 } 436 | Popular Tags |