| 1 |
/** |
| 2 |
* Copyright (c) 2002-2006 IBM Corporation and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v2.0 |
| 5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v20.html |
| 7 |
* |
| 8 |
* Contributors: |
| 9 |
* IBM - Initial API and implementation |
| 10 |
*/ |
| 11 |
package org.eclipse.xsd.impl; |
| 12 |
|
| 13 |
|
| 14 |
import java.util.List; |
| 15 |
|
| 16 |
import javax.xml.namespace.QName; |
| 17 |
|
| 18 |
import org.w3c.dom.Element; |
| 19 |
import org.w3c.dom.Node; |
| 20 |
|
| 21 |
import org.eclipse.emf.common.notify.Notification; |
| 22 |
import org.eclipse.emf.ecore.EAttribute; |
| 23 |
import org.eclipse.emf.ecore.EClass; |
| 24 |
import org.eclipse.emf.ecore.impl.ENotificationImpl; |
| 25 |
|
| 26 |
import org.eclipse.xsd.XSDAnnotation; |
| 27 |
import org.eclipse.xsd.XSDConcreteComponent; |
| 28 |
import org.eclipse.xsd.XSDDiagnostic; |
| 29 |
import org.eclipse.xsd.XSDFactory; |
| 30 |
import org.eclipse.xsd.XSDFixedFacet; |
| 31 |
import org.eclipse.xsd.XSDMaxLengthFacet; |
| 32 |
import org.eclipse.xsd.XSDPackage; |
| 33 |
import org.eclipse.xsd.util.XSDConstants; |
| 34 |
import org.eclipse.xsd.util.XSDUtil; |
| 35 |
|
| 36 |
|
| 37 |
/** |
| 38 |
* <!-- begin-user-doc --> |
| 39 |
* An implementation of the model object '<em><b>Max Length Facet</b></em>'. |
| 40 |
* <!-- end-user-doc --> |
| 41 |
* <p> |
| 42 |
* The following features are implemented: |
| 43 |
* </p> |
| 44 |
* <ul> |
| 45 |
* <li>{@link org.eclipse.xsd.impl.XSDMaxLengthFacetImpl#getValue <em>Value</em>}</li> |
| 46 |
* </ul> |
| 47 |
* |
| 48 |
* @generated |
| 49 |
*/ |
| 50 |
public class XSDMaxLengthFacetImpl |
| 51 |
extends XSDFixedFacetImpl |
| 52 |
implements XSDMaxLengthFacet |
| 53 |
{ |
| 54 |
/** |
| 55 |
* The default value of the '{@link #getValue() <em>Value</em>}' attribute. |
| 56 |
* <!-- begin-user-doc --> |
| 57 |
* <!-- end-user-doc --> |
| 58 |
* @see #getValue() |
| 59 |
* @generated |
| 60 |
* @ordered |
| 61 |
*/ |
| 62 |
protected static final int VALUE_EDEFAULT = 0; |
| 63 |
|
| 64 |
/** |
| 65 |
* The cached value of the '{@link #getValue() <em>Value</em>}' attribute. |
| 66 |
* <!-- begin-user-doc --> |
| 67 |
* <!-- end-user-doc --> |
| 68 |
* @see #getValue() |
| 69 |
* @generated |
| 70 |
* @ordered |
| 71 |
*/ |
| 72 |
protected int value = VALUE_EDEFAULT; |
| 73 |
|
| 74 |
public static XSDMaxLengthFacet createMaxLengthFacet(Node node) |
| 75 |
{ |
| 76 |
if (XSDConstants.nodeType(node) == XSDConstants.MAXLENGTH_ELEMENT) |
| 77 |
{ |
| 78 |
XSDMaxLengthFacet xsdMaxLengthFacet = XSDFactory.eINSTANCE.createXSDMaxLengthFacet(); |
| 79 |
xsdMaxLengthFacet.setElement((Element)node); |
| 80 |
return xsdMaxLengthFacet; |
| 81 |
} |
| 82 |
|
| 83 |
return null; |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* <!-- begin-user-doc --> |
| 88 |
* <!-- end-user-doc --> |
| 89 |
* @generated |
| 90 |
*/ |
| 91 |
protected XSDMaxLengthFacetImpl() |
| 92 |
{ |
| 93 |
super(); |
| 94 |
} |
| 95 |
|
| 96 |
/** |
| 97 |
* <!-- begin-user-doc --> |
| 98 |
* <!-- end-user-doc --> |
| 99 |
* @generated |
| 100 |
*/ |
| 101 |
@Override |
| 102 |
protected EClass eStaticClass() |
| 103 |
{ |
| 104 |
return XSDPackage.Literals.XSD_MAX_LENGTH_FACET; |
| 105 |
} |
| 106 |
|
| 107 |
/** |
| 108 |
* <!-- begin-user-doc --> |
| 109 |
* <!-- end-user-doc --> |
| 110 |
* @generated |
| 111 |
*/ |
| 112 |
@Override |
| 113 |
public int getValue() |
| 114 |
{ |
| 115 |
return value; |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* <!-- begin-user-doc --> |
| 120 |
* <!-- end-user-doc --> |
| 121 |
* @generated |
| 122 |
*/ |
| 123 |
@Override |
| 124 |
public void setValue(int newValue) |
| 125 |
{ |
| 126 |
int oldValue = value; |
| 127 |
value = newValue; |
| 128 |
if (eNotificationRequired()) |
| 129 |
eNotify(new ENotificationImpl(this, Notification.SET, XSDPackage.XSD_MAX_LENGTH_FACET__VALUE, oldValue, value)); |
| 130 |
} |
| 131 |
|
| 132 |
/** |
| 133 |
* <!-- begin-user-doc --> |
| 134 |
* <!-- end-user-doc --> |
| 135 |
* @generated |
| 136 |
*/ |
| 137 |
@Override |
| 138 |
public Object eGet(int featureID, boolean resolve, boolean coreType) |
| 139 |
{ |
| 140 |
switch (featureID) |
| 141 |
{ |
| 142 |
case XSDPackage.XSD_MAX_LENGTH_FACET__VALUE: |
| 143 |
return getValue(); |
| 144 |
} |
| 145 |
return super.eGet(featureID, resolve, coreType); |
| 146 |
} |
| 147 |
|
| 148 |
/** |
| 149 |
* <!-- begin-user-doc --> |
| 150 |
* <!-- end-user-doc --> |
| 151 |
* @generated |
| 152 |
*/ |
| 153 |
@Override |
| 154 |
public void eSet(int featureID, Object newValue) |
| 155 |
{ |
| 156 |
switch (featureID) |
| 157 |
{ |
| 158 |
case XSDPackage.XSD_MAX_LENGTH_FACET__VALUE: |
| 159 |
setValue((Integer)newValue); |
| 160 |
return; |
| 161 |
} |
| 162 |
super.eSet(featureID, newValue); |
| 163 |
} |
| 164 |
|
| 165 |
/** |
| 166 |
* <!-- begin-user-doc --> |
| 167 |
* <!-- end-user-doc --> |
| 168 |
* @generated |
| 169 |
*/ |
| 170 |
@Override |
| 171 |
public void eUnset(int featureID) |
| 172 |
{ |
| 173 |
switch (featureID) |
| 174 |
{ |
| 175 |
case XSDPackage.XSD_MAX_LENGTH_FACET__VALUE: |
| 176 |
setValue(VALUE_EDEFAULT); |
| 177 |
return; |
| 178 |
} |
| 179 |
super.eUnset(featureID); |
| 180 |
} |
| 181 |
|
| 182 |
/** |
| 183 |
* <!-- begin-user-doc --> |
| 184 |
* <!-- end-user-doc --> |
| 185 |
* @generated |
| 186 |
*/ |
| 187 |
@Override |
| 188 |
public boolean eIsSet(int featureID) |
| 189 |
{ |
| 190 |
switch (featureID) |
| 191 |
{ |
| 192 |
case XSDPackage.XSD_MAX_LENGTH_FACET__VALUE: |
| 193 |
return value != VALUE_EDEFAULT; |
| 194 |
} |
| 195 |
return super.eIsSet(featureID); |
| 196 |
} |
| 197 |
|
| 198 |
/** |
| 199 |
* <!-- begin-user-doc --> |
| 200 |
* <!-- end-user-doc --> |
| 201 |
* @generated |
| 202 |
*/ |
| 203 |
@Override |
| 204 |
public String toString() |
| 205 |
{ |
| 206 |
if (eIsProxy()) return super.toString(); |
| 207 |
|
| 208 |
StringBuilder result = new StringBuilder(super.toString()); |
| 209 |
result.append(" (value: "); |
| 210 |
result.append(value); |
| 211 |
result.append(')'); |
| 212 |
return result.toString(); |
| 213 |
} |
| 214 |
|
| 215 |
@Override |
| 216 |
public Element createElement() |
| 217 |
{ |
| 218 |
Element newElement = createElement(XSDConstants.MAXLENGTH_ELEMENT); |
| 219 |
setElement(newElement); |
| 220 |
return newElement; |
| 221 |
} |
| 222 |
|
| 223 |
@Override |
| 224 |
protected void validateRestriction(XSDFixedFacet xsdFixedFacet) |
| 225 |
{ |
| 226 |
if (getValue() > ((XSDMaxLengthFacet)xsdFixedFacet).getValue()) |
| 227 |
{ |
| 228 |
XSDDiagnostic xsdDiagnostic = |
| 229 |
reportConstraintViolation |
| 230 |
(XSDConstants.PART2, |
| 231 |
"maxLength-valid-restriction", |
| 232 |
getElement(), |
| 233 |
XSDConstants.VALUE_ATTRIBUTE, |
| 234 |
new Object [] { getValue(), xsdFixedFacet.getEffectiveValue(), xsdFixedFacet.getSimpleTypeDefinition().getURI() }); |
| 235 |
xsdDiagnostic.getComponents().add(xsdFixedFacet); |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
@Override |
| 240 |
protected void changeAttribute(EAttribute eAttribute) |
| 241 |
{ |
| 242 |
super.changeAttribute(eAttribute); |
| 243 |
if (eAttribute == XSDPackage.Literals.XSD_FACET__LEXICAL_VALUE) |
| 244 |
{ |
| 245 |
if (getLexicalValue() == null) |
| 246 |
{ |
| 247 |
setValue(0); |
| 248 |
} |
| 249 |
else |
| 250 |
{ |
| 251 |
try |
| 252 |
{ |
| 253 |
int newValue = Integer.parseInt(getLexicalValue()); |
| 254 |
if (newValue != getValue()) |
| 255 |
{ |
| 256 |
setValue(newValue); |
| 257 |
} |
| 258 |
} |
| 259 |
catch (NumberFormatException exception) |
| 260 |
{ |
| 261 |
setValue(0); |
| 262 |
} |
| 263 |
} |
| 264 |
traverseToRootForAnalysis(); |
| 265 |
} |
| 266 |
} |
| 267 |
|
| 268 |
@Override |
| 269 |
public boolean isConstraintSatisfied(Object value) |
| 270 |
{ |
| 271 |
if (value instanceof List<?>) |
| 272 |
{ |
| 273 |
return ((List<?>)value).size() <= getValue(); |
| 274 |
} |
| 275 |
else if (value instanceof String) |
| 276 |
{ |
| 277 |
return ((String)value).length() <= getValue(); |
| 278 |
} |
| 279 |
else if (value instanceof XSDUtil.ByteSequence) |
| 280 |
{ |
| 281 |
return ((XSDUtil.ByteSequence)value).getBytes().length <= getValue(); |
| 282 |
} |
| 283 |
else if (value instanceof QName) |
| 284 |
{ |
| 285 |
QName qName = (QName)value; |
| 286 |
int size = qName.getPrefix().length(); |
| 287 |
if (size > 0) ++size; |
| 288 |
size += qName.getLocalPart().length(); |
| 289 |
return size <= getValue(); |
| 290 |
} |
| 291 |
else |
| 292 |
{ |
| 293 |
return false; |
| 294 |
} |
| 295 |
} |
| 296 |
|
| 297 |
@Override |
| 298 |
public Object getEffectiveValue() |
| 299 |
{ |
| 300 |
return getValue(); |
| 301 |
} |
| 302 |
|
| 303 |
@Override |
| 304 |
public XSDConcreteComponent cloneConcreteComponent(boolean deep, boolean shareDOM) |
| 305 |
{ |
| 306 |
XSDMaxLengthFacetImpl clonedMaxLengthFacet = |
| 307 |
(XSDMaxLengthFacetImpl)getXSDFactory().createXSDMaxLengthFacet(); |
| 308 |
clonedMaxLengthFacet.isReconciling = true; |
| 309 |
|
| 310 |
if (getLexicalValue() != null) |
| 311 |
{ |
| 312 |
clonedMaxLengthFacet.setLexicalValue(getLexicalValue()); |
| 313 |
} |
| 314 |
if (isSetFixed()) |
| 315 |
{ |
| 316 |
clonedMaxLengthFacet.setFixed(isFixed()); |
| 317 |
} |
| 318 |
|
| 319 |
if (deep) |
| 320 |
{ |
| 321 |
if (getAnnotation() != null) |
| 322 |
{ |
| 323 |
clonedMaxLengthFacet.setAnnotation((XSDAnnotation)getAnnotation().cloneConcreteComponent(deep, shareDOM)); |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
if (shareDOM && getElement() != null) |
| 328 |
{ |
| 329 |
clonedMaxLengthFacet.setElement(getElement()); |
| 330 |
} |
| 331 |
|
| 332 |
clonedMaxLengthFacet.isReconciling = shareDOM; |
| 333 |
return clonedMaxLengthFacet; |
| 334 |
} |
| 335 |
} |