Search in sources :

Example 1 with IJSONSchemaDocument

use of org.eclipse.json.schema.IJSONSchemaDocument in project webtools.sourceediting by eclipse.

the class HTMLJSONPrinter method getAdditionalProposalInfo.

public static String getAdditionalProposalInfo(IJSONPair pair) {
    StringBuilder builder = new StringBuilder();
    ImageDescriptor descriptor = null;
    IJSONValue value = pair.getValue();
    if (value != null) {
        descriptor = JSONEditorPluginImageHelper.getInstance().getImageDescriptor(value.getNodeType());
    }
    startPage(builder, getTitleKey(value), descriptor);
    startDefinitionList(builder);
    StringBuilder build = new StringBuilder();
    build.append(pair.getName());
    try {
        IJSONSchemaDocument schemaDocument = JSONCorePlugin.getDefault().getSchemaDocument(pair);
        if (schemaDocument != null) {
            IJSONPath path = pair.getPath();
            IJSONSchemaProperty property = schemaDocument.getProperty(path);
            if (property != null) {
                String description = property.getDescription();
                if (description != null) {
                    build.append(" - ");
                    build.append(description);
                }
            }
        }
    } catch (IOException e) {
        Logger.logException(e);
    }
    addDefinitionListItem(builder, "Key", build.toString());
    addDefinitionListItem(builder, "Type", getValueType(pair.getValue()));
    endDefinitionList(builder);
    endPage(builder);
    return builder.toString();
}
Also used : IJSONSchemaProperty(org.eclipse.json.schema.IJSONSchemaProperty) IJSONPath(org.eclipse.json.jsonpath.IJSONPath) ImageDescriptor(org.eclipse.jface.resource.ImageDescriptor) IJSONValue(org.eclipse.wst.json.core.document.IJSONValue) IJSONSchemaDocument(org.eclipse.json.schema.IJSONSchemaDocument) IOException(java.io.IOException)

Example 2 with IJSONSchemaDocument

use of org.eclipse.json.schema.IJSONSchemaDocument 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 3 with IJSONSchemaDocument

use of org.eclipse.json.schema.IJSONSchemaDocument in project webtools.sourceediting by eclipse.

the class JSONCompletionProposalComputer method collectProposalsFromSchema.

/**
 * Collect completion proposals from JSON Schema.
 *
 * @param contentAssistRequest
 * @param context
 */
private void collectProposalsFromSchema(ContentAssistRequest contentAssistRequest, CompletionProposalInvocationContext context) {
    try {
        IJSONNode node = contentAssistRequest.getNode();
        IJSONSchemaDocument schemaDocument = JSONCorePlugin.getDefault().getSchemaDocument(node);
        if (schemaDocument != null) {
            String matchString = contentAssistRequest.getMatchString();
            if (matchString == null) {
                // $NON-NLS-1$
                matchString = "";
            }
            if ((matchString.length() > 0) && (matchString.startsWith(QUOTE))) {
                matchString = matchString.substring(1);
            }
            // Loop for each properties of the JSON Schema.
            IJSONPath path = node.getPath();
            if (node instanceof IJSONPair) {
                IJSONSchemaProperty thisProperty = schemaDocument.getProperty(path);
                ITextRegion region = contentAssistRequest.getRegion();
                boolean isValue = isPairValue(context, node);
                if (thisProperty != null && isValue) {
                    if (thisProperty.getFirstType() == JSONSchemaType.Boolean) {
                        if (beginsWith(FALSE, matchString.trim())) {
                            addStringProposal(contentAssistRequest, FALSE, false);
                        }
                        if (beginsWith(TRUE, matchString.trim())) {
                            addStringProposal(contentAssistRequest, TRUE, false);
                        }
                        return;
                    }
                    if (thisProperty.getFirstType() == JSONSchemaType.String) {
                        if (thisProperty.getEnumList() != null && thisProperty.getEnumList().size() > 0) {
                            for (String prop : thisProperty.getEnumList()) {
                                boolean showProperty = beginsWith(prop, matchString.trim());
                                if (showProperty) {
                                    addStringProposal(contentAssistRequest, prop, !(region.getType() == JSONRegionContexts.JSON_VALUE_STRING));
                                }
                            }
                        } else {
                            if (thisProperty.getDefaultValue() != null) {
                                boolean showProperty = beginsWith(thisProperty.getDefaultValue(), matchString.trim());
                                if (showProperty) {
                                    addStringProposal(contentAssistRequest, thisProperty.getDefaultValue(), !(region.getType() == JSONRegionContexts.JSON_VALUE_STRING));
                                }
                            }
                        }
                        return;
                    }
                }
            }
            if (!(node instanceof IJSONObject && node.getOwnerPairNode() != null)) {
                if (path.getSegments().length > 0) {
                    String[] segments = new String[path.getSegments().length - 1];
                    System.arraycopy(path.getSegments(), 0, segments, 0, path.getSegments().length - 1);
                    path = new JSONPath(segments);
                }
            }
            IJSONSchemaProperty parentProperty = schemaDocument.getProperty(path);
            Set<String> existing = new HashSet<String>();
            boolean addComma = false;
            if (node instanceof IJSONObject) {
                addExisting(existing, node);
                addComma = addComma(context, node);
            } else if (node instanceof IJSONPair && node.getParentNode() instanceof IJSONObject) {
                addExisting(existing, node.getParentNode());
            }
            if (parentProperty != null) {
                for (IJSONSchemaProperty property : parentProperty.getPropertyValues()) {
                    boolean showProperty = !existing.contains(property.getName()) && beginsWith(property.getName(), matchString.trim());
                    if (showProperty) {
                        String replacementString;
                        if (node instanceof IJSONPair) {
                            replacementString = property.getName();
                        } else {
                            replacementString = ContentAssistHelper.getRequiredName(node, property);
                            if (addComma) {
                                replacementString = replacementString + ",";
                            }
                        }
                        String additionalProposalInfo = property.getDescription();
                        Image icon = JSONEditorPluginImageHelper.getInstance().getImage(property.getFirstType());
                        String displayString = property.getName();
                        JSONKeyCompletionProposal proposal = new JSONKeyCompletionProposal(replacementString, contentAssistRequest.getReplacementBeginPosition(), contentAssistRequest.getReplacementLength(), replacementString.length() - 2, icon, displayString, null, additionalProposalInfo, JSONRelevanceConstants.R_OBJECT_KEY);
                        contentAssistRequest.addProposal(proposal);
                    }
                }
            }
        }
    } catch (IOException e) {
        Logger.logException(e);
    }
}
Also used : IJSONSchemaProperty(org.eclipse.json.schema.IJSONSchemaProperty) JSONKeyCompletionProposal(org.eclipse.wst.json.ui.contentassist.JSONKeyCompletionProposal) IJSONPath(org.eclipse.json.jsonpath.IJSONPath) IOException(java.io.IOException) Image(org.eclipse.swt.graphics.Image) IJSONNode(org.eclipse.wst.json.core.document.IJSONNode) IJSONPath(org.eclipse.json.jsonpath.IJSONPath) JSONPath(org.eclipse.json.jsonpath.JSONPath) IJSONPair(org.eclipse.wst.json.core.document.IJSONPair) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) IJSONObject(org.eclipse.wst.json.core.document.IJSONObject) IJSONSchemaDocument(org.eclipse.json.schema.IJSONSchemaDocument) HashSet(java.util.HashSet)

Example 4 with IJSONSchemaDocument

use of org.eclipse.json.schema.IJSONSchemaDocument in project webtools.sourceediting by eclipse.

the class JSONSchemaProcessor method getSchema.

@Override
public IJSONSchemaDocument getSchema(String uriString) throws IOException {
    IJSONSchemaDocument schemaDocument = schemaDocuments.get(uriString);
    if (schemaDocument != null) {
        return schemaDocument;
    }
    int size = schemaDocuments.size();
    if (size > MAP_SIZE) {
        String key = schemaDocuments.keySet().iterator().next();
        schemaDocuments.remove(key);
    }
    URL url = new URL(uriString);
    InputStream is = null;
    try {
        if ("jar".equals(url.getProtocol())) {
            is = url.openStream();
        } else {
            File f = HttpClientProvider.getFile(url);
            if (f != null) {
                is = new FileInputStream(f);
            }
        }
        if (is != null) {
            schemaDocument = new JSONSchemaDocument(new InputStreamReader(is));
        }
    } finally {
        if (is != null) {
            is.close();
        }
    }
    if (schemaDocument != null) {
        schemaDocuments.put(uriString, schemaDocument);
    }
    return schemaDocument;
}
Also used : IJSONSchemaDocument(org.eclipse.json.schema.IJSONSchemaDocument) JSONSchemaDocument(org.eclipse.json.impl.schema.JSONSchemaDocument) InputStreamReader(java.io.InputStreamReader) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) IJSONSchemaDocument(org.eclipse.json.schema.IJSONSchemaDocument) File(java.io.File) URL(java.net.URL) FileInputStream(java.io.FileInputStream)

Aggregations

IJSONSchemaDocument (org.eclipse.json.schema.IJSONSchemaDocument)4 IOException (java.io.IOException)3 IJSONPath (org.eclipse.json.jsonpath.IJSONPath)2 IJSONSchemaProperty (org.eclipse.json.schema.IJSONSchemaProperty)2 File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 URL (java.net.URL)1 HashSet (java.util.HashSet)1 IEclipsePreferences (org.eclipse.core.runtime.preferences.IEclipsePreferences)1 ImageDescriptor (org.eclipse.jface.resource.ImageDescriptor)1 JSONSchemaDocument (org.eclipse.json.impl.schema.JSONSchemaDocument)1 JSONPath (org.eclipse.json.jsonpath.JSONPath)1 Image (org.eclipse.swt.graphics.Image)1 IJSONModel (org.eclipse.wst.json.core.document.IJSONModel)1 IJSONNode (org.eclipse.wst.json.core.document.IJSONNode)1 IJSONObject (org.eclipse.wst.json.core.document.IJSONObject)1 IJSONPair (org.eclipse.wst.json.core.document.IJSONPair)1 IJSONValue (org.eclipse.wst.json.core.document.IJSONValue)1