| 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.XSDLengthFacet; |
| 32 |
import org.eclipse.xsd.XSDMaxLengthFacet; |
| 33 |
import org.eclipse.xsd.XSDMinLengthFacet; |
| 34 |
import org.eclipse.xsd.XSDPackage; |
| 35 |
import org.eclipse.xsd.XSDSimpleTypeDefinition; |
| 36 |
import org.eclipse.xsd.util.XSDConstants; |
| 37 |
import org.eclipse.xsd.util.XSDUtil; |
| 38 |
|
| 39 |
|
| 40 |
/** |
| 41 |
* <!-- begin-user-doc --> |
| 42 |
* An implementation of the model object '<em><b>Length Facet</b></em>'. |
| 43 |
* <!-- end-user-doc --> |
| 44 |
* <p> |
| 45 |
* The following features are implemented: |
| 46 |
* </p> |
| 47 |
* <ul> |
| 48 |
* <li>{@link org.eclipse.xsd.impl.XSDLengthFacetImpl#getValue <em>Value</em>}</li> |
| 49 |
* </ul> |
| 50 |
* |
| 51 |
* @generated |
| 52 |
*/ |
| 53 |
public class XSDLengthFacetImpl |
| 54 |
extends XSDFixedFacetImpl |
| 55 |
implements XSDLengthFacet |
| 56 |
{ |
| 57 |
/** |
| 58 |
* The default value of the '{@link #getValue() <em>Value</em>}' attribute. |
| 59 |
* <!-- begin-user-doc --> |
| 60 |
* <!-- end-user-doc --> |
| 61 |
* @see #getValue() |
| 62 |
* @generated |
| 63 |
* @ordered |
| 64 |
*/ |
| 65 |
protected static final int VALUE_EDEFAULT = 0; |
| 66 |
|
| 67 |
/** |
| 68 |
* The cached value of the '{@link #getValue() <em>Value</em>}' attribute. |
| 69 |
* <!-- begin-user-doc --> |
| 70 |
* <!-- end-user-doc --> |
| 71 |
* @see #getValue() |
| 72 |
* @generated |
| 73 |
* @ordered |
| 74 |
*/ |
| 75 |
protected int value = VALUE_EDEFAULT; |
| 76 |
|
| 77 |
public static XSDLengthFacet createLengthFacet(Node node) |
| 78 |
{ |
| 79 |
if (XSDConstants.nodeType(node) == XSDConstants.LENGTH_ELEMENT) |
| 80 |
{ |
| 81 |
XSDLengthFacet xsdLengthFacet = XSDFactory.eINSTANCE.createXSDLengthFacet(); |
| 82 |
xsdLengthFacet.setElement((Element)node); |
| 83 |
return xsdLengthFacet; |
| 84 |
} |
| 85 |
|
| 86 |
return null; |
| 87 |
} |
| 88 |
|
| 89 |
/** |
| 90 |
* <!-- begin-user-doc --> |
| 91 |
* <!-- end-user-doc --> |
| 92 |
* @generated |
| 93 |
*/ |
| 94 |
protected XSDLengthFacetImpl() |
| 95 |
{ |
| 96 |
super(); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* <!-- begin-user-doc --> |
| 101 |
* <!-- end-user-doc --> |
| 102 |
* @generated |
| 103 |
*/ |
| 104 |
@Override |
| 105 |
protected EClass eStaticClass() |
| 106 |
{ |
| 107 |
return XSDPackage.Literals.XSD_LENGTH_FACET; |
| 108 |
} |
| 109 |
|
| 110 |
/** |
| 111 |
* <!-- begin-user-doc --> |
| 112 |
* <!-- end-user-doc --> |
| 113 |
* @generated |
| 114 |
*/ |
| 115 |
@Override |
| 116 |
public int getValue() |
| 117 |
{ |
| 118 |
return value; |
| 119 |
} |
| 120 |
|
| 121 |
/** |
| 122 |
* <!-- begin-user-doc --> |
| 123 |
* <!-- end-user-doc --> |
| 124 |
* @generated |
| 125 |
*/ |
| 126 |
@Override |
| 127 |
public void setValue(int newValue) |
| 128 |
{ |
| 129 |
int oldValue = value; |
| 130 |
value = newValue; |
| 131 |
if (eNotificationRequired()) |
| 132 |
eNotify(new ENotificationImpl(this, Notification.SET, XSDPackage.XSD_LENGTH_FACET__VALUE, oldValue, value)); |
| 133 |
} |
| 134 |
|
| 135 |
/** |
| 136 |
* <!-- begin-user-doc --> |
| 137 |
* <!-- end-user-doc --> |
| 138 |
* @generated |
| 139 |
*/ |
| 140 |
@Override |
| 141 |
public Object eGet(int featureID, boolean resolve, boolean coreType) |
| 142 |
{ |
| 143 |
switch (featureID) |
| 144 |
{ |
| 145 |
case XSDPackage.XSD_LENGTH_FACET__VALUE: |
| 146 |
return getValue(); |
| 147 |
} |
| 148 |
return super.eGet(featureID, resolve, coreType); |
| 149 |
} |
| 150 |
|
| 151 |
/** |
| 152 |
* <!-- begin-user-doc --> |
| 153 |
* <!-- end-user-doc --> |
| 154 |
* @generated |
| 155 |
*/ |
| 156 |
@Override |
| 157 |
public void eSet(int featureID, Object newValue) |
| 158 |
{ |
| 159 |
switch (featureID) |
| 160 |
{ |
| 161 |
case XSDPackage.XSD_LENGTH_FACET__VALUE: |
| 162 |
setValue((Integer)newValue); |
| 163 |
return; |
| 164 |
} |
| 165 |
super.eSet(featureID, newValue); |
| 166 |
} |
| 167 |
|
| 168 |
/** |
| 169 |
* <!-- begin-user-doc --> |
| 170 |
* <!-- end-user-doc --> |
| 171 |
* @generated |
| 172 |
*/ |
| 173 |
@Override |
| 174 |
public void eUnset(int featureID) |
| 175 |
{ |
| 176 |
switch (featureID) |
| 177 |
{ |
| 178 |
case XSDPackage.XSD_LENGTH_FACET__VALUE: |
| 179 |
setValue(VALUE_EDEFAULT); |
| 180 |
return; |
| 181 |
} |
| 182 |
super.eUnset(featureID); |
| 183 |
} |
| 184 |
|
| 185 |
/** |
| 186 |
* <!-- begin-user-doc --> |
| 187 |
* <!-- end-user-doc --> |
| 188 |
* @generated |
| 189 |
*/ |
| 190 |
@Override |
| 191 |
public boolean eIsSet(int featureID) |
| 192 |
{ |
| 193 |
switch (featureID) |
| 194 |
{ |
| 195 |
case XSDPackage.XSD_LENGTH_FACET__VALUE: |
| 196 |
return value != VALUE_EDEFAULT; |
| 197 |
} |
| 198 |
return super.eIsSet(featureID); |
| 199 |
} |
| 200 |
|
| 201 |
/** |
| 202 |
* <!-- begin-user-doc --> |
| 203 |
* <!-- end-user-doc --> |
| 204 |
* @generated |
| 205 |
*/ |
| 206 |
@Override |
| 207 |
public String toString() |
| 208 |
{ |
| 209 |
if (eIsProxy()) return super.toString(); |
| 210 |
|
| 211 |
StringBuilder result = new StringBuilder(super.toString()); |
| 212 |
result.append(" (value: "); |
| 213 |
result.append(value); |
| 214 |
result.append(')'); |
| 215 |
return result.toString(); |
| 216 |
} |
| 217 |
|
| 218 |
@Override |
| 219 |
public Element createElement() |
| 220 |
{ |
| 221 |
Element newElement = createElement(XSDConstants.LENGTH_ELEMENT); |
| 222 |
setElement(newElement); |
| 223 |
return newElement; |
| 224 |
} |
| 225 |
|
| 226 |
@Override |
| 227 |
public void validate() |
| 228 |
{ |
| 229 |
super.validate(); |
| 230 |
|
| 231 |
XSDSimpleTypeDefinition xsdSimpleTypeDefinition = getSimpleTypeDefinition(); |
| 232 |
XSDMinLengthFacet xsdMinLengthFacet = xsdSimpleTypeDefinition.getMinLengthFacet(); |
| 233 |
XSDMaxLengthFacet xsdMaxLengthFacet = xsdSimpleTypeDefinition.getMaxLengthFacet(); |
| 234 |
if (xsdMinLengthFacet != null || xsdMaxLengthFacet != null) |
| 235 |
{ |
| 236 |
XSDDiagnostic xsdDiagnostic = |
| 237 |
reportConstraintViolation |
| 238 |
(XSDConstants.PART2, |
| 239 |
"length-minLength-maxLength", |
| 240 |
getElement(), |
| 241 |
null, |
| 242 |
new Object [] {}); |
| 243 |
if (xsdMinLengthFacet != null) |
| 244 |
{ |
| 245 |
xsdDiagnostic.getComponents().add(xsdMinLengthFacet); |
| 246 |
} |
| 247 |
if (xsdMaxLengthFacet != null) |
| 248 |
{ |
| 249 |
xsdDiagnostic.getComponents().add(xsdMaxLengthFacet); |
| 250 |
} |
| 251 |
} |
| 252 |
} |
| 253 |
|
| 254 |
@Override |
| 255 |
protected void validateRestriction(XSDFixedFacet xsdFixedFacet) |
| 256 |
{ |
| 257 |
if (getValue() != ((XSDLengthFacet)xsdFixedFacet).getValue()) |
| 258 |
{ |
| 259 |
XSDDiagnostic xsdDiagnostic = |
| 260 |
reportConstraintViolation |
| 261 |
(XSDConstants.PART2, |
| 262 |
"length-valid-restriction", |
| 263 |
getElement(), |
| 264 |
XSDConstants.VALUE_ATTRIBUTE, |
| 265 |
new Object [] { getValue(), xsdFixedFacet.getEffectiveValue(), xsdFixedFacet.getSimpleTypeDefinition().getURI() }); |
| 266 |
xsdDiagnostic.getComponents().add(xsdFixedFacet); |
| 267 |
} |
| 268 |
} |
| 269 |
|
| 270 |
@Override |
| 271 |
protected void changeAttribute(EAttribute eAttribute) |
| 272 |
{ |
| 273 |
super.changeAttribute(eAttribute); |
| 274 |
if (eAttribute == XSDPackage.Literals.XSD_FACET__LEXICAL_VALUE) |
| 275 |
{ |
| 276 |
if (getLexicalValue() == null) |
| 277 |
{ |
| 278 |
setValue(0); |
| 279 |
} |
| 280 |
else |
| 281 |
{ |
| 282 |
try |
| 283 |
{ |
| 284 |
int newValue = Integer.parseInt(getLexicalValue()); |
| 285 |
if (newValue != getValue()) |
| 286 |
{ |
| 287 |
setValue(newValue); |
| 288 |
} |
| 289 |
} |
| 290 |
catch (NumberFormatException exception) |
| 291 |
{ |
| 292 |
setValue(0); |
| 293 |
} |
| 294 |
} |
| 295 |
traverseToRootForAnalysis(); |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
@Override |
| 300 |
public boolean isConstraintSatisfied(Object value) |
| 301 |
{ |
| 302 |
if (value instanceof List<?>) |
| 303 |
{ |
| 304 |
return ((List<?>)value).size() == getValue(); |
| 305 |
} |
| 306 |
else if (value instanceof String) |
| 307 |
{ |
| 308 |
return ((String)value).length() == getValue(); |
| 309 |
} |
| 310 |
else if (value instanceof XSDUtil.ByteSequence) |
| 311 |
{ |
| 312 |
return ((XSDUtil.ByteSequence)value).getBytes().length == getValue(); |
| 313 |
} |
| 314 |
else if (value instanceof QName) |
| 315 |
{ |
| 316 |
QName qName = (QName)value; |
| 317 |
int size = qName.getPrefix().length(); |
| 318 |
if (size > 0) ++size; |
| 319 |
size += qName.getLocalPart().length(); |
| 320 |
return size == getValue(); |
| 321 |
} |
| 322 |
else |
| 323 |
{ |
| 324 |
return false; |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
@Override |
| 329 |
public Object getEffectiveValue() |
| 330 |
{ |
| 331 |
return getValue(); |
| 332 |
} |
| 333 |
|
| 334 |
@Override |
| 335 |
public XSDConcreteComponent cloneConcreteComponent(boolean deep, boolean shareDOM) |
| 336 |
{ |
| 337 |
XSDLengthFacetImpl clonedLengthFacet = |
| 338 |
(XSDLengthFacetImpl)getXSDFactory().createXSDLengthFacet(); |
| 339 |
clonedLengthFacet.isReconciling = true; |
| 340 |
|
| 341 |
if (getLexicalValue() != null) |
| 342 |
{ |
| 343 |
clonedLengthFacet.setLexicalValue(getLexicalValue()); |
| 344 |
} |
| 345 |
if (isSetFixed()) |
| 346 |
{ |
| 347 |
clonedLengthFacet.setFixed(isFixed()); |
| 348 |
} |
| 349 |
|
| 350 |
if (deep) |
| 351 |
{ |
| 352 |
if (getAnnotation() != null) |
| 353 |
{ |
| 354 |
clonedLengthFacet.setAnnotation((XSDAnnotation)getAnnotation().cloneConcreteComponent(deep, shareDOM)); |
| 355 |
} |
| 356 |
} |
| 357 |
|
| 358 |
if (shareDOM && getElement() != null) |
| 359 |
{ |
| 360 |
clonedLengthFacet.setElement(getElement()); |
| 361 |
} |
| 362 |
|
| 363 |
clonedLengthFacet.isReconciling = shareDOM; |
| 364 |
return clonedLengthFacet; |
| 365 |
} |
| 366 |
} |