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();
}
}
}
}
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()]));
}
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();
}
}
}
}
Aggregations