| 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.XSDAnnotation; |
| 23 |
import org.eclipse.xsd.XSDAttributeUse; |
| 24 |
import org.eclipse.xsd.XSDComplexTypeDefinition; |
| 25 |
import org.eclipse.xsd.XSDConcreteComponent; |
| 26 |
import org.eclipse.xsd.XSDDiagnostic; |
| 27 |
import org.eclipse.xsd.XSDFactory; |
| 28 |
import org.eclipse.xsd.XSDFixedFacet; |
| 29 |
import org.eclipse.xsd.XSDPackage; |
| 30 |
import org.eclipse.xsd.XSDSimpleTypeDefinition; |
| 31 |
import org.eclipse.xsd.XSDWhiteSpace; |
| 32 |
import org.eclipse.xsd.XSDWhiteSpaceFacet; |
| 33 |
import org.eclipse.xsd.util.XSDConstants; |
| 34 |
|
| 35 |
|
| 36 |
/** |
| 37 |
* <!-- begin-user-doc --> |
| 38 |
* An implementation of the model object '<em><b>White Space Facet</b></em>'. |
| 39 |
* <!-- end-user-doc --> |
| 40 |
* <p> |
| 41 |
* The following features are implemented: |
| 42 |
* </p> |
| 43 |
* <ul> |
| 44 |
* <li>{@link org.eclipse.xsd.impl.XSDWhiteSpaceFacetImpl#getValue <em>Value</em>}</li> |
| 45 |
* </ul> |
| 46 |
* |
| 47 |
* @generated |
| 48 |
*/ |
| 49 |
public class XSDWhiteSpaceFacetImpl |
| 50 |
extends XSDFixedFacetImpl |
| 51 |
implements XSDWhiteSpaceFacet |
| 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 XSDWhiteSpace VALUE_EDEFAULT = XSDWhiteSpace.PRESERVE_LITERAL; |
| 62 |
|
| 63 |
/** |
| 64 |
* The offset of the flags representing the value of the '{@link #getValue() <em>Value</em>}' attribute. |
| 65 |
* <!-- begin-user-doc --> |
| 66 |
* <!-- end-user-doc --> |
| 67 |
* @generated |
| 68 |
* @ordered |
| 69 |
*/ |
| 70 |
protected static final int VALUE_EFLAG_OFFSET = 10; |
| 71 |
|
| 72 |
/** |
| 73 |
* The flags representing the default value of the '{@link #getValue() <em>Value</em>}' attribute. |
| 74 |
* <!-- begin-user-doc --> |
| 75 |
* <!-- end-user-doc --> |
| 76 |
* @generated |
| 77 |
* @ordered |
| 78 |
*/ |
| 79 |
protected static final int VALUE_EFLAG_DEFAULT = VALUE_EDEFAULT.ordinal() << VALUE_EFLAG_OFFSET; |
| 80 |
|
| 81 |
/** |
| 82 |
* The array of enumeration values for '{@link XSDWhiteSpace White Space}' |
| 83 |
* <!-- begin-user-doc --> |
| 84 |
* <!-- end-user-doc --> |
| 85 |
* @generated |
| 86 |
* @ordered |
| 87 |
*/ |
| 88 |
private static final XSDWhiteSpace[] VALUE_EFLAG_VALUES = XSDWhiteSpace.values(); |
| 89 |
|
| 90 |
/** |
| 91 |
* The flags representing the value of the '{@link #getValue() <em>Value</em>}' attribute. |
| 92 |
* <!-- begin-user-doc --> |
| 93 |
* <!-- end-user-doc --> |
| 94 |
* @see #getValue() |
| 95 |
* @generated |
| 96 |
* @ordered |
| 97 |
*/ |
| 98 |
protected static final int VALUE_EFLAG = 0x3 << VALUE_EFLAG_OFFSET; |
| 99 |
|
| 100 |
public static XSDWhiteSpaceFacet createWhiteSpaceFacet(Node node) |
| 101 |
{ |
| 102 |
if (XSDConstants.nodeType(node) == XSDConstants.WHITESPACE_ELEMENT) |
| 103 |
{ |
| 104 |
XSDWhiteSpaceFacet xsdWhiteSpaceFacet = XSDFactory.eINSTANCE.createXSDWhiteSpaceFacet(); |
| 105 |
xsdWhiteSpaceFacet.setElement((Element)node); |
| 106 |
return xsdWhiteSpaceFacet; |
| 107 |
} |
| 108 |
|
| 109 |
return null; |
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* <!-- begin-user-doc --> |
| 114 |
* <!-- end-user-doc --> |
| 115 |
* @generated |
| 116 |
*/ |
| 117 |
protected XSDWhiteSpaceFacetImpl() |
| 118 |
{ |
| 119 |
super(); |
| 120 |
} |
| 121 |
|
| 122 |
/** |
| 123 |
* <!-- begin-user-doc --> |
| 124 |
* <!-- end-user-doc --> |
| 125 |
* @generated |
| 126 |
*/ |
| 127 |
@Override |
| 128 |
protected EClass eStaticClass() |
| 129 |
{ |
| 130 |
return XSDPackage.Literals.XSD_WHITE_SPACE_FACET; |
| 131 |
} |
| 132 |
|
| 133 |
/** |
| 134 |
* <!-- begin-user-doc --> |
| 135 |
* <!-- end-user-doc --> |
| 136 |
* @generated |
| 137 |
*/ |
| 138 |
@Override |
| 139 |
public XSDWhiteSpace getValue() |
| 140 |
{ |
| 141 |
return VALUE_EFLAG_VALUES[(eFlags & VALUE_EFLAG) >>> VALUE_EFLAG_OFFSET]; |
| 142 |
} |
| 143 |
|
| 144 |
/** |
| 145 |
* <!-- begin-user-doc --> |
| 146 |
* <!-- end-user-doc --> |
| 147 |
* @generated |
| 148 |
*/ |
| 149 |
@Override |
| 150 |
public void setValue(XSDWhiteSpace newValue) |
| 151 |
{ |
| 152 |
XSDWhiteSpace oldValue = VALUE_EFLAG_VALUES[(eFlags & VALUE_EFLAG) >>> VALUE_EFLAG_OFFSET]; |
| 153 |
if (newValue == null) newValue = VALUE_EDEFAULT; |
| 154 |
eFlags = eFlags & ~VALUE_EFLAG | newValue.ordinal() << VALUE_EFLAG_OFFSET; |
| 155 |
if (eNotificationRequired()) |
| 156 |
eNotify(new ENotificationImpl(this, Notification.SET, XSDPackage.XSD_WHITE_SPACE_FACET__VALUE, oldValue, newValue)); |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* <!-- begin-user-doc --> |
| 161 |
* <!-- end-user-doc --> |
| 162 |
* @generated |
| 163 |
*/ |
| 164 |
@Override |
| 165 |
public Object eGet(int featureID, boolean resolve, boolean coreType) |
| 166 |
{ |
| 167 |
switch (featureID) |
| 168 |
{ |
| 169 |
case XSDPackage.XSD_WHITE_SPACE_FACET__VALUE: |
| 170 |
return getValue(); |
| 171 |
} |
| 172 |
return super.eGet(featureID, resolve, coreType); |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* <!-- begin-user-doc --> |
| 177 |
* <!-- end-user-doc --> |
| 178 |
* @generated |
| 179 |
*/ |
| 180 |
@Override |
| 181 |
public void eSet(int featureID, Object newValue) |
| 182 |
{ |
| 183 |
switch (featureID) |
| 184 |
{ |
| 185 |
case XSDPackage.XSD_WHITE_SPACE_FACET__VALUE: |
| 186 |
setValue((XSDWhiteSpace)newValue); |
| 187 |
return; |
| 188 |
} |
| 189 |
super.eSet(featureID, newValue); |
| 190 |
} |
| 191 |
|
| 192 |
/** |
| 193 |
* <!-- begin-user-doc --> |
| 194 |
* <!-- end-user-doc --> |
| 195 |
* @generated |
| 196 |
*/ |
| 197 |
@Override |
| 198 |
public void eUnset(int featureID) |
| 199 |
{ |
| 200 |
switch (featureID) |
| 201 |
{ |
| 202 |
case XSDPackage.XSD_WHITE_SPACE_FACET__VALUE: |
| 203 |
setValue(VALUE_EDEFAULT); |
| 204 |
return; |
| 205 |
} |
| 206 |
super.eUnset(featureID); |
| 207 |
} |
| 208 |
|
| 209 |
/** |
| 210 |
* <!-- begin-user-doc --> |
| 211 |
* <!-- end-user-doc --> |
| 212 |
* @generated |
| 213 |
*/ |
| 214 |
@Override |
| 215 |
public boolean eIsSet(int featureID) |
| 216 |
{ |
| 217 |
switch (featureID) |
| 218 |
{ |
| 219 |
case XSDPackage.XSD_WHITE_SPACE_FACET__VALUE: |
| 220 |
return (eFlags & VALUE_EFLAG) != VALUE_EFLAG_DEFAULT; |
| 221 |
} |
| 222 |
return super.eIsSet(featureID); |
| 223 |
} |
| 224 |
|
| 225 |
/** |
| 226 |
* <!-- begin-user-doc --> |
| 227 |
* <!-- end-user-doc --> |
| 228 |
* @generated |
| 229 |
*/ |
| 230 |
@Override |
| 231 |
public String toString() |
| 232 |
{ |
| 233 |
if (eIsProxy()) return super.toString(); |
| 234 |
|
| 235 |
StringBuilder result = new StringBuilder(super.toString()); |
| 236 |
result.append(" (value: "); |
| 237 |
result.append(VALUE_EFLAG_VALUES[(eFlags & VALUE_EFLAG) >>> VALUE_EFLAG_OFFSET]); |
| 238 |
result.append(')'); |
| 239 |
return result.toString(); |
| 240 |
} |
| 241 |
|
| 242 |
@Override |
| 243 |
public Element createElement() |
| 244 |
{ |
| 245 |
Element newElement = createElement(XSDConstants.WHITESPACE_ELEMENT); |
| 246 |
setElement(newElement); |
| 247 |
return newElement; |
| 248 |
} |
| 249 |
|
| 250 |
@Override |
| 251 |
protected void validateRestriction(XSDFixedFacet xsdFixedFacet) |
| 252 |
{ |
| 253 |
if (getValue().getValue() < ((XSDWhiteSpaceFacet)xsdFixedFacet).getValue().getValue()) |
| 254 |
{ |
| 255 |
XSDDiagnostic xsdDiagnostic = |
| 256 |
reportConstraintViolation |
| 257 |
(XSDConstants.PART2, |
| 258 |
"whiteSpace-valid-restriction", |
| 259 |
getElement(), |
| 260 |
XSDConstants.VALUE_ATTRIBUTE, |
| 261 |
new Object [] |
| 262 |
{ |
| 263 |
getLexicalValue(), |
| 264 |
xsdFixedFacet.getLexicalValue(), |
| 265 |
xsdFixedFacet.getSimpleTypeDefinition().getURI() |
| 266 |
}); |
| 267 |
xsdDiagnostic.getComponents().add(xsdFixedFacet); |
| 268 |
} |
| 269 |
} |
| 270 |
|
| 271 |
@Override |
| 272 |
protected void validateValue() |
| 273 |
{ |
| 274 |
XSDSimpleTypeDefinition whiteSpaceEnumeration = |
| 275 |
((XSDAttributeUse) |
| 276 |
((XSDComplexTypeDefinition) |
| 277 |
getSimpleTypeDefinition(). |
| 278 |
getSchema(). |
| 279 |
getSchemaForSchema(). |
| 280 |
resolveElementDeclaration("whiteSpace"). |
| 281 |
getTypeDefinition()). |
| 282 |
getAttributeContents().get(0)). |
| 283 |
getAttributeDeclaration().getTypeDefinition(); |
| 284 |
|
| 285 |
checkSimpleTypeConstraint |
| 286 |
(whiteSpaceEnumeration, |
| 287 |
getLexicalValue(), |
| 288 |
XSDConstants.PART2, |
| 289 |
"element-whiteSpace", |
| 290 |
getElement(), |
| 291 |
XSDConstants.VALUE_ATTRIBUTE, |
| 292 |
true); |
| 293 |
} |
| 294 |
|
| 295 |
@Override |
| 296 |
protected void changeAttribute(EAttribute eAttribute) |
| 297 |
{ |
| 298 |
super.changeAttribute(eAttribute); |
| 299 |
if (eAttribute == XSDPackage.Literals.XSD_FACET__LEXICAL_VALUE) |
| 300 |
{ |
| 301 |
String newValue = getLexicalValue(); |
| 302 |
XSDWhiteSpace literal = XSDWhiteSpace.get(newValue); |
| 303 |
if (literal == null) |
| 304 |
{ |
| 305 |
if (getValue() != XSDWhiteSpace.PRESERVE_LITERAL) |
| 306 |
{ |
| 307 |
setValue(XSDWhiteSpace.PRESERVE_LITERAL); |
| 308 |
} |
| 309 |
} |
| 310 |
else if (literal != getValue()) |
| 311 |
{ |
| 312 |
setValue(literal); |
| 313 |
} |
| 314 |
} |
| 315 |
} |
| 316 |
public String getNormalizedLiteral(String literal) |
| 317 |
{ |
| 318 |
switch (getValue().getValue()) |
| 319 |
{ |
| 320 |
case XSDWhiteSpace.COLLAPSE: |
| 321 |
{ |
| 322 |
StringBuffer stringBuffer = new StringBuffer(); |
| 323 |
boolean holdWhitespace = false; |
| 324 |
for (int i = 0, length = literal.length(); i < length; ++i) |
| 325 |
{ |
| 326 |
char character = literal.charAt(i); |
| 327 |
if (Character.isWhitespace(character)) |
| 328 |
{ |
| 329 |
holdWhitespace = true; |
| 330 |
} |
| 331 |
else |
| 332 |
{ |
| 333 |
if (stringBuffer.length() == 0) |
| 334 |
{ |
| 335 |
holdWhitespace = false; |
| 336 |
} |
| 337 |
if (holdWhitespace) |
| 338 |
{ |
| 339 |
holdWhitespace = false; |
| 340 |
stringBuffer.append(" "); |
| 341 |
} |
| 342 |
stringBuffer.append(character); |
| 343 |
} |
| 344 |
} |
| 345 |
return stringBuffer.toString(); |
| 346 |
} |
| 347 |
case XSDWhiteSpace.REPLACE: |
| 348 |
{ |
| 349 |
StringBuffer stringBuffer = new StringBuffer(literal); |
| 350 |
for (int i = 0, length = literal.length(); i < length; ++i) |
| 351 |
{ |
| 352 |
char character = literal.charAt(i); |
| 353 |
if (Character.isWhitespace(character)) |
| 354 |
{ |
| 355 |
stringBuffer.setCharAt(i, Character.isWhitespace(character) ? ' ' : character); |
| 356 |
} |
| 357 |
} |
| 358 |
return stringBuffer.toString(); |
| 359 |
} |
| 360 |
default: |
| 361 |
{ |
| 362 |
return literal; |
| 363 |
} |
| 364 |
} |
| 365 |
} |
| 366 |
|
| 367 |
@Override |
| 368 |
public Object getEffectiveValue() |
| 369 |
{ |
| 370 |
return getValue(); |
| 371 |
} |
| 372 |
|
| 373 |
@Override |
| 374 |
public XSDConcreteComponent cloneConcreteComponent(boolean deep, boolean shareDOM) |
| 375 |
{ |
| 376 |
XSDWhiteSpaceFacetImpl clonedWhiteSpaceFacet = |
| 377 |
(XSDWhiteSpaceFacetImpl)getXSDFactory().createXSDWhiteSpaceFacet(); |
| 378 |
clonedWhiteSpaceFacet.isReconciling = true; |
| 379 |
|
| 380 |
if (getLexicalValue() != null) |
| 381 |
{ |
| 382 |
clonedWhiteSpaceFacet.setLexicalValue(getLexicalValue()); |
| 383 |
} |
| 384 |
if (isSetFixed()) |
| 385 |
{ |
| 386 |
clonedWhiteSpaceFacet.setFixed(isFixed()); |
| 387 |
} |
| 388 |
|
| 389 |
if (deep) |
| 390 |
{ |
| 391 |
if (getAnnotation() != null) |
| 392 |
{ |
| 393 |
clonedWhiteSpaceFacet.setAnnotation((XSDAnnotation)getAnnotation().cloneConcreteComponent(deep, shareDOM)); |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
if (shareDOM && getElement() != null) |
| 398 |
{ |
| 399 |
clonedWhiteSpaceFacet.setElement(getElement()); |
| 400 |
} |
| 401 |
|
| 402 |
clonedWhiteSpaceFacet.isReconciling = shareDOM; |
| 403 |
return clonedWhiteSpaceFacet; |
| 404 |
} |
| 405 |
} |