david_williams | 49d24fb | 2005-04-08 21:16:59 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
| 2 | * Copyright (c) 2001, 2004 IBM Corporation and others. |
| 3 | * 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_williams | 96643dd | 2005-04-13 04:35:24 +0000 | [diff] [blame] | 11 | package org.eclipse.wst.html.internal.validation; |
pavery | e6107e6 | 2005-02-23 18:01:10 +0000 | [diff] [blame] | 12 | |
| 13 | import java.io.IOException; |
| 14 | import java.io.UnsupportedEncodingException; |
| 15 | |
pavery | a0e2eae | 2005-10-20 20:24:29 +0000 | [diff] [blame] | 16 | import org.eclipse.core.filebuffers.ITextFileBuffer; |
pavery | e6107e6 | 2005-02-23 18:01:10 +0000 | [diff] [blame] | 17 | import org.eclipse.core.resources.IContainer; |
| 18 | import org.eclipse.core.resources.IFile; |
| 19 | import org.eclipse.core.resources.IProject; |
| 20 | import org.eclipse.core.resources.IResource; |
| 21 | import org.eclipse.core.resources.ResourcesPlugin; |
| 22 | import org.eclipse.core.runtime.CoreException; |
david_williams | e6ed732 | 2005-10-10 17:32:24 +0000 | [diff] [blame] | 23 | import org.eclipse.core.runtime.IPath; |
pavery | e6107e6 | 2005-02-23 18:01:10 +0000 | [diff] [blame] | 24 | import org.eclipse.core.runtime.NullProgressMonitor; |
| 25 | import org.eclipse.core.runtime.OperationCanceledException; |
| 26 | import org.eclipse.core.runtime.Path; |
david_williams | e6ed732 | 2005-10-10 17:32:24 +0000 | [diff] [blame] | 27 | import org.eclipse.core.runtime.Platform; |
nitind | a06daf8 | 2005-11-01 22:42:33 +0000 | [diff] [blame] | 28 | import org.eclipse.core.runtime.content.IContentDescription; |
david_williams | e6ed732 | 2005-10-10 17:32:24 +0000 | [diff] [blame] | 29 | import org.eclipse.core.runtime.content.IContentType; |
| 30 | import org.eclipse.core.runtime.content.IContentTypeManager; |
pavery | a0e2eae | 2005-10-20 20:24:29 +0000 | [diff] [blame] | 31 | import org.eclipse.jface.text.IDocument; |
| 32 | import org.eclipse.jface.text.IRegion; |
nitind | e83b1a4 | 2005-06-22 21:22:20 +0000 | [diff] [blame] | 33 | import org.eclipse.osgi.util.NLS; |
david_williams | 5677702 | 2005-04-11 06:21:55 +0000 | [diff] [blame] | 34 | import org.eclipse.wst.html.core.internal.validate.HTMLValidationAdapterFactory; |
david_williams | 3a2ab2d | 2005-04-11 16:32:45 +0000 | [diff] [blame] | 35 | import org.eclipse.wst.html.ui.internal.HTMLUIMessages; |
pavery | a0e2eae | 2005-10-20 20:24:29 +0000 | [diff] [blame] | 36 | import org.eclipse.wst.sse.core.internal.FileBufferModelManager; |
david_williams | 4ad020f | 2005-04-18 08:00:30 +0000 | [diff] [blame] | 37 | import org.eclipse.wst.sse.core.internal.provisional.IModelManager; |
| 38 | import org.eclipse.wst.sse.core.internal.provisional.INodeAdapterFactory; |
pavery | a0e2eae | 2005-10-20 20:24:29 +0000 | [diff] [blame] | 39 | import org.eclipse.wst.sse.core.internal.provisional.INodeNotifier; |
david_williams | 4ad020f | 2005-04-18 08:00:30 +0000 | [diff] [blame] | 40 | import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel; |
pavery | a0e2eae | 2005-10-20 20:24:29 +0000 | [diff] [blame] | 41 | import org.eclipse.wst.sse.core.internal.provisional.IndexedRegion; |
david_williams | 4ad020f | 2005-04-18 08:00:30 +0000 | [diff] [blame] | 42 | import org.eclipse.wst.sse.core.internal.provisional.StructuredModelManager; |
david_williams | 2aecf08 | 2005-04-13 05:03:21 +0000 | [diff] [blame] | 43 | import org.eclipse.wst.sse.core.internal.util.URIResolver; |
david_williams | 3bcfe39 | 2005-04-13 04:49:36 +0000 | [diff] [blame] | 44 | import org.eclipse.wst.sse.core.internal.validate.ValidationAdapter; |
pavery | a0e2eae | 2005-10-20 20:24:29 +0000 | [diff] [blame] | 45 | import org.eclipse.wst.sse.ui.internal.reconcile.validator.ISourceValidator; |
vbhadrir | 0b14da3 | 2005-04-18 15:40:15 +0000 | [diff] [blame] | 46 | import org.eclipse.wst.validation.internal.core.Message; |
vbhadrir | 271d28a | 2005-04-12 19:36:05 +0000 | [diff] [blame] | 47 | import org.eclipse.wst.validation.internal.operations.IWorkbenchContext; |
pavery | e6107e6 | 2005-02-23 18:01:10 +0000 | [diff] [blame] | 48 | import org.eclipse.wst.validation.internal.operations.WorkbenchReporter; |
david_williams | 49d24fb | 2005-04-08 21:16:59 +0000 | [diff] [blame] | 49 | import org.eclipse.wst.validation.internal.provisional.core.IMessage; |
| 50 | import org.eclipse.wst.validation.internal.provisional.core.IReporter; |
| 51 | import org.eclipse.wst.validation.internal.provisional.core.IValidationContext; |
| 52 | import org.eclipse.wst.validation.internal.provisional.core.IValidator; |
david_williams | 646a5e2 | 2005-04-02 07:16:27 +0000 | [diff] [blame] | 53 | import org.eclipse.wst.xml.core.internal.document.DocumentTypeAdapter; |
david_williams | 4ad020f | 2005-04-18 08:00:30 +0000 | [diff] [blame] | 54 | import org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument; |
| 55 | import org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel; |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 56 | import org.w3c.dom.Text; |
pavery | e6107e6 | 2005-02-23 18:01:10 +0000 | [diff] [blame] | 57 | |
pavery | a0e2eae | 2005-10-20 20:24:29 +0000 | [diff] [blame] | 58 | public class HTMLValidator implements IValidator, ISourceValidator { |
nitind | 73e7641 | 2005-11-10 01:50:19 +0000 | [diff] [blame] | 59 | private static final String ORG_ECLIPSE_JST_JSP_CORE_JSPSOURCE = "org.eclipse.jst.jsp.core.jspsource"; //$NON-NLS-1$ |
| 60 | private static final String ORG_ECLIPSE_WST_HTML_CORE_HTMLSOURCE = "org.eclipse.wst.html.core.htmlsource"; //$NON-NLS-1$ |
david_williams | e6ed732 | 2005-10-10 17:32:24 +0000 | [diff] [blame] | 61 | |
nitind | 165498a | 2005-07-12 18:47:49 +0000 | [diff] [blame] | 62 | static boolean shouldValidate(IFile file) { |
| 63 | IResource resource = file; |
| 64 | do { |
| 65 | if (resource.isDerived() || resource.isTeamPrivateMember() || !resource.isAccessible() || (resource.getName().charAt(0) == '.' && resource.getType() == IResource.FOLDER)) { |
| 66 | return false; |
| 67 | } |
| 68 | resource = resource.getParent(); |
| 69 | } |
| 70 | while ((resource.getType() & IResource.PROJECT) == 0); |
| 71 | return true; |
| 72 | } |
| 73 | |
pavery | a0e2eae | 2005-10-20 20:24:29 +0000 | [diff] [blame] | 74 | private IDocument fDocument; |
| 75 | |
pavery | e6107e6 | 2005-02-23 18:01:10 +0000 | [diff] [blame] | 76 | public HTMLValidator() { |
| 77 | super(); |
| 78 | } |
| 79 | |
| 80 | /** |
| 81 | */ |
| 82 | public void cleanup(IReporter reporter) { |
| 83 | // nothing to do |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | */ |
david_williams | c39caaf | 2005-04-05 06:07:16 +0000 | [diff] [blame] | 88 | protected IDOMModel getModel(IProject project, IFile file) { |
pavery | e6107e6 | 2005-02-23 18:01:10 +0000 | [diff] [blame] | 89 | if (project == null || file == null) |
| 90 | return null; |
| 91 | if (!file.exists()) |
| 92 | return null; |
| 93 | if (!canHandle(file)) |
| 94 | return null; |
| 95 | |
| 96 | IStructuredModel model = null; |
| 97 | IModelManager manager = StructuredModelManager.getModelManager(); |
| 98 | |
| 99 | try { |
| 100 | try { |
| 101 | model = manager.getModelForRead(file); |
| 102 | } |
| 103 | catch (UnsupportedEncodingException ex) { |
david_williams | 49d24fb | 2005-04-08 21:16:59 +0000 | [diff] [blame] | 104 | // retry ignoring META charset for invalid META charset |
| 105 | // specification |
pavery | e6107e6 | 2005-02-23 18:01:10 +0000 | [diff] [blame] | 106 | // recreate input stream, because it is already partially read |
| 107 | model = manager.getModelForRead(file, new String(), null); |
| 108 | } |
| 109 | } |
| 110 | catch (UnsupportedEncodingException ex) { |
| 111 | } |
| 112 | catch (IOException ex) { |
| 113 | } |
| 114 | catch (CoreException ex) { |
| 115 | } |
| 116 | |
| 117 | if (model == null) |
| 118 | return null; |
david_williams | c39caaf | 2005-04-05 06:07:16 +0000 | [diff] [blame] | 119 | if (!(model instanceof IDOMModel)) { |
pavery | e6107e6 | 2005-02-23 18:01:10 +0000 | [diff] [blame] | 120 | releaseModel(model); |
| 121 | return null; |
| 122 | } |
david_williams | c39caaf | 2005-04-05 06:07:16 +0000 | [diff] [blame] | 123 | return (IDOMModel) model; |
pavery | e6107e6 | 2005-02-23 18:01:10 +0000 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | /** |
| 127 | */ |
david_williams | c39caaf | 2005-04-05 06:07:16 +0000 | [diff] [blame] | 128 | protected HTMLValidationReporter getReporter(IReporter reporter, IFile file, IDOMModel model) { |
pavery | e6107e6 | 2005-02-23 18:01:10 +0000 | [diff] [blame] | 129 | return new HTMLValidationReporter(this, reporter, file, model); |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Check file extension to validate |
| 134 | */ |
| 135 | private boolean canHandle(IFile file) { |
david_williams | e6ed732 | 2005-10-10 17:32:24 +0000 | [diff] [blame] | 136 | boolean result = false; |
| 137 | if (file != null) { |
| 138 | try { |
| 139 | IContentTypeManager contentTypeManager = Platform.getContentTypeManager(); |
| 140 | |
nitind | a06daf8 | 2005-11-01 22:42:33 +0000 | [diff] [blame] | 141 | IContentDescription contentDescription = file.getContentDescription(); |
david_williams | e6ed732 | 2005-10-10 17:32:24 +0000 | [diff] [blame] | 142 | IContentType htmlContentType = contentTypeManager.getContentType(ORG_ECLIPSE_WST_HTML_CORE_HTMLSOURCE); |
nitind | a06daf8 | 2005-11-01 22:42:33 +0000 | [diff] [blame] | 143 | if (contentDescription != null) { |
| 144 | IContentType fileContentType = contentDescription.getContentType(); |
| 145 | |
| 146 | if (htmlContentType != null) { |
| 147 | if (fileContentType.isKindOf(htmlContentType)) { |
| 148 | result = true; |
| 149 | } |
| 150 | else { |
| 151 | // ISSUE: here's a little "backwards" dependancy. |
| 152 | // there should be a "JSPEMBEDDEDHTML validator" |
| 153 | // contributed by JSP plugin. |
| 154 | IContentType jspContentType = contentTypeManager.getContentType(ORG_ECLIPSE_JST_JSP_CORE_JSPSOURCE); |
| 155 | if (jspContentType != null) { |
| 156 | result = fileContentType.isKindOf(jspContentType); |
| 157 | } |
david_williams | e6ed732 | 2005-10-10 17:32:24 +0000 | [diff] [blame] | 158 | } |
| 159 | } |
| 160 | } |
nitind | a06daf8 | 2005-11-01 22:42:33 +0000 | [diff] [blame] | 161 | else if (htmlContentType != null) { |
| 162 | result = htmlContentType.isAssociatedWith(file.getName()); |
| 163 | } |
david_williams | e6ed732 | 2005-10-10 17:32:24 +0000 | [diff] [blame] | 164 | } |
| 165 | catch (CoreException e) { |
| 166 | // should be rare, but will ignore to avoid logging "encoding |
| 167 | // exceptions" and the like here. |
| 168 | // Logger.logException(e); |
| 169 | } |
| 170 | } |
| 171 | return result; |
pavery | e6107e6 | 2005-02-23 18:01:10 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /** |
| 175 | */ |
david_williams | c39caaf | 2005-04-05 06:07:16 +0000 | [diff] [blame] | 176 | private boolean hasHTMLFeature(IDOMDocument document) { |
pavery | e6107e6 | 2005-02-23 18:01:10 +0000 | [diff] [blame] | 177 | DocumentTypeAdapter adapter = (DocumentTypeAdapter) document.getAdapterFor(DocumentTypeAdapter.class); |
| 178 | if (adapter == null) |
| 179 | return false; |
| 180 | return adapter.hasFeature("HTML");//$NON-NLS-1$ |
| 181 | } |
| 182 | |
| 183 | /** |
| 184 | */ |
| 185 | protected void releaseModel(IStructuredModel model) { |
| 186 | if (model != null) |
| 187 | model.releaseFromRead(); |
| 188 | } |
| 189 | |
| 190 | /** |
| 191 | */ |
vbhadrir | 271d28a | 2005-04-12 19:36:05 +0000 | [diff] [blame] | 192 | public void validate(IValidationContext helper, IReporter reporter) { |
pavery | e6107e6 | 2005-02-23 18:01:10 +0000 | [diff] [blame] | 193 | if (helper == null) |
| 194 | return; |
| 195 | if ((reporter != null) && (reporter.isCancelled() == true)) { |
| 196 | throw new OperationCanceledException(); |
| 197 | } |
vbhadrir | 271d28a | 2005-04-12 19:36:05 +0000 | [diff] [blame] | 198 | String[] deltaArray = helper.getURIs(); |
pavery | e6107e6 | 2005-02-23 18:01:10 +0000 | [diff] [blame] | 199 | if (deltaArray != null && deltaArray.length > 0) { |
vbhadrir | 271d28a | 2005-04-12 19:36:05 +0000 | [diff] [blame] | 200 | validateDelta(helper, reporter); |
pavery | e6107e6 | 2005-02-23 18:01:10 +0000 | [diff] [blame] | 201 | } |
| 202 | else { |
vbhadrir | 271d28a | 2005-04-12 19:36:05 +0000 | [diff] [blame] | 203 | validateFull(helper, reporter); |
pavery | e6107e6 | 2005-02-23 18:01:10 +0000 | [diff] [blame] | 204 | } |
| 205 | } |
pavery | a0e2eae | 2005-10-20 20:24:29 +0000 | [diff] [blame] | 206 | |
| 207 | /** |
| 208 | * This validate call is for the ISourceValidator partial document validation approach |
| 209 | * @param dirtyRegion |
| 210 | * @param helper |
| 211 | * @param reporter |
| 212 | * @see org.eclipse.wst.sse.ui.internal.reconcile.validator.ISourceValidator |
| 213 | */ |
| 214 | public void validate(IRegion dirtyRegion, IValidationContext helper, IReporter reporter) { |
| 215 | |
| 216 | if (helper == null || fDocument == null) |
| 217 | return; |
| 218 | |
| 219 | if ((reporter != null) && (reporter.isCancelled() == true)) { |
| 220 | throw new OperationCanceledException(); |
| 221 | } |
| 222 | |
| 223 | IStructuredModel model = StructuredModelManager.getModelManager().getExistingModelForRead(fDocument); |
| 224 | if (model == null) |
| 225 | return; // error |
| 226 | |
| 227 | try { |
| 228 | |
| 229 | IDOMDocument document = null; |
| 230 | if(model instanceof IDOMModel) { |
| 231 | document = ((IDOMModel)model).getDocument(); |
| 232 | } |
pavery | e6107e6 | 2005-02-23 18:01:10 +0000 | [diff] [blame] | 233 | |
pavery | a0e2eae | 2005-10-20 20:24:29 +0000 | [diff] [blame] | 234 | if (document == null || !hasHTMLFeature(document)) |
| 235 | return ; // ignore |
| 236 | |
| 237 | ITextFileBuffer fb = FileBufferModelManager.getInstance().getBuffer(fDocument); |
| 238 | if(fb == null) |
| 239 | return; |
| 240 | |
| 241 | IFile file = ResourcesPlugin.getWorkspace().getRoot().getFile(fb.getLocation()); |
| 242 | if(file == null || !file.exists()) |
| 243 | return; |
| 244 | |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 245 | // this will be the wrong region if it's Text (instead of Element) |
| 246 | // we don't know how to validate Text |
pavery | a0e2eae | 2005-10-20 20:24:29 +0000 | [diff] [blame] | 247 | IndexedRegion ir = model.getIndexedRegion(dirtyRegion.getOffset()); |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 248 | if(ir instanceof Text) { |
| 249 | while(ir != null && ir instanceof Text) { |
| 250 | // it's assumed that this gets the IndexedRegion to |
| 251 | // the right of the end offset |
| 252 | ir = model.getIndexedRegion(ir.getEndOffset()); |
| 253 | } |
| 254 | } |
| 255 | |
pavery | a0e2eae | 2005-10-20 20:24:29 +0000 | [diff] [blame] | 256 | if(ir instanceof INodeNotifier) { |
| 257 | |
| 258 | INodeAdapterFactory factory = HTMLValidationAdapterFactory.getInstance(); |
| 259 | ValidationAdapter adapter = (ValidationAdapter) factory.adapt((INodeNotifier)ir); |
| 260 | if (adapter == null) |
| 261 | return; // error |
| 262 | |
| 263 | if (reporter != null) { |
| 264 | HTMLValidationReporter rep = null; |
| 265 | rep = getReporter(reporter, file, (IDOMModel)model); |
| 266 | rep.clear(); |
| 267 | adapter.setReporter(rep); |
| 268 | |
nitind | 73e7641 | 2005-11-10 01:50:19 +0000 | [diff] [blame] | 269 | String fileName = ""; //$NON-NLS-1$ |
pavery | a0e2eae | 2005-10-20 20:24:29 +0000 | [diff] [blame] | 270 | IPath filePath = file.getFullPath(); |
| 271 | if (filePath != null) { |
| 272 | fileName = filePath.toString(); |
| 273 | } |
| 274 | String args[] = new String[]{fileName}; |
| 275 | |
| 276 | Message mess = new LocalizedMessage(IMessage.LOW_SEVERITY, NLS.bind(HTMLUIMessages.MESSAGE_HTML_VALIDATION_MESSAGE_UI_, args)); |
| 277 | mess.setParams(args); |
| 278 | reporter.displaySubtask(this, mess); |
| 279 | } |
| 280 | adapter.validate(ir); |
| 281 | } |
| 282 | } |
| 283 | finally { |
| 284 | if(model != null) |
| 285 | model.releaseFromRead(); |
| 286 | } |
| 287 | } |
| 288 | |
| 289 | /** |
| 290 | * @see org.eclipse.wst.sse.ui.internal.reconcile.validator.ISourceValidator |
| 291 | */ |
| 292 | public void connect(IDocument document) { |
| 293 | fDocument = document; |
| 294 | } |
| 295 | /** |
| 296 | * @see org.eclipse.wst.sse.ui.internal.reconcile.validator.ISourceValidator |
| 297 | */ |
| 298 | public void disconnect(IDocument document) { |
| 299 | // don't need to do anything |
| 300 | } |
| 301 | |
pavery | e6107e6 | 2005-02-23 18:01:10 +0000 | [diff] [blame] | 302 | /** |
| 303 | */ |
david_williams | c39caaf | 2005-04-05 06:07:16 +0000 | [diff] [blame] | 304 | protected HTMLValidationResult validate(IDOMModel model, IFile file) { |
pavery | e6107e6 | 2005-02-23 18:01:10 +0000 | [diff] [blame] | 305 | IProject prj = null; |
| 306 | if (file != null) { |
| 307 | prj = file.getProject(); |
| 308 | } |
david_williams | 49d24fb | 2005-04-08 21:16:59 +0000 | [diff] [blame] | 309 | if ((prj == null) && (model != null)) { |
pavery | e6107e6 | 2005-02-23 18:01:10 +0000 | [diff] [blame] | 310 | URIResolver res = model.getResolver(); |
| 311 | if (res != null) { |
| 312 | prj = res.getProject(); |
| 313 | } |
| 314 | } |
| 315 | final WorkbenchReporter reporter = new WorkbenchReporter(prj, new NullProgressMonitor()); |
david_williams | 49d24fb | 2005-04-08 21:16:59 +0000 | [diff] [blame] | 316 | return validate(reporter, file, model); |
pavery | e6107e6 | 2005-02-23 18:01:10 +0000 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | /** |
| 320 | */ |
david_williams | c39caaf | 2005-04-05 06:07:16 +0000 | [diff] [blame] | 321 | private HTMLValidationResult validate(IReporter reporter, IFile file, IDOMModel model) { |
pavery | e6107e6 | 2005-02-23 18:01:10 +0000 | [diff] [blame] | 322 | if (file == null || model == null) |
| 323 | return null; // error |
david_williams | c39caaf | 2005-04-05 06:07:16 +0000 | [diff] [blame] | 324 | IDOMDocument document = model.getDocument(); |
pavery | e6107e6 | 2005-02-23 18:01:10 +0000 | [diff] [blame] | 325 | if (document == null) |
| 326 | return null; // error |
| 327 | if (!hasHTMLFeature(document)) |
| 328 | return null; // ignore |
| 329 | |
david_williams | 285ee2d | 2005-04-10 18:21:21 +0000 | [diff] [blame] | 330 | INodeAdapterFactory factory = HTMLValidationAdapterFactory.getInstance(); |
pavery | e6107e6 | 2005-02-23 18:01:10 +0000 | [diff] [blame] | 331 | ValidationAdapter adapter = (ValidationAdapter) factory.adapt(document); |
| 332 | if (adapter == null) |
| 333 | return null; // error |
| 334 | |
| 335 | HTMLValidationReporter rep = getReporter(reporter, file, model); |
| 336 | rep.clear(); |
| 337 | adapter.setReporter(rep); |
| 338 | if (reporter != null) { |
nitind | 73e7641 | 2005-11-10 01:50:19 +0000 | [diff] [blame] | 339 | String fileName = ""; //$NON-NLS-1$ |
david_williams | e6ed732 | 2005-10-10 17:32:24 +0000 | [diff] [blame] | 340 | IPath filePath = file.getFullPath(); |
| 341 | if (filePath != null) { |
| 342 | fileName = filePath.toString(); |
| 343 | } |
| 344 | String args[] = new String[]{fileName}; |
david_williams | 49d24fb | 2005-04-08 21:16:59 +0000 | [diff] [blame] | 345 | |
| 346 | // Message mess = new Message("HTMLValidation", //$NON-NLS-1$ |
| 347 | // SeverityEnum.LOW_SEVERITY, |
| 348 | // "MESSAGE_HTML_VALIDATION_MESSAGE_UI_", //$NON-NLS-1$ |
| 349 | // args); |
nitind | e83b1a4 | 2005-06-22 21:22:20 +0000 | [diff] [blame] | 350 | Message mess = new LocalizedMessage(IMessage.LOW_SEVERITY, NLS.bind(HTMLUIMessages.MESSAGE_HTML_VALIDATION_MESSAGE_UI_, args)); |
pavery | e6107e6 | 2005-02-23 18:01:10 +0000 | [diff] [blame] | 351 | mess.setParams(args); |
| 352 | reporter.displaySubtask(this, mess); |
| 353 | } |
| 354 | adapter.validate(document); |
| 355 | return rep.getResult(); |
| 356 | } |
| 357 | |
| 358 | /** |
| 359 | */ |
david_williams | 4327167 | 2005-04-01 05:19:26 +0000 | [diff] [blame] | 360 | private void validateContainer(IValidationContext helper, IReporter reporter, IContainer container) { |
pavery | e6107e6 | 2005-02-23 18:01:10 +0000 | [diff] [blame] | 361 | try { |
| 362 | IResource[] resourceArray = container.members(false); |
| 363 | for (int i = 0; i < resourceArray.length; i++) { |
| 364 | IResource resource = resourceArray[i]; |
| 365 | if (resource == null) |
| 366 | continue; |
| 367 | if (resource instanceof IFile) { |
| 368 | validateFile(helper, reporter, (IFile) resource); |
| 369 | } |
| 370 | else if (resource instanceof IContainer) { |
| 371 | validateContainer(helper, reporter, (IContainer) resource); |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | catch (CoreException ex) { |
| 376 | } |
| 377 | } |
| 378 | |
| 379 | /** |
| 380 | */ |
vbhadrir | 271d28a | 2005-04-12 19:36:05 +0000 | [diff] [blame] | 381 | private void validateDelta(IValidationContext helper, IReporter reporter) { |
| 382 | String[] deltaArray = helper.getURIs(); |
pavery | e6107e6 | 2005-02-23 18:01:10 +0000 | [diff] [blame] | 383 | for (int i = 0; i < deltaArray.length; i++) { |
vbhadrir | 271d28a | 2005-04-12 19:36:05 +0000 | [diff] [blame] | 384 | String delta = deltaArray[i]; |
| 385 | if (delta == null) |
pavery | e6107e6 | 2005-02-23 18:01:10 +0000 | [diff] [blame] | 386 | continue; |
| 387 | IResource resource = getResource(delta); |
| 388 | if (resource == null || !(resource instanceof IFile)) |
| 389 | continue; |
| 390 | validateFile(helper, reporter, (IFile) resource); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | */ |
david_williams | 4327167 | 2005-04-01 05:19:26 +0000 | [diff] [blame] | 396 | private void validateFile(IValidationContext helper, IReporter reporter, IFile file) { |
pavery | e6107e6 | 2005-02-23 18:01:10 +0000 | [diff] [blame] | 397 | if ((reporter != null) && (reporter.isCancelled() == true)) { |
| 398 | throw new OperationCanceledException(); |
| 399 | } |
nitind | 165498a | 2005-07-12 18:47:49 +0000 | [diff] [blame] | 400 | if (!shouldValidate(file)) { |
| 401 | return; |
| 402 | } |
david_williams | c39caaf | 2005-04-05 06:07:16 +0000 | [diff] [blame] | 403 | IDOMModel model = getModel(file.getProject(), file); |
pavery | e6107e6 | 2005-02-23 18:01:10 +0000 | [diff] [blame] | 404 | if (model == null) |
| 405 | return; |
| 406 | |
| 407 | try { |
| 408 | validate(reporter, file, model); |
| 409 | } |
| 410 | finally { |
| 411 | releaseModel(model); |
| 412 | } |
| 413 | } |
| 414 | |
| 415 | /** |
| 416 | */ |
vbhadrir | 271d28a | 2005-04-12 19:36:05 +0000 | [diff] [blame] | 417 | private void validateFull(IValidationContext helper, IReporter reporter) { |
pavery | e6107e6 | 2005-02-23 18:01:10 +0000 | [diff] [blame] | 418 | IProject project = null; |
vbhadrir | 271d28a | 2005-04-12 19:36:05 +0000 | [diff] [blame] | 419 | String[] fileDelta = helper.getURIs(); |
| 420 | if (helper instanceof IWorkbenchContext) { |
| 421 | IWorkbenchContext wbHelper = (IWorkbenchContext) helper; |
pavery | e6107e6 | 2005-02-23 18:01:10 +0000 | [diff] [blame] | 422 | project = wbHelper.getProject(); |
| 423 | } |
| 424 | else { |
| 425 | // won't work for project validation (b/c nothing in file delta) |
| 426 | project = getResource(fileDelta[0]).getProject(); |
| 427 | } |
| 428 | if (project == null) |
| 429 | return; |
| 430 | validateContainer(helper, reporter, project); |
| 431 | } |
| 432 | |
david_williams | 49d24fb | 2005-04-08 21:16:59 +0000 | [diff] [blame] | 433 | /* |
pavery | e6107e6 | 2005-02-23 18:01:10 +0000 | [diff] [blame] | 434 | * added to get rid or dependency on IWorkbenchHelper |
david_williams | 49d24fb | 2005-04-08 21:16:59 +0000 | [diff] [blame] | 435 | * |
pavery | e6107e6 | 2005-02-23 18:01:10 +0000 | [diff] [blame] | 436 | */ |
vbhadrir | 271d28a | 2005-04-12 19:36:05 +0000 | [diff] [blame] | 437 | public IResource getResource(String delta) { |
david_williams | e6ed732 | 2005-10-10 17:32:24 +0000 | [diff] [blame] | 438 | return ResourcesPlugin.getWorkspace().getRoot().getFile(new Path(delta)); |
pavery | e6107e6 | 2005-02-23 18:01:10 +0000 | [diff] [blame] | 439 | } |
| 440 | } |