org.eclipse.xsd.impl.XSDMaxLengthFacetImpl 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.List;
import javax.xml.namespace.QName;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
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.xsd.XSDAnnotation;
import org.eclipse.xsd.XSDConcreteComponent;
import org.eclipse.xsd.XSDDiagnostic;
import org.eclipse.xsd.XSDFactory;
import org.eclipse.xsd.XSDFixedFacet;
import org.eclipse.xsd.XSDMaxLengthFacet;
import org.eclipse.xsd.XSDPackage;
import org.eclipse.xsd.util.XSDConstants;
import org.eclipse.xsd.util.XSDUtil;
/**
*
* An implementation of the model object 'Max Length Facet'.
*
*
* The following features are implemented:
*
*
* - {@link org.eclipse.xsd.impl.XSDMaxLengthFacetImpl#getValue Value}
*
*
* @generated
*/
public class XSDMaxLengthFacetImpl
extends XSDFixedFacetImpl
implements XSDMaxLengthFacet
{
/**
* The default value of the '{@link #getValue() Value}' attribute.
*
*
* @see #getValue()
* @generated
* @ordered
*/
protected static final int VALUE_EDEFAULT = 0;
/**
* The cached value of the '{@link #getValue() Value}' attribute.
*
*
* @see #getValue()
* @generated
* @ordered
*/
protected int value = VALUE_EDEFAULT;
public static XSDMaxLengthFacet createMaxLengthFacet(Node node)
{
if (XSDConstants.nodeType(node) == XSDConstants.MAXLENGTH_ELEMENT)
{
XSDMaxLengthFacet xsdMaxLengthFacet = XSDFactory.eINSTANCE.createXSDMaxLengthFacet();
xsdMaxLengthFacet.setElement((Element)node);
return xsdMaxLengthFacet;
}
return null;
}
/**
*
*
* @generated
*/
protected XSDMaxLengthFacetImpl()
{
super();
}
/**
*
*
* @generated
*/
@Override
protected EClass eStaticClass()
{
return XSDPackage.Literals.XSD_MAX_LENGTH_FACET;
}
/**
*
*
* @generated
*/
@Override
public int getValue()
{
return value;
}
/**
*
*
* @generated
*/
@Override
public void setValue(int newValue)
{
int oldValue = value;
value = newValue;
if (eNotificationRequired())
eNotify(new ENotificationImpl(this, Notification.SET, XSDPackage.XSD_MAX_LENGTH_FACET__VALUE, oldValue, value));
}
/**
*
*
* @generated
*/
@Override
public Object eGet(int featureID, boolean resolve, boolean coreType)
{
switch (featureID)
{
case XSDPackage.XSD_MAX_LENGTH_FACET__VALUE:
return getValue();
}
return super.eGet(featureID, resolve, coreType);
}
/**
*
*
* @generated
*/
@Override
public void eSet(int featureID, Object newValue)
{
switch (featureID)
{
case XSDPackage.XSD_MAX_LENGTH_FACET__VALUE:
setValue((Integer)newValue);
return;
}
super.eSet(featureID, newValue);
}
/**
*
*
* @generated
*/
@Override
public void eUnset(int featureID)
{
switch (featureID)
{
case XSDPackage.XSD_MAX_LENGTH_FACET__VALUE:
setValue(VALUE_EDEFAULT);
return;
}
super.eUnset(featureID);
}
/**
*
*
* @generated
*/
@Override
public boolean eIsSet(int featureID)
{
switch (featureID)
{
case XSDPackage.XSD_MAX_LENGTH_FACET__VALUE:
return value != VALUE_EDEFAULT;
}
return super.eIsSet(featureID);
}
/**
*
*
* @generated
*/
@Override
public String toString()
{
if (eIsProxy()) return super.toString();
StringBuilder result = new StringBuilder(super.toString());
result.append(" (value: ");
result.append(value);
result.append(')');
return result.toString();
}
@Override
public Element createElement()
{
Element newElement = createElement(XSDConstants.MAXLENGTH_ELEMENT);
setElement(newElement);
return newElement;
}
@Override
protected void validateRestriction(XSDFixedFacet xsdFixedFacet)
{
if (getValue() > ((XSDMaxLengthFacet)xsdFixedFacet).getValue())
{
XSDDiagnostic xsdDiagnostic =
reportConstraintViolation
(XSDConstants.PART2,
"maxLength-valid-restriction",
getElement(),
XSDConstants.VALUE_ATTRIBUTE,
new Object [] { getValue(), xsdFixedFacet.getEffectiveValue(), xsdFixedFacet.getSimpleTypeDefinition().getURI() });
xsdDiagnostic.getComponents().add(xsdFixedFacet);
}
}
@Override
protected void changeAttribute(EAttribute eAttribute)
{
super.changeAttribute(eAttribute);
if (eAttribute == XSDPackage.Literals.XSD_FACET__LEXICAL_VALUE)
{
if (getLexicalValue() == null)
{
setValue(0);
}
else
{
try
{
int newValue = Integer.parseInt(getLexicalValue());
if (newValue != getValue())
{
setValue(newValue);
}
}
catch (NumberFormatException exception)
{
setValue(0);
}
}
traverseToRootForAnalysis();
}
}
@Override
public boolean isConstraintSatisfied(Object value)
{
if (value instanceof List>)
{
return ((List>)value).size() <= getValue();
}
else if (value instanceof String)
{
return ((String)value).length() <= getValue();
}
else if (value instanceof XSDUtil.ByteSequence)
{
return ((XSDUtil.ByteSequence)value).getBytes().length <= getValue();
}
else if (value instanceof QName)
{
QName qName = (QName)value;
int size = qName.getPrefix().length();
if (size > 0) ++size;
size += qName.getLocalPart().length();
return size <= getValue();
}
else
{
return false;
}
}
@Override
public Object getEffectiveValue()
{
return getValue();
}
@Override
public XSDConcreteComponent cloneConcreteComponent(boolean deep, boolean shareDOM)
{
XSDMaxLengthFacetImpl clonedMaxLengthFacet =
(XSDMaxLengthFacetImpl)getXSDFactory().createXSDMaxLengthFacet();
clonedMaxLengthFacet.isReconciling = true;
if (getLexicalValue() != null)
{
clonedMaxLengthFacet.setLexicalValue(getLexicalValue());
}
if (isSetFixed())
{
clonedMaxLengthFacet.setFixed(isFixed());
}
if (deep)
{
if (getAnnotation() != null)
{
clonedMaxLengthFacet.setAnnotation((XSDAnnotation)getAnnotation().cloneConcreteComponent(deep, shareDOM));
}
}
if (shareDOM && getElement() != null)
{
clonedMaxLengthFacet.setElement(getElement());
}
clonedMaxLengthFacet.isReconciling = shareDOM;
return clonedMaxLengthFacet;
}
}