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