private XSDSchema getSchema(String publicName, String schemaURIString) {
    XSDSchema xsdSchema = null;
    try {
      String schemaLocation = DocumentCache.getInstance().getLocation(publicName, schemaURIString);
      schemaLocation = FileUtils.addProtocol(schemaLocation);
      ResourceSet resourceSet = new ResourceSetImpl();

      XSDResourceFactoryImpl resourceFactoryImpl = new XSDResourceFactoryImpl();
      Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put("xsd", resourceFactoryImpl);
      Resource.Factory.Registry.INSTANCE.getExtensionToFactoryMap().put(null, resourceFactoryImpl);

      resourceSet.getAdapterFactories().add(new XSDSchemaLocatorAdapterFactory());

      URI uri = createURI(schemaURIString);

      InputStream inputStream =
          resourceSet.getURIConverter().createInputStream(URI.createURI(schemaLocation));
      XSDResourceImpl resource =
          (XSDResourceImpl) resourceSet.createResource(URI.createURI("*.xsd"));
      resource.setURI(uri);
      resource.load(inputStream, null);
      xsdSchema = resource.getSchema();
    } catch (Exception e) {
      XMLEditorPlugin.logErrorMessage(
          "Error retrieving Schema for publicName: "
              + publicName
              + " schemaURIString: "
              + schemaURIString,
          e);
    }

    return xsdSchema;
  }
  private void resetXsdSimpleTypeUuids(final Resource resource) throws Exception {
    if (resource instanceof XSDResourceImpl) {
      final XSDResourceImpl xsdResource = (XSDResourceImpl) resource;
      final XSDSchema schema = xsdResource.getSchema();
      if (schema != null) {
        for (final Iterator iter = schema.getContents().iterator(); iter.hasNext(); ) {
          EObject eObj = (EObject) iter.next();

          // Only process global simple type definitions ...
          if (eObj instanceof XSDSimpleTypeDefinition) {
            final XSDSimpleTypeDefinition type = (XSDSimpleTypeDefinition) eObj;

            // Get the application information ...
            final XSDAnnotation annotation = type.getAnnotation();

            // If no annotation exists then no UUID attribute exists to reset ...
            if (annotation == null) {
              continue;
            }
            for (final Iterator appInfos = annotation.getApplicationInformation().iterator();
                appInfos.hasNext(); ) {
              final Element appInfo = (Element) appInfos.next();
              String uuid = appInfo.getAttribute(UUID_ATTRIBUTE_NAME);
              if (uuid != null) {
                uuid = IDGenerator.getInstance().create().toString();
                appInfo.setAttribute(UUID_ATTRIBUTE_NAME, uuid);
                uuid = appInfo.getAttribute(UUID_ATTRIBUTE_NAME);
              }
            }
          }
        }
      }
    }
  }
 private void setXsdIncrementalBuild(final Resource resource, final boolean isIncrementalUpdate) {
   if (resource instanceof XSDResourceImpl) {
     final XSDResourceImpl xsdResource = (XSDResourceImpl) resource;
     final XSDSchema schema = xsdResource.getSchema();
     if (schema != null) {
       schema.setIncrementalUpdate(isIncrementalUpdate);
     }
   }
 }
 private boolean getXsdIncrementalBuild(final Resource resource) {
   if (resource instanceof XSDResourceImpl) {
     final XSDResourceImpl xsdResource = (XSDResourceImpl) resource;
     final XSDSchema schema = xsdResource.getSchema();
     if (schema != null) {
       return schema.isIncrementalUpdate();
     }
   }
   return false;
 }
  /**
   * Walk through all XSDDirectives for the specified XSDResource and attempt to resolve those that
   * are undefined.
   *
   * @param eResource
   * @param recurse
   * @param visited
   * @since 4.3
   */
  protected void resolveSchemaDirectives(
      final XSDResourceImpl eResource,
      final boolean recurse,
      final Set visited,
      final Set unresolvedResourceURIs) {

    if (eResource != null && !visited.contains(eResource)) {
      // The resource must be loaded to retrieve its contents
      if (!eResource.isLoaded()) {
        try {
          eResource.load(getContainer().getLoadOptions());
        } catch (IOException err) {
          String msg =
              ModelerCore.Util.getString(
                  "DefaultResourceFinder.Error_loading_resource", eResource); // $NON-NLS-1$
          ModelerCore.Util.log(IStatus.ERROR, msg);
        }
      }
      // Add this resource to the list of those visited
      visited.add(eResource);

      // Check all imports to see if they were resolved
      for (final Iterator i = eResource.getSchema().eContents().iterator(); i.hasNext(); ) {
        EObject eObj = (EObject) i.next();
        if (eObj instanceof XSDSchemaDirective) {
          XSDSchema resolvedSchema = resolveSchemaDirective((XSDSchemaDirective) eObj);

          // Log any unresolved schema directives
          if (resolvedSchema == null
              || resolvedSchema.eResource() == null
              || resolvedSchema.eResource().getResourceSet() == null) {
            URI unresolvedURI = URI.createURI(((XSDSchemaDirective) eObj).getSchemaLocation());
            unresolvedResourceURIs.add(unresolvedURI);
            continue;
          }

          // Follow the chain and resolve all directives for the schema being imported
          if (recurse)
            resolveSchemaDirectives(
                (XSDResourceImpl) resolvedSchema.eResource(),
                recurse,
                visited,
                unresolvedResourceURIs);
        }
      }
    }
  }
  /**
   * Resolve the specified XSDSchemaDirective and resolve against resources in the resource set
   *
   * @param eResource
   * @param recurse
   * @param visited
   * @since 4.3
   */
  protected XSDSchema resolveSchemaDirective(final XSDSchemaDirective directive) {
    XSDSchema resolvedSchema = null;
    if (directive != null) {
      resolvedSchema = directive.getResolvedSchema();

      // Import is not yet resolved, attempt to locate the reference ...
      if (resolvedSchema == null && directive instanceof XSDImportImpl) {
        resolvedSchema = ((XSDImportImpl) directive).importSchema();
      }

      // If the resolvedSchema reference exists but is an eProxy then attempt to resolve it
      if (resolvedSchema != null && resolvedSchema.eIsProxy()) {
        resolvedSchema = (XSDSchema) EcoreUtil.resolve(resolvedSchema, getContainer());
      }

      // Directive is not yet resolved, attempt to locate the referenced
      // XSDResource using the schema location information in the directive
      String location = directive.getSchemaLocation();
      XSDResourceImpl eResource = (XSDResourceImpl) directive.eResource();
      if (resolvedSchema == null && eResource != null && !CoreStringUtil.isEmpty(location)) {
        XSDResourceImpl refdResource = null;

        URI schemaLocationUri = UriHelper.makeAbsoluteUri(eResource.getURI(), location);
        // URI schemaLocationUri = getAbsoluteLocation(eResource.getURI(), location);
        refdResource = (XSDResourceImpl) findByURI(schemaLocationUri, false);

        // Update the directive with the resolved schema
        if (refdResource != null) {
          resolvedSchema = refdResource.getSchema();
          directive.setResolvedSchema(resolvedSchema);
          if (directive instanceof XSDImport) {
            ((XSDSchemaImpl) resolvedSchema).imported((XSDImport) directive);
          } else if (directive instanceof XSDInclude) {
            ((XSDSchemaImpl) resolvedSchema).included((XSDInclude) directive);
          } else if (directive instanceof XSDRedefine) {
            ((XSDSchemaImpl) resolvedSchema).redefined((XSDRedefine) directive);
          }
        }
      }
    }
    return resolvedSchema;
  }
 /**
  * {@inheritDoc}
  *
  * @see org.eclipse.xsd.util.XSDResourceImpl#doLoad(java.io.InputStream, java.util.Map)
  */
 @Override
 public void doLoad(InputStream inputStream, Map<?, ?> options) throws IOException {
   if (delegate.isLoading()) {
     return;
   }
   delegate.setLoading(true);
   try {
     super.doLoad(inputStream, options);
   } finally {
     delegate.setLoading(false);
   }
 }
 @Override
 protected void doUnload() {
   if (delegate.isUnloading()) {
     return;
   }
   delegate.setUnloading(true);
   try {
     super.doUnload();
     delegate.doUnload();
   } finally {
     delegate.setUnloading(false);
   }
 }
  /**
   * Add external resources referenced by the specified XSD resource to the resultant list
   *
   * @param eResource the resource to process for references
   * @param recurse if true, the result will include all direct and indirect dependent resources
   *     otherwise only the direct dependencies are returned.
   * @param includeExternal If true, external resource references will be included in the resulant
   *     array, otherwise they will be excluded from the result.
   * @param result the resultant list to add to
   */
  protected void addExternallyReferencedResourcesForXsd(
      final XSDResourceImpl eResource,
      final boolean recurse,
      final boolean includeExternal,
      final List result,
      final Set unresolvedResourceURIs) {
    if (eResource != null) {
      // Resolve all schema directives (import/include/redefine)
      Set visitedXsdResources = new HashSet();
      resolveSchemaDirectives(eResource, recurse, visitedXsdResources, unresolvedResourceURIs);

      // Add the resource referenced through the directive to the overall result
      XSDSchema schema = eResource.getSchema();
      for (final Iterator i = schema.eContents().iterator(); i.hasNext(); ) {
        EObject eObj = (EObject) i.next();
        if (eObj instanceof XSDSchemaDirective) {
          XSDSchema resolvedSchema = ((XSDSchemaDirective) eObj).getResolvedSchema();
          Resource rsrc = (resolvedSchema != null ? resolvedSchema.eResource() : null);
          if (rsrc != null && !result.contains(rsrc)) {
            if (!includeExternal && isExternalResource(rsrc)) {
              continue;
            }
            result.add(rsrc);
            if (recurse) {
              addExternallyReferencedResources(
                  rsrc, recurse, includeExternal, result, unresolvedResourceURIs);
            }
          }
        }
      }

      // Ensure that the schema for schema resource (e.g. "http://www.w3.org/2001/XMLSchema") is
      // added to the result;
      if (includeExternal) {
        Resource rsrc = schema.getSchemaForSchema().eResource();
        if (rsrc != null && !result.contains(rsrc)) {
          result.add(rsrc);
        }
      }
    }
  }
 /**
  * {@inheritDoc}
  *
  * @see
  *     org.eclipse.emf.ecore.resource.impl.ResourceImpl#detachedHelper(org.eclipse.emf.ecore.EObject)
  */
 @Override
 protected void detachedHelper(EObject eObject) {
   delegate.detachedHelper(eObject);
   super.detachedHelper(eObject);
 }
  protected Resource findResourceByImport(
      final XSDSchemaDirective theImport, final List eResources) {
    Resource result = null;
    if (theImport != null && eResources != null) {

      // An XSDSchemaDirective referencing an XSDResource instance will have
      // the relative path to that resource in its location
      final String schemaLocation = theImport.getSchemaLocation();
      if (!CoreStringUtil.isEmpty(schemaLocation) && theImport.eResource() != null) {

        // Check if the path represents a logic URI that can be found
        final URI uri = URI.createURI(schemaLocation);
        result = findByURI(uri, true);
        if (result != null && eResources.contains(result)) {
          return result;
        }
        result = null;

        XSDResourceImpl eResource = (XSDResourceImpl) theImport.eResource();
        URI baseLocationURI = eResource.getURI();
        // If the base resource URI was created as a file URI then it's path is encoded so before we
        // resolve the referenced resource we need to encode it's relative path
        URI schemaLocationURI = UriHelper.makeAbsoluteUri(baseLocationURI, schemaLocation);
        // URI schemaLocationURI = (baseLocationURI.isFile() ? URI.createURI(schemaLocation, false):
        // URI.createURI(schemaLocation));
        // if (baseLocationURI.isHierarchical() && !baseLocationURI.isRelative() &&
        // schemaLocationURI.isRelative()) {
        // schemaLocationURI = schemaLocationURI.resolve(baseLocationURI);
        // }
        result = findByURI(schemaLocationURI, true);
        if (result != null && eResources.contains(result)) {
          return result;
        }
        result = null;

        // Check if the resource name matches any resource in the list
        final String name = URI.createURI(schemaLocation).lastSegment();
        final Collection results = findResourcesByName(name, true, eResources);
        if (results.size() == 1) {
          result = (Resource) results.iterator().next();

        } else if (results.size() > 1) {
          // Ensure that all referenced schemas are resolved
          getExternallyReferencedResources(eResource, true, false);

          // Match the input XSDSchemaDirective to one in the resource
          for (final Iterator i = eResource.getSchema().eContents().iterator(); i.hasNext(); ) {
            EObject eObj = (EObject) i.next();
            if (eObj instanceof XSDSchemaDirective && theImport == eObj) {
              XSDSchema resolvedSchema = ((XSDSchemaDirective) eObj).getResolvedSchema();
              if (resolvedSchema != null) {
                Resource refResource = resolvedSchema.eResource();
                if (eResources.contains(refResource)) {
                  return refResource;
                }
              }
            }
          }
        }
      }
    }
    return result;
  }