Search in sources :

Example 1 with DocumentAdapter

use of org.eclipse.wst.sse.ui.internal.reconcile.DocumentAdapter in project webtools.sourceediting by eclipse.

the class ValidatorStrategy method reconcile.

/**
 * @param tr
 *            Partition of the region to reconcile.
 * @param dr
 *            Dirty region representation of the typed region
 */
public void reconcile(ITypedRegion tr, DirtyRegion dr) {
    /*
		 * Abort if no workspace file is known (new validation framework does
		 * not support that scenario) or no validators have been specified
		 */
    if (isCanceled() || fMetaData.isEmpty() || fValidatorsSuspended)
        return;
    IDocument doc = getDocument();
    // for external files, this can be null
    if (doc == null)
        return;
    String partitionType = tr.getType();
    ValidatorMetaData vmd = null;
    List annotationsToAdd = new ArrayList();
    List stepsRanOnThisDirtyRegion = new ArrayList(1);
    /*
		 * Keep track of the disabled validators by source id for the V2
		 * validators.
		 */
    Set disabledValsBySourceId = new HashSet(20);
    /*
		 * Keep track of the disabled validators by class id for the v1
		 * validators.
		 */
    Set disabledValsByClass = new HashSet(20);
    IFile file = getFile();
    if (file != null) {
        if (!file.isAccessible())
            return;
        Collection disabledValidators = null;
        try {
            /*
				 * Take extra care when calling this external code, as it
				 * can indirectly cause bundles to start
				 */
            disabledValidators = ValidationFramework.getDefault().getDisabledValidatorsFor(file);
        } catch (Exception e) {
            Logger.logException(e);
        }
        if (disabledValidators != null) {
            for (Iterator it = disabledValidators.iterator(); it.hasNext(); ) {
                Validator v = (Validator) it.next();
                Validator.V1 v1 = null;
                try {
                    v1 = v.asV1Validator();
                } catch (Exception e) {
                    Logger.logException(e);
                }
                if (v1 != null)
                    disabledValsByClass.add(v1.getId());
                else // not a V1 validator
                if (v.getSourceId() != null) {
                    // could be more then one sourceid per batch validator
                    String[] sourceIDs = StringUtils.unpack(v.getSourceId());
                    disabledValsBySourceId.addAll(Arrays.asList(sourceIDs));
                }
            }
        }
    }
    /*
		 * Loop through all of the relevant validator meta data to find
		 * supporting validators for this partition type. Don't check
		 * this.canHandlePartition() before-hand since it just loops through
		 * and calls vmd.canHandlePartitionType()...which we're already doing
		 * here anyway to find the right vmd.
		 */
    for (int i = 0; i < fMetaData.size() && !isCanceled(); i++) {
        vmd = (ValidatorMetaData) fMetaData.get(i);
        if (vmd.canHandlePartitionType(getContentTypeIds(), partitionType)) {
            /*
				 * Check if validator is enabled according to validation
				 * preferences before attempting to create/use it
				 */
            if (!disabledValsBySourceId.contains(vmd.getValidatorId()) && !disabledValsByClass.contains(vmd.getValidatorClass())) {
                if (DEBUG_VALIDATION_UNSUPPORTED) {
                    Logger.log(Logger.INFO, "Source validator " + vmd.getValidatorId() + " handling (content types:[" + StringUtils.pack(getContentTypeIds()) + "] partition type:" + partitionType);
                }
                int validatorScope = vmd.getValidatorScope();
                ReconcileStepForValidator validatorStep = null;
                // get step for partition type
                Object o = fVidToVStepMap.get(vmd.getValidatorId());
                if (o != null) {
                    validatorStep = (ReconcileStepForValidator) o;
                } else {
                    // if doesn't exist, create one
                    IValidator validator = vmd.createValidator();
                    validatorStep = new ReconcileStepForValidator(validator, validatorScope);
                    validatorStep.setInputModel(new DocumentAdapter(doc));
                    fVidToVStepMap.put(vmd.getValidatorId(), validatorStep);
                }
                if (!fTotalScopeValidatorsAlreadyRun.contains(vmd) && !fIsCancelled) {
                    annotationsToAdd.addAll(Arrays.asList(validatorStep.reconcile(dr, dr)));
                    stepsRanOnThisDirtyRegion.add(validatorStep);
                    if (validatorScope == ReconcileAnnotationKey.TOTAL) {
                        // mark this validator as "run"
                        fTotalScopeValidatorsAlreadyRun.add(vmd);
                    }
                }
            } else if (DEBUG_VALIDATION_CAPABLE_BUT_DISABLED) {
                String message = "Source validator able (id:" + vmd.getValidatorId() + " class:" + vmd.getValidatorClass() + " but skipped because it was reported as disabled";
                Logger.log(Logger.INFO, message);
            }
        } else if (DEBUG_VALIDATION_UNSUPPORTED) {
            Logger.log(Logger.INFO, "Source validator " + vmd.getValidatorId() + " can not handle (content types:[" + StringUtils.pack(getContentTypeIds()) + "] partition type:" + partitionType);
        }
    }
    TemporaryAnnotation[] annotationsToRemove = getAnnotationsToRemove(dr, stepsRanOnThisDirtyRegion);
    if (annotationsToRemove.length + annotationsToAdd.size() > 0 && !fIsCancelled)
        smartProcess(annotationsToRemove, (IReconcileResult[]) annotationsToAdd.toArray(new IReconcileResult[annotationsToAdd.size()]));
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) IFile(org.eclipse.core.resources.IFile) ArrayList(java.util.ArrayList) DocumentAdapter(org.eclipse.wst.sse.ui.internal.reconcile.DocumentAdapter) IValidator(org.eclipse.wst.validation.internal.provisional.core.IValidator) IReconcileResult(org.eclipse.jface.text.reconciler.IReconcileResult) Iterator(java.util.Iterator) Collection(java.util.Collection) ArrayList(java.util.ArrayList) List(java.util.List) TemporaryAnnotation(org.eclipse.wst.sse.ui.internal.reconcile.TemporaryAnnotation) IDocument(org.eclipse.jface.text.IDocument) IValidator(org.eclipse.wst.validation.internal.provisional.core.IValidator) Validator(org.eclipse.wst.validation.Validator) HashSet(java.util.HashSet)

Example 2 with DocumentAdapter

use of org.eclipse.wst.sse.ui.internal.reconcile.DocumentAdapter in project webtools.sourceediting by eclipse.

the class ValidatorStrategy method setDocument.

/**
 * @see org.eclipse.wst.sse.ui.internal.reconcile.AbstractStructuredTextReconcilingStrategy#setDocument(org.eclipse.jface.text.IDocument)
 */
public void setDocument(IDocument document) {
    super.setDocument(document);
    fFile = null;
    try {
        fValidatorsSuspended = false;
        if (document != null) {
            IFile file = getFile();
            if (file != null) {
                // Validation is suspended for this resource, do nothing
                fValidatorsSuspended = !file.isAccessible() || ValidationFramework.getDefault().isSuspended(file.getProject()) || ValidationFramework.getDefault().getProjectSettings(file.getProject()).getSuspend();
            }
        }
    } catch (Exception e) {
        fValidatorsSuspended = true;
        Logger.logException(e);
    }
    // validator steps are in "fVIdToVStepMap" (as opposed to fFirstStep >
    // next step etc...)
    Iterator it = fVidToVStepMap.values().iterator();
    IReconcileStep step = null;
    while (it.hasNext()) {
        step = (IReconcileStep) it.next();
        step.setInputModel(new DocumentAdapter(document));
    }
}
Also used : IFile(org.eclipse.core.resources.IFile) IReconcileStep(org.eclipse.jface.text.reconciler.IReconcileStep) Iterator(java.util.Iterator) DocumentAdapter(org.eclipse.wst.sse.ui.internal.reconcile.DocumentAdapter)

Aggregations

Iterator (java.util.Iterator)2 IFile (org.eclipse.core.resources.IFile)2 DocumentAdapter (org.eclipse.wst.sse.ui.internal.reconcile.DocumentAdapter)2 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 List (java.util.List)1 Set (java.util.Set)1 IDocument (org.eclipse.jface.text.IDocument)1 IReconcileResult (org.eclipse.jface.text.reconciler.IReconcileResult)1 IReconcileStep (org.eclipse.jface.text.reconciler.IReconcileStep)1 TemporaryAnnotation (org.eclipse.wst.sse.ui.internal.reconcile.TemporaryAnnotation)1 Validator (org.eclipse.wst.validation.Validator)1 IValidator (org.eclipse.wst.validation.internal.provisional.core.IValidator)1