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

The following examples show how to use org.eclipse.emf.common.util.URI#toFileString() . 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: Storage2UriMapperImpl.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
private Iterable<Pair<IStorage, IProject>> getStorages(/* @NonNull */ URI uri, IFile file) {
	if (file == null || !file.isAccessible()) {
		Iterable<Pair<IStorage, IProject>> result = contribution.getStorages(uri);
		if (result == null || !result.iterator().hasNext()) {
			 // Handle files external to the workspace. But check contribution first to be backwards compatible.
			if (uri.isFile()) {
				Path filePath = new Path(uri.toFileString());
				IFileStore fileStore = EFS.getLocalFileSystem().getStore(filePath);
				IFileInfo fileInfo = fileStore.fetchInfo();
				if (fileInfo.exists() && !fileInfo.isDirectory()) {
					return Collections.singletonList(
							Tuples.<IStorage, IProject> create(new FileStoreStorage(fileStore, fileInfo, filePath), (IProject) null));
				}
			}
		}
		return result;
	}
	return Collections.singleton(Tuples.<IStorage, IProject> create(file, file.getProject()));
}
 
Example 2
Source File: GamlResourceServices.java    From gama with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Properly encodes and partially verifies the uri passed in parameter. In the case of an URI that does not use the
 * "resource:" scheme, it is first converted into a file URI so that headless operations that do not use a workspace
 * can still perform correctly
 *
 * @param uri
 * @return null if the parameter is null or does not represent neither a file or a resource, otherwise a properly
 *         encoded version of the parameter.
 */
public static URI properlyEncodedURI(final URI uri) {
	if (uri == null) { return null; }
	URI pre_properlyEncodedURI = uri;
	if (GAMA.isInHeadLessMode() && !uri.isPlatformResource()) {
		final String filePath = uri.toFileString();
		if (filePath == null) { return null; }
		final File file = new File(filePath);
		try {
			pre_properlyEncodedURI = URI.createFileURI(file.getCanonicalPath());
		} catch (final IOException e) {
			e.printStackTrace();
		}
	}
	final URI result = URI.createURI(pre_properlyEncodedURI.toString(), true);
	// if (DEBUG.IS_ON()) {
	// DEBUG.OUT("Original URI: " + uri + " => " + result);
	// }
	return result;
}
 
Example 3
Source File: GamlResourceServices.java    From gama with GNU General Public License v3.0 6 votes vote down vote up
public static String getProjectPathOf(final Resource r) {
	if (r == null) { return ""; }
	final URI uri = r.getURI();
	if (uri == null) { return ""; }
	// Cf. #2983 -- we are likely in a headless scenario
	if (uri.isFile()) {
		File project = new File(uri.toFileString());
		while (project != null && !isProject(project)) {
			project = project.getParentFile();
		}
		return project == null ? "" : project.getAbsolutePath();
	} else {
		final IPath path = getPathOf(r);
		final IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
		final IPath fullPath = file.getProject().getLocation();
		return fullPath == null ? "" : fullPath.toOSString();
	}
}
 
Example 4
Source File: GamlSyntacticConverter.java    From gama with GNU General Public License v3.0 6 votes vote down vote up
public static String getAbsoluteContainerFolderPathOf(final Resource r) {
	URI uri = r.getURI();
	if (uri.isFile()) {
		uri = uri.trimSegments(1);
		return uri.toFileString();
	} else if (uri.isPlatform()) {
		final IPath path = GamlResourceServices.getPathOf(r);
		final IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(path);
		final IContainer folder = file.getParent();
		return folder.getLocation().toString();
	}
	return URI.decode(uri.toString());

	// final IPath fullPath = file.getLocation();
	// path = fullPath; // toOSString ?
	// if (path == null) { return null; }
	// return path.uptoSegment(path.segmentCount() - 1);
}
 
Example 5
Source File: MultiProjectWorkspaceConfigFactory.java    From xtext-core with Eclipse Public License 2.0 5 votes vote down vote up
public void findProjects(WorkspaceConfig workspaceConfig, URI uri) {
	if (uri == null) {
		return;
	}
	File baseFile = new File(uri.toFileString());
	if (!baseFile.isDirectory()) {
		return;
	}
	for (File dir : baseFile.listFiles(File::isDirectory)) {
		FileProjectConfig project = new FileProjectConfig(dir, workspaceConfig);
		project.addSourceFolder(".");
		workspaceConfig.addProject(project);
	}
}
 
Example 6
Source File: DefaultGenArtifactConfigurations.java    From statecharts with Eclipse Public License 1.0 5 votes vote down vote up
protected String relative(URI uri) {
	if (uri.isFile()) {
		return uri.toFileString();
	} else if (uri.isPlatform()) {
		return uri.toPlatformString(true);
	}
	throw new IllegalArgumentException("Unknown URI " + uri);
}
 
Example 7
Source File: EMFHelper.java    From statecharts with Eclipse Public License 1.0 5 votes vote down vote up
public static IFile getIFileFromEMFUri(URI uri) {
	if (uri.isPlatformResource()) {
		String platformString = uri.toPlatformString(true);
		return (IFile) ResourcesPlugin.getWorkspace().getRoot().findMember(platformString);
	} else if (uri.isFile()) {
		File file = new File(uri.toFileString());
		if (!file.exists())
			return null;
		IFile[] files = ResourcesPlugin.getWorkspace().getRoot().findFilesForLocationURI(file.toURI());
		if (files.length == 1) {
			return files[0];
		}
	}
	return null;
}
 
Example 8
Source File: EmfUriUtil.java    From statecharts with Eclipse Public License 1.0 5 votes vote down vote up
public static IFile toFile(URI emfUri) {
	if (emfUri.isPlatformResource()) {
		String platformString = emfUri.toPlatformString(true);
		return (IFile) ResourcesPlugin.getWorkspace().getRoot().findMember(platformString);
	} else if (emfUri.isFile()) {
		File file = new File(emfUri.toFileString());
		if (!file.exists())
			return null;
		IFile[] files = ResourcesPlugin.getWorkspace().getRoot().findFilesForLocationURI(file.toURI());
		if (files.length == 1) {
			return files[0];
		}
	}
	return null;
}
 
Example 9
Source File: Animator.java    From txtUML with Eclipse Public License 1.0 5 votes vote down vote up
private void addAnimationMarker(EObject eobject) {
	IResource resource = null;
	IWorkspace workspace = ResourcesPlugin.getWorkspace(); 
	if (workspace != null) {
		URI uri = eobject.eResource().getURI();
		if (uri.isPlatform()) {
			resource = workspace.getRoot().getFile(new Path(uri.toPlatformString(true)));
		} else {
			IPath fs = new Path(uri.toFileString());
			for (IProject proj : workspace.getRoot().getProjects()) {
				if (proj.getLocation().isPrefixOf(fs)) {
					IPath relative = fs.makeRelativeTo(proj.getLocation());
					resource = proj.findMember(relative);
				}
			}
		}
	}
	if (resource != null && resource.exists()) {
		IMarker imarker = null;
		try {
			imarker = resource.createMarker(AnimationConfig.TXTUML_ANIMATION_MARKER_ID);
			imarker.setAttribute(EValidator.URI_ATTRIBUTE, URI.createPlatformResourceURI(resource.getFullPath().toString(), true) + "#" + EcoreUtil.getURI(eobject).fragment());
		} catch (CoreException e) {
			e.printStackTrace();
		}
		if (imarker != null) {
			IPapyrusMarker marker = PapyrusMarkerAdapter.wrap(eobject.eResource(), imarker);
			eobjectToMarker.put(eobject, marker);
		}
	}
}
 
Example 10
Source File: ImportHelper.java    From fixflow with Apache License 2.0 5 votes vote down vote up
/**
 * Returns an absolute canonical representation of the given URI.
 * 
 * A relative uri is interpreted as relative to the working directory and made absolute. Furthermore,
 * redundant segments ("./") from the path are removed.
 * @param uri A relative or absolute URI.
 * @return <code>uri</code> in absolute and canonical form, obtained by creating a {@linkplain File file} 
 * from it and taking its {@linkplain File#getCanonicalPath() canonical path}.
 */
public static URI makeURICanonical(URI uri) {
    if (uri.isFile()) {
        File tmpFile = new File(uri.toFileString());
        try {
            return URI.createFileURI(tmpFile.getCanonicalPath());
        } catch (IOException e) {
            return URI.createFileURI(tmpFile.getAbsolutePath());
        }
    } else
        return uri;
}
 
Example 11
Source File: AbstractDefinitionContentProvider.java    From bonita-studio with GNU General Public License v2.0 5 votes vote down vote up
public AbstractDefinitionContentProvider(final boolean userDefinitionOnly) {
    final AbstractDefinitionRepositoryStore<?> connectorDefStore = (AbstractDefinitionRepositoryStore<?>) RepositoryManager
            .getInstance().getRepositoryStore(
                    getDefStoreClass());
    connectorDefList = connectorDefStore.getDefinitions();
    if (userDefinitionOnly) {
        final List<ConnectorDefinition> toRemove = new ArrayList<ConnectorDefinition>();
        final String absolutePathOfConnectorDefStoreResource = connectorDefStore.getResource().getLocation().toFile().getAbsolutePath();
        for (final ConnectorDefinition definition : connectorDefList) {
            final Resource eResource = definition.eResource();
            if(eResource != null){
                final URI uri = eResource.getURI();
                if(uri != null){
                    final String path = uri.toFileString();
                    if (!path.contains(absolutePathOfConnectorDefStoreResource)) {
                        toRemove.add(definition);
                    }
                }
            } else {
                BonitaStudioLog.debug("A connectorDefinition is outside of a Resource: "+definition.getId(), "org.bonitasoft.studio.connectors.model.edit");
            }
        }
        connectorDefList.removeAll(toRemove);
    }
    final Bundle bundle = getBundle();
    messageProvider = DefinitionResourceProvider.getInstance(
            connectorDefStore, bundle);
    definitionCategoryContentProvider = new DefinitionCategoryContentProvider(messageProvider.getAllCategories());
    unloadableCategoryName = messageProvider.getUnloadableCategory().getId();
    unCategorizedCategory = messageProvider.getUncategorizedCategory();
}
 
Example 12
Source File: AbstractIdeTest.java    From n4js with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Returns the files's path and name relative to the test workspace's {@link #getRoot() root folder}. Intended for
 * use in output presented to the user (e.g. messages of assertion errors).
 */
protected String getRelativePathFromFileUri(FileURI fileURI) {
	URI relativeUri = lspBuilder.makeWorkspaceRelative(fileURI.toURI());
	return relativeUri.toFileString();
}
 
Example 13
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());
}
 
Example 14
Source File: IFileSystemScanner.java    From xtext-core with Eclipse Public License 2.0 4 votes vote down vote up
@Override
public void scan(URI root, IAcceptor<URI> acceptor) {
	File file = new File(root.toFileString());
	scanRec(file, acceptor);
}
 
Example 15
Source File: MDDUtil.java    From textuml with Eclipse Public License 1.0 4 votes vote down vote up
public static boolean doesRepositoryExist(URI repositoryURI) {
    if (!repositoryURI.isFile())
        return false;
    File repositoryDir = new File(repositoryURI.toFileString());
    return repositoryDir.isDirectory() && new File(repositoryDir, IRepository.MDD_PROPERTIES).isFile();
}
 
Example 16
Source File: URIUtils.java    From n4js with Eclipse Public License 1.0 2 votes vote down vote up
/**
 * Converts the given URI to a {@link File} iff it is a {@link org.eclipse.emf.common.util.URI#isFile() file URI};
 * otherwise returns <code>null</code>.
 * <p>
 * Same as {@link org.eclipse.emf.common.util.URI#toFileString()}, but returns a {@link File} instance instead of a
 * string.
 */
static public File toFile(URI uri) {
	return uri != null && uri.isFile() ? new File(uri.toFileString()) : null;
}