All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.ow2.easywsdl.extensions.wsdl4complexwsdl.impl.DescriptionImpl Maven / Gradle / Ivy

The newest version!
/**
 * easyWSDL - easyWSDL toolbox Platform.
 * Copyright (c) 2008,  eBM Websourcing
 * All rights reserved.
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *     * Redistributions of source code must retain the above copyright
 *       notice, this list of conditions and the following disclaimer.
 *     * Redistributions in binary form must reproduce the above copyright
 *       notice, this list of conditions and the following disclaimer in the
 *       documentation and/or other materials provided with the distribution.
 *     * Neither the name of the University of California, Berkeley nor the
 *       names of its contributors may be used to endorse or promote products
 *       derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 */

package org.ow2.easywsdl.extensions.wsdl4complexwsdl.impl;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.ow2.easywsdl.extensions.wsdl4complexwsdl.api.Description;
import org.ow2.easywsdl.extensions.wsdl4complexwsdl.api.Document;
import org.ow2.easywsdl.extensions.wsdl4complexwsdl.api.ImportedDocuments;
import org.ow2.easywsdl.extensions.wsdl4complexwsdl.api.WSDL4ComplexWsdlElement;
import org.ow2.easywsdl.extensions.wsdl4complexwsdl.api.WSDL4ComplexWsdlException;
import org.ow2.easywsdl.schema.SchemaFactory;
import org.ow2.easywsdl.schema.api.Schema;
import org.ow2.easywsdl.schema.api.SchemaException;
import org.ow2.easywsdl.wsdl.WSDLFactory;
import org.ow2.easywsdl.wsdl.api.Binding;
import org.ow2.easywsdl.wsdl.api.Endpoint;
import org.ow2.easywsdl.wsdl.api.Import;
import org.ow2.easywsdl.wsdl.api.Include;
import org.ow2.easywsdl.wsdl.api.InterfaceType;
import org.ow2.easywsdl.wsdl.api.Service;
import org.ow2.easywsdl.wsdl.api.Types;
import org.ow2.easywsdl.wsdl.api.WSDLException;
import org.ow2.easywsdl.wsdl.api.abstractItf.AbsItfDescription;
import org.ow2.easywsdl.wsdl.decorator.DecoratorDescriptionImpl;

/**
 * @author Nicolas Salatge - eBM WebSourcing
 */
public class DescriptionImpl extends DecoratorDescriptionImpl implements Description {

	/**
	 *
	 */
	private static final long serialVersionUID = 1L;

	private WSDL4ComplexWsdlElement elmt = null;

	@SuppressWarnings("unchecked")
	public DescriptionImpl(final AbsItfDescription wsdl) throws WSDLException {
		super(wsdl, null);

		AbsItfDescription firstWsdl = wsdl;
		if (wsdl instanceof DecoratorDescriptionImpl) {
			firstWsdl = ((DecoratorDescriptionImpl) wsdl).getFirstDescription();
		}

		if (firstWsdl instanceof org.ow2.easywsdl.wsdl.api.Description) {
			this.elmt = new WSDL4ComplexWsdlElementImpl(firstWsdl);
		} else {
			throw new WSDL4ComplexWsdlException("Impossible to find the correct first description");
		}
	}

	public ImportedDocuments getImportedDocuments() {
		return this.elmt.getImportedDocuments();
	}

	public void setImportedDocuments(ImportedDocuments docs) throws WSDL4ComplexWsdlException {
		this.elmt.setImportedDocuments(docs);
	}

	public void addImportedDocumentsInWsdl() throws WSDL4ComplexWsdlException {
		ImportedDocuments imptDocs = new ImportedDocumentsImpl(new org.ow2.easywsdl.extensions.complexwsdl.ImportedDocuments(), this);
		Map documents = getAllWsdlDocuments(this, imptDocs);
		imptDocs.addAllDocuments(documents.values());
		this.elmt.setImportedDocuments(imptDocs);
	}

	private Map getAllWsdlDocuments(AbsItfDescription description, ImportedDocuments imptDocs) throws WSDL4ComplexWsdlException {
		Map res = new HashMap();

		List wsdlImports = description.getImports();
		List wsdlIncludes = description.getIncludes();
		List schemaImports = null;
		if (description.getTypes() != null) {
			schemaImports = description.getTypes().getImportedSchemas();
		}
		Map map = description.getSchemaLocation();

		// Insert all wsdl imports in document
		for (Import imptWsdl : wsdlImports) {
			String internalLocation = this.createInternalLocation(imptWsdl.getLocationURI());
			if (!res.containsKey(internalLocation)) {
				if (imptWsdl.getDescription() != null) {					
					Document doc = new DocumentImpl(new org.ow2.easywsdl.extensions.complexwsdl.Document(), imptDocs);
					doc.setOriginalName(imptWsdl.getLocationURI());
					doc.setName(internalLocation);
					doc.setImportedDescription(imptWsdl.getDescription());
					res.put(internalLocation, doc);
					// change location
					imptWsdl.setLocationURI(internalLocation);

					// add recursive import
					res.putAll(getAllWsdlDocuments(imptWsdl.getDescription(), imptDocs));
				}
			} else {
				// change location
				imptWsdl.setLocationURI(internalLocation);
			}

		}

		// Insert all wsdl includes in document
		for (Include inclWsdl : wsdlIncludes) {
			String internalLocation = this.createInternalLocation(inclWsdl.getLocationURI());
			if (!res.containsKey(internalLocation)) {
				if (inclWsdl.getDescription() != null) {
					Document doc = new DocumentImpl(new org.ow2.easywsdl.extensions.complexwsdl.Document(), imptDocs);
					doc.setOriginalName(inclWsdl.getLocationURI());
					doc.setName(internalLocation);
					doc.setImportedDescription(inclWsdl.getDescription());
					res.put(internalLocation, doc);

					// change location
					inclWsdl.setLocationURI(internalLocation);

					// add recursive includes
					res.putAll(getAllWsdlDocuments(inclWsdl.getDescription(), imptDocs));
				}
			} else {
				// change location
				inclWsdl.setLocationURI(internalLocation);
			}
		}

		// Insert all schema imports in document
		if (description.getTypes() != null) {
			if (schemaImports != null) {
				for (org.ow2.easywsdl.schema.api.Import imptSchema : schemaImports) {
					String internalLocation = this.createInternalLocation(imptSchema.getLocationURI());
					if (!res.containsKey(internalLocation)) {
						if (imptSchema.getSchema() != null) {
							Document doc = new DocumentImpl(new org.ow2.easywsdl.extensions.complexwsdl.Document(), imptDocs);
							doc.setOriginalName(imptSchema.getLocationURI());
							doc.setName(internalLocation);
							doc.setImportedSchema(imptSchema.getSchema());
							res.put(internalLocation, doc);

							// change location
							imptSchema.setLocationURI(internalLocation);

							// add recursive schema imports
							res.putAll(getAllSchemaDocuments(imptSchema.getSchema(), imptDocs));
						}
					} else {
						// change location
						imptSchema.setLocationURI(internalLocation);
					}

				}
			}


			for (Schema schema : (List) description.getTypes().getSchemas()) {
				// add recursive schema imports
				res.putAll(getAllSchemaDocuments(schema, imptDocs));
			}
		}
		return res;
	}

	private Map getAllSchemaDocuments(Schema schema, ImportedDocuments imptDocs) throws WSDL4ComplexWsdlException {
		Map res = new HashMap();

		for (org.ow2.easywsdl.schema.api.Import imptSchema : schema.getImports()) {
			String internalLocation = this.createInternalLocation(imptSchema.getLocationURI());
			if (!res.containsKey(internalLocation)) {
				if (imptSchema.getSchema() != null) {
					Document doc = new DocumentImpl(new org.ow2.easywsdl.extensions.complexwsdl.Document(), imptDocs);

					doc.setName(internalLocation);
					doc.setImportedSchema(imptSchema.getSchema());
					res.put(internalLocation, doc);

					// change location
					imptSchema.setLocationURI(internalLocation);

					// add recursive schema imports
					res.putAll(getAllSchemaDocuments(imptSchema.getSchema(), imptDocs));
				}
			} else {
				// change location
				imptSchema.setLocationURI(internalLocation);
			}

		}

		for (org.ow2.easywsdl.schema.api.Include inclSchema : schema.getIncludes()) {
			String internalLocation = this.createInternalLocation(inclSchema.getLocationURI());
			if (!res.containsKey(internalLocation)) {
				if (inclSchema.getSchema() != null) {
					Document doc = new DocumentImpl(new org.ow2.easywsdl.extensions.complexwsdl.Document(), imptDocs);

					doc.setName(internalLocation);
					doc.setImportedSchema(inclSchema.getSchema());
					res.put(internalLocation, doc);

					// change location
					inclSchema.setLocationURI(internalLocation);

					// add recursive schema imports
					res.putAll(getAllSchemaDocuments(inclSchema.getSchema(), imptDocs));
				}
			} else {
				// change location
				inclSchema.setLocationURI(internalLocation);
			}
		}

		return res;
	}

	private String createInternalLocation(String currentLocation) throws WSDL4ComplexWsdlException {
		String internalLocation = null;
		try {
			if ((currentLocation != null) && (!currentLocation.startsWith("xpath://description/importedDocuments/document/"))) {
				String fileName = null;
				if ((currentLocation.endsWith(".xsd")) || (currentLocation.endsWith(".wsdl"))) {
					URI uri = new URI(currentLocation);
					fileName = uri.getPath();
					//					if (currentLocation.indexOf("/") > 0) {
					//						fileName = currentLocation.substring(currentLocation.indexOf("/"));
					//					} else {
					//						fileName = currentLocation;
					//					}
				} else {
					fileName = "unnamedDocument" + this.getImportedDocuments().getDocuments().size();
				}
				internalLocation = "xpath://description/importedDocuments/document/" + fileName;
			} else {
				internalLocation = currentLocation;
			}
		} catch (URISyntaxException e) {
			throw new WSDL4ComplexWsdlException(e);
		}
		return internalLocation;
	}

	/*
	 * (non-Javadoc)
	 * @see org.ow2.easywsdl.extensions.wsdl4complexwsdl.api.Description#deleteImportedDocumentsInWsdl(java.lang.String)
	 */
	public Map deleteImportedDocumentsInWsdl(String externalPath) throws WSDL4ComplexWsdlException {
		Map imports = new HashMap();
		try {
			// The following has to be done only when the importedDocuments is not null ;-)
			if (this.getImportedDocuments() != null) {
				// get All imports
				String externalLocation = null;
				org.w3c.dom.Document document = null;

				// get All import and include schema
				List schemaImportList = new ArrayList();

				if(this.getTypes() != null) {
					schemaImportList.addAll(this.getTypes().getImportedSchemas());
					for(Schema schema: this.getTypes().getSchemas()) {
						schemaImportList.addAll(schema.getImports());
					}
				}
				for (Document doc : this.getImportedDocuments().getDocuments()) {
					if (externalPath != null) {
						externalLocation = this.createExternalLocation(externalPath, doc.getName());
					} else if (doc.getOriginalName() != null) {
						externalLocation = doc.getOriginalName();
					}
					if (externalLocation != null) {
						List descriptionImportList = this.findImportWithLocation(this.getImports(), doc.getName());
						for (Import descriptionImport : descriptionImportList) {
							descriptionImport.setLocationURI(externalLocation);
						}
						List descriptionIncludeList = this.findIncludeWithLocation(this.getIncludes(), doc.getName());
						for (Include descriptionInclude : descriptionIncludeList) {
							descriptionInclude.setLocationURI(externalLocation);
						}
						for (org.ow2.easywsdl.schema.api.absItf.AbsItfInclude schemaImport : schemaImportList) {
							if(schemaImport.getLocationURI().equals(doc.getName())) {
								schemaImport.setLocationURI(externalLocation);
							}
						}
						if (doc.getImportedDescription() != null) {
							changeImportedLocationInWsdl(doc.getImportedDescription(), externalPath);
							document = WSDLFactory.newInstance().newWSDLWriter().getDocument(doc.getImportedDescription());
						} else if (doc.getImportedSchema() != null) {
							changeImportedLocationInSchema(doc.getImportedSchema(), externalPath);
							document = SchemaFactory.newInstance().newSchemaWriter().getDocument(doc.getImportedSchema());
						}
						imports.put(new URI(externalLocation), document);
					}
				}
				// remove imported documents
				this.setImportedDocuments(null);
			}
		} catch (URISyntaxException e) {
			throw new WSDL4ComplexWsdlException(e);
		} catch (WSDLException e) {
			throw new WSDL4ComplexWsdlException(e);
		} catch (SchemaException e) {
			throw new WSDL4ComplexWsdlException(e);
		}
		return imports;
	}



	private List findIncludeWithLocation(List includes,
			String name) {
		List res = new ArrayList();
		if(includes != null) {
			for(Include incl: includes) {
				if(incl.getLocationURI().equals(name)) {
					res.add(incl);
				}
			}
		}
		return res;
	}

	private List findImportWithLocation(List imports,
			String name) {
		List res = new ArrayList();
		if(imports != null) {
			for(Import impt: imports) {
				if(impt.getLocationURI().equals(name)) {
					res.add(impt);
				}
			}
		}
		return res;
	}

	private void changeImportedLocationInWsdl(AbsItfDescription description, String externalPath) {

		// TODO: change schemaLocation import

		List wsdlImports = description.getImports();
		List wsdlIncludes = description.getIncludes();
		List schemaImports = null;
		if (description.getTypes() != null) {
			schemaImports = description.getTypes().getImportedSchemas();
		}

		// change location in wsdl import
		for (Import impt : wsdlImports) {
			impt.setLocationURI(this.createExternalLocation(externalPath, impt.getLocationURI()));
		}

		// change location in wsdl include
		for (Include incl : wsdlIncludes) {
			incl.setLocationURI(this.createExternalLocation(externalPath, incl.getLocationURI()));
		}

		// change location in schema import
		if (schemaImports != null) {
			for (org.ow2.easywsdl.schema.api.Import impt : schemaImports) {
				impt.setLocationURI(this.createExternalLocation(externalPath, impt.getLocationURI()));
			}
		}

		// change location all schema imports in document
		if (description.getTypes() != null) {
			for (Schema schema : (List) description.getTypes().getSchemas()) {
				this.changeImportedLocationInSchema(schema, externalPath);
			}
		}
	}

	private void changeImportedLocationInSchema(Schema schema, String externalPath) {

		// change location in schema import
		for (org.ow2.easywsdl.schema.api.Import impt : schema.getImports()) {
			impt.setLocationURI(this.createExternalLocation(externalPath, impt.getLocationURI()));
		}

		// change location in schema include
		for (org.ow2.easywsdl.schema.api.Include incl : schema.getIncludes()) {
			incl.setLocationURI(this.createExternalLocation(externalPath, incl.getLocationURI()));
		}
	}

	private String createExternalLocation(String externalPath, String currentLocation) {
		String externalLocation = null;
		if (externalPath != null && currentLocation.startsWith("xpath://description/importedDocuments/document/")) {
			externalLocation = currentLocation.replace("xpath://description/importedDocuments/document/", externalPath);
		} else {
			externalLocation = currentLocation;
		}
		return externalLocation;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy