|
Apache CXF example source code file (IDLToWSDLProcessor.java)
The Apache CXF IDLToWSDLProcessor.java source code/** * Licensed to the Apache Software Foundation (ASF) under one * or more contributor license agreements. See the NOTICE file * distributed with this work for additional information * regarding copyright ownership. The ASF licenses this file * to you under the Apache License, Version 2.0 (the * "License"); you may not use this file except in compliance * with the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, * software distributed under the License is distributed on an * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * KIND, either express or implied. See the License for the * specific language governing permissions and limitations * under the License. */ package org.apache.cxf.tools.corba.processors.idl; import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.Writer; import java.net.URI; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Set; import java.util.StringTokenizer; import javax.wsdl.Binding; import javax.wsdl.Definition; import javax.wsdl.Port; import javax.wsdl.Service; import javax.wsdl.Types; import javax.wsdl.extensions.schema.Schema; import javax.wsdl.extensions.schema.SchemaImport; import javax.xml.namespace.QName; import antlr.collections.AST; import org.apache.cxf.binding.corba.wsdl.AddressType; import org.apache.cxf.binding.corba.wsdl.CorbaConstants; import org.apache.cxf.binding.corba.wsdl.CorbaTypeImpl; import org.apache.cxf.binding.corba.wsdl.TypeMappingType; import org.apache.cxf.helpers.CastUtils; import org.apache.cxf.tools.common.ToolConstants; import org.apache.cxf.tools.common.ToolException; import org.apache.cxf.tools.corba.common.ReferenceConstants; import org.apache.cxf.tools.corba.common.ToolCorbaConstants; import org.apache.cxf.tools.util.FileWriterUtil; import org.apache.ws.commons.schema.XmlSchema; import org.apache.ws.commons.schema.constants.Constants; public class IDLToWSDLProcessor extends IDLProcessor { private String idl; private String schemaFilename; private String importSchemaFilename; private String logical; private String physical; private String outputDir; private Writer outputWriter; private Writer schemaOutputWriter; private Writer logicalOutputWriter; private Writer physicalOutputWriter; private Map<String, File> importDefnWriters; private Map<String, File> importSchemaWriters; private boolean ignoreImports; public void process() throws ToolException { super.process(); process(getIDLTree()); } public void process(AST idlTree) throws ToolException { idl = getBaseFilename(env.get(ToolCorbaConstants.CFG_IDLFILE).toString()); checkFileOptions(); try { parseIDL(idlTree); } catch (Exception e) { throw new ToolException(e); } } public void setOutputWriter(Writer writer) { outputWriter = writer; } public void setSchemaOutputWriter(Writer writer) { schemaOutputWriter = writer; } public void setLogicalOutputWriter(Writer writer) { logicalOutputWriter = writer; } public void setPhysicalOutputWriter(Writer writer) { physicalOutputWriter = writer; } /** * Used only for test cases to set writers for imports when using * the -mns option */ protected void setImportDefinitionWriters(Map<String, File> writers) { importDefnWriters = writers; } protected void setImportSchemaWriters(Map<String, File> writers) { importSchemaWriters = writers; } protected void setIgnoreImports(boolean flag) { ignoreImports = flag; } private void checkFileOptions() { if (env.optionSet(ToolCorbaConstants.CFG_LOGICAL)) { // set the logical filename logical = env.get(ToolCorbaConstants.CFG_LOGICAL).toString(); } if (env.optionSet(ToolCorbaConstants.CFG_PHYSICAL)) { // set the physical file name physical = env.get(ToolCorbaConstants.CFG_PHYSICAL).toString(); } if (env.optionSet(ToolCorbaConstants.CFG_SCHEMA)) { // deal with writing schema types to the schema specified file schemaFilename = env.get(ToolCorbaConstants.CFG_SCHEMA).toString(); } if (env.optionSet(ToolCorbaConstants.CFG_IMPORTSCHEMA)) { // deal with importing schema types importSchemaFilename = env.get(ToolCorbaConstants.CFG_IMPORTSCHEMA).toString(); } } public void parseIDL(AST idlTree) throws Exception { if (env.isVerbose()) { System.out.println(idlTree.toStringTree()); } // target namespace String tns = (String) env.get(ToolCorbaConstants.CFG_TNS); if (tns == null) { tns = CorbaConstants.WSDL_NS_URI + idl; } // XmlSchema namespace String schemans = (String) env.get(ToolCorbaConstants.CFG_SCHEMA_NAMESPACE); // corba typemap namespace String corbatypemaptns = (String) env.get(ToolCorbaConstants.CFG_CORBATYPEMAP_NAMESPACE); outputDir = "."; try { WSDLASTVisitor visitor = new WSDLASTVisitor(tns, schemans, corbatypemaptns); visitor.getManager().setIgnoreImports(ignoreImports); if (env.optionSet(ToolConstants.CFG_OUTPUTDIR)) { outputDir = (String) env.get(ToolConstants.CFG_OUTPUTDIR); } visitor.setOutputDir(outputDir); Definition def = visitor.getDefinition(); if (env.optionSet(ToolCorbaConstants.CFG_SEQUENCE_OCTET_TYPE)) { visitor.setSequenceOctetType((String) env.get(ToolCorbaConstants.CFG_SEQUENCE_OCTET_TYPE)); } if (env.optionSet(ToolCorbaConstants.CFG_SCHEMA_NAMESPACE)) { //visitor.getDefinition() def.addNamespace(ToolCorbaConstants.CFG_SCHEMA_NAMESPACE_PREFIX, (String) env.get(ToolCorbaConstants.CFG_SCHEMA_NAMESPACE)); } if (env.optionSet(ToolCorbaConstants.CFG_BOUNDEDSTRINGS)) { visitor.setBoundedStringOverride(true); } if (env.optionSet(ToolCorbaConstants.CFG_MODULETONS)) { String mapping = (String) env.get(ToolCorbaConstants.CFG_MODULETONS); //parse the mapping & set a map of module to namespace mapping in the visitor visitor.setModuleToNSMapping(getModuleToNSMapping(mapping)); } if (env.optionSet(ToolCorbaConstants.CFG_QUALIFIED)) { visitor.setQualified(true); } if (env.optionSet(ToolCorbaConstants.CFG_POLYMORPHIC_FACTORIES)) { visitor.setSupportPolymorphicFactories(true); } if (env.optionSet(ToolCorbaConstants.CFG_SCHEMA)) { visitor.setSchemaGenerated(true); // generate default namespace for schema if -T is used alone. if (env.get(ToolCorbaConstants.CFG_SCHEMA_NAMESPACE) == null) { visitor.updateSchemaNamespace(def.getTargetNamespace() + "-types"); def.addNamespace(ToolCorbaConstants.CFG_SCHEMA_NAMESPACE_PREFIX, def.getTargetNamespace() + "-types"); } } if (env.optionSet(ToolCorbaConstants.CFG_EXCLUDEMODULES)) { String modules = (String) env.get(ToolCorbaConstants.CFG_EXCLUDEMODULES); //parse the mapping & set a map of module to namespace mapping in the visitor visitor.setExcludedModules(getExcludedModules(modules)); } visitor.visit(idlTree); cleanUpTypeMap(visitor.getTypeMap()); Binding[] bindings = visitor.getCorbaBindings(); generateCORBAService(def, bindings, visitor.getModuleToNSMapper().isDefaultMapping()); writeDefinitions(visitor); } catch (Exception ex) { throw new ToolException(ex.getMessage(), ex); } } // Sets the output directory and the generated filenames. // Output directory is specified // - File names have no path specified // - File names do have specified so they take precedence. // Output directory is not specified // - File names have no path specified so use current directory. // - File names have full path specified. private void writeDefinitions(WSDLASTVisitor visitor) throws Exception { if (env.optionSet(ToolCorbaConstants.CFG_LOGICAL) || env.optionSet(ToolCorbaConstants.CFG_PHYSICAL) || env.optionSet(ToolCorbaConstants.CFG_SCHEMA) || env.optionSet(ToolCorbaConstants.CFG_IMPORTSCHEMA)) { if (logical == null || physical == null) { if (outputWriter == null) { outputWriter = getOutputWriter(idl + ".wsdl", outputDir); } String separator = System.getProperty("file.separator"); File file = null; if (env.get(ToolConstants.CFG_OUTPUTDIR) != null) { file = new File(outputDir + separator + idl + ".wsdl"); } else { file = new File(idl + ".wsdl"); } visitor.setIdlFile(file.getAbsolutePath()); } if (logical != null) { logical = getFilePath(logical).getAbsolutePath(); if (logicalOutputWriter == null) { logicalOutputWriter = createOutputWriter(logical); } } if (physical != null) { physical = getFilePath(physical).getAbsolutePath(); if (physicalOutputWriter == null) { physicalOutputWriter = createOutputWriter(physical); } } if (schemaFilename != null) { schemaFilename = getFilePath(schemaFilename).getAbsolutePath(); if (schemaOutputWriter == null) { schemaOutputWriter = createOutputWriter(schemaFilename); } } if (importSchemaFilename != null) { importSchemaFilename = getImportFile(importSchemaFilename); visitor.setImportSchema(importSchemaFilename); } visitor.writeDefinitions(outputWriter, schemaOutputWriter, logicalOutputWriter, physicalOutputWriter, schemaFilename, logical, physical); } else { if (outputWriter == null) { String outputFile = idl + ".wsdl"; if (env.optionSet(ToolCorbaConstants.CFG_WSDLOUTPUTFILE)) { outputFile = (String) env.get(ToolCorbaConstants.CFG_WSDLOUTPUTFILE); if (!outputFile.endsWith(".wsdl")) { outputFile = outputFile + ".wsdl"; } } outputWriter = getOutputWriter(outputFile, outputDir); } Definition defn = visitor.getDefinition(); if (!visitor.getModuleToNSMapper().isDefaultMapping()) { addTypeMapSchemaImports(defn, visitor); visitor.getManager().attachDeferredSchemasToWSDL(); } visitor.writeDefinition(defn, outputWriter); writeImportedDefinitionsAndSchemas(visitor); } } private void writeImportedDefinitionsAndSchemas(WSDLASTVisitor visitor) throws Exception { Map<File, Definition> defns = visitor.getManager().getImportedWSDLDefinitions(); Map<File, XmlSchema> schemas = visitor.getManager().getImportedXmlSchemas(); if (importDefnWriters != null) { assert importDefnWriters.size() == defns.size(); } if (importSchemaWriters != null) { assert importSchemaWriters.size() == schemas.size(); } for (java.util.Iterator<File> it = defns.keySet().iterator(); it.hasNext();) { File file = it.next(); Definition defn = defns.get(file); Writer writer = null; if (importDefnWriters != null) { writer = getOutputWriter(importDefnWriters.get(defn.getTargetNamespace())); } if (writer == null) { writer = getOutputWriter(file); } visitor.writeDefinition(defn, writer); writer.close(); } for (java.util.Iterator<File> it = schemas.keySet().iterator(); it.hasNext();) { File file = it.next(); XmlSchema schema = schemas.get(file); Writer writer = null; if (importSchemaWriters != null) { writer = getOutputWriter(importSchemaWriters.get(schema.getTargetNamespace())); } if (writer == null) { writer = getOutputWriter(file); } visitor.writeSchema(schema, writer); writer.close(); } } // Get the imported schema file. private String getImportFile(String importFilename) { // check that file exists File file = new File(importFilename); if (!file.exists()) { if (!file.isAbsolute()) { String separator = System.getProperty("file.separator"); String userdir = System.getProperty("user.dir"); file = new File(userdir + separator + importFilename); } if (!file.exists()) { String msg = importFilename + " File not found"; FileNotFoundException ex = new FileNotFoundException(msg); System.err.println("IDLToWsdl Error : " + ex.getMessage()); System.err.println(); ex.printStackTrace(); System.exit(1); } else { URI url = file.toURI(); return url.toString(); } } else { URI url = file.toURI(); return url.toString(); } return null; } private Writer createOutputWriter(String name) throws Exception { String outDir = outputDir; String filename = name; int index = name.lastIndexOf(System.getProperty("file.separator")); outDir = name.substring(0, index); filename = name.substring(index + 1, name.length()); return getOutputWriter(filename, outDir); } // Gets the fully qualified path of a file. private File getFilePath(String ifile) { String separator = System.getProperty("file.separator"); StringTokenizer token = new StringTokenizer(ifile, separator); if (token.countTokens() == 1) { if (env.get(ToolConstants.CFG_OUTPUTDIR) != null) { return new File(outputDir + separator + ifile); } else { return new File(ifile); } } else { return new File(ifile); } } public Writer getOutputWriter(String filename, String outputDirectory) throws Exception { if (env.optionSet(ToolCorbaConstants.CFG_WSDL_ENCODING)) { String encoding = env.get(ToolCorbaConstants.CFG_WSDL_ENCODING).toString(); return FileWriterUtil.getWriter(new File(outputDirectory, filename), encoding); } else { FileWriterUtil fw = new FileWriterUtil(outputDirectory); return fw.getWriter("", filename); } } public Writer getOutputWriter(File file) throws Exception { if (env.optionSet(ToolCorbaConstants.CFG_WSDL_ENCODING)) { String encoding = env.get(ToolCorbaConstants.CFG_WSDL_ENCODING).toString(); return FileWriterUtil.getWriter(file, encoding); } else { return FileWriterUtil.getWriter(file); } } public String getBaseFilename(String ifile) { String fileName = ifile; StringTokenizer token = new StringTokenizer(ifile, "\\/"); while (token.hasMoreTokens()) { fileName = token.nextToken(); } if (fileName.endsWith(".idl")) { fileName = new String(fileName.substring(0, fileName.length() - 4)); } return fileName; } private Map<String, String> getServiceNames(Binding[] bindings, boolean isDefaultMapping) { Map<String, String> serviceNames = new HashMap Other Apache CXF examples (source code examples)Here is a short list of links related to this Apache CXF IDLToWSDLProcessor.java source code file: |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.