/**
* 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.io.PrintStream;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.eclipse.emf.common.notify.Notification;
import org.eclipse.emf.common.notify.NotificationChain;
import org.eclipse.emf.common.util.BasicEList;
import org.eclipse.emf.common.util.UniqueEList;
import org.eclipse.emf.ecore.EAttribute;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EReference;
import org.eclipse.emf.ecore.InternalEObject;
import org.eclipse.emf.ecore.impl.ENotificationImpl;
import org.eclipse.xsd.XSDComponent;
import org.eclipse.xsd.XSDCompositor;
import org.eclipse.xsd.XSDConcreteComponent;
import org.eclipse.xsd.XSDConstraint;
import org.eclipse.xsd.XSDDiagnostic;
import org.eclipse.xsd.XSDDiagnosticSeverity;
import org.eclipse.xsd.XSDElementDeclaration;
import org.eclipse.xsd.XSDFactory;
import org.eclipse.xsd.XSDModelGroup;
import org.eclipse.xsd.XSDModelGroupDefinition;
import org.eclipse.xsd.XSDPackage;
import org.eclipse.xsd.XSDParticle;
import org.eclipse.xsd.XSDParticleContent;
import org.eclipse.xsd.XSDPlugin;
import org.eclipse.xsd.XSDSimpleTypeDefinition;
import org.eclipse.xsd.XSDTerm;
import org.eclipse.xsd.XSDTypeDefinition;
import org.eclipse.xsd.XSDWildcard;
import org.eclipse.xsd.util.XSDConstants;
import org.eclipse.xsd.util.XSDSwitch;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
/**
*
* An implementation of the model object 'Particle'.
*
*
* The following features are implemented:
*
*
* - {@link org.eclipse.xsd.impl.XSDParticleImpl#getMinOccurs Min Occurs}
* - {@link org.eclipse.xsd.impl.XSDParticleImpl#getMaxOccurs Max Occurs}
* - {@link org.eclipse.xsd.impl.XSDParticleImpl#getContent Content}
* - {@link org.eclipse.xsd.impl.XSDParticleImpl#getTerm Term}
*
*
* @generated
*/
public class XSDParticleImpl
extends XSDComplexTypeContentImpl
implements XSDParticle
{
protected static final int MAXIMUM_STATES = 10000;
/**
* The default value of the '{@link #getMinOccurs() Min Occurs}' attribute.
*
*
* @see #getMinOccurs()
* @generated
* @ordered
*/
protected static final int MIN_OCCURS_EDEFAULT = 1;
/**
* The cached value of the '{@link #getMinOccurs() Min Occurs}' attribute.
*
*
* @see #getMinOccurs()
* @generated
* @ordered
*/
protected int minOccurs = MIN_OCCURS_EDEFAULT;
/**
* The flag representing whether the Min Occurs attribute has been set.
*
*
* @generated
* @ordered
*/
protected static final int MIN_OCCURS_ESETFLAG = 1 << 8;
/**
* The default value of the '{@link #getMaxOccurs() Max Occurs}' attribute.
*
*
* @see #getMaxOccurs()
* @generated
* @ordered
*/
protected static final int MAX_OCCURS_EDEFAULT = 1;
/**
* The cached value of the '{@link #getMaxOccurs() Max Occurs}' attribute.
*
*
* @see #getMaxOccurs()
* @generated
* @ordered
*/
protected int maxOccurs = MAX_OCCURS_EDEFAULT;
/**
* The flag representing whether the Max Occurs attribute has been set.
*
*
* @generated
* @ordered
*/
protected static final int MAX_OCCURS_ESETFLAG = 1 << 9;
/**
* The cached value of the '{@link #getContent() Content}' containment reference.
*
*
* @see #getContent()
* @generated
* @ordered
*/
protected XSDParticleContent content;
/**
* The cached value of the '{@link #getTerm() Term}' reference.
*
*
* @see #getTerm()
* @generated
* @ordered
*/
protected XSDTerm term;
public static XSDParticle createParticle(Node node)
{
XSDParticleContent xsdParticleContent = XSDParticleContentImpl.createParticleContent(node);
if (xsdParticleContent != null)
{
XSDParticle xsdParticle = XSDFactory.eINSTANCE.createXSDParticle();
xsdParticle.setElement((Element)node);
xsdParticle.setContent(xsdParticleContent);
return xsdParticle;
}
return null;
}
/**
*
*
* @generated
*/
protected XSDParticleImpl()
{
super();
}
/**
*
*
* @generated
*/
@Override
protected EClass eStaticClass()
{
return XSDPackage.Literals.XSD_PARTICLE;
}
/**
*
*
* @generated
*/
@Override
public int getMinOccurs()
{
return minOccurs;
}
/**
*
*
* @generated
*/
@Override
public void setMinOccurs(int newMinOccurs)
{
int oldMinOccurs = minOccurs;
minOccurs = newMinOccurs;
boolean oldMinOccursESet = (eFlags & MIN_OCCURS_ESETFLAG) != 0;
eFlags |= MIN_OCCURS_ESETFLAG;
if (eNotificationRequired())
eNotify(new ENotificationImpl(this, Notification.SET, XSDPackage.XSD_PARTICLE__MIN_OCCURS, oldMinOccurs, minOccurs, !oldMinOccursESet));
}
/**
*
*
* @generated
*/
@Override
public void unsetMinOccurs()
{
int oldMinOccurs = minOccurs;
boolean oldMinOccursESet = (eFlags & MIN_OCCURS_ESETFLAG) != 0;
minOccurs = MIN_OCCURS_EDEFAULT;
eFlags &= ~MIN_OCCURS_ESETFLAG;
if (eNotificationRequired())
eNotify(new ENotificationImpl(this, Notification.UNSET, XSDPackage.XSD_PARTICLE__MIN_OCCURS, oldMinOccurs, MIN_OCCURS_EDEFAULT, oldMinOccursESet));
}
/**
*
*
* @generated
*/
@Override
public boolean isSetMinOccurs()
{
return (eFlags & MIN_OCCURS_ESETFLAG) != 0;
}
/**
*
*
* @generated
*/
@Override
public int getMaxOccurs()
{
return maxOccurs;
}
/**
*
*
* @generated
*/
@Override
public void setMaxOccurs(int newMaxOccurs)
{
int oldMaxOccurs = maxOccurs;
maxOccurs = newMaxOccurs;
boolean oldMaxOccursESet = (eFlags & MAX_OCCURS_ESETFLAG) != 0;
eFlags |= MAX_OCCURS_ESETFLAG;
if (eNotificationRequired())
eNotify(new ENotificationImpl(this, Notification.SET, XSDPackage.XSD_PARTICLE__MAX_OCCURS, oldMaxOccurs, maxOccurs, !oldMaxOccursESet));
}
/**
*
*
* @generated
*/
@Override
public void unsetMaxOccurs()
{
int oldMaxOccurs = maxOccurs;
boolean oldMaxOccursESet = (eFlags & MAX_OCCURS_ESETFLAG) != 0;
maxOccurs = MAX_OCCURS_EDEFAULT;
eFlags &= ~MAX_OCCURS_ESETFLAG;
if (eNotificationRequired())
eNotify(new ENotificationImpl(this, Notification.UNSET, XSDPackage.XSD_PARTICLE__MAX_OCCURS, oldMaxOccurs, MAX_OCCURS_EDEFAULT, oldMaxOccursESet));
}
/**
*
*
* @generated
*/
@Override
public boolean isSetMaxOccurs()
{
return (eFlags & MAX_OCCURS_ESETFLAG) != 0;
}
/**
*
*
* @generated
*/
@Override
public XSDParticleContent getContent()
{
return content;
}
/**
*
*
* @generated
*/
@Override
public void setContent(XSDParticleContent newContent)
{
if (newContent != content)
{
NotificationChain msgs = null;
if (content != null)
msgs = ((InternalEObject)content).eInverseRemove(this, EOPPOSITE_FEATURE_BASE - XSDPackage.XSD_PARTICLE__CONTENT, null, msgs);
if (newContent != null)
msgs = ((InternalEObject)newContent).eInverseAdd(this, EOPPOSITE_FEATURE_BASE - XSDPackage.XSD_PARTICLE__CONTENT, null, msgs);
msgs = basicSetContent(newContent, msgs);
if (msgs != null) msgs.dispatch();
}
else if (eNotificationRequired())
eNotify(new ENotificationImpl(this, Notification.SET, XSDPackage.XSD_PARTICLE__CONTENT, newContent, newContent));
}
/**
*
*
* @generated
*/
public NotificationChain basicSetContent(XSDParticleContent newContent, NotificationChain msgs)
{
XSDParticleContent oldContent = content;
content = newContent;
if (eNotificationRequired())
{
ENotificationImpl notification = new ENotificationImpl(this, Notification.SET, XSDPackage.XSD_PARTICLE__CONTENT, oldContent, newContent);
if (msgs == null) msgs = notification; else msgs.add(notification);
}
return msgs;
}
/**
*
*
* @generated
*/
@Override
public XSDTerm getTerm()
{
return term;
}
/**
*
*
* @generated
*/
@Override
public void setTerm(XSDTerm newTerm)
{
XSDTerm oldTerm = term;
term = newTerm;
if (eNotificationRequired())
eNotify(new ENotificationImpl(this, Notification.SET, XSDPackage.XSD_PARTICLE__TERM, oldTerm, term));
}
/**
*
*
* @generated
*/
@Override
public NotificationChain eInverseRemove(InternalEObject otherEnd, int featureID, NotificationChain msgs)
{
switch (featureID)
{
case XSDPackage.XSD_PARTICLE__CONTENT:
return basicSetContent(null, msgs);
}
return super.eInverseRemove(otherEnd, featureID, msgs);
}
/**
*
*
* @generated
*/
@Override
public Object eGet(int featureID, boolean resolve, boolean coreType)
{
switch (featureID)
{
case XSDPackage.XSD_PARTICLE__MIN_OCCURS:
return getMinOccurs();
case XSDPackage.XSD_PARTICLE__MAX_OCCURS:
return getMaxOccurs();
case XSDPackage.XSD_PARTICLE__CONTENT:
return getContent();
case XSDPackage.XSD_PARTICLE__TERM:
return getTerm();
}
return super.eGet(featureID, resolve, coreType);
}
/**
*
*
* @generated
*/
@Override
public void eSet(int featureID, Object newValue)
{
switch (featureID)
{
case XSDPackage.XSD_PARTICLE__MIN_OCCURS:
setMinOccurs((Integer)newValue);
return;
case XSDPackage.XSD_PARTICLE__MAX_OCCURS:
setMaxOccurs((Integer)newValue);
return;
case XSDPackage.XSD_PARTICLE__CONTENT:
setContent((XSDParticleContent)newValue);
return;
case XSDPackage.XSD_PARTICLE__TERM:
setTerm((XSDTerm)newValue);
return;
}
super.eSet(featureID, newValue);
}
/**
*
*
* @generated
*/
@Override
public void eUnset(int featureID)
{
switch (featureID)
{
case XSDPackage.XSD_PARTICLE__MIN_OCCURS:
unsetMinOccurs();
return;
case XSDPackage.XSD_PARTICLE__MAX_OCCURS:
unsetMaxOccurs();
return;
case XSDPackage.XSD_PARTICLE__CONTENT:
setContent((XSDParticleContent)null);
return;
case XSDPackage.XSD_PARTICLE__TERM:
setTerm((XSDTerm)null);
return;
}
super.eUnset(featureID);
}
/**
*
*
* @generated
*/
@Override
public boolean eIsSet(int featureID)
{
switch (featureID)
{
case XSDPackage.XSD_PARTICLE__MIN_OCCURS:
return isSetMinOccurs();
case XSDPackage.XSD_PARTICLE__MAX_OCCURS:
return isSetMaxOccurs();
case XSDPackage.XSD_PARTICLE__CONTENT:
return content != null;
case XSDPackage.XSD_PARTICLE__TERM:
return term != null;
}
return super.eIsSet(featureID);
}
/**
*
*
* @generated
*/
@Override
public String toString()
{
if (eIsProxy()) return super.toString();
StringBuilder result = new StringBuilder(super.toString());
result.append(" (minOccurs: ");
if ((eFlags & MIN_OCCURS_ESETFLAG) != 0) result.append(minOccurs); else result.append("");
result.append(", maxOccurs: ");
if ((eFlags & MAX_OCCURS_ESETFLAG) != 0) result.append(maxOccurs); else result.append("");
result.append(')');
return result.toString();
}
@Override
public Element createElement()
{
Element newElement = null;
XSDParticleContent theContent = getContent();
if (theContent != null)
{
newElement = ((XSDConcreteComponentImpl)theContent).createElement();
setElement(newElement);
}
return newElement;
}
@Override
protected void patch()
{
super.patch();
patchHelper();
}
protected void patchHelper()
{
XSDParticleContent theContent = getContent();
XSDTerm newTerm = null;
if (theContent instanceof XSDModelGroupDefinition)
{
XSDModelGroupDefinition xsdModelGroupDefinition = (XSDModelGroupDefinition)theContent;
newTerm = xsdModelGroupDefinition.getResolvedModelGroupDefinition().getModelGroup();
}
else if (theContent instanceof XSDElementDeclaration)
{
XSDElementDeclaration xsdElementDeclaration = (XSDElementDeclaration)theContent;
newTerm = xsdElementDeclaration.getResolvedElementDeclaration();
}
else
{
newTerm = (XSDTerm)theContent;
}
if (getTerm() != newTerm)
{
setTerm(newTerm);
}
}
@Override
public boolean analyze()
{
xsdNFA = null;
return super.analyze();
}
@Override
public void validate()
{
super.validate();
if (getMaxOccurs() != -1)
{
if (getMinOccurs() > getMaxOccurs())
{
createDiagnostic(XSDDiagnosticSeverity.ERROR_LITERAL, "coss-particle.2.1");
}
}
}
@Override
protected Collection getContentNodes(Element changedElement)
{
return Collections.singleton(getElement());
}
@Override
protected void reconcileAttributes(Element changedElement)
{
super.reconcileAttributes(changedElement);
if (changedElement == getElement())
{
if (changedElement.hasAttributeNS(null, XSDConstants.MINOCCURS_ATTRIBUTE))
{
String newMinOccurs = changedElement.getAttributeNS(null, XSDConstants.MINOCCURS_ATTRIBUTE);
try
{
int newMinOccursValue = Integer.parseInt(newMinOccurs);
if (!isSetMinOccurs() || newMinOccursValue != getMinOccurs())
{
setMinOccurs(newMinOccursValue);
}
}
catch (NumberFormatException exception)
{
// Ignore
}
}
else if (isSetMinOccurs())
{
unsetMinOccurs();
}
if (changedElement.hasAttributeNS(null, XSDConstants.MAXOCCURS_ATTRIBUTE))
{
String newMaxOccurs = changedElement.getAttributeNS(null, XSDConstants.MAXOCCURS_ATTRIBUTE);
try
{
int newMaxOccursValue = "unbounded".equals(newMaxOccurs) ? -1 : Integer.parseInt(newMaxOccurs);
if (!isSetMaxOccurs() || newMaxOccursValue != getMaxOccurs())
{
setMaxOccurs(newMaxOccursValue);
}
}
catch (NumberFormatException exception)
{
// Ignore
}
}
else if (isSetMaxOccurs())
{
unsetMaxOccurs();
}
XSDParticleContent theContent = getContent();
if (theContent != null)
{
theContent.elementAttributesChanged(changedElement);
}
}
}
@Override
protected void reconcileContents(Element changedElement)
{
super.reconcileContents(changedElement);
XSDParticleContent theContent = getContent();
if (theContent != null)
{
theContent.elementContentsChanged(changedElement);
XSDTerm newTerm =
(XSDTerm)
new XSDSwitch