blob: ce7625d067288451515cfd06cb8b6ad2b730f067 [file] [log] [blame]
david_williams49d24fb2005-04-08 21:16:59 +00001/*******************************************************************************
amywu32bdd412007-02-14 08:12:56 +00002 * Copyright (c) 2001, 2007 IBM Corporation and others.
david_williams49d24fb2005-04-08 21:16:59 +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 *******************************************************************************/
david_williams96643dd2005-04-13 04:35:24 +000011package org.eclipse.wst.html.internal.validation;
paverye6107e62005-02-23 18:01:10 +000012
13import java.io.IOException;
14import java.io.UnsupportedEncodingException;
nitinda9458e52006-11-02 00:59:27 +000015import java.util.ArrayList;
16import java.util.List;
paverye6107e62005-02-23 18:01:10 +000017
paverya0e2eae2005-10-20 20:24:29 +000018import org.eclipse.core.filebuffers.ITextFileBuffer;
paverye6107e62005-02-23 18:01:10 +000019import org.eclipse.core.resources.IContainer;
20import org.eclipse.core.resources.IFile;
21import org.eclipse.core.resources.IProject;
22import org.eclipse.core.resources.IResource;
23import org.eclipse.core.resources.ResourcesPlugin;
24import org.eclipse.core.runtime.CoreException;
nitinda9458e52006-11-02 00:59:27 +000025import org.eclipse.core.runtime.IConfigurationElement;
26import org.eclipse.core.runtime.IExecutableExtension;
nitind7780ed62007-12-18 04:45:17 +000027import org.eclipse.core.runtime.IPath;
pavery9e4a23d2006-03-13 20:28:03 +000028import org.eclipse.core.runtime.IStatus;
paverye6107e62005-02-23 18:01:10 +000029import org.eclipse.core.runtime.NullProgressMonitor;
30import org.eclipse.core.runtime.OperationCanceledException;
31import org.eclipse.core.runtime.Path;
david_williamse6ed7322005-10-10 17:32:24 +000032import org.eclipse.core.runtime.Platform;
pavery9e4a23d2006-03-13 20:28:03 +000033import org.eclipse.core.runtime.Status;
nitinda06daf82005-11-01 22:42:33 +000034import org.eclipse.core.runtime.content.IContentDescription;
david_williamse6ed7322005-10-10 17:32:24 +000035import org.eclipse.core.runtime.content.IContentType;
36import org.eclipse.core.runtime.content.IContentTypeManager;
pavery9e4a23d2006-03-13 20:28:03 +000037import org.eclipse.core.runtime.jobs.ISchedulingRule;
paverya0e2eae2005-10-20 20:24:29 +000038import org.eclipse.jface.text.IDocument;
39import org.eclipse.jface.text.IRegion;
nitinda9458e52006-11-02 00:59:27 +000040import org.eclipse.wst.html.core.internal.document.HTMLDocumentTypeConstants;
david_williams56777022005-04-11 06:21:55 +000041import org.eclipse.wst.html.core.internal.validate.HTMLValidationAdapterFactory;
nitindde5d5aa2007-02-14 22:58:32 +000042import org.eclipse.wst.html.ui.internal.Logger;
david_williamsb5d05632006-02-27 09:24:00 +000043import org.eclipse.wst.sse.core.StructuredModelManager;
paverya0e2eae2005-10-20 20:24:29 +000044import org.eclipse.wst.sse.core.internal.FileBufferModelManager;
david_williams4ad020f2005-04-18 08:00:30 +000045import org.eclipse.wst.sse.core.internal.provisional.IModelManager;
46import org.eclipse.wst.sse.core.internal.provisional.INodeAdapterFactory;
paverya0e2eae2005-10-20 20:24:29 +000047import org.eclipse.wst.sse.core.internal.provisional.INodeNotifier;
david_williams4ad020f2005-04-18 08:00:30 +000048import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel;
paverya0e2eae2005-10-20 20:24:29 +000049import org.eclipse.wst.sse.core.internal.provisional.IndexedRegion;
pavery99e577e2006-03-16 21:28:46 +000050import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument;
51import org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion;
david_williams2aecf082005-04-13 05:03:21 +000052import org.eclipse.wst.sse.core.internal.util.URIResolver;
david_williams3bcfe392005-04-13 04:49:36 +000053import org.eclipse.wst.sse.core.internal.validate.ValidationAdapter;
nitinda9458e52006-11-02 00:59:27 +000054import org.eclipse.wst.sse.core.utils.StringUtils;
paverya0e2eae2005-10-20 20:24:29 +000055import org.eclipse.wst.sse.ui.internal.reconcile.validator.ISourceValidator;
vbhadrir0b14da32005-04-18 15:40:15 +000056import org.eclipse.wst.validation.internal.core.Message;
pavery9e4a23d2006-03-13 20:28:03 +000057import org.eclipse.wst.validation.internal.core.ValidationException;
vbhadrir271d28a2005-04-12 19:36:05 +000058import org.eclipse.wst.validation.internal.operations.IWorkbenchContext;
paverye6107e62005-02-23 18:01:10 +000059import org.eclipse.wst.validation.internal.operations.WorkbenchReporter;
david_williams49d24fb2005-04-08 21:16:59 +000060import org.eclipse.wst.validation.internal.provisional.core.IMessage;
61import org.eclipse.wst.validation.internal.provisional.core.IReporter;
62import org.eclipse.wst.validation.internal.provisional.core.IValidationContext;
pavery9e4a23d2006-03-13 20:28:03 +000063import org.eclipse.wst.validation.internal.provisional.core.IValidatorJob;
david_williams646a5e22005-04-02 07:16:27 +000064import org.eclipse.wst.xml.core.internal.document.DocumentTypeAdapter;
david_williams4ad020f2005-04-18 08:00:30 +000065import org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument;
66import org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel;
paverydc8b3062005-10-24 20:43:42 +000067import org.w3c.dom.Text;
paverye6107e62005-02-23 18:01:10 +000068
nitinda9458e52006-11-02 00:59:27 +000069public class HTMLValidator implements IValidatorJob, ISourceValidator, IExecutableExtension {
nitind73e76412005-11-10 01:50:19 +000070 private static final String ORG_ECLIPSE_WST_HTML_CORE_HTMLSOURCE = "org.eclipse.wst.html.core.htmlsource"; //$NON-NLS-1$
david_williamse6ed7322005-10-10 17:32:24 +000071
nitind165498a2005-07-12 18:47:49 +000072 static boolean shouldValidate(IFile file) {
73 IResource resource = file;
74 do {
75 if (resource.isDerived() || resource.isTeamPrivateMember() || !resource.isAccessible() || (resource.getName().charAt(0) == '.' && resource.getType() == IResource.FOLDER)) {
76 return false;
77 }
78 resource = resource.getParent();
79 }
80 while ((resource.getType() & IResource.PROJECT) == 0);
81 return true;
82 }
83
paverya0e2eae2005-10-20 20:24:29 +000084 private IDocument fDocument;
nitinda9458e52006-11-02 00:59:27 +000085 private IContentTypeManager fContentTypeManager;
86 private IContentType[] fOtherSupportedContentTypes = null;
87 private String[] fAdditionalContentTypesIDs = null;
88 private IContentType fHTMLContentType;
paverya0e2eae2005-10-20 20:24:29 +000089
paverye6107e62005-02-23 18:01:10 +000090 public HTMLValidator() {
91 super();
nitinda9458e52006-11-02 00:59:27 +000092 fContentTypeManager = Platform.getContentTypeManager();
93 fHTMLContentType = fContentTypeManager.getContentType(ORG_ECLIPSE_WST_HTML_CORE_HTMLSOURCE);
paverye6107e62005-02-23 18:01:10 +000094 }
95
96 /**
97 */
98 public void cleanup(IReporter reporter) {
99 // nothing to do
100 }
101
102 /**
nitinda9458e52006-11-02 00:59:27 +0000103 * Gets list of content types this validator is interested in
104 *
105 * @return All HTML-related content types
106 */
107 private IContentType[] getOtherSupportedContentTypes() {
108 if (fOtherSupportedContentTypes == null) {
109 List contentTypes = new ArrayList(3);
nitinda9458e52006-11-02 00:59:27 +0000110 if (fAdditionalContentTypesIDs != null) {
111 for (int i = 0; i < fAdditionalContentTypesIDs.length; i++) {
112 IContentType type = Platform.getContentTypeManager().getContentType(fAdditionalContentTypesIDs[i]);
113 if (type != null) {
114 contentTypes.add(type);
115 }
116 }
117 }
118 fOtherSupportedContentTypes = (IContentType[]) contentTypes.toArray(new IContentType[contentTypes.size()]);
119 }
120 return fOtherSupportedContentTypes;
121 }
122
123
124 /**
paverye6107e62005-02-23 18:01:10 +0000125 */
david_williamsc39caaf2005-04-05 06:07:16 +0000126 protected IDOMModel getModel(IProject project, IFile file) {
paverye6107e62005-02-23 18:01:10 +0000127 if (project == null || file == null)
128 return null;
129 if (!file.exists())
130 return null;
131 if (!canHandle(file))
132 return null;
133
134 IStructuredModel model = null;
135 IModelManager manager = StructuredModelManager.getModelManager();
nitindde5d5aa2007-02-14 22:58:32 +0000136 try {
137 file.refreshLocal(IResource.DEPTH_ZERO, new NullProgressMonitor());
138 }
139 catch (CoreException e) {
140 Logger.logException(e);
141 }
paverye6107e62005-02-23 18:01:10 +0000142 try {
143 try {
144 model = manager.getModelForRead(file);
145 }
146 catch (UnsupportedEncodingException ex) {
david_williams49d24fb2005-04-08 21:16:59 +0000147 // retry ignoring META charset for invalid META charset
148 // specification
paverye6107e62005-02-23 18:01:10 +0000149 // recreate input stream, because it is already partially read
150 model = manager.getModelForRead(file, new String(), null);
151 }
152 }
153 catch (UnsupportedEncodingException ex) {
154 }
155 catch (IOException ex) {
156 }
nitindde5d5aa2007-02-14 22:58:32 +0000157 catch (CoreException e) {
158 Logger.logException(e);
paverye6107e62005-02-23 18:01:10 +0000159 }
160
161 if (model == null)
162 return null;
david_williamsc39caaf2005-04-05 06:07:16 +0000163 if (!(model instanceof IDOMModel)) {
paverye6107e62005-02-23 18:01:10 +0000164 releaseModel(model);
165 return null;
166 }
david_williamsc39caaf2005-04-05 06:07:16 +0000167 return (IDOMModel) model;
paverye6107e62005-02-23 18:01:10 +0000168 }
169
170 /**
171 */
david_williamsc39caaf2005-04-05 06:07:16 +0000172 protected HTMLValidationReporter getReporter(IReporter reporter, IFile file, IDOMModel model) {
paverye6107e62005-02-23 18:01:10 +0000173 return new HTMLValidationReporter(this, reporter, file, model);
174 }
175
176 /**
177 * Check file extension to validate
178 */
179 private boolean canHandle(IFile file) {
david_williamse6ed7322005-10-10 17:32:24 +0000180 boolean result = false;
181 if (file != null) {
182 try {
nitinda06daf82005-11-01 22:42:33 +0000183 IContentDescription contentDescription = file.getContentDescription();
nitinda06daf82005-11-01 22:42:33 +0000184 if (contentDescription != null) {
185 IContentType fileContentType = contentDescription.getContentType();
nitinda9458e52006-11-02 00:59:27 +0000186 if (fileContentType.isKindOf(fHTMLContentType)) {
187 result = true;
188 }
189 else {
190 IContentType[] otherTypes = getOtherSupportedContentTypes();
191 for (int i = 0; i < otherTypes.length; i++) {
192 result = result || fileContentType.isKindOf(otherTypes[i]);
david_williamse6ed7322005-10-10 17:32:24 +0000193 }
194 }
195 }
nitinda9458e52006-11-02 00:59:27 +0000196 else if (fHTMLContentType != null) {
197 result = fHTMLContentType.isAssociatedWith(file.getName());
nitinda06daf82005-11-01 22:42:33 +0000198 }
david_williamse6ed7322005-10-10 17:32:24 +0000199 }
200 catch (CoreException e) {
201 // should be rare, but will ignore to avoid logging "encoding
202 // exceptions" and the like here.
203 // Logger.logException(e);
204 }
205 }
206 return result;
paverye6107e62005-02-23 18:01:10 +0000207 }
208
209 /**
210 */
david_williamsc39caaf2005-04-05 06:07:16 +0000211 private boolean hasHTMLFeature(IDOMDocument document) {
paverye6107e62005-02-23 18:01:10 +0000212 DocumentTypeAdapter adapter = (DocumentTypeAdapter) document.getAdapterFor(DocumentTypeAdapter.class);
213 if (adapter == null)
214 return false;
nitinda9458e52006-11-02 00:59:27 +0000215 return adapter.hasFeature(HTMLDocumentTypeConstants.HTML);
paverye6107e62005-02-23 18:01:10 +0000216 }
217
218 /**
219 */
220 protected void releaseModel(IStructuredModel model) {
221 if (model != null)
222 model.releaseFromRead();
223 }
224
225 /**
226 */
vbhadrir271d28a2005-04-12 19:36:05 +0000227 public void validate(IValidationContext helper, IReporter reporter) {
paverye6107e62005-02-23 18:01:10 +0000228 if (helper == null)
229 return;
230 if ((reporter != null) && (reporter.isCancelled() == true)) {
231 throw new OperationCanceledException();
232 }
vbhadrir271d28a2005-04-12 19:36:05 +0000233 String[] deltaArray = helper.getURIs();
paverye6107e62005-02-23 18:01:10 +0000234 if (deltaArray != null && deltaArray.length > 0) {
vbhadrir271d28a2005-04-12 19:36:05 +0000235 validateDelta(helper, reporter);
paverye6107e62005-02-23 18:01:10 +0000236 }
237 else {
vbhadrir271d28a2005-04-12 19:36:05 +0000238 validateFull(helper, reporter);
paverye6107e62005-02-23 18:01:10 +0000239 }
240 }
pavery9e4a23d2006-03-13 20:28:03 +0000241
paverya0e2eae2005-10-20 20:24:29 +0000242 /**
pavery9e4a23d2006-03-13 20:28:03 +0000243 * This validate call is for the ISourceValidator partial document
244 * validation approach
245 *
paverya0e2eae2005-10-20 20:24:29 +0000246 * @param dirtyRegion
247 * @param helper
248 * @param reporter
pavery9e4a23d2006-03-13 20:28:03 +0000249 * @see org.eclipse.wst.sse.ui.internal.reconcile.validator.ISourceValidator
paverya0e2eae2005-10-20 20:24:29 +0000250 */
251 public void validate(IRegion dirtyRegion, IValidationContext helper, IReporter reporter) {
pavery9e4a23d2006-03-13 20:28:03 +0000252
paverya0e2eae2005-10-20 20:24:29 +0000253 if (helper == null || fDocument == null)
254 return;
pavery9e4a23d2006-03-13 20:28:03 +0000255
paverya0e2eae2005-10-20 20:24:29 +0000256 if ((reporter != null) && (reporter.isCancelled() == true)) {
257 throw new OperationCanceledException();
258 }
pavery9e4a23d2006-03-13 20:28:03 +0000259
paverya0e2eae2005-10-20 20:24:29 +0000260 IStructuredModel model = StructuredModelManager.getModelManager().getExistingModelForRead(fDocument);
261 if (model == null)
262 return; // error
pavery9e4a23d2006-03-13 20:28:03 +0000263
paverya0e2eae2005-10-20 20:24:29 +0000264 try {
pavery9e4a23d2006-03-13 20:28:03 +0000265
paverya0e2eae2005-10-20 20:24:29 +0000266 IDOMDocument document = null;
pavery9e4a23d2006-03-13 20:28:03 +0000267 if (model instanceof IDOMModel) {
268 document = ((IDOMModel) model).getDocument();
paverya0e2eae2005-10-20 20:24:29 +0000269 }
paverye6107e62005-02-23 18:01:10 +0000270
nitind7780ed62007-12-18 04:45:17 +0000271 if (document == null || !hasHTMLFeature(document)) {
272 model.releaseFromRead();
273 return; //ignore
274 }
275
276 IPath filePath = null;
277 IFile file = null;
pavery9e4a23d2006-03-13 20:28:03 +0000278
paverya0e2eae2005-10-20 20:24:29 +0000279 ITextFileBuffer fb = FileBufferModelManager.getInstance().getBuffer(fDocument);
nitind7780ed62007-12-18 04:45:17 +0000280 if (fb != null) {
281 filePath = fb.getLocation();
pavery9e4a23d2006-03-13 20:28:03 +0000282
nitind7780ed62007-12-18 04:45:17 +0000283 if (filePath.segmentCount() > 1) {
284 file = ResourcesPlugin.getWorkspace().getRoot().getFile(filePath);
285 if (!file.isAccessible()) {
286 file = null;
287 }
288 }
289 }
290 else {
291 filePath = new Path(model.getId());
292 }
pavery9e4a23d2006-03-13 20:28:03 +0000293
paverydc8b3062005-10-24 20:43:42 +0000294 // this will be the wrong region if it's Text (instead of Element)
295 // we don't know how to validate Text
pavery99e577e2006-03-16 21:28:46 +0000296 IndexedRegion ir = getCoveringNode(dirtyRegion); // model.getIndexedRegion(dirtyRegion.getOffset());
pavery9e4a23d2006-03-13 20:28:03 +0000297 if (ir instanceof Text) {
298 while (ir != null && ir instanceof Text) {
paverydc8b3062005-10-24 20:43:42 +0000299 // it's assumed that this gets the IndexedRegion to
300 // the right of the end offset
301 ir = model.getIndexedRegion(ir.getEndOffset());
302 }
303 }
pavery99e577e2006-03-16 21:28:46 +0000304
pavery9e4a23d2006-03-13 20:28:03 +0000305 if (ir instanceof INodeNotifier) {
306
paverya0e2eae2005-10-20 20:24:29 +0000307 INodeAdapterFactory factory = HTMLValidationAdapterFactory.getInstance();
pavery9e4a23d2006-03-13 20:28:03 +0000308 ValidationAdapter adapter = (ValidationAdapter) factory.adapt((INodeNotifier) ir);
paverya0e2eae2005-10-20 20:24:29 +0000309 if (adapter == null)
310 return; // error
pavery9e4a23d2006-03-13 20:28:03 +0000311
paverya0e2eae2005-10-20 20:24:29 +0000312 if (reporter != null) {
313 HTMLValidationReporter rep = null;
pavery9e4a23d2006-03-13 20:28:03 +0000314 rep = getReporter(reporter, file, (IDOMModel) model);
paverya0e2eae2005-10-20 20:24:29 +0000315 rep.clear();
316 adapter.setReporter(rep);
pavery9e4a23d2006-03-13 20:28:03 +0000317
nitind7780ed62007-12-18 04:45:17 +0000318 Message mess = new LocalizedMessage(IMessage.LOW_SEVERITY, filePath.toString().substring(1));
paverya0e2eae2005-10-20 20:24:29 +0000319 reporter.displaySubtask(this, mess);
320 }
321 adapter.validate(ir);
322 }
323 }
324 finally {
pavery9e4a23d2006-03-13 20:28:03 +0000325 if (model != null)
paverya0e2eae2005-10-20 20:24:29 +0000326 model.releaseFromRead();
327 }
328 }
329
pavery99e577e2006-03-16 21:28:46 +0000330 private IndexedRegion getCoveringNode(IRegion dirtyRegion) {
331
332 IndexedRegion largestRegion = null;
333 if(fDocument instanceof IStructuredDocument) {
nitindd7870fd2006-09-13 01:57:38 +0000334 IStructuredDocumentRegion[] regions = ((IStructuredDocument) fDocument).getStructuredDocumentRegions(dirtyRegion.getOffset(), dirtyRegion.getLength());
335 largestRegion = getLargest(regions);
pavery99e577e2006-03-16 21:28:46 +0000336 }
337 return largestRegion;
338 }
339 protected IndexedRegion getLargest(IStructuredDocumentRegion[] sdRegions) {
340
341 if(sdRegions == null || sdRegions.length == 0)
342 return null;
343
344 IndexedRegion currentLargest = getCorrespondingNode(sdRegions[0]);
345 for (int i = 0; i < sdRegions.length; i++) {
346 if(!sdRegions[i].isDeleted()) {
347 IndexedRegion corresponding = getCorrespondingNode(sdRegions[i]);
348
349 if(currentLargest instanceof Text)
350 currentLargest = corresponding;
351
352 if(corresponding != null) {
353 if(!(corresponding instanceof Text)) {
354 if (corresponding.getStartOffset() <= currentLargest.getStartOffset()
355 && corresponding.getEndOffset() >= currentLargest.getEndOffset() )
356 currentLargest = corresponding;
357 }
358 }
359
360 }
361 }
362 return currentLargest;
363 }
364 protected IndexedRegion getCorrespondingNode(IStructuredDocumentRegion sdRegion) {
365 IStructuredModel sModel = StructuredModelManager.getModelManager().getExistingModelForRead(fDocument);
366 IndexedRegion indexedRegion = null;
367 try {
368 if (sModel != null)
369 indexedRegion = sModel.getIndexedRegion(sdRegion.getStart());
370 } finally {
371 if (sModel != null)
372 sModel.releaseFromRead();
373 }
374 return indexedRegion;
375 }
376
paverya0e2eae2005-10-20 20:24:29 +0000377 /**
378 * @see org.eclipse.wst.sse.ui.internal.reconcile.validator.ISourceValidator
379 */
380 public void connect(IDocument document) {
381 fDocument = document;
382 }
pavery9e4a23d2006-03-13 20:28:03 +0000383
paverya0e2eae2005-10-20 20:24:29 +0000384 /**
385 * @see org.eclipse.wst.sse.ui.internal.reconcile.validator.ISourceValidator
386 */
387 public void disconnect(IDocument document) {
nitindd7870fd2006-09-13 01:57:38 +0000388 fDocument = null;
paverya0e2eae2005-10-20 20:24:29 +0000389 }
pavery9e4a23d2006-03-13 20:28:03 +0000390
paverye6107e62005-02-23 18:01:10 +0000391 /**
392 */
david_williamsc39caaf2005-04-05 06:07:16 +0000393 protected HTMLValidationResult validate(IDOMModel model, IFile file) {
paverye6107e62005-02-23 18:01:10 +0000394 IProject prj = null;
395 if (file != null) {
396 prj = file.getProject();
397 }
david_williams49d24fb2005-04-08 21:16:59 +0000398 if ((prj == null) && (model != null)) {
paverye6107e62005-02-23 18:01:10 +0000399 URIResolver res = model.getResolver();
400 if (res != null) {
401 prj = res.getProject();
402 }
403 }
404 final WorkbenchReporter reporter = new WorkbenchReporter(prj, new NullProgressMonitor());
david_williams49d24fb2005-04-08 21:16:59 +0000405 return validate(reporter, file, model);
paverye6107e62005-02-23 18:01:10 +0000406 }
407
408 /**
409 */
david_williamsc39caaf2005-04-05 06:07:16 +0000410 private HTMLValidationResult validate(IReporter reporter, IFile file, IDOMModel model) {
paverye6107e62005-02-23 18:01:10 +0000411 if (file == null || model == null)
412 return null; // error
david_williamsc39caaf2005-04-05 06:07:16 +0000413 IDOMDocument document = model.getDocument();
paverye6107e62005-02-23 18:01:10 +0000414 if (document == null)
415 return null; // error
416 if (!hasHTMLFeature(document))
417 return null; // ignore
418
david_williams285ee2d2005-04-10 18:21:21 +0000419 INodeAdapterFactory factory = HTMLValidationAdapterFactory.getInstance();
paverye6107e62005-02-23 18:01:10 +0000420 ValidationAdapter adapter = (ValidationAdapter) factory.adapt(document);
421 if (adapter == null)
422 return null; // error
423
424 HTMLValidationReporter rep = getReporter(reporter, file, model);
425 rep.clear();
426 adapter.setReporter(rep);
paverye6107e62005-02-23 18:01:10 +0000427 adapter.validate(document);
428 return rep.getResult();
429 }
430
431 /**
432 */
david_williams43271672005-04-01 05:19:26 +0000433 private void validateContainer(IValidationContext helper, IReporter reporter, IContainer container) {
paverye6107e62005-02-23 18:01:10 +0000434 try {
435 IResource[] resourceArray = container.members(false);
436 for (int i = 0; i < resourceArray.length; i++) {
437 IResource resource = resourceArray[i];
nitindbcb8cd02006-08-15 21:11:35 +0000438 if (resource == null || reporter.isCancelled())
paverye6107e62005-02-23 18:01:10 +0000439 continue;
440 if (resource instanceof IFile) {
nitinda9458e52006-11-02 00:59:27 +0000441 Message message = new LocalizedMessage(IMessage.LOW_SEVERITY, resource.getFullPath().toString().substring(1));
442 reporter.displaySubtask(this, message);
paverye6107e62005-02-23 18:01:10 +0000443 validateFile(helper, reporter, (IFile) resource);
444 }
445 else if (resource instanceof IContainer) {
446 validateContainer(helper, reporter, (IContainer) resource);
447 }
448 }
449 }
450 catch (CoreException ex) {
451 }
452 }
453
454 /**
455 */
vbhadrir271d28a2005-04-12 19:36:05 +0000456 private void validateDelta(IValidationContext helper, IReporter reporter) {
457 String[] deltaArray = helper.getURIs();
paverye6107e62005-02-23 18:01:10 +0000458 for (int i = 0; i < deltaArray.length; i++) {
vbhadrir271d28a2005-04-12 19:36:05 +0000459 String delta = deltaArray[i];
460 if (delta == null)
paverye6107e62005-02-23 18:01:10 +0000461 continue;
nitinda9458e52006-11-02 00:59:27 +0000462
463 if (reporter != null) {
464 Message message = new LocalizedMessage(IMessage.LOW_SEVERITY, "" + (i+1) + "/" + deltaArray.length + " - " + delta.substring(1));
465 reporter.displaySubtask(this, message);
466 }
467
paverye6107e62005-02-23 18:01:10 +0000468 IResource resource = getResource(delta);
469 if (resource == null || !(resource instanceof IFile))
470 continue;
471 validateFile(helper, reporter, (IFile) resource);
472 }
473 }
474
475 /**
476 */
david_williams43271672005-04-01 05:19:26 +0000477 private void validateFile(IValidationContext helper, IReporter reporter, IFile file) {
paverye6107e62005-02-23 18:01:10 +0000478 if ((reporter != null) && (reporter.isCancelled() == true)) {
479 throw new OperationCanceledException();
480 }
nitind165498a2005-07-12 18:47:49 +0000481 if (!shouldValidate(file)) {
482 return;
483 }
david_williamsc39caaf2005-04-05 06:07:16 +0000484 IDOMModel model = getModel(file.getProject(), file);
paverye6107e62005-02-23 18:01:10 +0000485 if (model == null)
486 return;
487
488 try {
489 validate(reporter, file, model);
490 }
491 finally {
492 releaseModel(model);
493 }
494 }
495
496 /**
497 */
vbhadrir271d28a2005-04-12 19:36:05 +0000498 private void validateFull(IValidationContext helper, IReporter reporter) {
paverye6107e62005-02-23 18:01:10 +0000499 IProject project = null;
vbhadrir271d28a2005-04-12 19:36:05 +0000500 String[] fileDelta = helper.getURIs();
501 if (helper instanceof IWorkbenchContext) {
502 IWorkbenchContext wbHelper = (IWorkbenchContext) helper;
paverye6107e62005-02-23 18:01:10 +0000503 project = wbHelper.getProject();
504 }
nitinda9458e52006-11-02 00:59:27 +0000505 else if(fileDelta.length > 0){
paverye6107e62005-02-23 18:01:10 +0000506 // won't work for project validation (b/c nothing in file delta)
507 project = getResource(fileDelta[0]).getProject();
508 }
509 if (project == null)
510 return;
511 validateContainer(helper, reporter, project);
512 }
513
david_williams49d24fb2005-04-08 21:16:59 +0000514 /*
paverye6107e62005-02-23 18:01:10 +0000515 * added to get rid or dependency on IWorkbenchHelper
david_williams49d24fb2005-04-08 21:16:59 +0000516 *
paverye6107e62005-02-23 18:01:10 +0000517 */
vbhadrir271d28a2005-04-12 19:36:05 +0000518 public IResource getResource(String delta) {
david_williamse6ed7322005-10-10 17:32:24 +0000519 return ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(delta));
paverye6107e62005-02-23 18:01:10 +0000520 }
pavery9e4a23d2006-03-13 20:28:03 +0000521
522 public ISchedulingRule getSchedulingRule(IValidationContext helper) {
523 return null;
524 }
525
526 public IStatus validateInJob(IValidationContext helper, IReporter reporter) throws ValidationException {
david_williams2deeabe2006-07-30 03:11:20 +0000527 // Exception catching was removed, see
528 // https://bugs.eclipse.org/bugs/show_bug.cgi?id=123600
pavery9e4a23d2006-03-13 20:28:03 +0000529 IStatus status = Status.OK_STATUS;
david_williams2deeabe2006-07-30 03:11:20 +0000530 validate(helper, reporter);
pavery9e4a23d2006-03-13 20:28:03 +0000531 return status;
532 }
nitinda9458e52006-11-02 00:59:27 +0000533
534 /**
535 * @see org.eclipse.core.runtime.IExecutableExtension#setInitializationData(org.eclipse.core.runtime.IConfigurationElement,
536 * java.lang.String, java.lang.Object)
537 */
538 public void setInitializationData(IConfigurationElement config, String propertyName, Object data) throws CoreException {
539 fAdditionalContentTypesIDs = new String[0];
540 if (data != null) {
541 if (data instanceof String && data.toString().length() > 0) {
542 fAdditionalContentTypesIDs = StringUtils.unpack(data.toString());
543 }
544 }
545 }
paverye6107e62005-02-23 18:01:10 +0000546}