david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 1 | /******************************************************************************* |
nitind | b7573d3 | 2008-02-07 07:30:45 +0000 | [diff] [blame] | 2 | * Copyright (c) 2001, 2008 IBM Corporation and others. |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 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 |
amywu | ecebb04 | 2007-04-10 20:07:35 +0000 | [diff] [blame] | 7 | * |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 8 | * Contributors: |
| 9 | * IBM Corporation - initial API and implementation |
| 10 | * Jens Lukowski/Innoopract - initial renaming/restructuring |
| 11 | * |
| 12 | *******************************************************************************/ |
| 13 | package org.eclipse.wst.sse.ui.internal.reconcile.validator; |
| 14 | |
| 15 | import java.util.ArrayList; |
| 16 | import java.util.Arrays; |
| 17 | import java.util.HashMap; |
pavery | 46c93e7 | 2005-02-08 16:07:33 +0000 | [diff] [blame] | 18 | import java.util.HashSet; |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 19 | import java.util.Iterator; |
| 20 | import java.util.List; |
pavery | 46c93e7 | 2005-02-08 16:07:33 +0000 | [diff] [blame] | 21 | import java.util.Set; |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 22 | |
amywu | 7d8efb0 | 2006-04-13 04:08:07 +0000 | [diff] [blame] | 23 | import org.eclipse.core.resources.IFile; |
amywu | 7d8efb0 | 2006-04-13 04:08:07 +0000 | [diff] [blame] | 24 | import org.eclipse.core.resources.ResourcesPlugin; |
| 25 | import org.eclipse.core.runtime.IPath; |
| 26 | import org.eclipse.core.runtime.Path; |
pavery | 46c93e7 | 2005-02-08 16:07:33 +0000 | [diff] [blame] | 27 | import org.eclipse.core.runtime.Platform; |
| 28 | import org.eclipse.core.runtime.content.IContentType; |
| 29 | import org.eclipse.core.runtime.content.IContentTypeManager; |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 30 | import org.eclipse.jface.text.IDocument; |
| 31 | import org.eclipse.jface.text.ITypedRegion; |
| 32 | import org.eclipse.jface.text.reconciler.DirtyRegion; |
| 33 | import org.eclipse.jface.text.reconciler.IReconcileResult; |
| 34 | import org.eclipse.jface.text.reconciler.IReconcileStep; |
nitind | f8e7763 | 2005-09-07 23:49:25 +0000 | [diff] [blame] | 35 | import org.eclipse.jface.text.source.ISourceViewer; |
amywu | 7d8efb0 | 2006-04-13 04:08:07 +0000 | [diff] [blame] | 36 | import org.eclipse.wst.sse.core.StructuredModelManager; |
| 37 | import org.eclipse.wst.sse.core.internal.provisional.IStructuredModel; |
pavery | b0763c0 | 2005-10-17 15:55:28 +0000 | [diff] [blame] | 38 | import org.eclipse.wst.sse.ui.internal.IReleasable; |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 39 | import org.eclipse.wst.sse.ui.internal.reconcile.DocumentAdapter; |
pavery | 1f1588d | 2006-02-15 20:44:54 +0000 | [diff] [blame] | 40 | import org.eclipse.wst.sse.ui.internal.reconcile.ReconcileAnnotationKey; |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 41 | import org.eclipse.wst.sse.ui.internal.reconcile.StructuredTextReconcilingStrategy; |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 42 | import org.eclipse.wst.sse.ui.internal.reconcile.TemporaryAnnotation; |
nitind | b7573d3 | 2008-02-07 07:30:45 +0000 | [diff] [blame] | 43 | import org.eclipse.wst.validation.ValidationFramework; |
| 44 | import org.eclipse.wst.validation.Validator; |
david_williams | 49d24fb | 2005-04-08 21:16:59 +0000 | [diff] [blame] | 45 | import org.eclipse.wst.validation.internal.provisional.core.IValidator; |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 46 | |
| 47 | |
| 48 | /** |
pavery | f918eb2 | 2005-03-29 18:26:53 +0000 | [diff] [blame] | 49 | * Special validator strategy. Runs validator steps contributed via the |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 50 | * <code>org.eclipse.wst.sse.ui.extensions.sourcevalidation</code> extension |
| 51 | * point |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 52 | * |
| 53 | * @author pavery |
| 54 | */ |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 55 | public class ValidatorStrategy extends StructuredTextReconcilingStrategy { |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 56 | |
| 57 | private String[] fContentTypeIds = null; |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 58 | private List fMetaData = null; |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 59 | /** validator id (as declared in ext point) -> ReconcileStepForValidator * */ |
| 60 | private HashMap fVidToVStepMap = null; |
| 61 | |
| 62 | /* |
| 63 | * List of ValidatorMetaDatas of total scope validators that have been run |
| 64 | * since beginProcessing() was called. |
| 65 | */ |
| 66 | private List fTotalScopeValidatorsAlreadyRun; |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 67 | |
nitind | f8e7763 | 2005-09-07 23:49:25 +0000 | [diff] [blame] | 68 | public ValidatorStrategy(ISourceViewer sourceViewer, String contentType) { |
| 69 | super(sourceViewer); |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 70 | fMetaData = new ArrayList(); |
pavery | 46c93e7 | 2005-02-08 16:07:33 +0000 | [diff] [blame] | 71 | fContentTypeIds = calculateParentContentTypeIds(contentType); |
pavery | 55ae447 | 2005-02-16 23:00:16 +0000 | [diff] [blame] | 72 | fVidToVStepMap = new HashMap(); |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 73 | } |
| 74 | |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 75 | public void addValidatorMetaData(ValidatorMetaData vmd) { |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 76 | fMetaData.add(vmd); |
| 77 | } |
| 78 | |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 79 | public void beginProcessing() { |
| 80 | if (fTotalScopeValidatorsAlreadyRun == null) |
| 81 | fTotalScopeValidatorsAlreadyRun = new ArrayList(); |
nitind | f3f1087 | 2007-09-03 06:36:56 +0000 | [diff] [blame] | 82 | else |
| 83 | fTotalScopeValidatorsAlreadyRun.clear(); |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | /** |
| 87 | * The content type passed in should be the most specific one. TODO: This |
| 88 | * exact method is also in ValidatorMetaData. Should be in a common place. |
| 89 | * |
| 90 | * @param contentType |
| 91 | * @return |
| 92 | */ |
| 93 | private String[] calculateParentContentTypeIds(String contentTypeId) { |
| 94 | |
| 95 | Set parentTypes = new HashSet(); |
| 96 | |
| 97 | IContentTypeManager ctManager = Platform.getContentTypeManager(); |
| 98 | IContentType ct = ctManager.getContentType(contentTypeId); |
| 99 | String id = contentTypeId; |
| 100 | |
| 101 | while (ct != null && id != null) { |
| 102 | |
| 103 | parentTypes.add(id); |
| 104 | ct = ctManager.getContentType(id); |
| 105 | if (ct != null) { |
| 106 | IContentType baseType = ct.getBaseType(); |
| 107 | id = (baseType != null) ? baseType.getId() : null; |
| 108 | } |
| 109 | } |
| 110 | return (String[]) parentTypes.toArray(new String[parentTypes.size()]); |
| 111 | } |
| 112 | |
pavery | 3f79184 | 2006-02-15 21:32:25 +0000 | [diff] [blame] | 113 | protected boolean canHandlePartition(String partitionType) { |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 114 | ValidatorMetaData vmd = null; |
| 115 | for (int i = 0; i < fMetaData.size(); i++) { |
| 116 | vmd = (ValidatorMetaData) fMetaData.get(i); |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 117 | if (vmd.canHandlePartitionType(getContentTypeIds(), partitionType)) |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 118 | return true; |
| 119 | } |
| 120 | return false; |
| 121 | } |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 122 | |
| 123 | protected boolean containsStep(IReconcileStep step) { |
| 124 | return fVidToVStepMap.containsValue(step); |
| 125 | } |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 126 | |
| 127 | /** |
david_williams | 4ad020f | 2005-04-18 08:00:30 +0000 | [diff] [blame] | 128 | * @see org.eclipse.wst.sse.ui.internal.provisional.reconcile.AbstractStructuredTextReconcilingStrategy#createReconcileSteps() |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 129 | */ |
| 130 | public void createReconcileSteps() { |
| 131 | // do nothing, steps are created |
| 132 | } |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 133 | |
| 134 | public void endProcessing() { |
| 135 | fTotalScopeValidatorsAlreadyRun.clear(); |
| 136 | } |
| 137 | |
pavery | 1f1588d | 2006-02-15 20:44:54 +0000 | [diff] [blame] | 138 | /** |
| 139 | * All content types on which this ValidatorStrategy can run |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 140 | * |
pavery | 1f1588d | 2006-02-15 20:44:54 +0000 | [diff] [blame] | 141 | * @return |
| 142 | */ |
pavery | 46c93e7 | 2005-02-08 16:07:33 +0000 | [diff] [blame] | 143 | public String[] getContentTypeIds() { |
| 144 | return fContentTypeIds; |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 145 | } |
| 146 | |
pavery | dfec417 | 2005-11-09 21:42:30 +0000 | [diff] [blame] | 147 | /** |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 148 | * @param tr |
| 149 | * Partition of the region to reconcile. |
| 150 | * @param dr |
| 151 | * Dirty region representation of the typed region |
pavery | dfec417 | 2005-11-09 21:42:30 +0000 | [diff] [blame] | 152 | */ |
pavery | 46c93e7 | 2005-02-08 16:07:33 +0000 | [diff] [blame] | 153 | public void reconcile(ITypedRegion tr, DirtyRegion dr) { |
nitind | b7573d3 | 2008-02-07 07:30:45 +0000 | [diff] [blame] | 154 | /* |
| 155 | * Abort if no workspace file is known (new validation framework does |
| 156 | * not support that scenario) or no validators have been specified |
| 157 | */ |
| 158 | if (isCanceled() || fMetaData.isEmpty()) |
pavery | baf7e52 | 2005-07-07 20:44:14 +0000 | [diff] [blame] | 159 | return; |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 160 | |
pavery | dfec417 | 2005-11-09 21:42:30 +0000 | [diff] [blame] | 161 | IDocument doc = getDocument(); |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 162 | // for external files, this can be null |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 163 | if (doc == null) |
pavery | dfec417 | 2005-11-09 21:42:30 +0000 | [diff] [blame] | 164 | return; |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 165 | |
pavery | dfec417 | 2005-11-09 21:42:30 +0000 | [diff] [blame] | 166 | String partitionType = tr.getType(); |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 167 | |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 168 | ValidatorMetaData vmd = null; |
| 169 | List annotationsToAdd = new ArrayList(); |
david_williams | 5ce6d6c | 2006-05-28 23:58:12 +0000 | [diff] [blame] | 170 | List stepsRanOnThisDirtyRegion = new ArrayList(1); |
nitind | b7573d3 | 2008-02-07 07:30:45 +0000 | [diff] [blame] | 171 | |
| 172 | /* |
| 173 | * Keep track of the disabled validators by source id for the V2 |
| 174 | * validators. |
| 175 | */ |
| 176 | Set disabledValsBySourceId = new HashSet(20); |
| 177 | |
| 178 | /* |
| 179 | * Keep track of the disabled validators by class id for the v1 |
| 180 | * validators. |
| 181 | */ |
| 182 | Set disabledValsByClass = new HashSet(20); |
| 183 | if (getFile() != null) { |
| 184 | for (Iterator it = ValidationFramework.getDefault().getDisabledValidatorsFor(getFile()).iterator(); it.hasNext();) { |
| 185 | Validator v = (Validator) it.next(); |
| 186 | IValidator iv = v.asIValidator(); |
| 187 | if (iv != null && v.getSourceId() != null) |
| 188 | disabledValsBySourceId.add(v.getSourceId()); |
| 189 | Validator.V1 v1 = v.asV1Validator(); |
| 190 | if (v1 != null) |
| 191 | disabledValsByClass.add(v1.getId()); |
| 192 | } |
| 193 | } |
| 194 | |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 195 | /* |
| 196 | * Loop through all of the relevant validator meta data to find |
| 197 | * supporting validators for this partition type. Don't check |
| 198 | * this.canHandlePartition() before-hand since it just loops through |
| 199 | * and calls vmd.canHandlePartitionType()...which we're already doing |
| 200 | * here anyway to find the right vmd. |
| 201 | */ |
| 202 | for (int i = 0; i < fMetaData.size() && !isCanceled(); i++) { |
| 203 | vmd = (ValidatorMetaData) fMetaData.get(i); |
| 204 | if (vmd.canHandlePartitionType(getContentTypeIds(), partitionType)) { |
amywu | 7d8efb0 | 2006-04-13 04:08:07 +0000 | [diff] [blame] | 205 | /* |
| 206 | * Check if validator is enabled according to validation |
| 207 | * preferences before attempting to create/use it |
| 208 | */ |
nitind | b7573d3 | 2008-02-07 07:30:45 +0000 | [diff] [blame] | 209 | if (!disabledValsBySourceId.contains(vmd.getValidatorId()) && !disabledValsByClass.contains(vmd.getValidatorClass())) { |
amywu | 7d8efb0 | 2006-04-13 04:08:07 +0000 | [diff] [blame] | 210 | int validatorScope = vmd.getValidatorScope(); |
| 211 | ReconcileStepForValidator validatorStep = null; |
| 212 | // get step for partition type |
| 213 | Object o = fVidToVStepMap.get(vmd.getValidatorId()); |
| 214 | if (o != null) { |
| 215 | validatorStep = (ReconcileStepForValidator) o; |
| 216 | } |
| 217 | else { |
| 218 | // if doesn't exist, create one |
| 219 | IValidator validator = vmd.createValidator(); |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 220 | |
amywu | 7d8efb0 | 2006-04-13 04:08:07 +0000 | [diff] [blame] | 221 | validatorStep = new ReconcileStepForValidator(validator, validatorScope); |
| 222 | validatorStep.setInputModel(new DocumentAdapter(doc)); |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 223 | |
amywu | 7d8efb0 | 2006-04-13 04:08:07 +0000 | [diff] [blame] | 224 | fVidToVStepMap.put(vmd.getValidatorId(), validatorStep); |
| 225 | } |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 226 | |
amywu | 7d8efb0 | 2006-04-13 04:08:07 +0000 | [diff] [blame] | 227 | if (!fTotalScopeValidatorsAlreadyRun.contains(vmd)) { |
| 228 | annotationsToAdd.addAll(Arrays.asList(validatorStep.reconcile(dr, dr))); |
david_williams | 5ce6d6c | 2006-05-28 23:58:12 +0000 | [diff] [blame] | 229 | stepsRanOnThisDirtyRegion.add(validatorStep); |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 230 | |
amywu | 7d8efb0 | 2006-04-13 04:08:07 +0000 | [diff] [blame] | 231 | if (validatorScope == ReconcileAnnotationKey.TOTAL) { |
| 232 | // mark this validator as "run" |
| 233 | fTotalScopeValidatorsAlreadyRun.add(vmd); |
| 234 | } |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 235 | } |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 236 | } |
| 237 | } |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 238 | } |
| 239 | |
david_williams | 5ce6d6c | 2006-05-28 23:58:12 +0000 | [diff] [blame] | 240 | TemporaryAnnotation[] annotationsToRemove = getAnnotationsToRemove(dr, stepsRanOnThisDirtyRegion); |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 241 | if (annotationsToRemove.length + annotationsToAdd.size() > 0) |
| 242 | smartProcess(annotationsToRemove, (IReconcileResult[]) annotationsToAdd.toArray(new IReconcileResult[annotationsToAdd.size()])); |
| 243 | } |
| 244 | |
| 245 | public void release() { |
| 246 | super.release(); |
| 247 | Iterator it = fVidToVStepMap.values().iterator(); |
| 248 | IReconcileStep step = null; |
| 249 | while (it.hasNext()) { |
| 250 | step = (IReconcileStep) it.next(); |
| 251 | if (step instanceof IReleasable) |
| 252 | ((IReleasable) step).release(); |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 253 | } |
| 254 | } |
| 255 | |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 256 | /** |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 257 | * @see org.eclipse.wst.sse.ui.internal.reconcile.AbstractStructuredTextReconcilingStrategy#setDocument(org.eclipse.jface.text.IDocument) |
| 258 | */ |
| 259 | public void setDocument(IDocument document) { |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 260 | |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 261 | super.setDocument(document); |
nitind | b044597 | 2006-02-16 22:19:20 +0000 | [diff] [blame] | 262 | |
pavery | 55ae447 | 2005-02-16 23:00:16 +0000 | [diff] [blame] | 263 | // validator steps are in "fVIdToVStepMap" (as opposed to fFirstStep > |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 264 | // next step etc...) |
pavery | 55ae447 | 2005-02-16 23:00:16 +0000 | [diff] [blame] | 265 | Iterator it = fVidToVStepMap.values().iterator(); |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 266 | IReconcileStep step = null; |
| 267 | while (it.hasNext()) { |
| 268 | step = (IReconcileStep) it.next(); |
| 269 | step.setInputModel(new DocumentAdapter(document)); |
| 270 | } |
| 271 | } |
amywu | 7d8efb0 | 2006-04-13 04:08:07 +0000 | [diff] [blame] | 272 | |
| 273 | /** |
amywu | 7d8efb0 | 2006-04-13 04:08:07 +0000 | [diff] [blame] | 274 | * Gets IFile from current document |
| 275 | * |
nitind | 6f36001 | 2006-05-02 18:48:59 +0000 | [diff] [blame] | 276 | * @return IFile the IFile, null if no such file exists |
amywu | 7d8efb0 | 2006-04-13 04:08:07 +0000 | [diff] [blame] | 277 | */ |
| 278 | private IFile getFile() { |
| 279 | IStructuredModel model = null; |
| 280 | IFile file = null; |
| 281 | try { |
| 282 | model = StructuredModelManager.getModelManager().getExistingModelForRead(getDocument()); |
| 283 | if (model != null) { |
| 284 | String baseLocation = model.getBaseLocation(); |
| 285 | // The baseLocation may be a path on disk or relative to the |
| 286 | // workspace root. Don't translate on-disk paths to |
| 287 | // in-workspace resources. |
| 288 | IPath basePath = new Path(baseLocation); |
nitind | 6f36001 | 2006-05-02 18:48:59 +0000 | [diff] [blame] | 289 | if (basePath.segmentCount() > 1) { |
amywu | 7d8efb0 | 2006-04-13 04:08:07 +0000 | [diff] [blame] | 290 | file = ResourcesPlugin.getWorkspace().getRoot().getFile(basePath); |
nitind | 6f36001 | 2006-05-02 18:48:59 +0000 | [diff] [blame] | 291 | /* |
| 292 | * If the IFile doesn't exist, make sure it's not |
| 293 | * returned |
| 294 | */ |
| 295 | if (!file.exists()) |
| 296 | file = null; |
amywu | 7d8efb0 | 2006-04-13 04:08:07 +0000 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | } |
| 300 | finally { |
| 301 | if (model != null) { |
| 302 | model.releaseFromRead(); |
| 303 | } |
| 304 | } |
| 305 | return file; |
| 306 | } |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 307 | } |