Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright (c) 2004, 2009 Richard Hoefter 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 Contributors: Richard Hoefter (richard.hoefter@web.de) - initial API and implementation, bug 95300, bug 95297, bug 128104, bug 201180 IBM Corporation - NLS'ing and incorporating into Eclipse. - Bug 177833 Class created from combination of all utility classes of contribution - Bug 267459 Java project with an external jar file from C:\ on the build path throws a NPE during the Ant Buildfile generation. /
 
 
 package org.eclipse.ant.internal.ui.datatransfer;
 
 import java.io.File;
 import java.net.URI;
 import java.util.List;
 import java.util.Map;
 import java.util.Set;
 
 
 import  org.eclipse.jdt.junit.JUnitCore;
 import  org.eclipse.swt.widgets.Shell;
Collection of utility methods to help when exporting to an Ant build file.
 
 public class ExportUtil {
 	private ExportUtil() {
 	}

Get resource from selection.
 
 	public static IResource getResource(ISelection selection) {
 		if (selection instanceof IStructuredSelection) {
 			for (Iterator iter = ((IStructuredSelectionselection).iterator(); iter
 					.hasNext();) {
 				IAdaptable adaptable = (IAdaptableiter.next();
 				return (IResourceadaptable.getAdapter(IResource.class);
 			}
 		}
 		return null;
	}

Get Java project from resource.
	public static IJavaProject getJavaProjectByName(String name) {
		try {
			IProject project = ResourcesPlugin.getWorkspace().getRoot()
					.getProject(name);
			if (project.exists()) {
				return JavaCore.create(project);
			}
catch (IllegalArgumentException iae) {
		}
		return null;
	}

Get project root for given project.
	public static String getProjectRoot(IJavaProject project) {
		if (project == null) {
			return null;
		}
		IResource resource = project.getResource();
		if (resource == null) {
			return null;
		}
		IPath location = resource.getLocation();
		if (location == null) {
			return null;
		}
		return location.toString();
	}

Convert Eclipse path to absolute filename.

Parameters:
file Project root optionally followed by resource name. An absolute path is simply converted to a string.
Returns:
full qualified path
	public static String resolve(IPath file) {
		if (file == null) {
			return null;
		}
		try {
			IFile f = ResourcesPlugin.getWorkspace().getRoot().getFile(file);
			URI uri = f.getLocationURI();
			return (uri != null) ? uri.toString() : f.toString();
catch (IllegalArgumentException e) {
			// resource is missing
			String projectName = removePrefix(file.toString(), "/"); //$NON-NLS-1$
			IJavaProject project = getJavaProjectByName(projectName);
			if (project != null) {
				return getProjectRoot(project);
			}
			// project is null because file is not enclosed in a project i.e.
			// external jar
			// https://bugs.eclipse.org/bugs/show_bug.cgi?id=267459
			return file.toOSString();
		}
	}

Get Java project for given root.
	public static IJavaProject getJavaProject(String root) {
		IPath path = new Path(root);
		if (path.segmentCount() == 1) {
			return getJavaProjectByName(root);
		}
		IResource resource = ResourcesPlugin.getWorkspace().getRoot()
				.findMember(path);
		if (resource != null && resource.getType() == .) {
			if (resource.exists()) {
				return (IJavaProject) JavaCore.create(resource);
			}
		}
		return null;
	}

Remove project root from given project file.
	public static String removeProjectRoot(String fileIProject project) {
		String res = removePrefix(file'/' + project.getName() + '/');
		if (res.equals('/' + project.getName())) {
			return "."//$NON-NLS-1$
		}
		return res;
	}

Remove project root from given project file.

Parameters:
newProjectRoot replace project root, e.g. with a variable ${project.location}
	public static String replaceProjectRoot(String fileIProject project,
			String newProjectRoot) {
		String res = removeProjectRoot(fileproject);
		if (res.equals(".")) //$NON-NLS-1$
		{
			return newProjectRoot;
		}
		if (newProjectRoot == null) {
			return res;
		}
		if (!res.equals(file)) {
			return newProjectRoot + '/' + res;
		}
		return res;
	}

Get for given project all directly dependent projects.

Returns:
set of IJavaProject objects
	public static List getClasspathProjects(IJavaProject project)
		List projects = new ArrayList();
		IClasspathEntry entries[] = project.getRawClasspath();
		addClasspathProjects(projectsentries);
		return sortProjectsUsingBuildOrder(projects);
	}
	private static void addClasspathProjects(List projects,
			IClasspathEntry[] entries) {
		for (int i = 0; i < entries.lengthi++) {
			IClasspathEntry classpathEntry = entries[i];
			if (classpathEntry.getContentKind() == .
					&& classpathEntry.getEntryKind() == .) {
				// found required project on build path
				String subProjectRoot = classpathEntry.getPath().toString();
				IJavaProject subProject = getJavaProject(subProjectRoot);
				// is project available in workspace
				if (subProject != null) {
					projects.add(subProject);
				}
			}
		}
	}

Get for given project all directly and indirectly dependent projects.

Returns:
set of IJavaProject objects
		LinkedList result = new LinkedList();
	}
	private static void getClasspathProjectsRecursive(IJavaProject project,
			LinkedList resultthrows JavaModelException {
		List projects = getClasspathProjects(project);
		for (Iterator iter = projects.iterator(); iter.hasNext();) {
			IJavaProject javaProject = (IJavaProjectiter.next();
			if (!result.contains(javaProject)) {
				result.addFirst(javaProject);
				getClasspathProjectsRecursive(javaProjectresult); // recursion
			}
		}
	}

Sort projects according to General -> Workspace -> Build Order.

Parameters:
projects list of IJavaProject objects
Returns:
list of IJavaProject objects with new order
	private static List sortProjectsUsingBuildOrder(List javaProjects) {
		if (javaProjects.isEmpty()) {
			return javaProjects;
		}
		List result = new ArrayList(javaProjects.size());
		IWorkspace workspace = ResourcesPlugin.getWorkspace();
		String[] buildOrder = workspace.getDescription().getBuildOrder();
		if (buildOrder == null) {// default build order
			IProject[] projects = new IProject[javaProjects.size()];
			int i = 0;
			for (Iterator iter = javaProjects.iterator(); iter.hasNext(); i++) {
				IJavaProject javaProject = (IJavaProjectiter.next();
				projects[i] = javaProject.getProject();
			}
			IWorkspace.ProjectOrder po = ResourcesPlugin.getWorkspace()
			projects = po.projects;
			buildOrder = new String[projects.length];
			for (i = 0; i < projects.lengthi++) {
				buildOrder[i] = projects[i].getName();
			}
		}
		for (int i = 0; i < buildOrder.length && !javaProjects.isEmpty(); i++) {
			String projectName = buildOrder[i];
			for (Iterator iter = javaProjects.iterator(); iter.hasNext();) {
				IJavaProject javaProject = (IJavaProjectiter.next();
				if (javaProject.getProject().getName().equals(projectName)) {
					result.add(javaProject);
					iter.remove();
				}
			}
		}
		// add any remaining projects not specified in the build order
		result.addAll(javaProjects);
		return result;
	}

Check if given project has a cyclic dependency.

See org.eclipse.jdt.core.tests.model.ClasspathTests.numberOfCycleMarkers.

	public static boolean hasCyclicDependency(IJavaProject javaProject)
			throws CoreException {
		IMarker[] markers = javaProject.getProject().findMarkers(
		for (int i = 0; i < markers.lengthi++) {
			IMarker marker = markers[i];
			String cycleAttr = (Stringmarker
			if (cycleAttr != null && cycleAttr.equals("true")) //$NON-NLS-1$
			{
				return true;
			}
		}
		return false;
	}

Find JUnit tests. Same tests are also returned by Eclipse run configuration wizard.

Parameters:
containerHandle project, package or source folder
	public static IType[] findTestsInContainer(String containerHandle) {
		IJavaElement container = JavaCore.create(containerHandle);
		if (container == null) {
			return new IType[0];
		}
		try {
			return JUnitCore
					.findTestTypes(containernew NullProgressMonitor());
			AntUIPlugin.log(e);
catch (CoreException e) {
			AntUIPlugin.log(e);
		}
		return new IType[0];
	}

Compares projects by project name.
	public static synchronized Comparator getJavaProjectComparator() {
		if ( == null) {
		}
	}
	private static class JavaProjectComparator implements Comparator {
		public int compare(Object o1Object o2) {
		}
	}

Compares objects by class name.
	public static synchronized Comparator getClassnameComparator() {
		if ( == null) {
		}
	}
	private static class ClassnameComparator implements Comparator {
		public int compare(Object o1Object o2) {
			return o1.getClass().getName().compareTo(o2.getClass().getName());
		}
	}

Compares IFile objects.
	public static synchronized Comparator getIFileComparator() {
		if ( == null) {
		}
	}
	private static Comparator fileComparator;
	private static class IFileComparator implements Comparator {
		public int compare(Object o1Object o2) {
			IFile f1 = (IFileo1;
			IFile f2 = (IFileo2;
			return f1.toString().compareTo(f2.toString());
		}
	}

Compares IType objects.
	public static synchronized Comparator getITypeComparator() {
		if ( == null) {
		}
	}
	private static Comparator typeComparator;
	private static class TypeComparator implements Comparator {
		public int compare(Object o1Object o2) {
			IType t1 = (ITypeo1;
			IType t2 = (ITypeo2;
		}
	}

Platform specific newline character(s).
	public static final String NEWLINE = System.getProperty("line.separator"); //$NON-NLS-1$
	public static String removePrefix(String sString prefix) {
		if (s == null) {
			return null;
		}
		if (s.startsWith(prefix)) {
			return s.substring(prefix.length());
		}
		return s;
	}

Remove suffix from given string.
	public static String removeSuffix(String sString suffix) {
		if (s == null) {
			return null;
		}
		if (s.endsWith(suffix)) {
			return s.substring(0, s.length() - suffix.length());
		}
		return s;
	}

Remove prefix and suffix from given string.
	public static String removePrefixAndSuffix(String sString prefix,
			String suffix) {
		return removePrefix(removeSuffix(ssuffix), prefix);
	}

Convert document to formatted XML string.
	public static String toString(Document doc)
		// NOTE: There are different transformer implementations in the wild,
		// which are configured differently
		// regarding the indent size:
		// Java 1.4: org.apache.xalan.transformer.TransformerIdentityImpl
		// Java 1.5:
		// com.sun.org.apache.xalan.internal.xsltc.trax.TransformerImpl
		StringWriter writer = new StringWriter();
		Source source = new DOMSource(doc);
		Result result = new StreamResult(writer);
		TransformerFactory factory = TransformerFactory.newInstance();
		boolean indentFallback = false;
		try {
			// indent using TransformerImpl
			factory.setAttribute("indent-number""4"); //$NON-NLS-1$ //$NON-NLS-2$
catch (IllegalArgumentException e) {
			// option not supported, set indent size below
			indentFallback = true;
		}
		Transformer transformer = factory.newTransformer();
		transformer.setOutputProperty(."yes"); //$NON-NLS-1$
		if (indentFallback) {
			// indent using TransformerIdentityImpl
			transformer.setOutputProperty(
					"{http://xml.apache.org/xslt}indent-amount""4"); //$NON-NLS-1$ //$NON-NLS-2$
		}
		transformer.transform(sourceresult);
		return writer.toString();
	}

Read XML file.
	public static Document parseXmlFile(File filethrows SAXException,
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		factory.setValidating(false);
		Document doc = factory.newDocumentBuilder().parse(file);
		return doc;
	}

Read XML string.
	public static Document parseXmlString(String sthrows SAXException,
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		factory.setValidating(false);
		Document doc = factory.newDocumentBuilder().parse(
		return doc;
	}

Converts collection to a separated string.

Parameters:
c collection
separator string to separate items
Returns:
collection items separated with given separator
	public static String toString(Collection cString separator) {
		for (Iterator iter = c.iterator(); iter.hasNext();) {
			b.append((Stringiter.next());
			b.append(separator);
		}
		if (c.size() > 0) {
			b.delete(b.length() - separator.length(), b.length());
		}
		return b.toString();
	}

Remove duplicates preserving original order.

Parameters:
l list to remove duplicates from
Returns:
new list without duplicates
	public static List removeDuplicates(List l) {
		List res = new ArrayList();
		for (Iterator iter = l.iterator(); iter.hasNext();) {
			Object element = iter.next();
			if (!res.contains(element)) {
				res.add(element);
			}
		}
		return res;
	}

Check if given file exists that was not written by this export.
	public static boolean existsUserFile(String filename) {
		File buildFile = new File(filename);
		if (buildFile.exists()) {
			BufferedReader in = null;
			try {
				in = new BufferedReader(new FileReader(buildFile));
				String line;
				while ((line = in.readLine()) != null) {
					if (line.indexOf(warning) != -1) {
						return false;
					}
				}
				return true;
catch (FileNotFoundException e) {
				return false;
catch (IOException e) {
				return false;
finally {
				try {
					if (in != null) {
						in.close();
					}
catch (IOException e) {
					// ignore
				}
			}
		}
		return false;
	}

Request write access to given file. Depending on the version control plug-in opens a confirm checkout dialog.

Parameters:
shell parent instance for dialogs
file file to request write access for
Returns:
true if user confirmed checkout
	public static boolean validateEdit(Shell shellIFile file) {
		return file.getWorkspace().validateEdit(new IFile[] { file }, shell)
				.isOK();
	}

Request write access to given files. Depending on the version control plug-in opens a confirm checkout dialog.

Parameters:
shell parent instance for dialogs
Returns:
IFile objects for which user confirmed checkout
Throws:
CoreException thrown if project is under version control, but not connected
	public static Set validateEdit(Shell shellList files)
			throws CoreException {
		Set confirmedFiles = new TreeSet(getIFileComparator());
		IStatus status = ((IFilefiles.get(0)).getWorkspace().validateEdit(
				(IFile[]) files.toArray(new IFile[files.size()]), shell);
		if (status.isMultiStatus() && status.getChildren().length > 0) {
			for (int i = 0; i < status.getChildren().lengthi++) {
				IStatus statusChild = status.getChildren()[i];
				if (statusChild.isOK()) {
					confirmedFiles.add(files.get(i));
				}
			}
else if (status.isOK()) {
			for (Iterator iterator = files.iterator(); iterator.hasNext();) {
				IFile file = (IFileiterator.next();
				confirmedFiles.add(file);
			}
		}
		if (status.getSeverity() == .) {
			// not possible to checkout files: not connected to version
			// control plugin or hijacked files and made read-only, so
			// collect error messages provided by validator and re-throw
			StringBuffer message = new StringBuffer(status.getPlugin() + ": " //$NON-NLS-1$
status.getMessage() + );
			if (status.isMultiStatus()) {
				for (int i = 0; i < status.getChildren().lengthi++) {
					IStatus statusChild = status.getChildren()[i];
					message.append(statusChild.getMessage() + );
				}
			}
					., 0, message.toString(), null));
		}
		return confirmedFiles;
	}

Check if given classpath is a reference to the default classpath of the project. Ideal for testing if runtime classpath was customized.
	public static boolean isDefaultClasspath(IJavaProject project,
			EclipseClasspath classpath) {
		// default classpath contains exactly the JRE and the project reference
		List list = removeDuplicates(classpath.rawClassPathEntries);
		if (list.size() != 2) {
			return false;
		}
		String entry1 = (Stringlist.get(0);
		String entry2 = (Stringlist.get(1);
		if (!EclipseClasspath.isJreReference(entry1)) {
			return false;
		}
		if (EclipseClasspath.isProjectReference(entry2)) {
			IJavaProject referencedProject = EclipseClasspath
			if (referencedProject == null) {
				// project was not loaded in workspace
				return false;
else if (referencedProject.getProject().getName().equals(
					project.getProject().getName())) {
				return true;
			}
		}
		return false;
	}

Add variable/value for Eclipse variable. If given string is no variable, nothing is added.

Parameters:
variable2valueMap property map to add variable/value
s String which may contain Eclipse variables, e.g. ${project_name}
	public static void addVariable(Map variable2valueMapString s,
			String projectRoot) {
		if (s == null || s.equals("")) //$NON-NLS-1$
		{
			return;
		}
		Pattern pattern = Pattern.compile("\\$\\{.*?\\}"); // ${var} //$NON-NLS-1$
		Matcher matcher = pattern.matcher(s);
		while (matcher.find()) {
			String variable = matcher.group();
			String value;
			try {
				value = VariablesPlugin.getDefault().getStringVariableManager()
catch (CoreException e) {
				// cannot resolve variable
				value = variable;
			}
			variable = removePrefixAndSuffix(variable"${""}"); //$NON-NLS-1$ //$NON-NLS-2$
			// if it is an environment variable, convert to Ant environment
			// syntax
			if (variable.startsWith("env_var:")) //$NON-NLS-1$
			{
				value = "env." + variable.substring("env_var:".length()); //$NON-NLS-1$ //$NON-NLS-2$
			}
			File file = new File(value);
			if (file.exists()) {
				value = getRelativePath(file.getAbsolutePath(), projectRoot);
			}
			variable2valueMap.put(variablevalue);
		}
	}

Returns a path which is equivalent to the given location relative to the specified base path.
	public static String getRelativePath(String otherLocationString basePath) {
		IPath location = new Path(otherLocation);
		IPath base = new Path(basePath);
		if ((location.getDevice() != null && !location.getDevice()
				|| !location.isAbsolute()) {
			return otherLocation;
		}
		int baseCount = base.segmentCount();
		int count = base.matchingFirstSegments(location);
		String temp = ""//$NON-NLS-1$
		for (int j = 0; j < baseCount - countj++) {
			temp += "../"//$NON-NLS-1$
		}
		String relative = new Path(temp).append(
				location.removeFirstSegments(count)).toString();
		if (relative.length() == 0) {
			relative = "."//$NON-NLS-1$
		}
		return relative;
	}
New to GrepCode? Check out our FAQ X