david_williams | cfdb2cd | 2004-11-11 08:37:49 +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 | * 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 | |
pavery | 46c93e7 | 2005-02-08 16:07:33 +0000 | [diff] [blame] | 23 | import org.eclipse.core.runtime.Platform; |
| 24 | import org.eclipse.core.runtime.content.IContentType; |
| 25 | import org.eclipse.core.runtime.content.IContentTypeManager; |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 26 | import org.eclipse.jface.text.IDocument; |
| 27 | import org.eclipse.jface.text.ITypedRegion; |
| 28 | import org.eclipse.jface.text.reconciler.DirtyRegion; |
| 29 | import org.eclipse.jface.text.reconciler.IReconcileResult; |
| 30 | import org.eclipse.jface.text.reconciler.IReconcileStep; |
nitind | f8e7763 | 2005-09-07 23:49:25 +0000 | [diff] [blame] | 31 | import org.eclipse.jface.text.source.ISourceViewer; |
pavery | b0763c0 | 2005-10-17 15:55:28 +0000 | [diff] [blame] | 32 | import org.eclipse.wst.sse.ui.internal.IReleasable; |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 33 | import org.eclipse.wst.sse.ui.internal.reconcile.DocumentAdapter; |
pavery | 1f1588d | 2006-02-15 20:44:54 +0000 | [diff] [blame^] | 34 | import org.eclipse.wst.sse.ui.internal.reconcile.ReconcileAnnotationKey; |
pavery | f4a08b3 | 2005-02-02 21:39:25 +0000 | [diff] [blame] | 35 | import org.eclipse.wst.sse.ui.internal.reconcile.StructuredReconcileStep; |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 36 | import org.eclipse.wst.sse.ui.internal.reconcile.StructuredTextReconcilingStrategy; |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 37 | import org.eclipse.wst.sse.ui.internal.reconcile.TemporaryAnnotation; |
david_williams | 49d24fb | 2005-04-08 21:16:59 +0000 | [diff] [blame] | 38 | import org.eclipse.wst.validation.internal.provisional.core.IValidator; |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 39 | |
| 40 | |
| 41 | /** |
pavery | f918eb2 | 2005-03-29 18:26:53 +0000 | [diff] [blame] | 42 | * Special validator strategy. Runs validator steps contributed via the |
| 43 | * <code>org.eclipse.wst.sse.ui.extensions.sourcevalidation</code> extension point |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 44 | * |
| 45 | * @author pavery |
| 46 | */ |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 47 | public class ValidatorStrategy extends StructuredTextReconcilingStrategy { |
pavery | 46c93e7 | 2005-02-08 16:07:33 +0000 | [diff] [blame] | 48 | |
| 49 | private String[] fContentTypeIds = null; |
pavery | 55ae447 | 2005-02-16 23:00:16 +0000 | [diff] [blame] | 50 | /** validator id (as declared in ext point) -> ReconcileStepForValidator **/ |
| 51 | private HashMap fVidToVStepMap = null; |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 52 | private List fMetaData = null; |
| 53 | |
nitind | f8e7763 | 2005-09-07 23:49:25 +0000 | [diff] [blame] | 54 | public ValidatorStrategy(ISourceViewer sourceViewer, String contentType) { |
| 55 | super(sourceViewer); |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 56 | fMetaData = new ArrayList(); |
pavery | 46c93e7 | 2005-02-08 16:07:33 +0000 | [diff] [blame] | 57 | fContentTypeIds = calculateParentContentTypeIds(contentType); |
pavery | 55ae447 | 2005-02-16 23:00:16 +0000 | [diff] [blame] | 58 | fVidToVStepMap = new HashMap(); |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 59 | } |
| 60 | |
pavery | 46c93e7 | 2005-02-08 16:07:33 +0000 | [diff] [blame] | 61 | /** |
pavery | a389cae | 2005-02-11 16:23:22 +0000 | [diff] [blame] | 62 | * The content type passed in should be the most specific one. |
| 63 | * TODO: This exact method is also in ValidatorMetaData. Should be in a common place. |
| 64 | * |
pavery | 46c93e7 | 2005-02-08 16:07:33 +0000 | [diff] [blame] | 65 | * @param contentType |
| 66 | * @return |
| 67 | */ |
| 68 | private String[] calculateParentContentTypeIds(String contentTypeId) { |
pavery | efd06f0 | 2005-02-08 20:33:04 +0000 | [diff] [blame] | 69 | |
pavery | 46c93e7 | 2005-02-08 16:07:33 +0000 | [diff] [blame] | 70 | Set parentTypes = new HashSet(); |
| 71 | |
pavery | efd06f0 | 2005-02-08 20:33:04 +0000 | [diff] [blame] | 72 | IContentTypeManager ctManager = Platform.getContentTypeManager(); |
| 73 | IContentType ct = ctManager.getContentType(contentTypeId); |
| 74 | String id = contentTypeId; |
| 75 | |
| 76 | while(ct != null && id != null) { |
| 77 | |
pavery | 46c93e7 | 2005-02-08 16:07:33 +0000 | [diff] [blame] | 78 | parentTypes.add(id); |
pavery | efd06f0 | 2005-02-08 20:33:04 +0000 | [diff] [blame] | 79 | ct = ctManager.getContentType(id); |
| 80 | if(ct != null) { |
| 81 | IContentType baseType = ct.getBaseType(); |
| 82 | id = (baseType != null) ? baseType.getId() : null; |
pavery | 46c93e7 | 2005-02-08 16:07:33 +0000 | [diff] [blame] | 83 | } |
| 84 | } |
pavery | 46c93e7 | 2005-02-08 16:07:33 +0000 | [diff] [blame] | 85 | return (String[])parentTypes.toArray(new String[parentTypes.size()]); |
| 86 | } |
| 87 | |
pavery | 46c93e7 | 2005-02-08 16:07:33 +0000 | [diff] [blame] | 88 | public void addValidatorMetaData(ValidatorMetaData vmd) { |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 89 | fMetaData.add(vmd); |
| 90 | } |
| 91 | |
pavery | 46c93e7 | 2005-02-08 16:07:33 +0000 | [diff] [blame] | 92 | /** |
| 93 | * |
| 94 | * @param partitionType |
| 95 | * @return true if the strategy contains at least one ValidatorMetaData |
| 96 | * that says it can handle the partition type (for a given content type) |
| 97 | */ |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 98 | public boolean canValidatePartition(String partitionType) { |
| 99 | ValidatorMetaData vmd = null; |
| 100 | for (int i = 0; i < fMetaData.size(); i++) { |
| 101 | vmd = (ValidatorMetaData) fMetaData.get(i); |
pavery | 46c93e7 | 2005-02-08 16:07:33 +0000 | [diff] [blame] | 102 | if (vmd.canHandleParitionType(getContentTypeIds(), partitionType)) |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 103 | return true; |
| 104 | } |
| 105 | return false; |
| 106 | } |
pavery | 3b75d5d | 2005-11-10 02:27:12 +0000 | [diff] [blame] | 107 | |
| 108 | protected boolean canHandlePartition(String partition) { |
| 109 | return canValidatePartition(partition); |
| 110 | } |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 111 | |
| 112 | /* |
| 113 | * so that removal will work properly |
| 114 | * |
| 115 | * @see org.eclipse.wst.sse.ui.reconcile.AbstractStructuredTextReconcilingStrategy#containsStep(org.eclipse.jface.text.reconciler.IReconcileStep) |
| 116 | */ |
| 117 | protected boolean containsStep(IReconcileStep step) { |
pavery | 55ae447 | 2005-02-16 23:00:16 +0000 | [diff] [blame] | 118 | return step != null ? fVidToVStepMap.values().contains(step) : false; |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 119 | } |
| 120 | |
| 121 | /** |
david_williams | 4ad020f | 2005-04-18 08:00:30 +0000 | [diff] [blame] | 122 | * @see org.eclipse.wst.sse.ui.internal.provisional.reconcile.AbstractStructuredTextReconcilingStrategy#createReconcileSteps() |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 123 | */ |
| 124 | public void createReconcileSteps() { |
| 125 | // do nothing, steps are created |
| 126 | } |
pavery | 1f1588d | 2006-02-15 20:44:54 +0000 | [diff] [blame^] | 127 | /** |
| 128 | * All content types on which this ValidatorStrategy can run |
| 129 | * @return |
| 130 | */ |
pavery | 46c93e7 | 2005-02-08 16:07:33 +0000 | [diff] [blame] | 131 | public String[] getContentTypeIds() { |
| 132 | return fContentTypeIds; |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 133 | } |
| 134 | |
| 135 | /* |
| 136 | * so that removal will work properly |
| 137 | * |
| 138 | * @see org.eclipse.wst.sse.ui.reconcile.AbstractStructuredTextReconcilingStrategy#getPartitionTypes() |
| 139 | */ |
| 140 | public String[] getPartitionTypes() { |
| 141 | List partitionTypes = new ArrayList(); |
pavery | 55ae447 | 2005-02-16 23:00:16 +0000 | [diff] [blame] | 142 | Iterator keys = fVidToVStepMap.keySet().iterator(); |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 143 | String key = null; |
| 144 | while (keys.hasNext()) { |
| 145 | key = (String) keys.next(); |
pavery | 55ae447 | 2005-02-16 23:00:16 +0000 | [diff] [blame] | 146 | StructuredReconcileStep step = (StructuredReconcileStep) fVidToVStepMap.get(key); |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 147 | partitionTypes.addAll(Arrays.asList(step.getPartitionTypes())); |
| 148 | } |
| 149 | return (String[]) partitionTypes.toArray(new String[partitionTypes.size()]); |
| 150 | } |
pavery | dfec417 | 2005-11-09 21:42:30 +0000 | [diff] [blame] | 151 | /** |
| 152 | * @param tr Partition of the region to reconcile. |
| 153 | * @param dr Dirty region representation of the typed region |
| 154 | */ |
pavery | 46c93e7 | 2005-02-08 16:07:33 +0000 | [diff] [blame] | 155 | public void reconcile(ITypedRegion tr, DirtyRegion dr) { |
pavery | baf7e52 | 2005-07-07 20:44:14 +0000 | [diff] [blame] | 156 | |
| 157 | if(isCanceled()) |
| 158 | return; |
| 159 | |
pavery | dfec417 | 2005-11-09 21:42:30 +0000 | [diff] [blame] | 160 | IDocument doc = getDocument(); |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 161 | // for external files, this can be null |
pavery | dfec417 | 2005-11-09 21:42:30 +0000 | [diff] [blame] | 162 | if (doc == null) |
| 163 | return; |
| 164 | |
| 165 | String partitionType = tr.getType(); |
| 166 | if (canValidatePartition(partitionType)) { |
| 167 | ValidatorMetaData vmd = null; |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 168 | |
pavery | dfec417 | 2005-11-09 21:42:30 +0000 | [diff] [blame] | 169 | // IReconcileResult[] |
| 170 | ArrayList annotationsToAdd = new ArrayList(); |
| 171 | // loop all of the relevant validator meta data |
| 172 | // to find new annotations |
| 173 | for (int i = 0; i < fMetaData.size() && !isCanceled(); i++) { |
| 174 | vmd = (ValidatorMetaData) fMetaData.get(i); |
| 175 | if (vmd.canHandleParitionType(getContentTypeIds(), partitionType)) { |
| 176 | // get step for partition type |
| 177 | Object o = fVidToVStepMap.get(vmd.getValidatorId()); |
| 178 | ReconcileStepForValidator validatorStep = null; |
| 179 | if (o != null) { |
| 180 | validatorStep = (ReconcileStepForValidator) o; |
| 181 | } else { |
| 182 | // if doesn't exist, create one |
| 183 | IValidator validator = vmd.createValidator(); |
| 184 | validatorStep = new ReconcileStepForValidator(validator, vmd.getValidatorScope()); |
| 185 | validatorStep.setInputModel(new DocumentAdapter(doc)); |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 186 | |
pavery | dfec417 | 2005-11-09 21:42:30 +0000 | [diff] [blame] | 187 | fVidToVStepMap.put(vmd.getValidatorId(), validatorStep); |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 188 | } |
pavery | dfec417 | 2005-11-09 21:42:30 +0000 | [diff] [blame] | 189 | annotationsToAdd.addAll(Arrays.asList(validatorStep.reconcile(dr, dr))); |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 190 | } |
| 191 | } |
pavery | dfec417 | 2005-11-09 21:42:30 +0000 | [diff] [blame] | 192 | |
| 193 | TemporaryAnnotation[] annotationsToRemove= getAnnotationsToRemove(dr); |
| 194 | if (annotationsToRemove.length + annotationsToAdd.size() > 0) |
| 195 | smartProcess(annotationsToRemove, (IReconcileResult[]) annotationsToAdd.toArray(new IReconcileResult[annotationsToAdd.size()])); |
| 196 | |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
pavery | 55ae447 | 2005-02-16 23:00:16 +0000 | [diff] [blame] | 200 | /** |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 201 | * @see org.eclipse.wst.sse.ui.internal.reconcile.AbstractStructuredTextReconcilingStrategy#setDocument(org.eclipse.jface.text.IDocument) |
| 202 | */ |
| 203 | public void setDocument(IDocument document) { |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 204 | |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 205 | super.setDocument(document); |
pavery | dc8b306 | 2005-10-24 20:43:42 +0000 | [diff] [blame] | 206 | |
pavery | 55ae447 | 2005-02-16 23:00:16 +0000 | [diff] [blame] | 207 | // validator steps are in "fVIdToVStepMap" (as opposed to fFirstStep > |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 208 | // next step etc...) |
pavery | 55ae447 | 2005-02-16 23:00:16 +0000 | [diff] [blame] | 209 | Iterator it = fVidToVStepMap.values().iterator(); |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 210 | IReconcileStep step = null; |
| 211 | while (it.hasNext()) { |
| 212 | step = (IReconcileStep) it.next(); |
| 213 | step.setInputModel(new DocumentAdapter(document)); |
| 214 | } |
| 215 | } |
pavery | baf7e52 | 2005-07-07 20:44:14 +0000 | [diff] [blame] | 216 | |
pavery | b0763c0 | 2005-10-17 15:55:28 +0000 | [diff] [blame] | 217 | public void release() { |
| 218 | super.release(); |
| 219 | Iterator it = fVidToVStepMap.values().iterator(); |
| 220 | IReconcileStep step = null; |
| 221 | while (it.hasNext()) { |
| 222 | step = (IReconcileStep) it.next(); |
| 223 | if(step instanceof IReleasable) |
| 224 | ((IReleasable)step).release(); |
| 225 | } |
| 226 | } |
pavery | 1f1588d | 2006-02-15 20:44:54 +0000 | [diff] [blame^] | 227 | /** |
| 228 | * |
| 229 | * @param partitionType |
| 230 | * @return true if all validators associated with this |
| 231 | * parition type are total scope, otherwise return false |
| 232 | */ |
| 233 | public boolean allTotalScope(String partitionType) { |
| 234 | Iterator vmds = fMetaData.iterator(); |
| 235 | while(vmds.hasNext()) { |
| 236 | ValidatorMetaData vmd = (ValidatorMetaData)vmds.next(); |
| 237 | if(vmd.canHandleParitionType(getContentTypeIds(), partitionType)) { |
| 238 | if(vmd.getValidatorScope() == ReconcileAnnotationKey.PARTIAL) |
| 239 | return false; |
| 240 | } |
| 241 | } |
| 242 | return true; |
| 243 | } |
pavery | b0763c0 | 2005-10-17 15:55:28 +0000 | [diff] [blame] | 244 | |
pavery | baf7e52 | 2005-07-07 20:44:14 +0000 | [diff] [blame] | 245 | public boolean isTotalScope() { |
| 246 | return true; |
| 247 | } |
david_williams | cfdb2cd | 2004-11-11 08:37:49 +0000 | [diff] [blame] | 248 | } |