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

org.ow2.easywsdl.wsdl.api.abstractElmt.AbstractIncludeImpl 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.wsdl.api.abstractElmt;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import java.net.URI;
import java.util.Map;
import java.util.logging.Logger;

import javax.xml.transform.dom.DOMSource;

import org.ow2.easywsdl.schema.util.SourceHelper;
import org.ow2.easywsdl.schema.util.Util;
import org.ow2.easywsdl.wsdl.api.WSDLException;
import org.ow2.easywsdl.wsdl.api.WSDLImportException;
import org.ow2.easywsdl.wsdl.api.WSDLReader;
import org.ow2.easywsdl.wsdl.api.WSDLReader.FeatureConstants;
import org.ow2.easywsdl.wsdl.api.abstractItf.AbsItfDescription;
import org.ow2.easywsdl.wsdl.api.abstractItf.AbsItfInclude;
import org.xml.sax.InputSource;

/**
 * @author Nicolas Salatge - eBM WebSourcing
 */
@SuppressWarnings("unchecked")
public abstract class AbstractIncludeImpl
		extends AbstractWSDLElementImpl implements AbsItfInclude {

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

	private static Logger log = Logger.getLogger(AbstractIncludeImpl.class
			.getName());

	private static final int maxImportDepth = 6;

	/**
	 * the desc
	 */
	protected D desc;

	/**
	 * the parent description
	 */
	protected D parent;

	/**
	 * Default constructor
	 * 
	 * @param model
	 *            the model
	 * @param parent
	 *            the parent description
	 * @throws WSDLException
	 * @throws WSDLImportException
	 */
	public AbstractIncludeImpl(final E model, final D parent,
			Map imports, int importDepth) throws WSDLException,
			WSDLImportException {
		super(model, (AbstractWSDLElementImpl) parent);
		this.parent = parent;

		String location = this.getLocationURI();

		if ((!Util.isURL(location)) && (parent.getDocumentBaseURI() != null)) {
			location = parent.getDocumentBaseURI().getPath() + location;
		}

		if (this.parent != null) {
			if (((AbstractDescriptionImpl) this.parent).getFeatures() != null) {
				if ((((AbstractDescriptionImpl) this.parent).getFeatures().get(
						FeatureConstants.IMPORT_DOCUMENTS) != null)
						&& ((Boolean) ((AbstractDescriptionImpl) this.parent)
								.getFeatures().get(
										FeatureConstants.IMPORT_DOCUMENTS))) {
					this.retrieveInclude(location, imports, importDepth);
				}
			}
		}

		if ((this.parent != null) && (this.desc != null)) {
			((AbstractDescriptionImpl) this.desc)
					.setFeatures(((AbstractDescriptionImpl) this.parent)
							.getFeatures());
		}
	}

	private void retrieveInclude(final String location,
			Map imports, int importDepth) throws WSDLException,
			WSDLImportException {

		// Try to identify a cyclic import loop
		if (importDepth > maxImportDepth) {
			throw new WSDLImportException("Maximum import depth excedeed. "
					+ "This probably mean there's a cyclic import");
		} else {
			importDepth = importDepth + 1;
		}

		if (location != null) {

			InputStream is = null;

			// Create WSDL Reader
			WSDLReader reader = null;
			final AbsItfDescription absParent = this.parent;
			if (absParent instanceof org.ow2.easywsdl.wsdl.impl.wsdl11.DescriptionImpl) {
				reader = new org.ow2.easywsdl.wsdl.impl.wsdl11.WSDLReaderImpl();
			} else if (absParent instanceof org.ow2.easywsdl.wsdl.impl.wsdl20.DescriptionImpl) {
				reader = new org.ow2.easywsdl.wsdl.impl.wsdl20.WSDLReaderImpl();
			}
			try {
				// Retrieve Source
				final URI loc = new URI(location);
				DOMSource source = null;
				if (imports != null && imports.containsKey(loc)) {
					// Search in imports Map
					source = imports.get(loc);
				} else {
					// Try to download it
					final File f = new File(location);
					if (f.exists()) {
						is = new FileInputStream(new File(location));
					} else {
						is = loc.toURL().openStream();
					}
					source = SourceHelper
							.convertInputSource2DOMSource(new InputSource(is));
				}

				// Read Source
				if (reader != null) {
					((AbstractWSDLReaderImpl) reader)
							.setFeatures(((AbstractDescriptionImpl) this.parent)
									.getFeatures());
					final AbsItfDescription absDesc = (AbsItfDescription) reader
							.readWSDL(loc, source, imports, importDepth);

					this.desc = (D) absDesc;
				}

			} catch (final Exception e) {
				// do nothing
				// the document is not included in imported document
				throw new WSDLImportException(
						"the imported document cannot be imported at: "
								+ location, e);
			}
		}
	}

	/**
	 * @return the desc
	 */
	public D getDescription() {
		return this.desc;
	}

	public void setDescription(final D d) {
		this.desc = d;
	}

	/**
	 * @return the parent desc
	 */
	public D getParentDescription() {
		return this.parent;
	}

	public void setParentDescription(final D p) {
		this.parent = p;
	}

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy