Search in sources :

Example 1 with IValidatorJob

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

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

ByteArrayInputStream (java.io.ByteArrayInputStream)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 IFile (org.eclipse.core.resources.IFile)2 IValidationContext (org.eclipse.wst.validation.internal.provisional.core.IValidationContext)2 IValidator (org.eclipse.wst.validation.internal.provisional.core.IValidator)2 IValidatorJob (org.eclipse.wst.validation.internal.provisional.core.IValidatorJob)2 IJSONDocument (org.eclipse.wst.json.core.document.IJSONDocument)1 IJSONModel (org.eclipse.wst.json.core.document.IJSONModel)1 IDOMDocument (org.eclipse.wst.xml.core.internal.provisional.document.IDOMDocument)1 IDOMModel (org.eclipse.wst.xml.core.internal.provisional.document.IDOMModel)1 NodeList (org.w3c.dom.NodeList)1