Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2009 itemis AG (http://www.itemis.eu) and others. All rights reserved. This program and the accompanying materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution, and is available at http://www.eclipse.org/legal/epl-v10.html /
  
  package org.eclipse.xtext.ui.resource;
  
 import static com.google.common.collect.Lists.*;
 import static com.google.common.collect.Maps.*;
 import static com.google.common.collect.Sets.*;
 import static java.util.Collections.*;
 
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 import  org.eclipse.core.resources.IFile;
 import  org.eclipse.core.resources.IFolder;
 import  org.eclipse.core.resources.IProject;
 import  org.eclipse.core.resources.IResource;
 import  org.eclipse.core.resources.IStorage;
 import  org.eclipse.core.resources.IWorkspace;
 import  org.eclipse.core.runtime.IPath;
 import  org.eclipse.core.runtime.Path;
 import  org.eclipse.jdt.core.ElementChangedEvent;
 import  org.eclipse.jdt.core.IElementChangedListener;
 import  org.eclipse.jdt.core.IJarEntryResource;
 import  org.eclipse.jdt.core.IJavaElement;
 import  org.eclipse.jdt.core.IJavaElementDelta;
 import  org.eclipse.jdt.core.IJavaModel;
 import  org.eclipse.jdt.core.IJavaProject;
 import  org.eclipse.jdt.core.IPackageFragmentRoot;
 import  org.eclipse.jdt.core.JavaCore;
 import  org.eclipse.jdt.core.JavaModelException;
 import  org.eclipse.jdt.internal.core.JavaProject;
 

Author(s):
Sven Efftinge - Initial contribution and API
Noextend:
This class is not intended to be subclassed by clients.
 
 public class Storage2UriMapperJavaImpl implements IStorage2UriMapperJdtExtensionsIStorage2UriMapperContribution, IElementChangedListener {
 	
 	private static final Logger log = Logger.getLogger(Storage2UriMapperJavaImpl.class);

Since:
2.5
 
 	public static class PackageFragmentRootData {
 		public URI uriPrefix;
 		public final Object modificationStamp;
 		public final Map<String, IPackageFragmentRoot> associatedRoots;
 
 		public PackageFragmentRootData(Object modificationStamp) {
 			this. = modificationStamp;
 		}
 
 		public Map<URI, IStorage> uri2Storage = newLinkedHashMap();
 		
 		public String toString() {
 			return getPath() + " / " + ;
 		}
 
 		public boolean exists() {
 			if (.size() == 0) {
 				return false;
 			}
 			return .values().iterator().next().exists();
 		}
 
 		public IPath getPath() {
 			if (.size() == 0) {
 				return null;
 			}
 			return .values().iterator().next().getPath();
 		}
 
 		public void addRoot(IPackageFragmentRoot root) {
 			if (root != null) {
				.put(root.getHandleIdentifier(), root);
			}
		}
	}
Public for testing purpose

Since:
2.4
Nooverride:
This method is not intended to be re-implemented or extended by clients.
Noreference:
This method is not intended to be referenced by clients.
	public void setJdtHelper(IJdtHelper jdtHelper) {
		this. = jdtHelper;
	}

Public for testing purpose

Nooverride:
This method is not intended to be re-implemented or extended by clients.
Noreference:
This method is not intended to be referenced by clients.
	public void setLocator(JarEntryLocator locator) {
		this. = locator;
	}

Public for testing purpose

Nooverride:
This method is not intended to be re-implemented or extended by clients.
Noreference:
This method is not intended to be referenced by clients.
	public void setUriValidator(UriValidator uriValidator) {
		this. = uriValidator;
	}

Public for testing purpose

Since:
2.5
Nooverride:
This method is not intended to be re-implemented or extended by clients.
Noreference:
This method is not intended to be referenced by clients.
	public void setHost(IStorage2UriMapper host) {
		this. = host;
	}
Rejects Java output folders when traversing a project.

Returns:
true if the folder is a java output folder. Otherwise false.
Since:
2.5
	public boolean isRejected(/* @NonNull */ IFolder folder) {
		return .isFromOutputPath(folder);
	}

Since:
2.4
	public Pair<URIURIgetURIMapping(IPackageFragmentRoot rootthrows JavaModelException {
		if (data.uriPrefix == null)
			return null;
		IPath path = root.isExternal() ? root.getPath() : root.getUnderlyingResource().getLocation();
		URI physical = null;
		if (root.isArchive()) {
			String archiveScheme = "zip".equalsIgnoreCase(root.getPath().getFileExtension()) ? "zip" : "jar";
			physical = URI.createURI(archiveScheme+":file:"+path.toFile().getPath()+"!/");
else {
			physical = URI.createFileURI(path.toFile().getPath()+"/");
		}
		return Tuples.create(data.uriPrefixphysical);
	}

Since:
2.4
	public Map<URI, IStorage> getAllEntries(IPackageFragmentRoot root) {
		try {
			IResource underlyingResource = root.getUnderlyingResource();
			if (underlyingResource instanceof IFolder) {
				return .getAllEntries((IFolder) underlyingResource);
			}
catch (JavaModelException e) {
			if (!e.isDoesNotExist())
				.error(e.getMessage(), e);
			return emptyMap();
		}
		return data.uri2Storage;
	}
	private PackageFragmentRootData getData(IPackageFragmentRoot root) {
		final boolean isCachable = root.isArchive() || root.isExternal();
		if (isCachable) {
			return getCachedData(root);
		}
		return data;
	}
	private PackageFragmentRootData getCachedData(IPackageFragmentRoot root) {
		final String path = root.getPath().toString();
				if (isUpToDate(dataroot)) {
					data.addRoot(root);
					return data;
else {
				}
			}
		}
		}
		return data;
	}
	private boolean isUpToDate(PackageFragmentRootData data, IPackageFragmentRoot root) {
		return Objects.equal(data.modificationStampcomputeModificationStamp(root));
	}
	private Object computeModificationStamp(IPackageFragmentRoot root) {
		try {
			if (root.exists()) {
				IResource resource = root.getUnderlyingResource();
				if (resource != null) {
					return resource.getLocation().toFile().lastModified();
				}
				return root.getPath().toFile().lastModified();
			}
catch (JavaModelException e) {
			.error(e.getMessage(), e);
		}
		return new Object();
	}

Since:
2.4
	protected PackageFragmentRootData initializeData(final IPackageFragmentRoot root) {
		data.addRoot(root);
		try {
				protected URI getURI(IFile file,
						return null;
					return super.getURI(filestate);
				}
				protected URI getURI(IJarEntryResource jarEntry,
						return null;
					final URI uri = .getURI(rootjarEntrystate);
					if (!.isValid(urijarEntry))
						return null;
					return uri;
				}
				protected Void handle(URI uri, IStorage storage,
					data.uri2Storage.put(uristorage);
					return null;
				}
			};
			walker.traverse(rootfalse);
			if (walker.getBundleSymbolicName() != null)
				data.uriPrefix = URI.createPlatformResourceURI(walker.getBundleSymbolicName()+"/"true);
catch (RuntimeException e) {
catch (JavaModelException e) {
			.debug(e.getMessage(), e);
		}
		return data;
	}
	/* @NonNull */
	public Iterable<Pair<IStorage, IProject>> getStorages(/* @NonNull */ URI uri) {
		List<Pair<IStorage, IProject>> result = newArrayListWithCapacity(1);
		List<PackageFragmentRootDatapackageFragmentRootDatas;
			packageFragmentRootDatas = newArrayList(.values());
		}
		Iterator<PackageFragmentRootDataiterator = packageFragmentRootDatas.iterator();
		while (iterator.hasNext()) {
			PackageFragmentRootData data = iterator.next();
			if (data.exists()) {
				if (data.uriPrefix == null || uri.toString().startsWith(data.uriPrefix.toString())) {
					IStorage storage = data.uri2Storage.get(uri);
					if (storage != null) {
						for (IPackageFragmentRoot root : data.associatedRoots.values()) {
							result.add(Tuples.create(storageroot.getJavaProject().getProject()));
						}
					}
				}
else {
				iterator.remove();
			}
		}
		if (result.isEmpty() && uri.isArchive()) {
			String authority = uri.authority();
			authority = authority.substring(0, authority.length() - 1);
			URI archiveURI = URI.createURI(authority);
			if (archiveURI.isFile() || archiveURI.isPlatformResource()) {
				IPath archivePath = new Path(archiveURI.isPlatformResource()? archiveURI.toPlatformString(true): archiveURI.toFileString());
				for (PackageFragmentRootData data : packageFragmentRootDatas) {
					if (data.uriPrefix != null && archivePath.equals(data.getPath())) {
						// prefixes have an empty last segment.
						URI prefix = data.uriPrefix.lastSegment().length()==0 ? data.uriPrefix.trimSegments(1) : data.uriPrefix;
						URI expectedURI = prefix.appendSegments(uri.segments());
						IStorage storage = data.uri2Storage.get(expectedURI);
						if (storage != null) {
							for (IPackageFragmentRoot root : data.associatedRoots.values()) {
								result.add(Tuples.create(storageroot.getJavaProject().getProject()));
							}
						}
					}
				}
			}
		}
		return result;
	}

Since:
2.5
	public URI getUri(/* @NonNull */ IStorage storage) {
		if (storage instanceof IJarEntryResource) {
			final IJarEntryResource casted = (IJarEntryResource) storage;
			IPackageFragmentRoot packageFragmentRoot = casted.getPackageFragmentRoot();
			Map<URI, IStorage> data = getAllEntries(packageFragmentRoot);
			for (Map.Entry<URI, IStorage> entry : data.entrySet()) {
				if (entry.getValue().equals(casted))
					return entry.getKey();
			}
			if (packageFragmentRoot.exists() && packageFragmentRoot.isArchive()) {
				IPath jarPath = packageFragmentRoot.getPath();
				URI jarURI;
				if (packageFragmentRoot.isExternal()) {
					jarURI = URI.createFileURI(jarPath.toOSString());
else {
					jarURI = URI.createPlatformResourceURI(jarPath.toString(), true);
				}
				URI result = URI.createURI("archive:" + jarURI + "!" + storage.getFullPath());
				return result;
			}
		}
		return null;
	}
	public void elementChanged(ElementChangedEvent event) {
		Set<IJavaProject> javaProjectsWithClasspathChange = getJavaProjectsWithClasspathChange(event.getDelta());
		if(!javaProjectsWithClasspathChange.isEmpty()) {
			for(IJavaProject projectjavaProjectsWithClasspathChange) {
				updateCache(project);
			}
		for(IJavaElementDelta projectDeltagetProjectDeltas(event.getDelta())) {
			IJavaProject project = (IJavaProject) projectDelta.getElement();
			if((projectDelta.getKind() & IJavaElementDelta.REMOVED) != 0) {
				clearCache(project, Collections.<PackageFragmentRootData>emptySet());
				return;
			switch(projectDelta.getFlags()) {
				case IJavaElementDelta.F_OPENED: 
					updateCache(project);
					break;
				case IJavaElementDelta.F_CLOSED:
					clearCache(project, Collections.<PackageFragmentRootData>emptySet());
					break;
			}
		}
	}
	private IWorkspace workspace;

Since:
2.9
	public void setWorkspace(IWorkspace workspace) {
		this. = workspace;
	}
	private volatile boolean isInitialized = false;

Since:
2.4
	private void updateCache(IJavaProject project) {
		try {
			for(IPackageFragmentRoot rootproject.getPackageFragmentRoots()) {
				boolean isCachable = root.isArchive() || root.isExternal();
				if(isCachable
					datas.add(getCachedData(root));
			}
			clearCache(projectdatas);
catch (JavaModelException e) {
			.error("Error getting package fragments roots of " + project.getElementName(), e);
		}
	}
	private void clearCache(IJavaProject projectSet<PackageFragmentRootDatatoBeKept) {
		}
		for (PackageFragmentRootData data : values) {
			if (toBeKept.contains(data)) {
				continue;
			}
			Iterator<Entry<String, IPackageFragmentRoot>> i = data.associatedRoots.entrySet().iterator();
			while (i.hasNext()) {
				Entry<String, IPackageFragmentRoot> root = i.next();
				if (project.equals(root.getValue().getJavaProject())) {
					i.remove();
				}
			}
			if (data.associatedRoots.size() == 0) {
				toBeRemoved.add(data);
else {
				// get rid of cached storages that still point to roots / projects that are no longer available
				IPackageFragmentRoot someRoot = IterableExtensions.head(data.associatedRoots.values());
				PackageFragmentRootData newlyCollected = initializeData(someRoot);
				data.uri2Storage = newlyCollected.uri2Storage;
			}
		}
		if(!toBeRemoved.isEmpty()) {
			}
		}
	}

Since:
2.4
	public void initializeCache() {
			synchronized (this) {
					for(IProject project.getRoot().getProjects()) {
						if(project.isAccessible() && JavaProject.hasJavaNature(project)) {
							IJavaProject javaProject = JavaCore.create(project);
							updateCache(javaProject);
						}
					}
					 = true;
				}
			}
		}
	}
	private Set<IJavaElementDelta> getProjectDeltas(IJavaElementDelta delta) {
		IJavaElement element = delta.getElement();
		if(delta.getElement().getElementType() == IJavaElement.JAVA_PROJECT) {
			return Collections.singleton(delta);
		}
		Set<IJavaElementDelta> result = null;
		if(element instanceof IJavaModel) {
			for(IJavaElementDelta childdelta.getAffectedChildren()) {
				Set<IJavaElementDelta> projectDeltas = getProjectDeltas(child);
				if(!projectDeltas.isEmpty()) {
					if(result == null)
						result = newLinkedHashSet(); 
					result.addAll(projectDeltas);
				}
			}
		}
		return result == null ? Collections.<IJavaElementDelta>emptySet() : result;
	}
	private Set<IJavaProject> getJavaProjectsWithClasspathChange(IJavaElementDelta delta) {
		IJavaElement element = delta.getElement();
		if (element instanceof IPackageFragmentRoot) {
			IPackageFragmentRoot root = (IPackageFragmentRoot) element;
			if (delta.getKind() == IJavaElementDelta.REMOVED || delta.getKind() == IJavaElementDelta.ADDED
					|| (delta.getFlags() & IJavaElementDelta.F_REORDER) != 0
					|| (delta.getFlags() & IJavaElementDelta.F_REMOVED_FROM_CLASSPATH) != 0
					|| (delta.getFlags() & IJavaElementDelta.F_ADDED_TO_CLASSPATH) != 0
					|| (root.isExternal() && (delta.getFlags() & // external folders change
					(IJavaElementDelta.F_CONTENT | IJavaElementDelta.F_SOURCEATTACHED | IJavaElementDelta.F_SOURCEDETACHED)) == delta
							.getFlags())) {
				return Collections.singleton(root.getJavaProject());
			}
else if (element instanceof IJavaModel) {
			return getPackageFragmentRootDeltas(delta.getAffectedChildren());
else if (element instanceof IJavaProject) {
			if ((delta.getFlags() & IJavaElementDelta.F_CLASSPATH_CHANGED) != 0
					|| (delta.getFlags() & IJavaElementDelta.F_RESOLVED_CLASSPATH_CHANGED) != 0)
				return Collections.singleton((IJavaProject) element);
			return getPackageFragmentRootDeltas(delta.getAffectedChildren());
		}
		return Collections.emptySet();
	}
	private Set<IJavaProject> getPackageFragmentRootDeltas(IJavaElementDelta[] affectedChildren) {
		LinkedHashSet<IJavaProject> set = Sets.newLinkedHashSet();
		for (IJavaElementDelta delta : affectedChildren) {
		}
		return set;
	}
New to GrepCode? Check out our FAQ X