Debian Codegraph @

plugins/org.eclipse.xsd/src/org/eclipse/xsd/impl/XSDFixedFacetImpl.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 org.w3c.dom.Element;
15 import org.w3c.dom.Node;
16
17 import org.eclipse.emf.common.notify.Notification;
18 import org.eclipse.emf.ecore.EAttribute;
19 import org.eclipse.emf.ecore.EClass;
20 import org.eclipse.emf.ecore.impl.ENotificationImpl;
21
22 import org.eclipse.xsd.XSDConstrainingFacet;
23 import org.eclipse.xsd.XSDDiagnostic;
24 import org.eclipse.xsd.XSDFacet;
25 import org.eclipse.xsd.XSDFixedFacet;
26 import org.eclipse.xsd.XSDPackage;
27 import org.eclipse.xsd.XSDSimpleTypeDefinition;
28 import org.eclipse.xsd.util.XSDConstants;
29
30
31 /**
32 * <!-- begin-user-doc -->
33 * An implementation of the model object '<em><b>Fixed Facet</b></em>'.
34 * <!-- end-user-doc -->
35 * <p>
36 * The following features are implemented:
37 * </p>
38 * <ul>
39 * <li>{@link org.eclipse.xsd.impl.XSDFixedFacetImpl#isFixed <em>Fixed</em>}</li>
40 * </ul>
41 *
42 * @generated
43 */
44 public abstract class XSDFixedFacetImpl
45 extends XSDConstrainingFacetImpl
46 implements XSDFixedFacet
47 {
48 /**
49 * The default value of the '{@link #isFixed() <em>Fixed</em>}' attribute.
50 * <!-- begin-user-doc -->
51 * <!-- end-user-doc -->
52 * @see #isFixed()
53 * @generated
54 * @ordered
55 */
56 protected static final boolean FIXED_EDEFAULT = false;
57
58 /**
59 * The flag representing the value of the '{@link #isFixed() <em>Fixed</em>}' attribute.
60 * <!-- begin-user-doc -->
61 * <!-- end-user-doc -->
62 * @see #isFixed()
63 * @generated
64 * @ordered
65 */
66 protected static final int FIXED_EFLAG = 1 << 8;
67
68 /**
69 * The flag representing whether the Fixed attribute has been set.
70 * <!-- begin-user-doc -->
71 * <!-- end-user-doc -->
72 * @generated
73 * @ordered
74 */
75 protected static final int FIXED_ESETFLAG = 1 << 9;
76
77 public static XSDFixedFacet createFixedFacet(Node node)
78 {
79 switch (XSDConstants.nodeType(node))
80 {
81 case XSDConstants.FRACTIONDIGITS_ELEMENT:
82 {
83 return XSDFractionDigitsFacetImpl.createFractionDigitsFacet(node);
84 }
85 case XSDConstants.LENGTH_ELEMENT:
86 {
87 return XSDLengthFacetImpl.createLengthFacet(node);
88 }
89 case XSDConstants.MAXEXCLUSIVE_ELEMENT:
90 {
91 return XSDMaxExclusiveFacetImpl.createMaxExclusiveFacet(node);
92 }
93 case XSDConstants.MAXINCLUSIVE_ELEMENT:
94 {
95 return XSDMaxInclusiveFacetImpl.createMaxInclusiveFacet(node);
96 }
97 case XSDConstants.MAXLENGTH_ELEMENT:
98 {
99 return XSDMaxLengthFacetImpl.createMaxLengthFacet(node);
100 }
101 case XSDConstants.MINEXCLUSIVE_ELEMENT:
102 {
103 return XSDMinExclusiveFacetImpl.createMinExclusiveFacet(node);
104 }
105 case XSDConstants.MININCLUSIVE_ELEMENT:
106 {
107 return XSDMinInclusiveFacetImpl.createMinInclusiveFacet(node);
108 }
109 case XSDConstants.MINLENGTH_ELEMENT:
110 {
111 return XSDMinLengthFacetImpl.createMinLengthFacet(node);
112 }
113 case XSDConstants.TOTALDIGITS_ELEMENT:
114 {
115 return XSDTotalDigitsFacetImpl.createTotalDigitsFacet(node);
116 }
117 case XSDConstants.WHITESPACE_ELEMENT:
118 {
119 return XSDWhiteSpaceFacetImpl.createWhiteSpaceFacet(node);
120 }
121 }
122
123 return null;
124 }
125
126 /**
127 * <!-- begin-user-doc -->
128 * <!-- end-user-doc -->
129 * @generated
130 */
131 protected XSDFixedFacetImpl()
132 {
133 super();
134 }
135
136 /**
137 * <!-- begin-user-doc -->
138 * <!-- end-user-doc -->
139 * @generated
140 */
141 @Override
142 protected EClass eStaticClass()
143 {
144 return XSDPackage.Literals.XSD_FIXED_FACET;
145 }
146
147 /**
148 * <!-- begin-user-doc -->
149 * <!-- end-user-doc -->
150 * @generated
151 */
152 @Override
153 public boolean isFixed()
154 {
155 return (eFlags & FIXED_EFLAG) != 0;
156 }
157
158 /**
159 * <!-- begin-user-doc -->
160 * <!-- end-user-doc -->
161 * @generated
162 */
163 @Override
164 public void setFixed(boolean newFixed)
165 {
166 boolean oldFixed = (eFlags & FIXED_EFLAG) != 0;
167 if (newFixed) eFlags |= FIXED_EFLAG; else eFlags &= ~FIXED_EFLAG;
168 boolean oldFixedESet = (eFlags & FIXED_ESETFLAG) != 0;
169 eFlags |= FIXED_ESETFLAG;
170 if (eNotificationRequired())
171 eNotify(new ENotificationImpl(this, Notification.SET, XSDPackage.XSD_FIXED_FACET__FIXED, oldFixed, newFixed, !oldFixedESet));
172 }
173
174 /**
175 * <!-- begin-user-doc -->
176 * <!-- end-user-doc -->
177 * @generated
178 */
179 @Override
180 public void unsetFixed()
181 {
182 boolean oldFixed = (eFlags & FIXED_EFLAG) != 0;
183 boolean oldFixedESet = (eFlags & FIXED_ESETFLAG) != 0;
184 if (FIXED_EDEFAULT) eFlags |= FIXED_EFLAG; else eFlags &= ~FIXED_EFLAG;
185 eFlags &= ~FIXED_ESETFLAG;
186 if (eNotificationRequired())
187 eNotify(new ENotificationImpl(this, Notification.UNSET, XSDPackage.XSD_FIXED_FACET__FIXED, oldFixed, FIXED_EDEFAULT, oldFixedESet));
188 }
189
190 /**
191 * <!-- begin-user-doc -->
192 * <!-- end-user-doc -->
193 * @generated
194 */
195 @Override
196 public boolean isSetFixed()
197 {
198 return (eFlags & FIXED_ESETFLAG) != 0;
199 }
200
201 /**
202 * <!-- begin-user-doc -->
203 * <!-- end-user-doc -->
204 * @generated
205 */
206 @Override
207 public Object eGet(int featureID, boolean resolve, boolean coreType)
208 {
209 switch (featureID)
210 {
211 case XSDPackage.XSD_FIXED_FACET__FIXED:
212 return isFixed();
213 }
214 return super.eGet(featureID, resolve, coreType);
215 }
216
217 /**
218 * <!-- begin-user-doc -->
219 * <!-- end-user-doc -->
220 * @generated
221 */
222 @Override
223 public void eSet(int featureID, Object newValue)
224 {
225 switch (featureID)
226 {
227 case XSDPackage.XSD_FIXED_FACET__FIXED:
228 setFixed((Boolean)newValue);
229 return;
230 }
231 super.eSet(featureID, newValue);
232 }
233
234 /**
235 * <!-- begin-user-doc -->
236 * <!-- end-user-doc -->
237 * @generated
238 */
239 @Override
240 public void eUnset(int featureID)
241 {
242 switch (featureID)
243 {
244 case XSDPackage.XSD_FIXED_FACET__FIXED:
245 unsetFixed();
246 return;
247 }
248 super.eUnset(featureID);
249 }
250
251 /**
252 * <!-- begin-user-doc -->
253 * <!-- end-user-doc -->
254 * @generated
255 */
256 @Override
257 public boolean eIsSet(int featureID)
258 {
259 switch (featureID)
260 {
261 case XSDPackage.XSD_FIXED_FACET__FIXED:
262 return isSetFixed();
263 }
264 return super.eIsSet(featureID);
265 }
266
267 /**
268 * <!-- begin-user-doc -->
269 * <!-- end-user-doc -->
270 * @generated
271 */
272 @Override
273 public String toString()
274 {
275 if (eIsProxy()) return super.toString();
276
277 StringBuilder result = new StringBuilder(super.toString());
278 result.append(" (fixed: ");
279 if ((eFlags & FIXED_ESETFLAG) != 0) result.append((eFlags & FIXED_EFLAG) != 0); else result.append("<unset>");
280 result.append(')');
281 return result.toString();
282 }
283
284 @Override
285 public void validate()
286 {
287 super.validate();
288
289 Element theElement = getElement();
290
291 checkBuiltInTypeConstraint
292 ("boolean",
293 null,
294 XSDConstants.PART2,
295 "element-" + getFacetName(),
296 theElement,
297 XSDConstants.FIXED_ATTRIBUTE,
298 false);
299
300 checkAttributes
301 (XSDConstants.PART2,
302 "element-" + getFacetName(),
303 theElement,
304 new String []
305 {
306 XSDConstants.FIXED_ATTRIBUTE,
307 XSDConstants.VALUE_ATTRIBUTE,
308 XSDConstants.ID_ATTRIBUTE
309 });
310
311 checkBuiltInTypeConstraint
312 ("ID",
313 null,
314 XSDConstants.PART2,
315 "element-" + getFacetName(),
316 theElement,
317 XSDConstants.ID_ATTRIBUTE,
318 false);
319
320 XSDSimpleTypeDefinition xsdSimpleTypeDefinition = getSimpleTypeDefinition();
321 for (XSDFacet xsdFacet : xsdSimpleTypeDefinition.getFacetContents())
322 {
323 if (xsdFacet == this)
324 {
325 break;
326 }
327 else if (xsdFacet.eClass() == eClass())
328 {
329 XSDDiagnostic xsdDiagnostic =
330 reportConstraintViolation
331 (XSDConstants.PART2,
332 "src-single-facet-value",
333 theElement,
334 XSDConstants.VALUE_ATTRIBUTE,
335 new Object [] { getFacetName() });
336 xsdDiagnostic.getComponents().add(xsdFacet);
337 break;
338 }
339 }
340
341 XSDSimpleTypeDefinition baseTypeDefinition = xsdSimpleTypeDefinition.getBaseTypeDefinition();
342 if (baseTypeDefinition != null)
343 {
344 for (XSDConstrainingFacet xsdConstrainingFacet : baseTypeDefinition.getFacets())
345 {
346 if (xsdConstrainingFacet instanceof XSDFixedFacet)
347 {
348 XSDFixedFacet xsdFixedFacet = (XSDFixedFacet)xsdConstrainingFacet;
349
350 if (eClass() == xsdFixedFacet.eClass())
351 {
352 if (xsdFixedFacet.isFixed() && !baseTypeDefinition.equalValues(getEffectiveValue(), xsdFixedFacet.getEffectiveValue()))
353 {
354 XSDDiagnostic xsdDiagnostic =
355 reportConstraintViolation
356 (XSDConstants.PART2,
357 "facet-fixed-valid-restriction",
358 theElement,
359 XSDConstants.VALUE_ATTRIBUTE,
360 new Object [] { getLexicalValue(), xsdFixedFacet.getLexicalValue(), getFacetName(), baseTypeDefinition.getURI() });
361 xsdDiagnostic.getComponents().add(xsdFixedFacet);
362
363 // The spec has no anchor for this constraint
364 //
365 xsdDiagnostic.setAnnotationURI(XSDConstants.PART2 + "#" + "dc-" + getFacetName());
366
367 break;
368 }
369 }
370
371 if (restrictionMatch(xsdFixedFacet))
372 {
373 validateRestriction(xsdFixedFacet);
374 break;
375 }
376 }
377 }
378 }
379 }
380
381 @Override
382 protected void validateValue()
383 {
384 checkBuiltInTypeConstraint
385 ("nonNegativeInteger",
386 getLexicalValue(),
387 XSDConstants.PART2,
388 "element" + getFacetName(),
389 getElement(),
390 XSDConstants.VALUE_ATTRIBUTE,
391 true);
392 }
393
394 protected boolean restrictionMatch(XSDFixedFacet xsdFixedFacet)
395 {
396 return xsdFixedFacet.eClass() == eClass();
397 }
398
399 protected void validateRestriction(XSDFixedFacet xsdFixedFacet)
400 {
401 // Do nothing.
402 }
403
404 @Override
405 protected void reconcileAttributes(Element changedElement)
406 {
407 super.reconcileAttributes(changedElement);
408
409 if (changedElement == getElement())
410 {
411 if (changedElement.hasAttributeNS(null, XSDConstants.FIXED_ATTRIBUTE))
412 {
413 boolean newFixed = convertToBoolean(changedElement.getAttributeNS(null, XSDConstants.FIXED_ATTRIBUTE));
414 if (!isSetFixed() || newFixed != isFixed())
415 {
416 setFixed(newFixed);
417 }
418 }
419 else
420 {
421 unsetFixed();
422 }
423 }
424 }
425
426 @Override
427 protected void changeAttribute(EAttribute eAttribute)
428 {
429 super.changeAttribute(eAttribute);
430 if (!isReconciling)
431 {
432 if (eAttribute == null || eAttribute == XSDPackage.Literals.XSD_FIXED_FACET__FIXED)
433 {
434 Element theElement = getElement();
435 if (theElement != null)
436 {
437 niceSetAttribute(theElement, XSDConstants.FIXED_ATTRIBUTE, isSetFixed() ? (isFixed() ? "true" : "false") : null);
438 }
439 }
440 }
441 }
442 }