001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.axis2.wsdl.codegen.writer;
020:
021: import org.apache.axis2.description.AxisService;
022: import org.apache.axis2.util.FileWriter;
023: import org.w3c.dom.Element;
024: import org.w3c.dom.NamedNodeMap;
025: import org.w3c.dom.Node;
026: import org.w3c.dom.NodeList;
027:
028: import javax.wsdl.Definition;
029: import javax.wsdl.Import;
030: import javax.wsdl.Types;
031: import javax.wsdl.Service;
032: import javax.wsdl.extensions.schema.Schema;
033: import javax.wsdl.factory.WSDLFactory;
034: import javax.wsdl.xml.WSDLWriter;
035: import java.io.File;
036: import java.io.FileOutputStream;
037: import java.util.Iterator;
038: import java.util.List;
039: import java.util.Map;
040: import java.util.Vector;
041: import java.util.ArrayList;
042: import java.util.Stack;
043:
044: public class WSDL11Writer {
045:
046: public static final String IMPORT_TAG = "import";
047: public static final String INCLUDE_TAG = "include";
048: public static final String SCHEMA_LOCATION = "schemaLocation";
049:
050: private File baseFolder = null;
051: private int count;
052:
053: public WSDL11Writer(File baseFolder) {
054: this .baseFolder = baseFolder;
055: this .count = 0;
056: }
057:
058: public void writeWSDL(AxisService axisService) {
059: try {
060: if (axisService != null) {
061: //create a output file
062: File outputFile = FileWriter.createClassFile(
063: baseFolder, null, axisService.getName(),
064: ".wsdl");
065: FileOutputStream fos = new FileOutputStream(outputFile);
066: axisService.printWSDL(fos);
067: fos.flush();
068: fos.close();
069: }
070: } catch (Exception e) {
071: throw new RuntimeException("WSDL writing failed!", e);
072: }
073: }
074:
075: public void writeWSDL(AxisService axisService,
076: Definition definition, Map changedMap) {
077: try {
078: if (axisService != null) {
079: writeWSDL(definition, axisService.getName(),
080: changedMap, new Stack());
081: }
082: } catch (Exception e) {
083: throw new RuntimeException("WSDL writing failed!", e);
084: }
085: }
086:
087: private void writeWSDL(Definition definition, String serviceName,
088: Map changedMap, Stack stack) throws Exception {
089: stack.push(definition);
090: // first process the imports and save them.
091: Map imports = definition.getImports();
092: if (imports != null && (imports.size() > 0)) {
093: Vector importsVector = null;
094: Import wsdlImport = null;
095: String wsdlName = null;
096: for (Iterator improtsVectorIter = imports.values()
097: .iterator(); improtsVectorIter.hasNext();) {
098: importsVector = (Vector) improtsVectorIter.next();
099: for (Iterator importsIter = importsVector.iterator(); importsIter
100: .hasNext();) {
101: wsdlImport = (Import) importsIter.next();
102: wsdlName = "wsdl_" + count++ + ".wsdl";
103: Definition innerDefinition = wsdlImport
104: .getDefinition();
105: if (!stack.contains(innerDefinition)) {
106: writeWSDL(innerDefinition, wsdlName,
107: changedMap, stack);
108: }
109: wsdlImport.setLocationURI(wsdlName);
110: }
111: }
112: }
113: // change the locations on the imported schemas
114: adjustWSDLSchemaLocations(definition, changedMap);
115: // finally save the file
116: WSDLWriter wsdlWriter = WSDLFactory.newInstance()
117: .newWSDLWriter();
118: File outputFile = FileWriter.createClassFile(baseFolder, null,
119: serviceName, ".wsdl");
120: FileOutputStream out = new FileOutputStream(outputFile);
121:
122: // we have a catch here
123: // if there are multimple services in the definition object
124: // we have to write only the relavent service.
125:
126: if (definition.getServices().size() > 1) {
127: List removedServices = new ArrayList();
128: List servicesList = new ArrayList();
129:
130: Map services = definition.getServices();
131: // populate the services list
132: for (Iterator iter = services.values().iterator(); iter
133: .hasNext();) {
134: servicesList.add(iter.next());
135: }
136: Service service;
137: for (Iterator iter = servicesList.iterator(); iter
138: .hasNext();) {
139: service = (Service) iter.next();
140: if (!service.getQName().getLocalPart().equals(
141: serviceName)) {
142: definition.removeService(service.getQName());
143: removedServices.add(service);
144: }
145: }
146:
147: //now we have only the required service so write it
148: wsdlWriter.writeWSDL(definition, out);
149:
150: // again add the removed services
151: for (Iterator iter = removedServices.iterator(); iter
152: .hasNext();) {
153: service = (Service) iter.next();
154: definition.addService(service);
155: }
156: } else {
157: // no problem proceed normaly
158: wsdlWriter.writeWSDL(definition, out);
159: }
160: out.flush();
161: out.close();
162: stack.pop();
163: }
164:
165: /**
166: * @deprecated please use adjustWSDLSchemaLocations
167: * @param definition
168: * @param changedSchemaLocations
169: */
170: public void adjustWSDLSchemaLocatins(Definition definition,
171: Map changedSchemaLocations) {
172: adjustWSDLSchemaLocations(definition, changedSchemaLocations);
173: }
174:
175: /**
176: * adjust the schema locations in the original wsdl
177: *
178: * @param definition
179: * @param changedSchemaLocations
180: */
181: public void adjustWSDLSchemaLocations(Definition definition,
182: Map changedSchemaLocations) {
183: Types wsdlTypes = definition.getTypes();
184: if (wsdlTypes != null) {
185: List extensibilityElements = wsdlTypes
186: .getExtensibilityElements();
187: Object currentObject;
188: Schema schema;
189: for (Iterator iter = extensibilityElements.iterator(); iter
190: .hasNext();) {
191: currentObject = iter.next();
192: if (currentObject instanceof Schema) {
193: schema = (Schema) currentObject;
194: changeLocations(schema.getElement(),
195: changedSchemaLocations);
196: }
197: }
198: }
199: }
200:
201: private void changeLocations(Element element,
202: Map changedScheamLocations) {
203: NodeList nodeList = element.getChildNodes();
204: String tagName;
205: for (int i = 0; i < nodeList.getLength(); i++) {
206: tagName = nodeList.item(i).getLocalName();
207: if (IMPORT_TAG.equals(tagName)
208: || INCLUDE_TAG.equals(tagName)) {
209: processImport(nodeList.item(i), changedScheamLocations);
210: }
211: }
212: }
213:
214: private void processImport(Node importNode,
215: Map changedScheamLocations) {
216: NamedNodeMap nodeMap = importNode.getAttributes();
217: Node attribute;
218: String attributeValue;
219: for (int i = 0; i < nodeMap.getLength(); i++) {
220: attribute = nodeMap.item(i);
221: if (attribute.getNodeName().equals("schemaLocation")) {
222: attributeValue = attribute.getNodeValue();
223: if (changedScheamLocations.get(attributeValue) != null) {
224: attribute
225: .setNodeValue((String) changedScheamLocations
226: .get(attributeValue));
227: }
228: }
229: }
230: }
231:
232: }
|