Skip to content

Package: WorkspaceUtil

WorkspaceUtil

nameinstructionbranchcomplexitylinemethod
getURIsInWorkspace(String)
M: 0 C: 16
100%
M: 0 C: 0
100%
M: 0 C: 1
100%
M: 0 C: 4
100%
M: 0 C: 1
100%
uriToFile(URI)
M: 24 C: 0
0%
M: 4 C: 0
0%
M: 3 C: 0
0%
M: 4 C: 0
0%
M: 1 C: 0
0%

Coverage

1: /*******************************************************************************
2: * Copyright (c) 2011-2018 EclipseSource Muenchen GmbH and others.
3: *
4: * All rights reserved. This program and the accompanying materials
5: * are made available under the terms of the Eclipse Public License 2.0
6: * which accompanies this distribution, and is available at
7: * https://www.eclipse.org/legal/epl-2.0/
8: *
9: * SPDX-License-Identifier: EPL-2.0
10: *
11: * Contributors:
12: * Lucas Koehler - initial API and implementation
13: ******************************************************************************/
14: package org.eclipse.emf.ecp.ide.spi.util;
15:
16: import java.io.File;
17: import java.net.MalformedURLException;
18: import java.util.ArrayList;
19: import java.util.List;
20:
21: import org.eclipse.core.resources.IResource;
22: import org.eclipse.core.resources.IResourceVisitor;
23: import org.eclipse.core.resources.IWorkspace;
24: import org.eclipse.core.resources.ResourcesPlugin;
25: import org.eclipse.core.runtime.CoreException;
26: import org.eclipse.core.runtime.Path;
27: import org.eclipse.emf.common.util.URI;
28:
29: /**
30: * Utility class containing common functionality for services using the workspace.
31: *
32: * @author Lucas Koehler
33: * @since 1.17
34: *
35: */
36: public final class WorkspaceUtil {
37:
38:         // Utility class should not be instantiated by clients.
39:         private WorkspaceUtil() {
40:         }
41:
42:         /**
43:          * Get the {@link URI URIs} of all files in the workspace that have the given file extension.
44:          *
45:          * @param fileExtension The file extension of the files to search for in the workspace.
46:          * @return The list of {@link URI URIs}
47:          * @throws CoreException If something goes wrong while analyzing the workspace
48:          */
49:         public static List<URI> getURIsInWorkspace(final String fileExtension) throws CoreException {
50:                 final ArrayList<URI> uris = new ArrayList<URI>();
51:                 final IWorkspace workspace = ResourcesPlugin.getWorkspace();
52:                 workspace.getRoot().accept(new IResourceVisitor() {
53:                         @Override
54:                         public boolean visit(IResource resource) throws CoreException {
55:                                 if (resource.getFileExtension() != null && resource.getFileExtension().equals(fileExtension)) {
56:                                         try {
57:                                                 uris.add(URI.createURI(resource.getLocationURI().toURL().toExternalForm()));
58:                                         } catch (final MalformedURLException ex) {
59:                                                 return false;
60:                                         }
61:                                 }
62:                                 if (resource.getType() == IResource.FILE) {
63:                                         return false;
64:                                 }
65:                                 return true;
66:                         }
67:                 });
68:                 return uris;
69:         }
70:
71:         /**
72:          * Converts an EMF {@link URI} to a Java {@link File}.
73:          *
74:          * @param uri The {@link URI} to convert
75:          * @return The Java {@link File}
76:          */
77:         public static File uriToFile(URI uri) {
78:•                if (uri.isFile() && !uri.isRelative()) {
79:                         return new Path(uri.toFileString()).toFile();
80:                 }
81:                 return ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(uri.path()))
82:                         .getLocation().toFile();
83:         }
84: }