| 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 |
|
| 16 |
import org.w3c.dom.Element; |
| 17 |
import org.w3c.dom.Node; |
| 18 |
|
| 19 |
import org.eclipse.emf.common.util.EList; |
| 20 |
import org.eclipse.emf.ecore.EAttribute; |
| 21 |
import org.eclipse.emf.ecore.EClass; |
| 22 |
import org.eclipse.emf.ecore.util.EDataTypeUniqueEList; |
| 23 |
|
| 24 |
import org.eclipse.xsd.XSDAnnotation; |
| 25 |
import org.eclipse.xsd.XSDConcreteComponent; |
| 26 |
import org.eclipse.xsd.XSDEnumerationFacet; |
| 27 |
import org.eclipse.xsd.XSDFactory; |
| 28 |
import org.eclipse.xsd.XSDPackage; |
| 29 |
import org.eclipse.xsd.XSDSimpleTypeDefinition; |
| 30 |
import org.eclipse.xsd.util.XSDConstants; |
| 31 |
|
| 32 |
|
| 33 |
/** |
| 34 |
* <!-- begin-user-doc --> |
| 35 |
* An implementation of the model object '<em><b>Enumeration 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.XSDEnumerationFacetImpl#getValue <em>Value</em>}</li> |
| 42 |
* </ul> |
| 43 |
* |
| 44 |
* @generated |
| 45 |
*/ |
| 46 |
public class XSDEnumerationFacetImpl |
| 47 |
extends XSDRepeatableFacetImpl |
| 48 |
implements XSDEnumerationFacet |
| 49 |
{ |
| 50 |
/** |
| 51 |
* The cached value of the '{@link #getValue() <em>Value</em>}' attribute list. |
| 52 |
* <!-- begin-user-doc --> |
| 53 |
* <!-- end-user-doc --> |
| 54 |
* @see #getValue() |
| 55 |
* @generated |
| 56 |
* @ordered |
| 57 |
*/ |
| 58 |
protected EList<Object> value; |
| 59 |
|
| 60 |
public static XSDEnumerationFacet createEnumerationFacet(Node node) |
| 61 |
{ |
| 62 |
if (XSDConstants.nodeType(node) == XSDConstants.ENUMERATION_ELEMENT) |
| 63 |
{ |
| 64 |
XSDEnumerationFacet xsdEnumerationFacet = XSDFactory.eINSTANCE.createXSDEnumerationFacet(); |
| 65 |
xsdEnumerationFacet.setElement((Element)node); |
| 66 |
return xsdEnumerationFacet; |
| 67 |
} |
| 68 |
|
| 69 |
return null; |
| 70 |
} |
| 71 |
|
| 72 |
/** |
| 73 |
* <!-- begin-user-doc --> |
| 74 |
* <!-- end-user-doc --> |
| 75 |
* @generated |
| 76 |
*/ |
| 77 |
protected XSDEnumerationFacetImpl() |
| 78 |
{ |
| 79 |
super(); |
| 80 |
} |
| 81 |
|
| 82 |
/** |
| 83 |
* <!-- begin-user-doc --> |
| 84 |
* <!-- end-user-doc --> |
| 85 |
* @generated |
| 86 |
*/ |
| 87 |
@Override |
| 88 |
protected EClass eStaticClass() |
| 89 |
{ |
| 90 |
return XSDPackage.Literals.XSD_ENUMERATION_FACET; |
| 91 |
} |
| 92 |
|
| 93 |
/** |
| 94 |
* <!-- begin-user-doc --> |
| 95 |
* <!-- end-user-doc --> |
| 96 |
* @generated |
| 97 |
*/ |
| 98 |
@Override |
| 99 |
public EList<Object> getValue() |
| 100 |
{ |
| 101 |
if (value == null) |
| 102 |
{ |
| 103 |
value = new EDataTypeUniqueEList<Object>(Object.class, this, XSDPackage.XSD_ENUMERATION_FACET__VALUE); |
| 104 |
} |
| 105 |
return value; |
| 106 |
} |
| 107 |
|
| 108 |
/** |
| 109 |
* <!-- begin-user-doc --> |
| 110 |
* <!-- end-user-doc --> |
| 111 |
* @generated |
| 112 |
*/ |
| 113 |
@Override |
| 114 |
public Object eGet(int featureID, boolean resolve, boolean coreType) |
| 115 |
{ |
| 116 |
switch (featureID) |
| 117 |
{ |
| 118 |
case XSDPackage.XSD_ENUMERATION_FACET__VALUE: |
| 119 |
return getValue(); |
| 120 |
} |
| 121 |
return super.eGet(featureID, resolve, coreType); |
| 122 |
} |
| 123 |
|
| 124 |
/** |
| 125 |
* <!-- begin-user-doc --> |
| 126 |
* <!-- end-user-doc --> |
| 127 |
* @generated |
| 128 |
*/ |
| 129 |
@SuppressWarnings("unchecked") |
| 130 |
@Override |
| 131 |
public void eSet(int featureID, Object newValue) |
| 132 |
{ |
| 133 |
switch (featureID) |
| 134 |
{ |
| 135 |
case XSDPackage.XSD_ENUMERATION_FACET__VALUE: |
| 136 |
getValue().clear(); |
| 137 |
getValue().addAll((Collection<? extends Object>)newValue); |
| 138 |
return; |
| 139 |
} |
| 140 |
super.eSet(featureID, newValue); |
| 141 |
} |
| 142 |
|
| 143 |
/** |
| 144 |
* <!-- begin-user-doc --> |
| 145 |
* <!-- end-user-doc --> |
| 146 |
* @generated |
| 147 |
*/ |
| 148 |
@Override |
| 149 |
public void eUnset(int featureID) |
| 150 |
{ |
| 151 |
switch (featureID) |
| 152 |
{ |
| 153 |
case XSDPackage.XSD_ENUMERATION_FACET__VALUE: |
| 154 |
getValue().clear(); |
| 155 |
return; |
| 156 |
} |
| 157 |
super.eUnset(featureID); |
| 158 |
} |
| 159 |
|
| 160 |
/** |
| 161 |
* <!-- begin-user-doc --> |
| 162 |
* <!-- end-user-doc --> |
| 163 |
* @generated |
| 164 |
*/ |
| 165 |
@Override |
| 166 |
public boolean eIsSet(int featureID) |
| 167 |
{ |
| 168 |
switch (featureID) |
| 169 |
{ |
| 170 |
case XSDPackage.XSD_ENUMERATION_FACET__VALUE: |
| 171 |
return value != null && !value.isEmpty(); |
| 172 |
} |
| 173 |
return super.eIsSet(featureID); |
| 174 |
} |
| 175 |
|
| 176 |
/** |
| 177 |
* <!-- begin-user-doc --> |
| 178 |
* <!-- end-user-doc --> |
| 179 |
* @generated |
| 180 |
*/ |
| 181 |
@Override |
| 182 |
public String toString() |
| 183 |
{ |
| 184 |
if (eIsProxy()) return super.toString(); |
| 185 |
|
| 186 |
StringBuilder result = new StringBuilder(super.toString()); |
| 187 |
result.append(" (value: "); |
| 188 |
result.append(value); |
| 189 |
result.append(')'); |
| 190 |
return result.toString(); |
| 191 |
} |
| 192 |
|
| 193 |
@Override |
| 194 |
public Element createElement() |
| 195 |
{ |
| 196 |
Element newElement = createElement(XSDConstants.ENUMERATION_ELEMENT); |
| 197 |
setElement(newElement); |
| 198 |
return newElement; |
| 199 |
} |
| 200 |
|
| 201 |
@Override |
| 202 |
public void validateValue() |
| 203 |
{ |
| 204 |
XSDSimpleTypeDefinition xsdSimpleTypeDefinition = (XSDSimpleTypeDefinition)getContainer(); |
| 205 |
XSDSimpleTypeDefinition baseTypeDefinition = xsdSimpleTypeDefinition.getBaseTypeDefinition(); |
| 206 |
if (baseTypeDefinition != null) |
| 207 |
{ |
| 208 |
XSDSimpleTypeDefinitionImpl.AssessmentImpl assessment = |
| 209 |
(XSDSimpleTypeDefinitionImpl.AssessmentImpl)baseTypeDefinition.assess(getElement(), getLexicalValue()); |
| 210 |
if (!assessment.getDiagnostics().isEmpty()) |
| 211 |
{ |
| 212 |
assessment.assignDiagnostics(this, getElement(), XSDConstants.VALUE_ATTRIBUTE); |
| 213 |
getDiagnostics().addAll(assessment.getDiagnostics()); |
| 214 |
} |
| 215 |
else if (assessment.getValue() == null) |
| 216 |
{ |
| 217 |
createRequiredAttributeDiagnostic(XSDConstants.PART2, "element-enumeration", getElement(), XSDConstants.VALUE_ATTRIBUTE); |
| 218 |
} |
| 219 |
} |
| 220 |
} |
| 221 |
|
| 222 |
@Override |
| 223 |
protected boolean analyze() |
| 224 |
{ |
| 225 |
super.analyze(); |
| 226 |
String theLexicalValue = getLexicalValue(); |
| 227 |
Object newValue = null; |
| 228 |
if (theLexicalValue != null) |
| 229 |
{ |
| 230 |
XSDSimpleTypeDefinition xsdSimpleTypeDefinition = (XSDSimpleTypeDefinition)getContainer(); |
| 231 |
if (xsdSimpleTypeDefinition != null) |
| 232 |
{ |
| 233 |
XSDSimpleTypeDefinition baseTypeDefinition = xsdSimpleTypeDefinition.getBaseTypeDefinition(); |
| 234 |
if (baseTypeDefinition != null) |
| 235 |
{ |
| 236 |
try |
| 237 |
{ |
| 238 |
newValue = baseTypeDefinition.getValue(getElement(), theLexicalValue); |
| 239 |
} |
| 240 |
catch (RuntimeException exception) |
| 241 |
{ |
| 242 |
// Ignore. |
| 243 |
} |
| 244 |
} |
| 245 |
} |
| 246 |
} |
| 247 |
if (!getValue().contains(newValue)) |
| 248 |
{ |
| 249 |
getValue().clear(); |
| 250 |
if (newValue != null) |
| 251 |
{ |
| 252 |
getValue().add(newValue); |
| 253 |
} |
| 254 |
} |
| 255 |
|
| 256 |
return true; |
| 257 |
} |
| 258 |
|
| 259 |
@Override |
| 260 |
protected void changeAttribute(EAttribute eAttribute) |
| 261 |
{ |
| 262 |
super.changeAttribute(eAttribute); |
| 263 |
if (eAttribute == XSDPackage.Literals.XSD_FACET__LEXICAL_VALUE) |
| 264 |
{ |
| 265 |
traverseToRootForAnalysis(); |
| 266 |
} |
| 267 |
} |
| 268 |
|
| 269 |
@Override |
| 270 |
public boolean isConstraintSatisfied(Object value) |
| 271 |
{ |
| 272 |
EList<Object> values = getValue(); |
| 273 |
XSDSimpleTypeDefinition xsdSimpleTypeDefinition = getSimpleTypeDefinition(); |
| 274 |
for (int i = 0, size = values.size(); i < size; ++i) |
| 275 |
{ |
| 276 |
Object object = values.get(i); |
| 277 |
if (xsdSimpleTypeDefinition.equalValues(object, value)) |
| 278 |
{ |
| 279 |
return true; |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
return false; |
| 284 |
} |
| 285 |
|
| 286 |
@Override |
| 287 |
public Object getEffectiveValue() |
| 288 |
{ |
| 289 |
return getValue(); |
| 290 |
} |
| 291 |
|
| 292 |
@Override |
| 293 |
public XSDConcreteComponent cloneConcreteComponent(boolean deep, boolean shareDOM) |
| 294 |
{ |
| 295 |
XSDEnumerationFacetImpl clonedEnumerationFacet = |
| 296 |
(XSDEnumerationFacetImpl)getXSDFactory().createXSDEnumerationFacet(); |
| 297 |
clonedEnumerationFacet.isReconciling = true; |
| 298 |
|
| 299 |
if (getLexicalValue() != null) |
| 300 |
{ |
| 301 |
clonedEnumerationFacet.setLexicalValue(getLexicalValue()); |
| 302 |
} |
| 303 |
|
| 304 |
if (deep) |
| 305 |
{ |
| 306 |
if (getAnnotation() != null) |
| 307 |
{ |
| 308 |
clonedEnumerationFacet.setAnnotation((XSDAnnotation)getAnnotation().cloneConcreteComponent(deep, shareDOM)); |
| 309 |
} |
| 310 |
} |
| 311 |
|
| 312 |
if (shareDOM && getElement() != null) |
| 313 |
{ |
| 314 |
clonedEnumerationFacet.setElement(getElement()); |
| 315 |
} |
| 316 |
|
| 317 |
clonedEnumerationFacet.isReconciling = shareDOM; |
| 318 |
return clonedEnumerationFacet; |
| 319 |
} |
| 320 |
} |