| 1 |
/** |
| 2 |
* Copyright (c) 2002-2007 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.io.IOException; |
| 15 |
import java.io.InputStream; |
| 16 |
import org.w3c.dom.Element; |
| 17 |
|
| 18 |
import org.eclipse.emf.common.notify.Notification; |
| 19 |
import org.eclipse.emf.common.util.URI; |
| 20 |
import org.eclipse.emf.ecore.EAttribute; |
| 21 |
import org.eclipse.emf.ecore.EClass; |
| 22 |
import org.eclipse.emf.ecore.impl.ENotificationImpl; |
| 23 |
import org.eclipse.emf.ecore.resource.Resource; |
| 24 |
import org.eclipse.emf.ecore.resource.ResourceSet; |
| 25 |
import org.eclipse.emf.ecore.util.EcoreUtil; |
| 26 |
|
| 27 |
import org.eclipse.xsd.XSDConcreteComponent; |
| 28 |
import org.eclipse.xsd.XSDFactory; |
| 29 |
import org.eclipse.xsd.XSDPackage; |
| 30 |
import org.eclipse.xsd.XSDPlugin; |
| 31 |
import org.eclipse.xsd.XSDSchema; |
| 32 |
import org.eclipse.xsd.XSDSchemaDirective; |
| 33 |
import org.eclipse.xsd.util.XSDConstants; |
| 34 |
import org.eclipse.xsd.util.XSDResourceImpl; |
| 35 |
import org.eclipse.xsd.util.XSDSchemaLocationResolver; |
| 36 |
import org.eclipse.xsd.util.XSDSchemaLocator; |
| 37 |
|
| 38 |
|
| 39 |
/** |
| 40 |
* <!-- begin-user-doc --> |
| 41 |
* An implementation of the model object '<em><b>Schema Directive</b></em>'. |
| 42 |
* <!-- end-user-doc --> |
| 43 |
* <p> |
| 44 |
* The following features are implemented: |
| 45 |
* </p> |
| 46 |
* <ul> |
| 47 |
* <li>{@link org.eclipse.xsd.impl.XSDSchemaDirectiveImpl#getSchemaLocation <em>Schema Location</em>}</li> |
| 48 |
* <li>{@link org.eclipse.xsd.impl.XSDSchemaDirectiveImpl#getResolvedSchema <em>Resolved Schema</em>}</li> |
| 49 |
* </ul> |
| 50 |
* |
| 51 |
* @generated |
| 52 |
*/ |
| 53 |
public abstract class XSDSchemaDirectiveImpl |
| 54 |
extends XSDSchemaContentImpl |
| 55 |
implements XSDSchemaDirective |
| 56 |
{ |
| 57 |
/** |
| 58 |
* The default value of the '{@link #getSchemaLocation() <em>Schema Location</em>}' attribute. |
| 59 |
* <!-- begin-user-doc --> |
| 60 |
* <!-- end-user-doc --> |
| 61 |
* @see #getSchemaLocation() |
| 62 |
* @generated |
| 63 |
* @ordered |
| 64 |
*/ |
| 65 |
protected static final String SCHEMA_LOCATION_EDEFAULT = null; |
| 66 |
|
| 67 |
/** |
| 68 |
* The cached value of the '{@link #getSchemaLocation() <em>Schema Location</em>}' attribute. |
| 69 |
* <!-- begin-user-doc --> |
| 70 |
* <!-- end-user-doc --> |
| 71 |
* @see #getSchemaLocation() |
| 72 |
* @generated |
| 73 |
* @ordered |
| 74 |
*/ |
| 75 |
protected String schemaLocation = SCHEMA_LOCATION_EDEFAULT; |
| 76 |
|
| 77 |
/** |
| 78 |
* The cached value of the '{@link #getResolvedSchema() <em>Resolved Schema</em>}' reference. |
| 79 |
* <!-- begin-user-doc --> |
| 80 |
* <!-- end-user-doc --> |
| 81 |
* @see #getResolvedSchema() |
| 82 |
* @generated |
| 83 |
* @ordered |
| 84 |
*/ |
| 85 |
protected XSDSchema resolvedSchema; |
| 86 |
|
| 87 |
/** |
| 88 |
* <!-- begin-user-doc --> |
| 89 |
* <!-- end-user-doc --> |
| 90 |
* @generated |
| 91 |
*/ |
| 92 |
protected XSDSchemaDirectiveImpl() |
| 93 |
{ |
| 94 |
super(); |
| 95 |
} |
| 96 |
|
| 97 |
@Override |
| 98 |
public void reset() |
| 99 |
{ |
| 100 |
super.reset(); |
| 101 |
resolved = false; |
| 102 |
setResolvedSchema(null); |
| 103 |
} |
| 104 |
|
| 105 |
/** |
| 106 |
* <!-- begin-user-doc --> |
| 107 |
* <!-- end-user-doc --> |
| 108 |
* @generated |
| 109 |
*/ |
| 110 |
@Override |
| 111 |
protected EClass eStaticClass() |
| 112 |
{ |
| 113 |
return XSDPackage.Literals.XSD_SCHEMA_DIRECTIVE; |
| 114 |
} |
| 115 |
|
| 116 |
/** |
| 117 |
* <!-- begin-user-doc --> |
| 118 |
* <!-- end-user-doc --> |
| 119 |
* @generated |
| 120 |
*/ |
| 121 |
@Override |
| 122 |
public String getSchemaLocation() |
| 123 |
{ |
| 124 |
return schemaLocation; |
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* <!-- begin-user-doc --> |
| 129 |
* <!-- end-user-doc --> |
| 130 |
* @generated |
| 131 |
*/ |
| 132 |
@Override |
| 133 |
public void setSchemaLocation(String newSchemaLocation) |
| 134 |
{ |
| 135 |
String oldSchemaLocation = schemaLocation; |
| 136 |
schemaLocation = newSchemaLocation; |
| 137 |
if (eNotificationRequired()) |
| 138 |
eNotify(new ENotificationImpl(this, Notification.SET, XSDPackage.XSD_SCHEMA_DIRECTIVE__SCHEMA_LOCATION, oldSchemaLocation, schemaLocation)); |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* <!-- begin-user-doc --> |
| 143 |
* <!-- end-user-doc --> |
| 144 |
* @generated |
| 145 |
*/ |
| 146 |
@Override |
| 147 |
public XSDSchema getResolvedSchema() |
| 148 |
{ |
| 149 |
return resolvedSchema; |
| 150 |
} |
| 151 |
|
| 152 |
/** |
| 153 |
* <!-- begin-user-doc --> |
| 154 |
* <!-- end-user-doc --> |
| 155 |
* @generated |
| 156 |
*/ |
| 157 |
@Override |
| 158 |
public void setResolvedSchema(XSDSchema newResolvedSchema) |
| 159 |
{ |
| 160 |
XSDSchema oldResolvedSchema = resolvedSchema; |
| 161 |
resolvedSchema = newResolvedSchema; |
| 162 |
if (eNotificationRequired()) |
| 163 |
eNotify(new ENotificationImpl(this, Notification.SET, XSDPackage.XSD_SCHEMA_DIRECTIVE__RESOLVED_SCHEMA, oldResolvedSchema, resolvedSchema)); |
| 164 |
} |
| 165 |
|
| 166 |
/** |
| 167 |
* <!-- begin-user-doc --> |
| 168 |
* <!-- end-user-doc --> |
| 169 |
* @generated |
| 170 |
*/ |
| 171 |
@Override |
| 172 |
public Object eGet(int featureID, boolean resolve, boolean coreType) |
| 173 |
{ |
| 174 |
switch (featureID) |
| 175 |
{ |
| 176 |
case XSDPackage.XSD_SCHEMA_DIRECTIVE__SCHEMA_LOCATION: |
| 177 |
return getSchemaLocation(); |
| 178 |
case XSDPackage.XSD_SCHEMA_DIRECTIVE__RESOLVED_SCHEMA: |
| 179 |
return getResolvedSchema(); |
| 180 |
} |
| 181 |
return super.eGet(featureID, resolve, coreType); |
| 182 |
} |
| 183 |
|
| 184 |
/** |
| 185 |
* <!-- begin-user-doc --> |
| 186 |
* <!-- end-user-doc --> |
| 187 |
* @generated |
| 188 |
*/ |
| 189 |
@Override |
| 190 |
public void eSet(int featureID, Object newValue) |
| 191 |
{ |
| 192 |
switch (featureID) |
| 193 |
{ |
| 194 |
case XSDPackage.XSD_SCHEMA_DIRECTIVE__SCHEMA_LOCATION: |
| 195 |
setSchemaLocation((String)newValue); |
| 196 |
return; |
| 197 |
case XSDPackage.XSD_SCHEMA_DIRECTIVE__RESOLVED_SCHEMA: |
| 198 |
setResolvedSchema((XSDSchema)newValue); |
| 199 |
return; |
| 200 |
} |
| 201 |
super.eSet(featureID, newValue); |
| 202 |
} |
| 203 |
|
| 204 |
/** |
| 205 |
* <!-- begin-user-doc --> |
| 206 |
* <!-- end-user-doc --> |
| 207 |
* @generated |
| 208 |
*/ |
| 209 |
@Override |
| 210 |
public void eUnset(int featureID) |
| 211 |
{ |
| 212 |
switch (featureID) |
| 213 |
{ |
| 214 |
case XSDPackage.XSD_SCHEMA_DIRECTIVE__SCHEMA_LOCATION: |
| 215 |
setSchemaLocation(SCHEMA_LOCATION_EDEFAULT); |
| 216 |
return; |
| 217 |
case XSDPackage.XSD_SCHEMA_DIRECTIVE__RESOLVED_SCHEMA: |
| 218 |
setResolvedSchema((XSDSchema)null); |
| 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_SCHEMA_DIRECTIVE__SCHEMA_LOCATION: |
| 235 |
return SCHEMA_LOCATION_EDEFAULT == null ? schemaLocation != null : !SCHEMA_LOCATION_EDEFAULT.equals(schemaLocation); |
| 236 |
case XSDPackage.XSD_SCHEMA_DIRECTIVE__RESOLVED_SCHEMA: |
| 237 |
return resolvedSchema != null; |
| 238 |
} |
| 239 |
return super.eIsSet(featureID); |
| 240 |
} |
| 241 |
|
| 242 |
/** |
| 243 |
* <!-- begin-user-doc --> |
| 244 |
* <!-- end-user-doc --> |
| 245 |
* @generated |
| 246 |
*/ |
| 247 |
@Override |
| 248 |
public String toString() |
| 249 |
{ |
| 250 |
if (eIsProxy()) return super.toString(); |
| 251 |
|
| 252 |
StringBuilder result = new StringBuilder(super.toString()); |
| 253 |
result.append(" (schemaLocation: "); |
| 254 |
result.append(schemaLocation); |
| 255 |
result.append(')'); |
| 256 |
return result.toString(); |
| 257 |
} |
| 258 |
|
| 259 |
@Override |
| 260 |
protected void reconcileAttributes(Element changedElement) |
| 261 |
{ |
| 262 |
super.reconcileAttributes(changedElement); |
| 263 |
|
| 264 |
String newSchemaLocation = null; |
| 265 |
if (changedElement.hasAttributeNS(null, XSDConstants.SCHEMALOCATION_ATTRIBUTE)) |
| 266 |
{ |
| 267 |
newSchemaLocation = changedElement.getAttributeNS(null, XSDConstants.SCHEMALOCATION_ATTRIBUTE); |
| 268 |
} |
| 269 |
|
| 270 |
if (newSchemaLocation == null ? getSchemaLocation() != null : !newSchemaLocation.equals(getSchemaLocation())) |
| 271 |
{ |
| 272 |
setSchemaLocation(newSchemaLocation); |
| 273 |
} |
| 274 |
} |
| 275 |
|
| 276 |
@Override |
| 277 |
protected void changeAttribute(EAttribute eAttribute) |
| 278 |
{ |
| 279 |
// Try to resolve again if the schema location changes and we previously failed. |
| 280 |
// |
| 281 |
if (resolved && getResolvedSchema() == null && eAttribute == XSDPackage.Literals.XSD_SCHEMA_DIRECTIVE__SCHEMA_LOCATION) |
| 282 |
{ |
| 283 |
resolved = false; |
| 284 |
} |
| 285 |
|
| 286 |
if (!isReconciling) |
| 287 |
{ |
| 288 |
super.changeAttribute(eAttribute); |
| 289 |
if (eAttribute == null || eAttribute == XSDPackage.Literals.XSD_SCHEMA_DIRECTIVE__SCHEMA_LOCATION) |
| 290 |
{ |
| 291 |
Element theElement = getElement(); |
| 292 |
if (theElement != null) |
| 293 |
{ |
| 294 |
niceSetAttribute(theElement, XSDConstants.SCHEMALOCATION_ATTRIBUTE, getSchemaLocation()); |
| 295 |
} |
| 296 |
} |
| 297 |
} |
| 298 |
|
| 299 |
if (eAttribute == XSDPackage.Literals.XSD_SCHEMA_DIRECTIVE__SCHEMA_LOCATION && getResolvedSchema() != null && getSchema().isIncrementalUpdate()) |
| 300 |
{ |
| 301 |
getSchema().reset(); |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
@Override |
| 306 |
protected void orphanBy(XSDSchema xsdSchema) |
| 307 |
{ |
| 308 |
super.orphanBy(xsdSchema); |
| 309 |
if (resolved && xsdSchema.isIncrementalUpdate()) |
| 310 |
{ |
| 311 |
xsdSchema.reset(); |
| 312 |
} |
| 313 |
} |
| 314 |
|
| 315 |
protected boolean resolved; |
| 316 |
protected void resolve(String namespace, String schemaLocation) |
| 317 |
{ |
| 318 |
if (!resolved) |
| 319 |
{ |
| 320 |
XSDSchema xsdSchema = getSchema(); |
| 321 |
if (xsdSchema != null && |
| 322 |
(xsdSchema.getSchemaLocation() != null || |
| 323 |
((XSDSchemaImpl)xsdSchema).getPendingSchemaLocation() != null)) |
| 324 |
{ |
| 325 |
Resource resource = xsdSchema.eResource(); |
| 326 |
if (resource != null) |
| 327 |
{ |
| 328 |
ResourceSet resourceSet = resource.getResourceSet(); |
| 329 |
if (resourceSet != null) |
| 330 |
{ |
| 331 |
if ("".equals(namespace)) |
| 332 |
{ |
| 333 |
namespace = xsdSchema.getTargetNamespace(); |
| 334 |
} |
| 335 |
|
| 336 |
String resolvedSchemaLocation = resolveSchemaLocation(xsdSchema, namespace, schemaLocation); |
| 337 |
if (schemaLocation == null) |
| 338 |
{ |
| 339 |
if (XSDConstants.isXMLNamespace(resolvedSchemaLocation)) |
| 340 |
{ |
| 341 |
resolvedSchemaLocation = XSDPlugin.INSTANCE.getBaseURL().toString() + "cache/www.w3.org/2001/xml.xsd"; |
| 342 |
} |
| 343 |
else if (XSDConstants.SCHEMA_FOR_SCHEMA_URI_2001.equals(resolvedSchemaLocation)) |
| 344 |
{ |
| 345 |
resolvedSchemaLocation = XSDPlugin.INSTANCE.getBaseURL().toString() + "cache/www.w3.org/2001/XMLSchema.xsd"; |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
XSDSchema resolvedSchema = locateSchema(xsdSchema, namespace, schemaLocation, resolvedSchemaLocation); |
| 350 |
if (resolvedSchema == null) |
| 351 |
{ |
| 352 |
URI uri = URI.createURI(resolvedSchemaLocation == null ? "" : resolvedSchemaLocation); |
| 353 |
Resource resolvedResource = resourceSet.getResource(uri, false); |
| 354 |
if (resolvedResource == null) |
| 355 |
{ |
| 356 |
try |
| 357 |
{ |
| 358 |
InputStream inputStream = resourceSet.getURIConverter().createInputStream(uri); |
| 359 |
resolvedResource = resourceSet.createResource(URI.createURI("*.xsd")); |
| 360 |
resolvedResource.setURI(uri); |
| 361 |
resolvedResource.load(inputStream, resourceSet.getLoadOptions()); |
| 362 |
} |
| 363 |
catch (IOException exception) |
| 364 |
{ |
| 365 |
// It is generally not an error to fail to resolve. |
| 366 |
// If a resource is actually created, |
| 367 |
// which happens only when we can create an input stream, |
| 368 |
// then it's an error if it's not a good schema |
| 369 |
} |
| 370 |
} |
| 371 |
else if (!resolvedResource.isLoaded()) |
| 372 |
{ |
| 373 |
try |
| 374 |
{ |
| 375 |
resolvedResource.load(resourceSet.getLoadOptions()); |
| 376 |
} |
| 377 |
catch (IOException exception) |
| 378 |
{ |
| 379 |
// Ignore. |
| 380 |
} |
| 381 |
} |
| 382 |
|
| 383 |
if (resolvedResource != null) |
| 384 |
{ |
| 385 |
if (resolvedResource instanceof XSDResourceImpl) |
| 386 |
{ |
| 387 |
resolvedSchema = ((XSDResourceImpl)resolvedResource).getSchema(); |
| 388 |
} |
| 389 |
else |
| 390 |
{ |
| 391 |
resolvedSchema = XSDFactory.eINSTANCE.createXSDSchema(); |
| 392 |
} |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
resolved = true; |
| 397 |
handleResolvedSchema(resolvedSchema); |
| 398 |
} |
| 399 |
} |
| 400 |
} |
| 401 |
} |
| 402 |
} |
| 403 |
|
| 404 |
protected XSDSchema locateSchema(XSDSchema xsdSchema, String namespace, String rawSchemaLocation, String resolvedSchemaLocation) |
| 405 |
{ |
| 406 |
XSDSchemaLocator xsdSchemaLocator = (XSDSchemaLocator)EcoreUtil.getRegisteredAdapter(xsdSchema.eResource(), XSDSchemaLocator.class); |
| 407 |
return |
| 408 |
xsdSchemaLocator == null ? |
| 409 |
null : |
| 410 |
xsdSchemaLocator.locateSchema(xsdSchema, namespace, rawSchemaLocation, resolvedSchemaLocation); |
| 411 |
} |
| 412 |
|
| 413 |
protected String resolveSchemaLocation(XSDSchema xsdSchema, String namespace, String schemaLocation) |
| 414 |
{ |
| 415 |
for (;;) |
| 416 |
{ |
| 417 |
XSDSchemaLocationResolver xsdSchemaLocationResolver = |
| 418 |
(XSDSchemaLocationResolver)EcoreUtil.getRegisteredAdapter(xsdSchema, XSDSchemaLocationResolver.class); |
| 419 |
if (xsdSchemaLocationResolver != null) |
| 420 |
{ |
| 421 |
return xsdSchemaLocationResolver.resolveSchemaLocation(xsdSchema, namespace, schemaLocation); |
| 422 |
} |
| 423 |
|
| 424 |
XSDConcreteComponent container = xsdSchema.getContainer(); |
| 425 |
if (container == null) |
| 426 |
{ |
| 427 |
break; |
| 428 |
} |
| 429 |
else |
| 430 |
{ |
| 431 |
XSDSchema containingXSDSchema = container.getSchema(); |
| 432 |
if (containingXSDSchema == null) |
| 433 |
{ |
| 434 |
break; |
| 435 |
} |
| 436 |
else |
| 437 |
{ |
| 438 |
xsdSchema = containingXSDSchema; |
| 439 |
} |
| 440 |
} |
| 441 |
} |
| 442 |
|
| 443 |
return XSDConstants.resolveSchemaLocation(xsdSchema.getSchemaLocation(), namespace, schemaLocation); |
| 444 |
} |
| 445 |
|
| 446 |
protected void handleResolvedSchema(XSDSchema xsdSchema) |
| 447 |
{ |
| 448 |
// Ignore |
| 449 |
} |
| 450 |
} |