org.eclipse.wst.validation.internal.operations.WorkbenchContext Maven / Gradle / Ivy
The newest version!
/*******************************************************************************
* Copyright (c) 2001, 2008 IBM Corporation 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:
* IBM Corporation - initial API and implementation
*******************************************************************************/
package org.eclipse.wst.validation.internal.operations;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Hashtable;
import java.util.List;
import org.eclipse.core.resources.IContainer;
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.runtime.IPath;
import org.eclipse.core.runtime.Path;
import org.eclipse.wst.validation.internal.RegistryConstants;
import org.eclipse.wst.validation.internal.Tracing;
import org.eclipse.wst.validation.internal.plugin.ValidationPlugin;
import org.eclipse.wst.validation.internal.provisional.core.IMessage;
/**
* Abstract base class for registration of symbolic model names, and also provides the mechanism for
* registering the load model method which loads a specific type of MOF model, as identified by the
* symbolic model name.
*/
public class WorkbenchContext implements IWorkbenchContext {
private IProject _project;
private Hashtable _modelRegistry;
private int _ruleGroup = RegistryConstants.ATT_RULE_GROUP_DEFAULT;
public List validationFileURIs;
public static final String GET_PROJECT_FILES = "getAllFiles"; //$NON-NLS-1$
public static final String GET_FILE = "getFile"; //$NON-NLS-1$
public static final String VALIDATION_MARKER = "com.ibm.etools.validation.problemmarker"; //$NON-NLS-1$
public static final String VALIDATION_MARKER_OWNER = "owner"; //$NON-NLS-1$
public WorkbenchContext() {
super();
_modelRegistry = new Hashtable();
registerModel(IRuleGroup.PASS_LEVEL, "loadRuleGroup"); //$NON-NLS-1$
//the following will register the helper's symbolic methods
Class [] args = new Class[1] ;
args[0] = String.class ; // a string argument denoting a specific JSP.
registerModel(GET_FILE, "getFile", args);//$NON-NLS-1$
registerModel(GET_PROJECT_FILES, "getFiles", args);//$NON-NLS-1$
}
/**
* When the validation is complete, this method will be called so that the IWorkbenchContext can
* clean up any resources it allocated during the validation.
*
* If the cleanup is a long-running operation, subtask messages should be sent to the IReporter.
*/
public void cleanup(WorkbenchReporter reporter) {
// Default: nothing to clean up
}
/**
* When the IProject is closing, perform any cleanup that needs to be done. When this method is
* called, if the helper has cached any resources from the IProject, it should release those
* resources. If you do not allocate resources in your helper, then this method should be a
* no-op.
*/
public void closing() {
closing(getProject());
}
/**
* When the IProject is closing, perform any cleanup that needs to be done. When this method is
* called, if the helper has cached any resources from the IProject, it should release those
* resources. If you do not allocate resources in your helper, then this method should be a
* no-op.
*
* @deprecated Override closing() instead, and use getProject()
*/
public void closing(IProject project) {
//do nothing
}
/**
* When the IProject is being deleted, perform any cleanup that needs to be done. When this
* method is called, if the helper has cached any resources from the IProject, it should release
* those resources. If you do not allocate resources in your helper, then this method should be
* a no-op.
*/
public void deleting() {
deleting(getProject());
}
/**
* When the IProject is being deleted, perform any cleanup that needs to be done. When this
* method is called, if the helper has cached any resources from the IProject, it should release
* those resources. If you do not allocate resources in your helper, then this method should be
* a no-op.
*
* @deprecated Override deleting() instead, and use getProject()
*/
public void deleting(IProject project) {
//do nothing
}
/**
* Returns the IPath of a resource, relative to the container. If the IResource is not a member
* of the container, return null. This method should be useful for implementors of this class;
* specifically, in their getPortableName method.
*/
public static String getContainerRelativePath(String fullPath, IContainer container) {
if ((fullPath == null) || (container == null))
return null;
IPath relPath = getContainerRelativePath(new Path(fullPath), container);
if (relPath != null)
return relPath.toString();
return null;
}
/**
* Returns the IPath of a resource, relative to the container. If the IResource is not a member
* of the container, return null. This method should be useful for implementors of this class;
* specifically, in their getPortableName method.
*/
public static IPath getContainerRelativePath(IResource resource, IContainer container) {
if ((resource == null) || (container == null)) {
return null;
}
IResource foundResource = null;
IPath relPath = getContainerRelativePath(resource.getFullPath(), container);
if (relPath != null) {
// if relPath is null, the resource is not a direct member of the container
try {
if (!resource.exists()) { // findMember won't work
if (resource instanceof IFile) {
foundResource = container.getFile(relPath);
} else if (resource instanceof IFolder) {
foundResource = container.getFolder(relPath);
}
} else {
foundResource = container.findMember(relPath, true); // true means include
// phantom resources
if ((foundResource != null) && !foundResource.exists()) {
foundResource = null;
}
}
} catch (IllegalArgumentException e) {
foundResource = null;
ValidationPlugin.getPlugin().handleException(e);
}
}
if (foundResource == null) {
return null;
}
// file has been found
int matchingFirstSegments = container.getProjectRelativePath().matchingFirstSegments(resource.getProjectRelativePath());
return resource.getProjectRelativePath().removeFirstSegments(matchingFirstSegments);
}
/**
* Given an IPath, if the IPath is absolute, and is a part of the IContainer, return an IPath
* which is relative to the container. If the IPath is not part of the IContainer, return null.
*/
public static IPath getContainerRelativePath(IPath path, IContainer container) {
if ((path == null) || (container == null)) {
return null;
}
if (path.isAbsolute()) {
// Is the path part of the IContainer?
int matchingFirstSegments = path.matchingFirstSegments(container.getFullPath());
if ((matchingFirstSegments > 0) && (matchingFirstSegments == container.getFullPath().segmentCount())) {
// part of the IContainer
return path.removeFirstSegments(matchingFirstSegments);
}
// not part of the IContainer
return null;
}
// path is relative
// Is the path part of the IContainer?
//TODO don't have time to implement this now, but should in future. - Ruth
return null;
}
/**
* Given an IMessage's target object, return a string which identifies the object, so that the
* user can locate it.
*/
public String getDescription(Object object) {
if (object == null) {
return ""; //$NON-NLS-1$
}
if (object instanceof WorkbenchFileDelta) {
WorkbenchFileDelta wfd = (WorkbenchFileDelta) object;
if (wfd.getResource() != null) {
// resource will be null if WorkbenchFileDelta was constructed from an Object
// instead of an IResource
return wfd.getResource().getFullPath().toString();
}
}
return object.toString();
}
/**
* Given an Object, if the object has a corresponding IFile in the workbench, return the IFile.
* Otherwise return null.
*
* This method is used by the WorkbenchReporter. In eclipse, in order to add or remove a task
* list entry, the IResource, to which the entry applies, must be identified. The IReporter
* interface passes in an Object in these methods:
*
* addValidationMessage(IValidator, IMessage) // Object is a part of IMessage
*
* removeAllMessages(IValidator, Object),
*
* Thus, the WorkbenchReporter needs to know how, given the Object, which IFile that the Object
* represents in the workbench, in order to be able to add the task list entry.
*
* If this method returns null, then the WorkbenchReporter will add the message to the IProject
* instead of an IFile.
*
*/
public IFile getFile(Object obj) {
return null;
}
public IResource getResource(Object obj) {
if (obj == null) {
return null;
}
IResource res = null;
if (obj instanceof WorkbenchFileDelta) {
// resource may be null if WorkbenchFileDelta was constructed from an Object instead of
// an IResource
res = ((WorkbenchFileDelta) obj).getResource();
} else if (obj instanceof IResource) {
res = (IResource) obj;
}
if ((res == null) || (!res.exists())) {
return getFile(obj);
}
return res;
}
/**
* If the IProject is associated with an EJBNatureRuntime, return the IJavaProject which
* represents it.
*/
// public static IJavaProject getJavaProject(IProject project) {
// if (project == null) {
// return null;
// }
// return JavaCore.create(project);
// }
/**
* Given an IMessage's target object, return the line number, of the IFile, which the target
* object represents. If the object is null, or if access to line numbers is not possible,
* return "0".
*/
public int getLineNo(Object object) {
IResourceUtil util = ValidatorManager.getResourceUtil();
if (util == null) {
return IMessage.LINENO_UNSET;
}
try {
return util.getLineNo(object);
} catch (Exception e) {
ValidationPlugin.getPlugin().handleException(e);
return IMessage.LINENO_UNSET;
}
}
/**
* Given an IMessage's target object, return the line number, of the IFile, which the target
* object represents, if possible. If the object is null, or if access to line numbers is not
* possible, return a text description of the location.
*
* This method will be called whether or not the IResource is an IFile, IFolder, or IProject.
* Line numbers are valid only for IFile types; if the resource is not an IFile, then a text
* description of the location must be returned.
*/
public String getLocation(Object object) {
IResource res = getResource(object);
if ((res == null) || !(res instanceof IFile))
// return a text description
return getDescription(object);
// default to a line number, if it's available. Else, use a text description.
int lineNumber = getLineNo(object);
if (lineNumber == IMessage.LINENO_UNSET) {
return getDescription(object);
}
// return the line number
return String.valueOf(lineNumber);
}
/**
* Given a name of a load method, and the types of parameters it takes (this method is always
* called with null as the second parameter), return the java.lang.reflect.Method which
* represents the load method.
*/
private final Method getMethod(String methodName, Class[] parmTypes) {
Method m = null;
try {
m = getClass().getMethod(methodName, parmTypes);
} catch (NoSuchMethodException e) {
ValidationPlugin.getPlugin().handleException(e);
return null;
}
return m;
}
/**
* @see IWorkbenchContext.getPortableName(IResource)
*/
public String getPortableName(IResource resource) {
return resource.getFullPath().toString();
}
/**
* Return the IProject which is about to be validated. Each IWorkbenchContext knows how to
* traverse a certain type of IProject, for example, an EJB project or a web project.
*/
public final IProject getProject() {
return _project;
}
/**
* Get the IFile for the given filename.
*
* @param filename The name of the file to retrieve.
* @return An IFile representing the file specified or null if it can't be resolved.
*/
public IFile getFile(String filename)
{
// System.out.println("file name = " + filename);
IResource res = getProject().findMember(filename, true); // true means include phantom resources
if (res instanceof IFile)
{
return (IFile) res;
}
return null;
}
/**
* Get the collection of files from the project that are relevant for the
* validator with the given class name.
*
* @param validatorClassName The name of the validator class.
* @return The collection of files relevant for the validator class specified.
*/
public Collection getFiles(String validatorClassName)
{
IProject project = getProject();
List files = new ArrayList();
getFiles(files, project, validatorClassName);
return files;
}
/**
* Get the collection of files from the project that are relevant for the
* validator with the given class name.
*
* @param files The files relevant for the class name.
* @param resource The resource to look for files in.
* @param validatorClassName The name of the validator class.
*/
protected void getFiles(Collection files, IContainer resource, String validatorClassName)
{
try
{
IResource [] resourceArray = resource.members(false);
for (int i=0; i getValidationFileURIs() {
return validationFileURIs;
}
/**
* @param validationFileURIs The validationFileURIs to set.
*/
public void setValidationFileURIs(List validationFileURIs) {
this.validationFileURIs = validationFileURIs;
}
}