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