Debian Codegraph @

plugins/org.eclipse.xsd/src/org/eclipse/xsd/impl/XSDFractionDigitsFacetImpl.java

raw
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.XSDFractionDigitsFacet;
29 import org.eclipse.xsd.XSDPackage;
30 import org.eclipse.xsd.XSDSimpleTypeDefinition;
31 import org.eclipse.xsd.XSDTotalDigitsFacet;
32 import org.eclipse.xsd.util.XSDConstants;
33
34
35 /**
36 * <!-- begin-user-doc -->
37 * An implementation of the model object '<em><b>Fraction Digits Facet</b></em>'.
38 * <!-- end-user-doc -->
39 * <p>
40 * The following features are implemented:
41 * </p>
42 * <ul>
43 * <li>{@link org.eclipse.xsd.impl.XSDFractionDigitsFacetImpl#getValue <em>Value</em>}</li>
44 * </ul>
45 *
46 * @generated
47 */
48 public class XSDFractionDigitsFacetImpl
49 extends XSDFixedFacetImpl
50 implements XSDFractionDigitsFacet
51 {
52 /**
53 * The default value of the '{@link #getValue() <em>Value</em>}' attribute.
54 * <!-- begin-user-doc -->
55 * <!-- end-user-doc -->
56 * @see #getValue()
57 * @generated
58 * @ordered
59 */
60 protected static final int VALUE_EDEFAULT = 0;
61
62 /**
63 * The cached value of the '{@link #getValue() <em>Value</em>}' attribute.
64 * <!-- begin-user-doc -->
65 * <!-- end-user-doc -->
66 * @see #getValue()
67 * @generated
68 * @ordered
69 */
70 protected int value = VALUE_EDEFAULT;
71
72 public static XSDFractionDigitsFacet createFractionDigitsFacet(Node node)
73 {
74 if (XSDConstants.nodeType(node) == XSDConstants.FRACTIONDIGITS_ELEMENT)
75 {
76 XSDFractionDigitsFacet xsdFractionDigitsFacet = XSDFactory.eINSTANCE.createXSDFractionDigitsFacet();
77 xsdFractionDigitsFacet.setElement((Element)node);
78 return xsdFractionDigitsFacet;
79 }
80
81 return null;
82 }
83
84 /**
85 * <!-- begin-user-doc -->
86 * <!-- end-user-doc -->
87 * @generated
88 */
89 protected XSDFractionDigitsFacetImpl()
90 {
91 super();
92 }
93
94 /**
95 * <!-- begin-user-doc -->
96 * <!-- end-user-doc -->
97 * @generated
98 */
99 @Override
100 protected EClass eStaticClass()
101 {
102 return XSDPackage.Literals.XSD_FRACTION_DIGITS_FACET;
103 }
104
105 /**
106 * <!-- begin-user-doc -->
107 * <!-- end-user-doc -->
108 * @generated
109 */
110 @Override
111 public int getValue()
112 {
113 return value;
114 }
115
116 /**
117 * <!-- begin-user-doc -->
118 * <!-- end-user-doc -->
119 * @generated
120 */
121 @Override
122 public void setValue(int newValue)
123 {
124 int oldValue = value;
125 value = newValue;
126 if (eNotificationRequired())
127 eNotify(new ENotificationImpl(this, Notification.SET, XSDPackage.XSD_FRACTION_DIGITS_FACET__VALUE, oldValue, value));
128 }
129
130 /**
131 * <!-- begin-user-doc -->
132 * <!-- end-user-doc -->
133 * @generated
134 */
135 @Override
136 public Object eGet(int featureID, boolean resolve, boolean coreType)
137 {
138 switch (featureID)
139 {
140 case XSDPackage.XSD_FRACTION_DIGITS_FACET__VALUE:
141 return getValue();
142 }
143 return super.eGet(featureID, resolve, coreType);
144 }
145
146 /**
147 * <!-- begin-user-doc -->
148 * <!-- end-user-doc -->
149 * @generated
150 */
151 @Override
152 public void eSet(int featureID, Object newValue)
153 {
154 switch (featureID)
155 {
156 case XSDPackage.XSD_FRACTION_DIGITS_FACET__VALUE:
157 setValue((Integer)newValue);
158 return;
159 }
160 super.eSet(featureID, newValue);
161 }
162
163 /**
164 * <!-- begin-user-doc -->
165 * <!-- end-user-doc -->
166 * @generated
167 */
168 @Override
169 public void eUnset(int featureID)
170 {
171 switch (featureID)
172 {
173 case XSDPackage.XSD_FRACTION_DIGITS_FACET__VALUE:
174 setValue(VALUE_EDEFAULT);
175 return;
176 }
177 super.eUnset(featureID);
178 }
179
180 /**
181 * <!-- begin-user-doc -->
182 * <!-- end-user-doc -->
183 * @generated
184 */
185 @Override
186 public boolean eIsSet(int featureID)
187 {
188 switch (featureID)
189 {
190 case XSDPackage.XSD_FRACTION_DIGITS_FACET__VALUE:
191 return value != VALUE_EDEFAULT;
192 }
193 return super.eIsSet(featureID);
194 }
195
196 /**
197 * <!-- begin-user-doc -->
198 * <!-- end-user-doc -->
199 * @generated
200 */
201 @Override
202 public String toString()
203 {
204 if (eIsProxy()) return super.toString();
205
206 StringBuilder result = new StringBuilder(super.toString());
207 result.append(" (value: ");
208 result.append(value);
209 result.append(')');
210 return result.toString();
211 }
212
213 @Override
214 public Element createElement()
215 {
216 Element newElement = createElement(XSDConstants.FRACTIONDIGITS_ELEMENT);
217 setElement(newElement);
218 return newElement;
219 }
220
221 @Override
222 public void validate()
223 {
224 super.validate();
225
226 XSDSimpleTypeDefinition xsdSimpleTypeDefinition = getSimpleTypeDefinition();
227 XSDTotalDigitsFacet xsdTotalDigitsFacet = xsdSimpleTypeDefinition.getTotalDigitsFacet();
228 if (xsdTotalDigitsFacet != null && getValue() > xsdTotalDigitsFacet.getValue())
229 {
230 XSDDiagnostic xsdDiagnostic =
231 reportConstraintViolation
232 (XSDConstants.PART2,
233 "fractionDigits-less-than-equal-to-totalDigits",
234 getElement(),
235 XSDConstants.VALUE_ATTRIBUTE,
236 new Object [] { getValue(), xsdTotalDigitsFacet.getValue() });
237 xsdDiagnostic.getComponents().add(xsdTotalDigitsFacet);
238 }
239 }
240
241 @Override
242 protected void validateValue()
243 {
244 checkBuiltInTypeConstraint
245 ("nonNegativeInteger",
246 getLexicalValue(),
247 XSDConstants.PART2,
248 "element-totalDigits",
249 getElement(),
250 XSDConstants.VALUE_ATTRIBUTE,
251 true);
252 }
253
254 @Override
255 protected void validateRestriction(XSDFixedFacet xsdFixedFacet)
256 {
257 if (getValue() > ((XSDFractionDigitsFacet)xsdFixedFacet).getValue())
258 {
259 XSDDiagnostic xsdDiagnostic =
260 reportConstraintViolation
261 (XSDConstants.PART2,
262 "fractionDigits-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 return value instanceof BigDecimal && ((BigDecimal)value).scale() <= getValue();
303 }
304
305 @Override
306 public Object getEffectiveValue()
307 {
308 return getValue();
309 }
310
311 @Override
312 public XSDConcreteComponent cloneConcreteComponent(boolean deep, boolean shareDOM)
313 {
314 XSDFractionDigitsFacetImpl clonedFractionDigitsFacet =
315 (XSDFractionDigitsFacetImpl)getXSDFactory().createXSDFractionDigitsFacet();
316 clonedFractionDigitsFacet.isReconciling = true;
317
318 if (getLexicalValue() != null)
319 {
320 clonedFractionDigitsFacet.setLexicalValue(getLexicalValue());
321 }
322 if (isSetFixed())
323 {
324 clonedFractionDigitsFacet.setFixed(isFixed());
325 }
326
327 if (deep)
328 {
329 if (getAnnotation() != null)
330 {
331 clonedFractionDigitsFacet.setAnnotation((XSDAnnotation)getAnnotation().cloneConcreteComponent(deep, shareDOM));
332 }
333 }
334
335 if (shareDOM && getElement() != null)
336 {
337 clonedFractionDigitsFacet.setElement(getElement());
338 }
339
340 clonedFractionDigitsFacet.isReconciling = shareDOM;
341 return clonedFractionDigitsFacet;
342 }
343 }