Java Code Examples for org.eclipse.xtext.resource.IResourceDescription#getURI()

The following examples show how to use org.eclipse.xtext.resource.IResourceDescription#getURI() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ResourceDescriptionDelta.java    From dsl-devkit with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Create a new delta from an old and a new resource description.
 *
 * @param oldDesc
 *          The old description
 * @param newDesc
 *          The new description
 * @param index
 *          index
 */
public ResourceDescriptionDelta(final IResourceDescription oldDesc, final IResourceDescription newDesc, final IResourceDescriptions index) {
  super();
  if (oldDesc == newDesc) {
    throw new AssertionError("'old != new' constraint violated"); //$NON-NLS-1$
  }
  if (newDesc != null && oldDesc != null && !oldDesc.getURI().equals(newDesc.getURI())) {
    URI oldURI = oldDesc.getURI();
    URI newURI = newDesc.getURI();
    throw new AssertionError("'new != null && old != null && !old.getURI().equals(new.getURI())' constraint violated, old was " + oldURI + " new was: " //$NON-NLS-1$ //$NON-NLS-2$
        + newURI);
  }

  this.uri = oldDesc == null ? newDesc.getURI() : oldDesc.getURI();
  this.oldDesc = oldDesc;
  if (newDesc != null) {
    this.newDesc = new SoftReference<IResourceDescription>(newDesc);
  }
  this.index = index;
}
 
Example 2
Source File: ExternalLibraryBuilder.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Deletes all entries in the Xtext index that start with one of the given project URIs will be cleaned from the
 * index.
 *
 * @param toBeWiped
 *            URIs of project roots
 */
public void wipeURIsFromIndex(IProgressMonitor monitor, Collection<FileURI> toBeWiped) {
	Set<String> toBeWipedStrings = new HashSet<>();
	for (FileURI toWipe : toBeWiped) {
		toBeWipedStrings.add(toWipe.toString());
		N4JSProjectName projectName = toWipe.getProjectName();
		validatorExtension.clearAllMarkersOfExternalProject(projectName);
	}

	ResourceSet resourceSet = core.createResourceSet(Optional.absent());
	IResourceDescriptions index = core.getXtextIndex(resourceSet);

	Set<URI> toBeRemoved = new HashSet<>();
	for (IResourceDescription res : index.getAllResourceDescriptions()) {
		URI resUri = res.getURI();
		String resUriString = resUri.toString();
		for (String toWipeProject : toBeWipedStrings) {
			if (resUriString.startsWith(toWipeProject)) {
				toBeRemoved.add(resUri);
				break;
			}
		}
	}

	builderState.clean(toBeRemoved, monitor);
}
 
Example 3
Source File: FixedCopiedResourceDescription.java    From dsl-devkit with Eclipse Public License 1.0 6 votes vote down vote up
public FixedCopiedResourceDescription(final IResourceDescription original) {
  super();
  this.uri = original.getURI();
  this.exported = ImmutableList.copyOf(Iterables.transform(original.getExportedObjects(), new Function<IEObjectDescription, IEObjectDescription>() {
    @Override
    @SuppressWarnings("unchecked")
    public IEObjectDescription apply(final IEObjectDescription from) {
      if (from.getEObjectOrProxy().eIsProxy()) {
        return from;
      } else if (from instanceof IDetachableDescription) {
        return ((IDetachableDescription<IEObjectDescription>) from).detach();
      }
      InternalEObject result = (InternalEObject) EcoreUtil.create(from.getEClass());
      result.eSetProxyURI(from.getEObjectURI());
      ImmutableMap.Builder<String, String> userData = ImmutableMap.builder();
      for (final String key : from.getUserDataKeys()) {
        userData.put(key, from.getUserData(key));
      }
      return EObjectDescription.create(from.getName(), result, userData.build());
    }
  }));
}
 
Example 4
Source File: GamlResourceDescriptionManager.java    From gama with GNU General Public License v3.0 6 votes vote down vote up
@Override
public boolean isAffected(final Collection<Delta> deltas, final IResourceDescription candidate,
		final IResourceDescriptions context) {
	// final boolean result = false;
	final URI newUri = candidate.getURI();
	try (ICollector<URI> deltaUris = Collector.getSet()) {
		for (final Delta d : deltas) {
			deltaUris.add(GamlResourceServices.properlyEncodedURI(d.getUri()));
		}
		final Iterator<URI> it = GamlResourceIndexer.allImportsOf(newUri);
		while (it.hasNext()) {
			final URI next = it.next();
			if (deltaUris.contains(next)) { return true; }
		}
		return super.isAffected(deltas, candidate, context);
	}
}
 
Example 5
Source File: Indexer.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
public ResolvedResourceDescription(IResourceDescription original) {
	uri = original.getURI();
	exported = FluentIterable.from(original.getExportedObjects()).transform(from -> {
		if (from instanceof SerializableEObjectDescriptionProvider) {
			return ((SerializableEObjectDescriptionProvider) from).toSerializableEObjectDescription();
		}
		if (from.getEObjectOrProxy().eIsProxy()) {
			return from;
		}
		InternalEObject result = (InternalEObject) EcoreUtil.create(from.getEClass());
		result.eSetProxyURI(from.getEObjectURI());
		Map<String, String> userData = null;
		String[] userDataKeys = from.getUserDataKeys();
		for (String key : userDataKeys) {
			if (userData == null) {
				userData = Maps.newHashMapWithExpectedSize(userDataKeys.length);
			}
			userData.put(key, from.getUserData(key));
		}
		return EObjectDescription.create(from.getName(), result, userData);
	}).toList();
}
 
Example 6
Source File: ReferenceFinder.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void findReferences(final TargetURIs targetURIs, IResourceDescription resourceDescription,
		IResourceAccess resourceAccess, final Acceptor acceptor, final IProgressMonitor monitor) {
	final URI resourceURI = resourceDescription.getURI();
	if (resourceAccess != null && targetURIs.containsResource(resourceURI)) {
		IUnitOfWork.Void<ResourceSet> finder = new IUnitOfWork.Void<ResourceSet>() {
			@Override
			public void process(final ResourceSet state) throws Exception {
				Resource resource = state.getResource(resourceURI, true);
				findReferences(targetURIs, resource, acceptor, monitor);
			}
		};
		resourceAccess.readOnly(resourceURI, finder);
	} else {
		findReferencesInDescription(targetURIs, resourceDescription, resourceAccess, acceptor, monitor);
	}
}
 
Example 7
Source File: CopiedResourceDescription.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
public CopiedResourceDescription(IResourceDescription original) {
    this.uri = original.getURI();
    this.exported = ImmutableList.copyOf(Iterables.transform(original.getExportedObjects(),
            new Function<IEObjectDescription, IEObjectDescription>() {
                @Override
	public IEObjectDescription apply(IEObjectDescription from) {
                    if (from.getEObjectOrProxy().eIsProxy()) {
                        return from;
                    }
                    InternalEObject result = (InternalEObject) EcoreUtil.create(from.getEClass());
                    result.eSetProxyURI(from.getEObjectURI());
                    Map<String, String> userData = null;
                    for (final String key : from.getUserDataKeys()) {
                        if (userData == null) {
                            userData = Maps.newHashMapWithExpectedSize(2);
                        }
                        userData.put(key, from.getUserData(key));
                    }
                    return EObjectDescription.create(from.getName(), result, userData);
                }
            }));
}
 
Example 8
Source File: ValidMarkerUpdateJob.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Gets the resource description URIs.
 *
 * @return the resource description URIs
 */
private List<URI> getResourceDescriptionURIs() {
  final List<URI> uris = Lists.<URI> newArrayList();
  final List<String> extensions = Lists.newArrayList(this.fileExtensions.toUpperCase().split("\\,")); //$NON-NLS-1$
  for (final IResourceDescription description : this.resourceDescriptions.getAllResourceDescriptions()) {
    final URI uri = description.getURI();
    if (extensions.contains(uri.fileExtension().toUpperCase())) {
      uris.add(uri);
    }
  }
  return uris;
}
 
Example 9
Source File: FingerprintResourceDescription.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
public FingerprintResourceDescription(final IResourceDescription original) {
  this.uri = original.getURI();
  ImmutableList.Builder<IEObjectDescription> builder = ImmutableList.builder();
  for (IEObjectDescription description : original.getExportedObjects()) {
    builder.add(createLightDescription(description));
  }
  this.exportedObjects = builder.build();
}
 
Example 10
Source File: DefaultResourceDescriptionDelta.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
public DefaultResourceDescriptionDelta(IResourceDescription old, IResourceDescription _new) {
	super();
	if (old == _new) {
		throw new AssertionError("'old!=_new' constraint violated");
	}
	if (_new != null && old != null && !old.getURI().equals(_new.getURI())) {
		URI oldURI = old.getURI();
		URI newURI = _new.getURI();
		throw new AssertionError(
				"'_new!=null && old!=null && !old.getURI().equals(_new.getURI())' constraint violated, old was "
						+ oldURI + " new was: " + newURI);
	}
	this.old = old;
	this._new = _new;
}
 
Example 11
Source File: JavaSearchHelper.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
public void search(URI uri, IProgressMonitor monitor) {
	int numResources = Iterables.size(resourceDescriptions.getAllResourceDescriptions());
	SubMonitor subMonitor = SubMonitor.convert(monitor, numResources / 10);
	subMonitor.subTask("Find references in EMF resources");
	try {
		int i = 0;
		for (IResourceDescription resourceDescription : resourceDescriptions.getAllResourceDescriptions()) {
			URI resourceURI = resourceDescription.getURI();
			IResourceServiceProvider resourceServiceProvider = serviceProviderRegistry.getResourceServiceProvider(resourceURI);
			if(resourceServiceProvider != null) {
				IJavaSearchParticipation javaSearchParticipation = resourceServiceProvider
						.get(IJavaSearchParticipation.class);
				if(javaSearchParticipation == null || javaSearchParticipation.canContainJvmReferences(resourceURI))
					searchIn(uri, resourceDescription);
			}
			if (++i % 10 == 0) {
				if (subMonitor.isCanceled()) {
					return; // not throwing OperationCanceledException, as the client in JDT doesn't seem to handle it properly
				}
				subMonitor.worked(1);
			}
		}
		for(ResourceSet resourceSet: projectToResourceSet.values()) {
			resourceSet.getResources().clear();
			resourceSet.eAdapters().clear();
		}
	} finally {
		subMonitor.done();
	}
}
 
Example 12
Source File: DocumentBasedDirtyResource.java    From xtext-eclipse with Eclipse Public License 2.0 5 votes vote down vote up
@Override
public URI getURI() {
	if (document == null)
		throw new IllegalStateException("Cannot use getURI if this dirty resource is not connected to a document");
	IResourceDescription description = this.description;
	if (description == null)
		throw new IllegalStateException("Cannot use getURI if this dirty resource is currently not initialized");
	return description.getURI();
}
 
Example 13
Source File: N4JSResourceDescriptionManager.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * {@inheritDoc}
 *
 * This marks {@code n4js} as affected if the manifest of the project changes. In turn, they will be revalidated and
 * taken into consideration for the code generation step.
 */
@Override
public boolean isAffected(Collection<IResourceDescription.Delta> deltas, IResourceDescription candidate,
		IResourceDescriptions context) {
	URI candidateURI = candidate.getURI();

	// Opaque modules cannot contain any references to one of the deltas.
	// Thus, they will never be affected by any change.
	if (langHelper.isOpaqueModule(candidateURI)) {
		return false;
	}

	boolean result = basicIsAffected(deltas, candidate);
	if (!result) {
		for (IResourceDescription.Delta delta : deltas) {
			URI uri = delta.getUri();
			// if uri looks like a N4JS project description file (i.e. package.json)
			if (IN4JSProject.PACKAGE_JSON.equalsIgnoreCase(uri.lastSegment())) {
				URI prefixURI = uri.trimSegments(1).appendSegment("");
				if (candidateURI.replacePrefix(prefixURI, prefixURI) != null) {
					return true;
				}
			}
		}
	}
	return result;
}
 
Example 14
Source File: OpenFilesManager.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
protected synchronized void updateSharedDirtyState(IResourceDescription newDesc) {
	// update my dirty state instance
	URI newDescURI = newDesc.getURI();
	sharedDirtyState.addDescription(newDescURI, newDesc);
	// update dirty state instances in the context of each open file (except the one that caused the change)
	for (URI currURI : openFiles.keySet()) {
		if (currURI.equals(newDescURI)) {
			continue;
		}
		runInOpenFileContextVoid(currURI, "updateSharedDirtyState in open file", (ofc, ci) -> {
			ofc.onDirtyStateChanged(newDesc, ci);
		});
	}
}
 
Example 15
Source File: FastReferenceSearchResultContentProvider.java    From dsl-devkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Adds the given resource to the contents or returns an existing one.
 *
 * @param resourceDescription
 *          resource to add
 * @return added resource or already existing resource, neved {@code null}
 */
private ReferenceSearchViewTreeNode resourceNode(final IResourceDescription resourceDescription) {
  URI uri = resourceDescription.getURI();
  ReferenceSearchViewTreeNode node = rootNodes.get(uri);
  if (node == null) {
    node = new ReferenceSearchViewTreeNode(null, resourceDescription, resourceDescription);
    rootNodes.put(uri, node);
    synchronized (batchAddNodes) {
      batchAddNodes.add(node);
    }
  }
  return node;
}
 
Example 16
Source File: ContributingResourceDescriptionPersister.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
private IResourceDescription detectOutdated(IResourceDescription description) {
	URI uri = description.getURI();
	if (uri.isFile() && uri.scheme() != null && !uri.hasAuthority()) {
		throw new RuntimeException("Outdated index information");
	}
	return description;
}
 
Example 17
Source File: N4JSEclipseResourceDescriptionManager.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public boolean isAffected(Collection<Delta> deltas, IResourceDescription candidate, IResourceDescriptions context) {
	URI candidateURI = candidate.getURI();
	if (candidateURI.isFile()) {
		return false;
	}
	return super.isAffected(deltas, candidate, context);
}
 
Example 18
Source File: StatefulResourceDescription.java    From xtext-eclipse with Eclipse Public License 2.0 4 votes vote down vote up
public StatefulResourceDescription(IResourceDescription original, Provider<IResourceDescription> snapShotProvider) {
	this.snapShotProvider = snapShotProvider;
	this.uri = original.getURI();
	this.exported = copyExportedObjects(original);
}
 
Example 19
Source File: AbstractN4JSCore.java    From n4js with Eclipse Public License 1.0 4 votes vote down vote up
@Override
public TModule loadModuleFromIndex(final ResourceSet resourceSet,
		final IResourceDescription resourceDescription, boolean allowFullLoad) {
	final URI resourceURI = resourceDescription.getURI();
	Resource resource = resourceSet.getResource(resourceURI, false);
	TModule result = loadModuleFromResource(resource);
	if (result != null) {
		return result;
	}
	if (resource == null) {
		if (resourceSet instanceof ISynchronizable<?>) {
			synchronized (((ISynchronizable<?>) resourceSet).getLock()) {
				resource = resourceSet.getResource(resourceURI, false);
				result = loadModuleFromResource(resource);
				if (result != null) {
					return result;
				}
				if (resource == null) {
					resource = resourceSet.createResource(resourceURI);
				}
			}
		} else {
			resource = resourceSet.createResource(resourceURI);
		}
	}
	if (resource instanceof N4JSResource) {
		if (resource.getContents().isEmpty()) {
			final N4JSResource casted = (N4JSResource) resource;
			try {
				if (casted.loadFromDescription(resourceDescription)) {
					casted.performPostProcessing();
					return casted.getModule();
				} else if (allowFullLoad) {
					casted.unload();
					casted.load(resourceSet.getLoadOptions());
					casted.installDerivedState(false);
					return casted.getModule();
				}
			} catch (final Exception e) {
				casted.unload();
				return null;
			}
		}
	}
	return null;
}
 
Example 20
Source File: ExternalPackagesPluginTest.java    From n4js with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Checks if expected list of stringified file locations matches
 *
 * @param expected
 *            collection of entries
 * @param actual
 *            collection of entries
 */
public void assertResourceDescriptions(Collection<String> expected, Iterable<IResourceDescription> actual) {
	Set<String> extraDescriptions = new HashSet<>();
	Set<String> missingDescriptions = new HashSet<>(expected);

	for (IResourceDescription iResourceDescription : actual) {
		URI uri = iResourceDescription.getURI();
		String stringUri = uri.isPlatform() ? uri.toPlatformString(false) : uri.toFileString();

		String missingDescription = "";
		for (String missingDescr : missingDescriptions) {
			if (stringUri.endsWith(missingDescr)) {
				missingDescription = missingDescr;
				break;
			}
		}

		if (missingDescription.isEmpty()) {
			extraDescriptions.add(stringUri);
		} else {
			missingDescriptions.remove(missingDescription);
		}
	}

	if (missingDescriptions.isEmpty() && extraDescriptions.isEmpty()) {
		return;
	}

	StringBuilder msg = new StringBuilder("unexpected actual resources" + "\n");

	if (!extraDescriptions.isEmpty()) {
		msg.append("actual contains " + extraDescriptions.size() + " extra resources" + "\n");
	}

	if (!missingDescriptions.isEmpty()) {
		msg.append("actual is missing  " + missingDescriptions.size() + " expected resources" + "\n");
	}

	for (String extra : extraDescriptions) {
		msg.append("[extra] " + extra + "\n");
	}
	for (String missing : missingDescriptions) {
		msg.append("[missing] " + missing + "\n");
	}
	fail(msg.toString());
}