Search in sources :

Example 1 with IValidator

use of org.eclipse.wst.validation.internal.provisional.core.IValidator in project webtools.sourceediting by eclipse.

the class DelegatingSourceValidator method validate.

/**
 * Calls a delegate validator getting and updates it's list of
 * ValidationMessages with a good squiggle offset and length.
 *
 * @param helper
 *            loads an object.
 * @param reporter
 *            Is an instance of an IReporter interface, which is used for
 *            interaction with the user.
 */
public void validate(IValidationContext helper, IReporter reporter) throws ValidationException {
    String[] delta = helper.getURIs();
    if (delta.length > 0) {
        // get the file, model and document:
        IFile file = getFile(delta[0]);
        IDOMModel xmlModel = null;
        if (file != null)
            xmlModel = getModelForResource(file);
        // some problem occurred, abort
        if (xmlModel == null)
            return;
        try {
            IDOMDocument document = xmlModel.getDocument();
            // store the text in a byte array; make a full copy to ease
            // any threading problems
            byte[] byteArray;
            try {
                byteArray = xmlModel.getStructuredDocument().get().getBytes("UTF-8");
            } catch (UnsupportedEncodingException e) {
                // Not likely to happen
                byteArray = xmlModel.getStructuredDocument().get().getBytes();
            }
            if (isDelegateValidatorEnabled(file)) {
                IValidator validator = getDelegateValidator();
                if (validator != null) {
                    // Validate the file:
                    IValidationContext vHelper = new MyHelper(new ByteArrayInputStream(byteArray), file);
                    MyReporter vReporter = new MyReporter();
                    if (validator instanceof IValidatorJob) {
                        ((IValidatorJob) validator).validateInJob(vHelper, vReporter);
                    } else {
                        validator.validate(vHelper, vReporter);
                    }
                    List messages = vReporter.list;
                    // set the offset and length
                    updateValidationMessages(messages, document, reporter);
                }
            }
        } finally {
            if (xmlModel != null) {
                xmlModel.releaseFromRead();
            }
        }
    }
}
Also used : IValidatorJob(org.eclipse.wst.validation.internal.provisional.core.IValidatorJob) IFile(org.eclipse.core.resources.IFile) IDOMModel(org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IDOMDocument(org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument) IValidator(org.eclipse.wst.validation.internal.provisional.core.IValidator) ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayList(java.util.ArrayList) NodeList(org.w3c.dom.NodeList) List(java.util.List) IValidationContext(org.eclipse.wst.validation.internal.provisional.core.IValidationContext)

Example 2 with IValidator

use of org.eclipse.wst.validation.internal.provisional.core.IValidator 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 3 with IValidator

use of org.eclipse.wst.validation.internal.provisional.core.IValidator in project webtools.sourceediting by eclipse.

the class DelegatingSourceValidator method validate.

/**
 * Calls a delegate validator getting and updates it's list of
 * ValidationMessages with a good squiggle offset and length.
 *
 * @param helper
 *            loads an object.
 * @param reporter
 *            Is an instance of an IReporter interface, which is used for
 *            interaction with the user.
 */
public void validate(IValidationContext helper, IReporter reporter) throws ValidationException {
    String[] delta = helper.getURIs();
    if (delta.length > 0) {
        // get the file, model and document:
        IFile file = getFile(delta[0]);
        IJSONModel jsonModel = null;
        if (file != null)
            jsonModel = getModelForResource(file);
        // some problem occurred, abort
        if (jsonModel == null)
            return;
        try {
            IJSONDocument document = jsonModel.getDocument();
            // store the text in a byte array; make a full copy to ease
            // any threading problems
            byte[] byteArray;
            try {
                byteArray = jsonModel.getStructuredDocument().get().getBytes("UTF-8");
            } catch (UnsupportedEncodingException e) {
                // Not likely to happen
                byteArray = jsonModel.getStructuredDocument().get().getBytes();
            }
            if (isDelegateValidatorEnabled(file)) {
                IValidator validator = getDelegateValidator();
                if (validator != null) {
                    // Validate the file:
                    IValidationContext vHelper = new MyHelper(new ByteArrayInputStream(byteArray), file);
                    MyReporter vReporter = new MyReporter();
                    if (validator instanceof IValidatorJob) {
                        ((IValidatorJob) validator).validateInJob(vHelper, vReporter);
                    } else {
                        validator.validate(vHelper, vReporter);
                    }
                    List messages = vReporter.list;
                    // set the offset and length
                    updateValidationMessages(messages, document, reporter);
                }
            }
        } finally {
            if (jsonModel != null) {
                jsonModel.releaseFromRead();
            }
        }
    }
}
Also used : IValidatorJob(org.eclipse.wst.validation.internal.provisional.core.IValidatorJob) IFile(org.eclipse.core.resources.IFile) UnsupportedEncodingException(java.io.UnsupportedEncodingException) IJSONModel(org.eclipse.wst.json.core.document.IJSONModel) IValidator(org.eclipse.wst.validation.internal.provisional.core.IValidator) ByteArrayInputStream(java.io.ByteArrayInputStream) ArrayList(java.util.ArrayList) List(java.util.List) IJSONDocument(org.eclipse.wst.json.core.document.IJSONDocument) IValidationContext(org.eclipse.wst.validation.internal.provisional.core.IValidationContext)

Aggregations

ArrayList (java.util.ArrayList)3 List (java.util.List)3 IFile (org.eclipse.core.resources.IFile)3 IValidator (org.eclipse.wst.validation.internal.provisional.core.IValidator)3 ByteArrayInputStream (java.io.ByteArrayInputStream)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 IValidationContext (org.eclipse.wst.validation.internal.provisional.core.IValidationContext)2 IValidatorJob (org.eclipse.wst.validation.internal.provisional.core.IValidatorJob)2 Collection (java.util.Collection)1 HashSet (java.util.HashSet)1 Iterator (java.util.Iterator)1 Set (java.util.Set)1 IDocument (org.eclipse.jface.text.IDocument)1 IReconcileResult (org.eclipse.jface.text.reconciler.IReconcileResult)1 IJSONDocument (org.eclipse.wst.json.core.document.IJSONDocument)1 IJSONModel (org.eclipse.wst.json.core.document.IJSONModel)1 DocumentAdapter (org.eclipse.wst.sse.ui.internal.reconcile.DocumentAdapter)1 TemporaryAnnotation (org.eclipse.wst.sse.ui.internal.reconcile.TemporaryAnnotation)1 Validator (org.eclipse.wst.validation.Validator)1 IDOMDocument (org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)1