blob: d9497af689ef5df78919f32c959ac97c6ca2d5ab [file] [log] [blame]
lmandel3c550af2005-06-16 05:46:02 +00001/*******************************************************************************
nitind95409d02005-11-10 05:32:16 +00002 * Copyright (c) 2001, 2005 IBM Corporation and others.
lmandel3c550af2005-06-16 05:46:02 +00003 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * IBM Corporation - initial API and implementation
10 *******************************************************************************/
11
12package org.eclipse.wst.xml.core.internal.validation.core;
13
14import java.util.ArrayList;
15import java.util.Collection;
16import java.util.List;
17
18import org.eclipse.core.resources.IContainer;
19import org.eclipse.core.resources.IFile;
20import org.eclipse.core.resources.IMarker;
21import org.eclipse.core.resources.IProject;
22import org.eclipse.core.resources.IResource;
23import org.eclipse.core.resources.IWorkspaceRunnable;
24import org.eclipse.core.resources.ResourcesPlugin;
25import org.eclipse.core.runtime.CoreException;
26import org.eclipse.core.runtime.IProgressMonitor;
27import org.eclipse.wst.validation.internal.operations.ValidatorManager;
28import org.eclipse.wst.validation.internal.operations.WorkbenchContext;
29import org.eclipse.wst.validation.internal.provisional.core.IMessage;
30
31
32/**
33 * A helper class for validation in the validation framework.
34 *
35 * @author Ernest Mah (ernest@ca.ibm.com)
36 * @author Lawrence Mandel, IBM
37 */
38public class Helper extends WorkbenchContext
39{
nitind95409d02005-11-10 05:32:16 +000040 public static final String GET_PROJECT_FILES = "getAllFiles"; //$NON-NLS-1$
41 public static final String GET_FILE = "getFile"; //$NON-NLS-1$
lmandel3c550af2005-06-16 05:46:02 +000042 //dw private static final IContainer[] NO_CONTAINERS = new IContainer[0];
nitind95409d02005-11-10 05:32:16 +000043 public static final String VALIDATION_MARKER = "org.eclipse.wst.validation.problemmarker"; //$NON-NLS-1$
44 public static final String VALIDATION_MARKER_OWNER = "owner"; //$NON-NLS-1$
lmandel3c550af2005-06-16 05:46:02 +000045
46 /**
47 * Constructor.
48 */
49 public Helper()
50 {
51 super();
52
53 // the following will register the helper's symbolic methods
54 Class [] args = new Class[1] ;
55 args[0] = String.class ; // a string argument denoting a specific JSP.
56
57 registerModel(GET_FILE, "getFile", args);//$NON-NLS-1$
58 registerModel(GET_PROJECT_FILES, "getFiles", args);//$NON-NLS-1$
59 }
60
61 /**
62 * Get the IFile for the given filename.
63 *
64 * @param filename The name of the file to retrieve.
65 * @return An IFile representing the file specified or null if it can't be resolved.
66 */
67 public IFile getFile(String filename)
68 {
69 // System.out.println("file name = " + filename);
70 IResource res = getProject().findMember(filename, true); // true means include phantom resources
71 if (res instanceof IFile)
72 {
73 return (IFile) res;
74 }
75 return null;
76 }
77
78 /**
79 * Get the collection of files from the project that are relevant for the
80 * validator with the given class name.
81 *
82 * @param validatorClassName The name of the validator class.
83 * @return The collection of files relevant for the validator class specified.
84 */
85 public Collection getFiles(String validatorClassName)
86 {
87 IProject project = getProject();
88 List files = new ArrayList();
89 getFiles(files, project, validatorClassName);
90 return files;
91 }
92
93 /**
94 * Get the collection of files from the project that are relevant for the
95 * validator with the given class name.
96 *
97 * @param files The files relevant for the class name.
98 * @param resource The resource to look for files in.
99 * @param validatorClassName The name of the validator class.
100 */
101 protected void getFiles(Collection files, IContainer resource, String validatorClassName)
102 {
103 try
104 {
105 IResource [] resourceArray = resource.members(false);
106 for (int i=0; i<resourceArray.length; i++)
107 {
108 if (ValidatorManager.getManager().isApplicableTo(validatorClassName, resourceArray[i]))
109 {
110 if (resourceArray[i] instanceof IFile)
111 {
112 files.add(resourceArray[i]);
113 }
114 }
115 if (resourceArray[i].getType() == IResource.FOLDER)
116 getFiles(files,(IContainer)resourceArray[i], validatorClassName) ;
117 }
118 }
119 catch (Exception e) {}
120 }
121
122
123/**
124 * Return the name of the resource, without the project-specific information
125 * in front.
126 *
127 * This method is used by ValidationOperation to calculate the non-environment
128 * specific names of the files. Only the IWorkbenchContext implementation knows how
129 * much information to strip off of the IResource name. For example, if there is
130 * an EJB Project named "MyEJBProject", and it uses the default names for the
131 * source and output folders, "source" and "ejbModule", respectively, then the
132 * current implementation of EJB Helper knows how much of that structure is
133 * eclipse-specific.
134 *
135 * Since the "source" folder contains Java source files, a portable name would
136 * be the fully-qualified name of the Java class, without the eclipse-specific
137 * project and folder names in front of the file name. The EJBHelper knows that
138 * everything up to the "source" folder, for example, can be removed, because,
139 * according to the definition of the EJB Project, everything contained
140 * in the source folder is java source code. So if there is an IResource in an
141 * EJB Project named "/MyEJBProject/source/com/ibm/myclasses/MyJavaFile.java",
142 * this method would make this name portable by stripping off the
143 * "/MyEJBProject/source", and returning "com/ibm/myclasses/MyJavaFile.java".
144 *
145 * The output of this method is used by the ValidationOperation, when it
146 * is calculating the list of added/changed/deleted file names for incremental
147 * validation. If getPortableName(IResource) returns null, that means
148 * that the IWorkbenchContext's implementation does not support that particular
149 * type of resource, and the resource should not be included in the array of
150 * IFileDelta objects in the IValidator's "validate" method.
151 *
152 * @param resource The resource to get the name from.
153 * @return The portable name of the resource.
154 */
155public String getPortableName(IResource resource)
156 {
157 // System.out.println("get portablename for " + resource);
158 return resource.getProjectRelativePath().toString();
159 }
160
161/**
162 * When an IValidator associates a target object with an IMessage,
163 * the WorkbenchReporter eventually resolves that target object
164 * with an IResource. Sometimes more than one target object resolves
165 * to the same IResource (usually the IProject, which is the default
166 * IResource when an IFile cannot be found). This method is called,
167 * by the WorkbenchReporter, so that the WorkbenchReporter can
168 * distinguish between the IMessages which are on the same IResource,
169 * but refer to different target objects. This is needed for the
170 * removeAllMessages(IValidator, Object) method, so that when one
171 * target object removes all of its messages, that it doesn't remove
172 * another target object's messages.
173 *
174 * This method may return null only if object is null. Otherwise, an
175 * id which can uniquely identify a particular object must be returned.
176 * The id needs to be unique only within one particular IValidator.
177 *
178 * @param object The object from which to get the name.
179 * @return The name of the object or null if the object is null.
180 */
181public String getTargetObjectName(Object object)
182 {
183 if (object == null)
184 {
185 return null;
186 }
187
188 // System.out.println("get targetname for " + object);
189 return object.toString();
190 }
191
192 /**
193 * Delete the markers of the specified type from the specified resource.
194 *
195 * @param resource The resource to delete the markers from.
196 * @param markerType The type of markers to delete from the resource.
197 * @param attributeName The name of the attribute which the markers must have to be deleted.
198 * @param attributeValue The value of the attribute corresponding to attributeName which the markers must have to be deleted.
199 * @throws CoreException
200 */
201 public static void deleteMarkers(IResource resource, String markerType, final String attributeName, final Object attributeValue) throws CoreException
202 {
203 final IMarker[] v400Markers = resource.findMarkers(IMarker.PROBLEM, false, IResource.DEPTH_INFINITE);
204 final IMarker[] markers = resource.findMarkers(markerType, true, IResource.DEPTH_INFINITE);
205 IWorkspaceRunnable op = new IWorkspaceRunnable()
206 {
207 public void run(IProgressMonitor progressMonitor) throws CoreException
208 {
209 // this fixes defect 193406
210 // here we remove markers that may have been added by the v400 code
211 // hopefully the '.markers' metadata files will be removed for the V5 install
212 // and this kludge will not be needed there
213 for (int i = 0; i < v400Markers.length; i++)
214 {
215 IMarker marker = markers[i];
216 marker.delete();
217 }
218
219 for (int i = 0; i < markers.length; i++)
220 {
221 IMarker marker = markers[i];
222
223 Object value = marker.getAttribute(attributeName);
224 if (value != null &&
225 value.equals(attributeValue))
226 {
227 marker.delete();
228 }
229 }
230 }
231 };
232
233 try
234 {
235 ResourcesPlugin.getWorkspace().run(op, null);
236 }
237 catch (Exception e) { }
238 }
239
240 /**
241 * Get the validation framework severity for the given severity.
242 *
243 * @param severity The severity to convert to validation framework severity.
244 * @return The validation framework severity for the given severity.
245 */
246 static public int getValidationFrameworkSeverity(int severity)
247 {
248 switch (severity)
249 {
250 case IMarker.SEVERITY_ERROR:
251 return IMessage.HIGH_SEVERITY;
252 case IMarker.SEVERITY_WARNING:
253 return IMessage.NORMAL_SEVERITY;
254 case IMarker.SEVERITY_INFO:
255 return IMessage.LOW_SEVERITY;
256 }
257 return IMessage.LOW_SEVERITY;
258 }
259}