use of org.eclipse.wst.json.core.document.IJSONModel in project webtools.sourceediting by eclipse.
the class AbstractJSONSourceFormatter method getCleanupStrategy.
/**
* Insert the method's description here.
*
* @return org.eclipse.wst.css.core.internal.cleanup.JSONCleanupStrategy
* @param node
* org.eclipse.wst.css.core.model.interfaces.IJSONNode
*/
protected IJSONCleanupStrategy getCleanupStrategy(IJSONNode node) {
IJSONCleanupStrategy currentStrategy = JSONCleanupStrategyImpl.getInstance();
IJSONDocument doc = node.getOwnerDocument();
if (doc == null)
return currentStrategy;
IJSONModel model = doc.getModel();
if (model == null)
return currentStrategy;
return currentStrategy;
}
use of org.eclipse.wst.json.core.document.IJSONModel in project webtools.sourceediting by eclipse.
the class CleanupProcessorJSON method cleanupModel.
public void cleanupModel(IStructuredModel structuredModel, int start, int length) {
JSONFormatUtil formatUtil = JSONFormatUtil.getInstance();
if (structuredModel instanceof IJSONModel) {
IJSONDocument doc = ((IJSONModel) structuredModel).getDocument();
IJSONSourceFormatter formatter = JSONSourceFormatterFactory.getInstance().getSourceFormatter((INodeNotifier) doc);
StringBuilder buf = formatter.cleanup(doc);
if (buf != null) {
int startOffset = ((IndexedRegion) doc).getStartOffset();
int endOffset = ((IndexedRegion) doc).getEndOffset();
formatUtil.replaceSource(doc.getModel(), startOffset, endOffset - startOffset, buf.toString());
}
}
}
use of org.eclipse.wst.json.core.document.IJSONModel in project webtools.sourceediting by eclipse.
the class Validator method validate.
@Override
public ValidationReport validate(String uri, InputStream inputstream, NestedValidatorContext context, ValidationResult result) {
JSONValidator validator = JSONValidator.getInstance();
JSONValidationConfiguration configuration = new JSONValidationConfiguration();
JSONValidationReport valreport = validator.validate(uri, inputstream, configuration, result, context);
String prefs = JSONCorePlugin.getDefault().getBundle().getSymbolicName();
IEclipsePreferences modelPreferences = InstanceScope.INSTANCE.getNode(prefs);
boolean validateSchema = modelPreferences.getBoolean(JSONCorePreferenceNames.SCHEMA_VALIDATION, false);
if (validateSchema) {
IJSONModel model = null;
try {
IStructuredModel temp = getModel(uri);
if (!(temp instanceof IJSONModel)) {
return valreport;
}
model = (IJSONModel) temp;
IJSONSchemaDocument schemaDocument = SchemaProcessorRegistryReader.getInstance().getSchemaDocument(model);
if (schemaDocument != null) {
JSONValidationInfo valinfo = null;
if (valreport instanceof JSONValidationInfo) {
valinfo = (JSONValidationInfo) valreport;
} else {
valinfo = new JSONValidationInfo(uri);
}
validate(model, schemaDocument, valinfo);
// valreport.getValidationMessages();
return valreport;
}
} catch (IOException e) {
logWarning(e);
return valreport;
} finally {
if (model != null) {
model.releaseFromRead();
}
}
}
return valreport;
}
use of org.eclipse.wst.json.core.document.IJSONModel in project webtools.sourceediting by eclipse.
the class DelegatingSourceValidator method getModelForResource.
/**
* @param file
* the file to get the model for
* @return the file's JSONModel or null
*/
protected IJSONModel getModelForResource(IFile file) {
IStructuredModel model = null;
IModelManager manager = StructuredModelManager.getModelManager();
try {
model = manager.getModelForRead(file);
// TODO.. HTML validator tries again to get a model a 2nd way
} catch (Exception e) {
Logger.log(Logger.ERROR_DEBUG, file.getFullPath().toString(), e);
}
if (model instanceof IJSONModel)
return (IJSONModel) model;
if (model != null)
model.releaseFromRead();
return null;
}
use of org.eclipse.wst.json.core.document.IJSONModel 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