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

org.ow2.easywsdl.schema.api.abstractElmt.AbstractSchemaImpl Maven / Gradle / Ivy

There is a newer version: 2.8.0
Show newest version
/**
 * easySchema - 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.schema.api.abstractElmt;

import java.net.URI;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.logging.Logger;

import javax.xml.namespace.QName;

import org.ow2.easywsdl.schema.SchemaFactory;
import org.ow2.easywsdl.schema.api.SchemaException;
import org.ow2.easywsdl.schema.api.SchemaReader.FeatureConstants;
import org.ow2.easywsdl.schema.api.absItf.AbsItfAttribute;
import org.ow2.easywsdl.schema.api.absItf.AbsItfComplexType;
import org.ow2.easywsdl.schema.api.absItf.AbsItfElement;
import org.ow2.easywsdl.schema.api.absItf.AbsItfImport;
import org.ow2.easywsdl.schema.api.absItf.AbsItfInclude;
import org.ow2.easywsdl.schema.api.absItf.AbsItfSchema;
import org.ow2.easywsdl.schema.api.absItf.AbsItfType;
import org.ow2.easywsdl.schema.api.extensions.NamespaceMapperImpl;
import org.ow2.easywsdl.schema.api.extensions.SchemaLocatorImpl;

/**
 * @author Nicolas Salatge - eBM WebSourcing
 */
public abstract class AbstractSchemaImpl extends AbstractSchemaElementImpl implements AbsItfSchema {

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

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

	/**
	 * Features
	 */
	protected Map features = new HashMap();

	/**
	 * the namespace context
	 */
	protected NamespaceMapperImpl namespaceMapper = new org.ow2.easywsdl.schema.api.extensions.NamespaceMapperImpl();

	/**
	 * list of imports
	 */
	protected List imports = new ArrayList();

	/**
	 * list of includes
	 */
	protected List includes = new ArrayList();

	/**
	 * the list of types
	 */
	public List types = new ArrayList ();

	/**
	 * the list of elements
	 */
	public List elements = new ArrayList();

	/**
	 * the list of attributes
	 */
	public List attributes = new ArrayList();

	/**
	 * the baseUri string
	 */
	protected String documentBaseURIString;

	private URI documentURI = null;

	public AbstractSchemaImpl() {

	}

	/**
	 * @param parent
	 * @param doc
	 */
	public AbstractSchemaImpl(URI documentURI, final E schema, final NamespaceMapperImpl namespaceMapper, final SchemaLocatorImpl schemaLocator) {
		super(schema, null);
		this.namespaceMapper = namespaceMapper;
		this.documentURI = documentURI;
	}

	public AbstractSchemaImpl(final String baseURIString) {
		this.documentBaseURIString = baseURIString;
	}

	public void addImportElementsInAllList() {
		for (final Impt impt : this.imports) {
			if (impt.getSchema() != null) {
				this.types.addAll(impt.getSchema().getTypes());
				this.elements.addAll(impt.getSchema().getElements());
				this.attributes.addAll(impt.getSchema().getAttributes());
			}
		}
	}

	public void addIncludeElementsInAllList() {
		for (final Incl incl : this.includes) {
			if (incl.getSchema() != null) {
				this.types.addAll(incl.getSchema().getTypes());
				this.elements.addAll(incl.getSchema().getElements());
				this.attributes.addAll(incl.getSchema().getAttributes());
			}
		}
	}

	/**
	 * ImportImpl operation
	 */
	public void addImport(final Impt impt) {
		if (this.imports == null) {
			this.imports = new ArrayList();
		}
		this.imports.add(impt);
	}

	public List getImports() {
		if (this.imports == null) {
			this.imports = new ArrayList();
		}
		return this.imports;
	}

	public List getImports(final String namespaceUri) {
		final List res = new ArrayList();
		for (final Impt impt : this.imports) {
			if (impt.getNamespaceURI().equals(namespaceUri)) {
				res.add(impt);
			}
		}
		return res;
	}

	/**
	 * IncludeImpl operation
	 * 
	 * @throws WSDLException
	 */
	public void addInclude(final Incl incl) throws SchemaException {
		if (this.includes == null) {
			this.includes = new ArrayList();
		}
		this.includes.add(incl);
	}

	public List getIncludes() {
		if (this.includes == null) {
			this.includes = new ArrayList();
		}
		return this.includes;
	}

	public List getIncludes(final URI locationUri) {
		final List res = new ArrayList();
		for (final Incl incl : this.includes) {
			if (incl.getLocationURI().equals(locationUri)) {
				res.add(incl);
			}
		}
		return res;
	}

	/*
	 * Types methods
	 */

	/*
	 * (non-Javadoc)
	 * @see org.ow2.easywsdl.schema.api.Schema#getTypes()
	 */
	public List getTypes() {
		if (this.types == null) {
			this.types = new ArrayList();
		}
		return this.types;
	}

	/*
	 * (non-Javadoc)
	 * @see org.ow2.easywsdl.schema.api.Schema#getType(javax.xml.namespace .QName)
	 */
	public T getType(final QName type) {
		T res = null;
		if (this.types == null) {
			this.types = new ArrayList();
		}
		for (final T t : this.types) {
			if (t.getQName().equals(type)) {
				res = t;
				break;
			}
		}

		if ((res == null) && (SchemaFactory.getDefaultSchema() != null)) {
			for (final Object ob : SchemaFactory.getDefaultSchema().getTypes()) {
				final AbsItfType t = (AbsItfType) ob;
				if (t.getQName().equals(type)) {
					res = (T) t;
					break;
				}
			}
		}
		return res;
	}

	/*
	 * (non-Javadoc)
	 * @see org.ow2.easywsdl.schema.api.Schema#addType(com.ebmwebsourcing .commons.schema.api.Type)
	 */
	public void addType(final T type) {
		if (this.types == null) {
			this.types = new ArrayList();
		}
		this.types.add(type);
	}

	/*
	 * Attributes methods
	 */

	/*
	 * (non-Javadoc)
	 * @see org.ow2.easywsdl.schema.api.Schema#getAttributes()
	 */
	public List getAttributes() {
		if (this.attributes == null) {
			this.attributes = new ArrayList();
		}
		return this.attributes;
	}

	/*
	 * (non-Javadoc)
	 * @see org.ow2.easywsdl.schema.api.Schema#getAttribute(javax.xml.namespace .QName)
	 */
	public A getAttribute(final QName element) {
		A res = null;
		if (this.attributes == null) {
			this.attributes = new ArrayList();
		}
		for (final A e : this.attributes) {
			if ((e.getName().equals(element.getLocalPart())) && (e.getNamespaceUri().equals(element.getNamespaceURI()))) {
				res = e;
				break;
			}
		}
		return res;
	}

	/*
	 * (non-Javadoc)
	 * @see org.ow2.easywsdl.schema.api.Schema#addAttribute(com.ebmwebsourcing .commons.schema.api.Attribute)
	 */
	public void addAttribute(final A elmt) {
		if (this.attributes == null) {
			this.attributes = new ArrayList();
		}
		this.attributes.add(elmt);
	}

	/*
	 * Elements methods
	 */

	/*
	 * (non-Javadoc)
	 * @see org.ow2.easywsdl.schema.api.Schema#getElements()
	 */
	public List getElements() {
		if (this.elements == null) {
			this.elements = new ArrayList();
		}
		return this.elements;
	}

	/*
	 * (non-Javadoc)
	 * @see org.ow2.easywsdl.schema.api.Schema#getAttribute(javax.xml.namespace .QName)
	 */
	public El getElement(final QName element) {
		El res = null;
		if (this.elements == null) {
			this.elements = new ArrayList();
		}
		for (final El e : this.elements) {
			if (e.getQName().equals(element)) {
				res = e;
				break;
			}
		}
		return res;
	}
	
	public List findElementsInAllSchema(QName element) {
		List res = new ArrayList();
		
		El elmt = this.getElement(element);
		if(elmt != null) {
			res.add(elmt);
		}
		
		// anonymous element
		AbsItfComplexType ct = null;
		for(El elmtA: this.getElements()) {
			if(elmtA.getType() instanceof AbsItfComplexType) {
				ct = (AbsItfComplexType)elmtA.getType();
				findElmtInComplexType(element, ct, res);
			}
		}
		for(T type: this.getTypes()) {
			if(type instanceof AbsItfComplexType) {
				ct = (AbsItfComplexType)type;
				findElmtInComplexType(element, ct, res);
			}
		}
		
		return res;
	}
	


	private void findElmtInComplexType(QName element, AbsItfComplexType ct, List res) {
		if(ct.getSequence() != null) {
			for(El elmtItem: (List)ct.getSequence().getElements()) {
				if(elmtItem.getQName().equals(element)) {
					res.add(elmtItem);
				}
			}
		}
		if(ct.getAll() != null) {
			for(El elmtItem: (List)ct.getAll().getElements()) {
				if(elmtItem.getQName().equals(element)) {
					res.add(elmtItem);
				}
			}
		}
	}

	/*
	 * (non-Javadoc)
	 * @see org.ow2.easywsdl.schema.api.Schema#addElement(com.ebmwebsourcing .commons.schema.api.Element)
	 */
	public void addElement(final El elmt) {
		if (this.elements == null) {
			this.elements = new ArrayList();
		}
		this.elements.add(elmt);
	}

	/*
	 * Namespace method
	 */

	/*
	 * (non-Javadoc)
	 * @see org.ow2.easywsdl.schema.api.Schema#getAllNamespaces()
	 */
	public NamespaceMapperImpl getAllNamespaces() {
		return this.namespaceMapper;
	}

	/**
	 * @return the features
	 */
	public Map getFeatures() {
		if (this.features == null) {
			this.features = new HashMap();
		}
		return this.features;
	}

	/**
	 * @param features
	 *            the features to set
	 */
	public void setFeatures(final Map features) {
		this.features = features;
	}

	/**
	 * methods for baseURI
	 */

	public URI getDocumentURI() {
		return documentURI;
	}

	public void setDocumentURI(final URI documentURI) {
		this.documentURI = documentURI;
	}

	@Override
	public String toString() {
		return this.model.toString();
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy