Search in sources :

Example 1 with IJSONModel

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;
}
Also used : IJSONCleanupStrategy(org.eclipse.wst.json.core.cleanup.IJSONCleanupStrategy) IJSONModel(org.eclipse.wst.json.core.document.IJSONModel) IJSONDocument(org.eclipse.wst.json.core.document.IJSONDocument)

Example 2 with IJSONModel

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());
        }
    }
}
Also used : JSONFormatUtil(org.eclipse.wst.json.core.internal.format.JSONFormatUtil) IJSONModel(org.eclipse.wst.json.core.document.IJSONModel) IJSONDocument(org.eclipse.wst.json.core.document.IJSONDocument) IJSONSourceFormatter(org.eclipse.wst.json.core.internal.format.IJSONSourceFormatter) IndexedRegion(org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)

Example 3 with IJSONModel

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;
}
Also used : JSONValidationReport(org.eclipse.wst.json.core.internal.validation.JSONValidationReport) JSONValidationConfiguration(org.eclipse.wst.json.core.internal.validation.JSONValidationConfiguration) IEclipsePreferences(org.eclipse.core.runtime.preferences.IEclipsePreferences) IJSONModel(org.eclipse.wst.json.core.document.IJSONModel) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) IJSONSchemaDocument(org.eclipse.json.schema.IJSONSchemaDocument) IOException(java.io.IOException) JSONValidationInfo(org.eclipse.wst.json.core.internal.validation.JSONValidationInfo)

Example 4 with IJSONModel

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;
}
Also used : IModelManager(org.eclipse.wst.sse.core.internal.provisional.IModelManager) IJSONModel(org.eclipse.wst.json.core.document.IJSONModel) IStructuredModel(org.eclipse.wst.sse.core.internal.provisional.IStructuredModel) ValidationException(org.eclipse.wst.validation.internal.core.ValidationException) BadLocationException(org.eclipse.jface.text.BadLocationException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Example 5 with IJSONModel

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

IJSONModel (org.eclipse.wst.json.core.document.IJSONModel)10 IJSONDocument (org.eclipse.wst.json.core.document.IJSONDocument)4 IndexedRegion (org.eclipse.wst.sse.core.internal.provisional.IndexedRegion)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 ArrayList (java.util.ArrayList)2 List (java.util.List)2 IJSONSourceFormatter (org.eclipse.wst.json.core.internal.format.IJSONSourceFormatter)2 JSONFormatUtil (org.eclipse.wst.json.core.internal.format.JSONFormatUtil)2 IStructuredModel (org.eclipse.wst.sse.core.internal.provisional.IStructuredModel)2 IStructuredDocument (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocument)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 IOException (java.io.IOException)1 IFile (org.eclipse.core.resources.IFile)1 IEclipsePreferences (org.eclipse.core.runtime.preferences.IEclipsePreferences)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 DocumentRewriteSession (org.eclipse.jface.text.DocumentRewriteSession)1 DocumentRewriteSessionType (org.eclipse.jface.text.DocumentRewriteSessionType)1 IDocumentExtension4 (org.eclipse.jface.text.IDocumentExtension4)1 Region (org.eclipse.jface.text.Region)1 IJSONSchemaDocument (org.eclipse.json.schema.IJSONSchemaDocument)1