org.eclipse.xsd.impl.XSDNamedComponentImpl Maven / Gradle / Ivy
/**
* Copyright (c) 2002-2006 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v2.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v20.html
*
* Contributors:
* IBM - Initial API and implementation
*/
package org.eclipse.xsd.impl;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Map;
import org.w3c.dom.Element;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.impl.ENotificationImpl;
import org.eclipse.emf.ecore.util.EObjectEList;
import org.eclipse.emf.ecore.xml.type.util.XMLTypeUtil;
import org.eclipse.xsd.XSDComplexTypeDefinition;
import org.eclipse.xsd.XSDConcreteComponent;
import org.eclipse.xsd.XSDNamedComponent;
import org.eclipse.xsd.XSDPackage;
import org.eclipse.xsd.XSDPlugin;
import org.eclipse.xsd.XSDSchema;
import org.eclipse.xsd.XSDSimpleTypeDefinition;
import org.eclipse.xsd.XSDVariety;
import org.eclipse.xsd.util.XSDConstants;
/**
*
* An implementation of the model object 'Named Component'.
*
*
* The following features are implemented:
*
*
* - {@link org.eclipse.xsd.impl.XSDNamedComponentImpl#getName Name}
* - {@link org.eclipse.xsd.impl.XSDNamedComponentImpl#getTargetNamespace Target Namespace}
* - {@link org.eclipse.xsd.impl.XSDNamedComponentImpl#getAliasName Alias Name}
* - {@link org.eclipse.xsd.impl.XSDNamedComponentImpl#getURI URI}
* - {@link org.eclipse.xsd.impl.XSDNamedComponentImpl#getAliasURI Alias URI}
* - {@link org.eclipse.xsd.impl.XSDNamedComponentImpl#getQName QName}
*
*
* @generated
*/
public abstract class XSDNamedComponentImpl
extends XSDComponentImpl
implements XSDNamedComponent
{
/**
* The default value of the '{@link #getName() Name}' attribute.
*
*
* @see #getName()
* @generated
* @ordered
*/
protected static final String NAME_EDEFAULT = null;
/**
* The cached value of the '{@link #getName() Name}' attribute.
*
*
* @see #getName()
* @generated
* @ordered
*/
protected String name = NAME_EDEFAULT;
/**
* The default value of the '{@link #getTargetNamespace() Target Namespace}' attribute.
*
*
* @see #getTargetNamespace()
* @generated
* @ordered
*/
protected static final String TARGET_NAMESPACE_EDEFAULT = null;
/**
* The cached value of the '{@link #getTargetNamespace() Target Namespace}' attribute.
*
*
* @see #getTargetNamespace()
* @generated
* @ordered
*/
protected String targetNamespace = TARGET_NAMESPACE_EDEFAULT;
/**
* The default value of the '{@link #getAliasName() Alias Name}' attribute.
*
*
* @see #getAliasName()
* @generated
* @ordered
*/
protected static final String ALIAS_NAME_EDEFAULT = null;
/**
* The default value of the '{@link #getURI() URI}' attribute.
*
*
* @see #getURI()
* @generated
* @ordered
*/
protected static final String URI_EDEFAULT = null;
/**
* The default value of the '{@link #getAliasURI() Alias URI}' attribute.
*
*
* @see #getAliasURI()
* @generated
* @ordered
*/
protected static final String ALIAS_URI_EDEFAULT = null;
/**
* The default value of the '{@link #getQName() QName}' attribute.
*
*
* @see #getQName()
* @generated
* @ordered
*/
protected static final String QNAME_EDEFAULT = null;
public static List sortNamedComponents(Collection xsdNamedComponents)
{
XSDNamedComponent [] objects = new XSDNamedComponent[xsdNamedComponents.size()];
xsdNamedComponents.toArray(objects);
Arrays.sort(objects, Comparator.getInstance());
return Arrays.asList(objects);
}
public static void addToSortedList(List xsdNamedComponents, T xsdNamedComponent)
{
int index = Collections.binarySearch(xsdNamedComponents, xsdNamedComponent, Comparator.getInstance());
if (index < 0)
{
xsdNamedComponents.add(-(index + 1), xsdNamedComponent);
}
else if (xsdNamedComponents.get(index) != xsdNamedComponent)
{
xsdNamedComponents.add(index, xsdNamedComponent);
}
}
public static class XSDNamedComponentList extends EObjectEList
{
private static final long serialVersionUID = 1L;
protected Map map;
protected XSDSchemaImpl xsdSchema;
public XSDNamedComponentList(Class> dataClass, XSDSchemaImpl owner, int featureID)
{
super(dataClass, owner, featureID);
xsdSchema = owner;
}
protected boolean isFromThisNamespace(XSDNamedComponent xsdNamedComponent)
{
String targetNamespace = xsdNamedComponent.getTargetNamespace();
return targetNamespace == null ? xsdSchema.getTargetNamespace() == null : targetNamespace.equals(xsdSchema.getTargetNamespace());
}
@Override
protected void didAdd(int index, Object newObject)
{
XSDNamedComponent xsdNamedComponent = (XSDNamedComponent)newObject;
if (isFromThisNamespace(xsdNamedComponent))
{
if (map == null)
{
map = new HashMap();
}
map.put(xsdNamedComponent.getName(), xsdNamedComponent);
}
}
@Override
protected void didClear(int size, Object[] oldObjects)
{
map = null;
}
@Override
protected void didRemove(int index, Object oldObject)
{
if (map != null)
{
XSDNamedComponent xsdNamedComponent = (XSDNamedComponent)oldObject;
if (isFromThisNamespace(xsdNamedComponent))
{
map.values().remove(xsdNamedComponent);
}
}
}
@Override
protected void didSet(int index, Object newObject, Object oldObject)
{
didRemove(index, oldObject);
didAdd(index, newObject);
}
public XSDNamedComponent get(String targetNamespace, String localName)
{
// If the list is empty, there can't be a match.
//
if (isEmpty())
{
return null;
}
// If we are looking up a name in the owning schema's namespace...
//
if (targetNamespace == null ? xsdSchema.getTargetNamespace() == null : targetNamespace.equals(xsdSchema.getTargetNamespace()))
{
// Look up the result in the map if there is one.
//
if (map == null)
{
return null;
}
XSDNamedComponent result = map.get(localName);
return result;
}
int index =
Collections.binarySearch(this, new String [] { targetNamespace, localName}, StringPairComparator.getInstance());
if (index < 0)
{
return null;
}
else
{
return this.get(index);
}
}
}
public static XSDNamedComponent findInSortedList(List extends XSDNamedComponent> xsdNamedComponents, String targetNamespace, String localName)
{
if (xsdNamedComponents instanceof XSDNamedComponentList>)
{
return ((XSDNamedComponentList extends XSDNamedComponent>)xsdNamedComponents).get(targetNamespace, localName);
}
int index =
Collections.binarySearch(xsdNamedComponents, new String [] { targetNamespace, localName}, StringPairComparator.getInstance());
if (index < 0)
{
return null;
}
else
{
return xsdNamedComponents.get(index);
}
}
public static void mergeToSortedList(List xsdNamedComponentsTarget, List xsdNamedComponentsSource)
{
Iterator sourceComponents = xsdNamedComponentsSource.iterator();
if (sourceComponents.hasNext())
{
Comparator comparator = Comparator.getInstance();
T sourceComponent = sourceComponents.next();
for (ListIterator targetComponents = xsdNamedComponentsTarget.listIterator(); targetComponents.hasNext(); )
{
T targetComponent = targetComponents.next();
if (targetComponent == sourceComponent)
{
// Do nothing.
}
else if (comparator.compare(sourceComponent, targetComponent) < 0)
{
targetComponents.previous();
targetComponents.add(sourceComponent);
}
else
{
continue;
}
if (sourceComponents.hasNext())
{
sourceComponent = sourceComponents.next();
}
else
{
sourceComponent = null;
break;
}
}
if (sourceComponent != null)
{
xsdNamedComponentsTarget.add(sourceComponent);
while (sourceComponents.hasNext())
{
sourceComponent = sourceComponents.next();
xsdNamedComponentsTarget.add(sourceComponent);
}
}
}
}
public static class Comparator implements java.util.Comparator
{
protected static Comparator instance = new Comparator();
public static Comparator getInstance()
{
if (instance == null)
{
instance = new Comparator();
}
return instance;
}
protected XSDPlugin.StringComparator collator = XSDPlugin.INSTANCE.getComparator();
public Comparator()
{
super();
}
@Override
public boolean equals(Object that)
{
return this == that;
}
public int compare(XSDNamedComponent xsdNamedComponent1, XSDNamedComponent xsdNamedComponent2)
{
String name1 = xsdNamedComponent1.getName();
String name2 = xsdNamedComponent2.getName();
if (name1 == null && name2 == null)
{
return xsdNamedComponent1.hashCode() - xsdNamedComponent2.hashCode();
}
else if (name1 == null)
{
return 1;
}
else if (name2 == null)
{
return -1;
}
else
{
int result = collator.compare(name1, name2);
if (result == 0)
{
String namespace1 = xsdNamedComponent1.getTargetNamespace();
String namespace2 = xsdNamedComponent2.getTargetNamespace();
if (namespace1 == null && namespace2 == null)
{
return xsdNamedComponent1.hashCode() - xsdNamedComponent2.hashCode();
}
else if (namespace1 == null)
{
return 1;
}
else if (namespace2 == null)
{
return -1;
}
else
{
result = collator.compare(namespace1, namespace2);
if (result == 0)
{
result = xsdNamedComponent1.hashCode() - xsdNamedComponent2.hashCode();
}
return result;
}
}
else
{
return result;
}
}
}
}
public static class StringPairComparator implements java.util.Comparator