Java Code Examples for org.eclipse.emf.common.util.URI#segmentCount()

The following examples show how to use org.eclipse.emf.common.util.URI#segmentCount() . 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: BuiltInSchemeRegistrar.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Convert the given n4scheme-URI to a classpath-URI.
 */
private URI toClasspathURI(URI uriWithN4Scheme) {
	String[] allSegments = new String[uriWithN4Scheme.segmentCount() + 1];
	allSegments[0] = "env";
	for (int i = 0; i < uriWithN4Scheme.segmentCount(); i++) {
		allSegments[i + 1] = uriWithN4Scheme.segment(i);
	}
	URI classpathURI = URI.createHierarchicalURI(
			ClasspathUriUtil.CLASSPATH_SCHEME,
			uriWithN4Scheme.authority(),
			uriWithN4Scheme.device(),
			allSegments,
			uriWithN4Scheme.query(),
			uriWithN4Scheme.fragment());
	return classpathURI;
}
 
Example 2
Source File: ProjectDescriptionUtils.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Given a URI as used internally to identify N4JS projects, this method returns the corresponding N4JS project name
 * which may or may not {@link #isProjectNameWithScope(String) include an npm scope}. The given URI may be a
 * {@link URI#isFile() file URI} (headless case) or a {@link URI#isPlatform() platform URI} (UI case).
 * <p>
 * For details on N4JS project name handling, see {@link #isProjectNameWithScope(String)}.
 */
public static String deriveN4JSProjectNameFromURI(URI uri) {
	if (uri == null) {
		return null;
	}
	int segCount = uri.segmentCount();
	String last = segCount > 0 ? uri.segment(segCount - 1) : null;
	if (uri.isPlatform()) {
		// due to Eclipse conventions we cannot influence, the scope name and plain project name are represented
		// within a single segment in platform URIs:
		if (last != null && last.startsWith(NPM_SCOPE_PREFIX)) {
			last = replaceFirst(last, NPM_SCOPE_SEPARATOR_ECLIPSE, NPM_SCOPE_SEPARATOR);
		}
		return last;
	} else if (uri.isFile()) {
		// due to conventions we cannot influence, the scope name and plain project name are represented as two
		// separate segments in file URIs:
		String secondToLast = segCount > 1 ? uri.segment(segCount - 2) : null;
		if (secondToLast != null && secondToLast.startsWith(NPM_SCOPE_PREFIX)) {
			return secondToLast + NPM_SCOPE_SEPARATOR + last;
		}
		return last;
	}
	throw new IllegalArgumentException("neither a file nor a platform URI: " + uri);
}
 
Example 3
Source File: N4JSModel.java    From n4js with Eclipse Public License 1.0 6 votes vote down vote up
private boolean isLocationInNestedInContainer(URI nestedLocation, IN4JSSourceContainer container) {
	URI containerLocation = container.getLocation().toURI();
	if (containerLocation == null || nestedLocation == null)
		return false;

	int maxSegments = containerLocation.segmentCount(); // directories have no empty last segment?
	if (nestedLocation.segmentCount() >= maxSegments) {
		for (int i = 0; i < maxSegments; i++) {
			if (!nestedLocation.segment(i).equals(containerLocation.segment(i))) {
				return false;
			}
		}
		return true;
	}
	return false;
}
 
Example 4
Source File: EclipseProjectPropertiesEncodingProvider.java    From xtext-core with Eclipse Public License 2.0 6 votes vote down vote up
protected String getFromProperties(URI uri) throws IOException {
	if (!uri.isHierarchical())
		return null;
	
	URI projectURI = uri;
	boolean projectFound = false;
	do {
		projectURI = projectURI.trimSegments(1);
		if (new File(projectURI.path(), ".project").exists()) {
			projectFound = true;
		}
		if (!projectFound && projectURI.segmentCount() == 0)
			// The resource is not contained in a project
			return null;
	} while (!projectFound);
	
	Properties properties = loadProperties(projectURI);
	URI resourceUri = URI.createHierarchicalURI(Arrays.copyOfRange(uri.segments(),
			projectURI.segmentCount(), uri.segmentCount()), null, null);
	return getValue(properties, resourceUri, ENCODING_PREFIX);
}
 
Example 5
Source File: ModelRegistry.java    From slr-toolkit with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Gets a file for a EMF resource.
 * 
 * @param A EMF resource
 * @return A file or null
 */
private IFile getFileFromResource(Resource resource)
{
	if (resource != null)
	{
		URI uri = resource.getResourceSet().getURIConverter().normalize(resource.getURI());
		if ("platform".equals(uri.scheme()) && uri.segmentCount() > 1 && "resource".equals(uri.segment(0)))
		{
			StringBuffer platformResourcePath = new StringBuffer();
			for (int i = 1, size = uri.segmentCount(); i < size; ++i)
			{
				platformResourcePath.append('/');
				platformResourcePath.append(uri.segment(i));
			}
			return ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(platformResourcePath.toString()));
		}
	}
	return null;
}
 
Example 6
Source File: ProjectDescriptionLoadListener.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
void onDescriptionLoaded(URI location) {
	if (location.isPlatformResource() && location.segmentCount() == 2) {
		IN4JSEclipseProject n4project = eclipseCore.create(location);
		IProject eclipseProject = n4project.getProject();
		updateProjectReferencesIfNecessary(eclipseProject);
	}
}
 
Example 7
Source File: PlatformResourceURI.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public PlatformResourceURI getParent() {
	URI uri = toURI();
	if (uri.segmentCount() > 2) {
		return new PlatformResourceURI(uri.trimSegments(1));
	}
	return null;
}
 
Example 8
Source File: SafeURI.java    From n4js with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Return the parent of this location. If this URI has a trailing path separator, the result will simply be a URI
 * stripped by this trailing separator.
 */
public U getParent() {
	URI uri = toURI();
	if (uri.segmentCount() <= 0) {
		return null;
	}
	return createFrom(uri.trimSegments(1));
}
 
Example 9
Source File: AbstractProjectAwareResourceDescriptionsProvider.java    From xtext-extras with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * @since 2.9
 */
protected boolean isProjectLocal(URI uri, final String encodedProjectName) {
	if (uri == null || uri.segmentCount() < 2 || !uri.isPlatformResource())
		return false;
	else 
		return !uri.segment(1).equals(encodedProjectName);
}
 
Example 10
Source File: AbstractDefaultFeatureValueProvider.java    From statecharts with Eclipse Public License 1.0 5 votes vote down vote up
protected String getProjectName(EObject contextElement) {
	URI uri = EcoreUtil.getURI(contextElement);
	if (uri.isPlatformResource() && uri.segmentCount() > 1) {
		return URI.decode(uri.segment(1)); // 0 is resource
	}
	return "ProjectName";
}
 
Example 11
Source File: Utils.java    From slr-toolkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns the resource file which contains the given bibtex document. Bases
 * on {@link http://www.eclipse.org/forums/index.php?t=msg&th=128695/}
 * 
 * @param doc
 *            {@link Document} whom's resource is wanted
 * @return {@link IFile} which contains doc. <code>null</code> if nothing
 *         was found.
 */
public static IFile getIFilefromEMFResource(Resource resource) {
	if (resource == null) {
		return null;
	}
	// TODO: remove logbuffer
	logBuffer = new StringBuffer();

	URI uri = resource.getURI();
	if(resource.getResourceSet() == null){
		return null;
	}
	uri = resource.getResourceSet().getURIConverter().normalize(uri);
	String scheme = uri.scheme();
	logBuffer.append("URI Scheme: " + scheme + "\n");

	if ("platform".equals(scheme) && uri.segmentCount() > 1
			&& "resource".equals(uri.segment(0))) {
		StringBuffer platformResourcePath = new StringBuffer();
		for (int j = 1, size = uri.segmentCount(); j < size; ++j) {
			platformResourcePath.append('/');
			platformResourcePath.append(uri.segment(j));
		}
		logBuffer.append("Platform path " + platformResourcePath.toString()
				+ "\n");

		IResource parent = ResourcesPlugin.getWorkspace().getRoot()
				.getFile(new Path(platformResourcePath.toString()));
		// TODO: remove syso
		logBuffer.append("IResource " + parent);

		if (parent instanceof IFile) {
			return (IFile) parent;
		}
	}
	// System.out.println(logBuffer.toString());
	return null;

}
 
Example 12
Source File: Utils.java    From slr-toolkit with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns the resource file which contains the given bibtex document. Bases
 * on {@link http://www.eclipse.org/forums/index.php?t=msg&th=128695/}
 * 
 * @param doc
 *            {@link Document} whom's resource is wanted
 * @return {@link IFile} which contains doc. <code>null</code> if nothing
 *         was found.
 */
public static IFile getIFilefromEMFResource(Resource resource) {
	if (resource == null) {
		return null;
	}
	// TODO: remove logbuffer
	logBuffer = new StringBuffer();

	URI uri = resource.getURI();
	if(resource.getResourceSet() == null){
		return null;
	}
	uri = resource.getResourceSet().getURIConverter().normalize(uri);
	String scheme = uri.scheme();
	logBuffer.append("URI Scheme: " + scheme + "\n");

	if ("platform".equals(scheme) && uri.segmentCount() > 1
			&& "resource".equals(uri.segment(0))) {
		StringBuffer platformResourcePath = new StringBuffer();
		for (int j = 1, size = uri.segmentCount(); j < size; ++j) {
			platformResourcePath.append('/');
			platformResourcePath.append(uri.segment(j));
		}
		logBuffer.append("Platform path " + platformResourcePath.toString()
				+ "\n");

		IResource parent = ResourcesPlugin.getWorkspace().getRoot()
				.getFile(new Path(platformResourcePath.toString()));
		// TODO: remove syso
		logBuffer.append("IResource " + parent);

		if (parent instanceof IFile) {
			return (IFile) parent;
		}
	}
	// System.out.println(logBuffer.toString());
	return null;

}
 
Example 13
Source File: HyperlinkXpectMethod.java    From n4js with Eclipse Public License 1.0 4 votes vote down vote up
private String getTargetDescription(XtextResource resource, IHyperlink hyperlink) {
	final StringBuffer sb = new StringBuffer();
	// append hyperlink text. Only consider the element name and ignore the qualified part.
	String hyperlinkText = hyperlink.getHyperlinkText();
	hyperlinkText = hyperlinkText.substring(
			hyperlinkText.lastIndexOf(N4JSHierarchicalNameComputerHelper.DELIMITER) + 1);
	final EObject target = getTarget(resource, hyperlink);

	if (target != null) {
		if (hyperlinkText != null)
			sb.append(hyperlinkText);
		else
			sb.append("<no hyperlink text>");
		// append description of target element (path from the element to the root of the AST)
		// build chain of ancestor AST elements
		sb.append(": ");
		final int startLen = sb.length();
		EObject currTarget = target;
		while (currTarget != null) {
			if (currTarget instanceof NamedElement || currTarget instanceof IdentifiableElement) {
				if (sb.length() > startLen)
					sb.append(" in ");
				String name = currTarget instanceof NamedElement ? ((NamedElement) currTarget).getName()
						: ((IdentifiableElement) currTarget).getName();
				if (name == null || name.trim().length() == 0)
					name = "<unnamed>";
				else
					name = "\"" + name + "\"";
				sb.append(name + "(" + currTarget.eClass().getName() + ")");
			}
			currTarget = currTarget.eContainer();
		}
		// add URI of resource
		final URI targetResURI = target.eResource() != null ? target.eResource().getURI() : null;
		final String fname = targetResURI != null ? targetResURI.lastSegment() : null;
		if (fname != null && fname.trim().length() > 0) {
			sb.append(" in file ");
			sb.append(fname);
		}

	} else {
		URI uri = getURI(hyperlink);
		if (uri != null) {
			if (uri.isFile()) {
				sb.append("file:/...");
				for (int i = Math.max(0, uri.segmentCount() - 2); i < uri.segmentCount(); i++) {
					sb.append("/");
					sb.append(uri.segment(i));
				}
			} else {
				sb.append(uri);
			}
		}
	}
	return sb.toString();
}