| 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.Collection; |
| 15 |
import java.util.Iterator; |
| 16 |
|
| 17 |
import org.eclipse.emf.common.notify.Notification; |
| 18 |
import org.eclipse.emf.ecore.EClass; |
| 19 |
import org.eclipse.emf.ecore.impl.ENotificationImpl; |
| 20 |
|
| 21 |
import org.eclipse.xsd.XSDConcreteComponent; |
| 22 |
import org.eclipse.xsd.XSDDiagnostic; |
| 23 |
import org.eclipse.xsd.XSDFixedFacet; |
| 24 |
import org.eclipse.xsd.XSDMaxExclusiveFacet; |
| 25 |
import org.eclipse.xsd.XSDMaxFacet; |
| 26 |
import org.eclipse.xsd.XSDMinExclusiveFacet; |
| 27 |
import org.eclipse.xsd.XSDMinFacet; |
| 28 |
import org.eclipse.xsd.XSDMinInclusiveFacet; |
| 29 |
import org.eclipse.xsd.XSDPackage; |
| 30 |
import org.eclipse.xsd.XSDSimpleTypeDefinition; |
| 31 |
import org.eclipse.xsd.util.XSDConstants; |
| 32 |
import org.w3c.dom.Element; |
| 33 |
|
| 34 |
|
| 35 |
/** |
| 36 |
* <!-- begin-user-doc --> |
| 37 |
* An implementation of the model object '<em><b>Min 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.XSDMinFacetImpl#getValue <em>Value</em>}</li> |
| 44 |
* <li>{@link org.eclipse.xsd.impl.XSDMinFacetImpl#isInclusive <em>Inclusive</em>}</li> |
| 45 |
* <li>{@link org.eclipse.xsd.impl.XSDMinFacetImpl#isExclusive <em>Exclusive</em>}</li> |
| 46 |
* </ul> |
| 47 |
* |
| 48 |
* @generated |
| 49 |
*/ |
| 50 |
public abstract class XSDMinFacetImpl |
| 51 |
extends XSDFixedFacetImpl |
| 52 |
implements XSDMinFacet |
| 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 Object VALUE_EDEFAULT = null; |
| 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 Object value = VALUE_EDEFAULT; |
| 73 |
|
| 74 |
/** |
| 75 |
* The default value of the '{@link #isInclusive() <em>Inclusive</em>}' attribute. |
| 76 |
* <!-- begin-user-doc --> |
| 77 |
* <!-- end-user-doc --> |
| 78 |
* @see #isInclusive() |
| 79 |
* @generated |
| 80 |
* @ordered |
| 81 |
*/ |
| 82 |
protected static final boolean INCLUSIVE_EDEFAULT = false; |
| 83 |
|
| 84 |
/** |
| 85 |
* The default value of the '{@link #isExclusive() <em>Exclusive</em>}' attribute. |
| 86 |
* <!-- begin-user-doc --> |
| 87 |
* <!-- end-user-doc --> |
| 88 |
* @see #isExclusive() |
| 89 |
* @generated |
| 90 |
* @ordered |
| 91 |
*/ |
| 92 |
protected static final boolean EXCLUSIVE_EDEFAULT = false; |
| 93 |
|
| 94 |
/** |
| 95 |
* <!-- begin-user-doc --> |
| 96 |
* <!-- end-user-doc --> |
| 97 |
* @generated |
| 98 |
*/ |
| 99 |
protected XSDMinFacetImpl() |
| 100 |
{ |
| 101 |
super(); |
| 102 |
} |
| 103 |
|
| 104 |
/** |
| 105 |
* <!-- begin-user-doc --> |
| 106 |
* <!-- end-user-doc --> |
| 107 |
* @generated |
| 108 |
*/ |
| 109 |
@Override |
| 110 |
protected EClass eStaticClass() |
| 111 |
{ |
| 112 |
return XSDPackage.Literals.XSD_MIN_FACET; |
| 113 |
} |
| 114 |
|
| 115 |
/** |
| 116 |
* <!-- begin-user-doc --> |
| 117 |
* <!-- end-user-doc --> |
| 118 |
* @generated |
| 119 |
*/ |
| 120 |
@Override |
| 121 |
public Object getValue() |
| 122 |
{ |
| 123 |
return value; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* <!-- begin-user-doc --> |
| 128 |
* <!-- end-user-doc --> |
| 129 |
* @generated |
| 130 |
*/ |
| 131 |
@Override |
| 132 |
public void setValue(Object newValue) |
| 133 |
{ |
| 134 |
Object oldValue = value; |
| 135 |
value = newValue; |
| 136 |
if (eNotificationRequired()) |
| 137 |
eNotify(new ENotificationImpl(this, Notification.SET, XSDPackage.XSD_MIN_FACET__VALUE, oldValue, value)); |
| 138 |
} |
| 139 |
|
| 140 |
/** |
| 141 |
* <!-- begin-user-doc --> |
| 142 |
* <!-- end-user-doc --> |
| 143 |
* @generated modifiable |
| 144 |
*/ |
| 145 |
public Boolean getInclusive() |
| 146 |
{ |
| 147 |
return isInclusive() ? Boolean.TRUE : Boolean.FALSE; |
| 148 |
} |
| 149 |
|
| 150 |
public boolean isInclusive() |
| 151 |
{ |
| 152 |
return false; |
| 153 |
} |
| 154 |
|
| 155 |
/** |
| 156 |
* <!-- begin-user-doc --> |
| 157 |
* <!-- end-user-doc --> |
| 158 |
* @generated modifiable |
| 159 |
*/ |
| 160 |
public Boolean getExclusive() |
| 161 |
{ |
| 162 |
return isExclusive() ? Boolean.TRUE : Boolean.FALSE; |
| 163 |
} |
| 164 |
|
| 165 |
public boolean isExclusive() |
| 166 |
{ |
| 167 |
return false; |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* <!-- begin-user-doc --> |
| 172 |
* <!-- end-user-doc --> |
| 173 |
* @generated |
| 174 |
*/ |
| 175 |
@Override |
| 176 |
public Object eGet(int featureID, boolean resolve, boolean coreType) |
| 177 |
{ |
| 178 |
switch (featureID) |
| 179 |
{ |
| 180 |
case XSDPackage.XSD_MIN_FACET__VALUE: |
| 181 |
return getValue(); |
| 182 |
case XSDPackage.XSD_MIN_FACET__INCLUSIVE: |
| 183 |
return isInclusive(); |
| 184 |
case XSDPackage.XSD_MIN_FACET__EXCLUSIVE: |
| 185 |
return isExclusive(); |
| 186 |
} |
| 187 |
return super.eGet(featureID, resolve, coreType); |
| 188 |
} |
| 189 |
|
| 190 |
/** |
| 191 |
* <!-- begin-user-doc --> |
| 192 |
* <!-- end-user-doc --> |
| 193 |
* @generated |
| 194 |
*/ |
| 195 |
@Override |
| 196 |
public void eSet(int featureID, Object newValue) |
| 197 |
{ |
| 198 |
switch (featureID) |
| 199 |
{ |
| 200 |
case XSDPackage.XSD_MIN_FACET__VALUE: |
| 201 |
setValue(newValue); |
| 202 |
return; |
| 203 |
} |
| 204 |
super.eSet(featureID, newValue); |
| 205 |
} |
| 206 |
|
| 207 |
/** |
| 208 |
* <!-- begin-user-doc --> |
| 209 |
* <!-- end-user-doc --> |
| 210 |
* @generated |
| 211 |
*/ |
| 212 |
@Override |
| 213 |
public void eUnset(int featureID) |
| 214 |
{ |
| 215 |
switch (featureID) |
| 216 |
{ |
| 217 |
case XSDPackage.XSD_MIN_FACET__VALUE: |
| 218 |
setValue(VALUE_EDEFAULT); |
| 219 |
return; |
| 220 |
} |
| 221 |
super.eUnset(featureID); |
| 222 |
} |
| 223 |
|
| 224 |
/** |
| 225 |
* <!-- begin-user-doc --> |
| 226 |
* <!-- end-user-doc --> |
| 227 |
* @generated |
| 228 |
*/ |
| 229 |
@Override |
| 230 |
public boolean eIsSet(int featureID) |
| 231 |
{ |
| 232 |
switch (featureID) |
| 233 |
{ |
| 234 |
case XSDPackage.XSD_MIN_FACET__VALUE: |
| 235 |
return VALUE_EDEFAULT == null ? value != null : !VALUE_EDEFAULT.equals(value); |
| 236 |
case XSDPackage.XSD_MIN_FACET__INCLUSIVE: |
| 237 |
return isInclusive() != INCLUSIVE_EDEFAULT; |
| 238 |
case XSDPackage.XSD_MIN_FACET__EXCLUSIVE: |
| 239 |
return isExclusive() != EXCLUSIVE_EDEFAULT; |
| 240 |
} |
| 241 |
return super.eIsSet(featureID); |
| 242 |
} |
| 243 |
|
| 244 |
/** |
| 245 |
* <!-- begin-user-doc --> |
| 246 |
* <!-- end-user-doc --> |
| 247 |
* @generated |
| 248 |
*/ |
| 249 |
@Override |
| 250 |
public String toString() |
| 251 |
{ |
| 252 |
if (eIsProxy()) return super.toString(); |
| 253 |
|
| 254 |
StringBuilder result = new StringBuilder(super.toString()); |
| 255 |
result.append(" (value: "); |
| 256 |
result.append(value); |
| 257 |
result.append(')'); |
| 258 |
return result.toString(); |
| 259 |
} |
| 260 |
|
| 261 |
@Override |
| 262 |
public void validate() |
| 263 |
{ |
| 264 |
super.validate(); |
| 265 |
|
| 266 |
XSDSimpleTypeDefinition xsdSimpleTypeDefinition = getSimpleTypeDefinition(); |
| 267 |
XSDMaxFacet xsdMaxFacet = xsdSimpleTypeDefinition.getMaxFacet(); |
| 268 |
|
| 269 |
if (xsdMaxFacet != null) |
| 270 |
{ |
| 271 |
int comparison = xsdSimpleTypeDefinition.compareValues(getValue(), xsdMaxFacet.getValue()); |
| 272 |
if (xsdMaxFacet.isInclusive() == isInclusive() ? comparison > 0 : comparison >= 0) |
| 273 |
{ |
| 274 |
XSDDiagnostic xsdDiagnostic = |
| 275 |
reportConstraintViolation |
| 276 |
(XSDConstants.PART2, |
| 277 |
getFacetName() + (xsdMaxFacet.isInclusive() == isInclusive() ? "-less-than-equal-to-" : "-less-than-") + xsdMaxFacet.getFacetName(), |
| 278 |
getElement(), |
| 279 |
XSDConstants.VALUE_ATTRIBUTE, |
| 280 |
new Object [] { getLexicalValue(), xsdMaxFacet.getLexicalValue() }); |
| 281 |
xsdDiagnostic.getComponents().add(xsdMaxFacet); |
| 282 |
} |
| 283 |
} |
| 284 |
|
| 285 |
if (isExclusive()) |
| 286 |
{ |
| 287 |
XSDMinInclusiveFacet xsdMinInclusiveFacet = xsdSimpleTypeDefinition.getMinInclusiveFacet(); |
| 288 |
if (xsdMinInclusiveFacet != null) |
| 289 |
{ |
| 290 |
XSDDiagnostic xsdDiagnostic = |
| 291 |
reportConstraintViolation |
| 292 |
(XSDConstants.PART2, |
| 293 |
"minInclusive-minExclusive", |
| 294 |
getElement(), |
| 295 |
XSDConstants.VALUE_ATTRIBUTE, |
| 296 |
new Object [] { }); |
| 297 |
xsdDiagnostic.getComponents().add(xsdMinInclusiveFacet); |
| 298 |
} |
| 299 |
} |
| 300 |
} |
| 301 |
|
| 302 |
@Override |
| 303 |
protected void validateValue() |
| 304 |
{ |
| 305 |
XSDSimpleTypeDefinition xsdSimpleTypeDefinition = (XSDSimpleTypeDefinition)getContainer(); |
| 306 |
XSDSimpleTypeDefinition baseTypeDefinition = xsdSimpleTypeDefinition.getBaseTypeDefinition(); |
| 307 |
if (baseTypeDefinition != null) |
| 308 |
{ |
| 309 |
Element theElement = getElement(); |
| 310 |
if (getLexicalValue() == null) |
| 311 |
{ |
| 312 |
createRequiredAttributeDiagnostic(XSDConstants.PART2, "element-" + getFacetName(), theElement, XSDConstants.VALUE_ATTRIBUTE); |
| 313 |
} |
| 314 |
else |
| 315 |
{ |
| 316 |
XSDSimpleTypeDefinitionImpl.AssessmentImpl assessment = |
| 317 |
(XSDSimpleTypeDefinitionImpl.AssessmentImpl)baseTypeDefinition.assess(theElement, getLexicalValue()); |
| 318 |
|
| 319 |
Collection<XSDDiagnostic> allDiagnostics = assessment.getDiagnostics(); |
| 320 |
if (!allDiagnostics.isEmpty()) |
| 321 |
{ |
| 322 |
// This is to ignores exclusive violations which should be caught via restriction validation. |
| 323 |
// |
| 324 |
if (isExclusive()) |
| 325 |
{ |
| 326 |
for (Iterator<XSDDiagnostic> i = allDiagnostics.iterator(); i.hasNext(); ) |
| 327 |
{ |
| 328 |
XSDDiagnostic xsdDiagnostic = i.next(); |
| 329 |
XSDConcreteComponent primaryComponent = xsdDiagnostic.getPrimaryComponent(); |
| 330 |
if (primaryComponent instanceof XSDMinExclusiveFacet && |
| 331 |
baseTypeDefinition.equalLiterals |
| 332 |
(theElement, getLexicalValue(), theElement, ((XSDMinExclusiveFacet)primaryComponent).getLexicalValue())) |
| 333 |
{ |
| 334 |
i.remove(); |
| 335 |
} |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
assessment.assignDiagnostics(this, theElement, XSDConstants.VALUE_ATTRIBUTE); |
| 340 |
getDiagnostics().addAll(allDiagnostics); |
| 341 |
} |
| 342 |
} |
| 343 |
} |
| 344 |
} |
| 345 |
|
| 346 |
@Override |
| 347 |
protected boolean restrictionMatch(XSDFixedFacet xsdFixedFacet) |
| 348 |
{ |
| 349 |
return xsdFixedFacet instanceof XSDMaxExclusiveFacet || xsdFixedFacet instanceof XSDMinExclusiveFacet; |
| 350 |
} |
| 351 |
|
| 352 |
@Override |
| 353 |
protected void validateRestriction(XSDFixedFacet xsdFixedFacet) |
| 354 |
{ |
| 355 |
XSDSimpleTypeDefinition xsdSimpleTypeDefinition = getSimpleTypeDefinition(); |
| 356 |
int comparison = xsdSimpleTypeDefinition.compareValues(getValue(), xsdFixedFacet.getEffectiveValue()); |
| 357 |
if (xsdFixedFacet instanceof XSDMinExclusiveFacet ? comparison < 0 : comparison >= 0) |
| 358 |
{ |
| 359 |
XSDDiagnostic xsdDiagnostic = |
| 360 |
reportConstraintViolation |
| 361 |
(XSDConstants.PART2, |
| 362 |
getFacetName() + (xsdFixedFacet instanceof XSDMinExclusiveFacet ? "-valid-restriction.2" : "-valid-restriction.4"), |
| 363 |
getElement(), |
| 364 |
XSDConstants.VALUE_ATTRIBUTE, |
| 365 |
new Object [] { getLexicalValue(), xsdFixedFacet.getLexicalValue(), xsdFixedFacet.getSimpleTypeDefinition().getURI() }); |
| 366 |
xsdDiagnostic.getComponents().add(xsdFixedFacet); |
| 367 |
} |
| 368 |
} |
| 369 |
} |