Search in sources :

Example 11 with IJSONPair

use of org.eclipse.wst.json.core.document.IJSONPair in project webtools.sourceediting by eclipse.

the class Validator method validateEnum.

private void validateEnum(IJSONNode node, JsonObject schema, JSONValidationInfo valinfo) {
    JsonValue value = schema.get(IJSONSchemaNode.ENUM);
    if (value instanceof JsonArray) {
        JsonArray array = value.asArray();
        Iterator<JsonValue> iter = array.iterator();
        Set<String> values = new HashSet<String>();
        while (iter.hasNext()) {
            String v = iter.next().toString();
            values.add(JSONUtil.removeQuote(v));
        }
        if (node instanceof IJSONPair) {
            IJSONPair pair = (IJSONPair) node;
            String v = JSONUtil.getString(pair.getValue());
            if (!values.contains(v)) {
                int offset = node.getStartOffset();
                int line = node.getModel().getStructuredDocument().getLineOfOffset(offset);
                valinfo.addMessage("Value is not an accepted value. Valid values " + values + "'", line, 0, offset == 0 ? 1 : offset);
            }
        }
    }
}
Also used : JsonArray(org.eclipse.json.provisonnal.com.eclipsesource.json.JsonArray) IJSONPair(org.eclipse.wst.json.core.document.IJSONPair) JsonValue(org.eclipse.json.provisonnal.com.eclipsesource.json.JsonValue) HashSet(java.util.HashSet)

Example 12 with IJSONPair

use of org.eclipse.wst.json.core.document.IJSONPair in project webtools.sourceediting by eclipse.

the class Validator method validateType.

private void validateType(IJSONNode node, Member member, JSONValidationInfo valinfo) {
    if (IJSONSchemaNode.TYPE.equals(member.getName())) {
        Set<String> types = new HashSet<String>();
        if (member.getValue().isString()) {
            types.add(member.getValue().asString());
        } else if (member.getValue().isArray()) {
            JsonArray array = (JsonArray) member.getValue();
            for (JsonValue item : array) {
                types.add(item.asString());
            }
        }
        boolean valid = false;
        for (String type : types) {
            if (node.getNodeType() == IJSONNode.OBJECT_NODE && JSONSchemaType.Object.getName().equals(type)) {
                valid = true;
                break;
            }
            if (node.getNodeType() == IJSONNode.PAIR_NODE) {
                IJSONValue value = ((IJSONPair) node).getValue();
                if (value == null && JSONSchemaType.Null.getName().equals(type)) {
                    valid = true;
                    break;
                }
                if (value == null) {
                    valid = false;
                    break;
                }
                if (value.getNodeType() == IJSONNode.OBJECT_NODE && JSONSchemaType.Object.getName().equals(type)) {
                    valid = true;
                    break;
                }
                if (value.getNodeType() == IJSONNode.VALUE_STRING_NODE && JSONSchemaType.String.getName().equals(type)) {
                    valid = true;
                    break;
                }
                if (value.getNodeType() == IJSONNode.ARRAY_NODE && JSONSchemaType.Array.getName().equals(type)) {
                    valid = true;
                    break;
                }
                if (value.getNodeType() == IJSONNode.VALUE_BOOLEAN_NODE && JSONSchemaType.Boolean.getName().equals(type)) {
                    valid = true;
                    break;
                }
                if (value.getNodeType() == IJSONNode.VALUE_NULL_NODE && JSONSchemaType.Null.getName().equals(type)) {
                    valid = true;
                    break;
                }
                if (value.getNodeType() == IJSONNode.VALUE_NUMBER_NODE && JSONSchemaType.Number.getName().equals(type)) {
                    valid = true;
                    break;
                }
                if (value.getNodeType() == IJSONNode.VALUE_NUMBER_NODE && JSONSchemaType.Integer.getName().equals(type)) {
                    valid = true;
                    break;
                }
            }
        }
        if (!valid) {
            int offset = node.getStartOffset();
            int line = node.getModel().getStructuredDocument().getLineOfOffset(offset);
            StringBuffer buffer = new StringBuffer();
            Iterator<String> iter = types.iterator();
            buffer.append(OPEN_BRACKET);
            while (iter.hasNext()) {
                buffer.append(iter.next());
                if (iter.hasNext()) {
                    buffer.append(COMMA);
                }
            }
            buffer.append(CLOSE_BRACKET);
            valinfo.addMessage("Incorrect type. Expected " + buffer.toString(), line, 0, offset == 0 ? 1 : offset);
        }
    }
}
Also used : JsonArray(org.eclipse.json.provisonnal.com.eclipsesource.json.JsonArray) IJSONPair(org.eclipse.wst.json.core.document.IJSONPair) JsonValue(org.eclipse.json.provisonnal.com.eclipsesource.json.JsonValue) IJSONValue(org.eclipse.wst.json.core.document.IJSONValue) HashSet(java.util.HashSet)

Example 13 with IJSONPair

use of org.eclipse.wst.json.core.document.IJSONPair 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 14 with IJSONPair

use of org.eclipse.wst.json.core.document.IJSONPair in project webtools.sourceediting by eclipse.

the class JFaceNodeAdapter method getStyledLabelText.

@Override
public StyledString getStyledLabelText(Object element) {
    StyledString styledString = new StyledString();
    if (element instanceof IJSONNode) {
        IJSONNode node = (IJSONNode) element;
        switch(node.getNodeType()) {
            case IJSONNode.PAIR_NODE:
                IJSONPair pair = ((IJSONPair) node);
                String name = pair.getName();
                if (name != null) {
                    styledString.append(name);
                    String value = pair.getSimpleValue();
                    if (value != null) {
                        styledString.append(" : ");
                        Styler styler = fAdapterFactory.getStyler(pair.getValueRegionType());
                        styledString.append(value, styler);
                    }
                }
                break;
            case IJSONNode.VALUE_BOOLEAN_NODE:
            case IJSONNode.VALUE_NULL_NODE:
            case IJSONNode.VALUE_NUMBER_NODE:
            case IJSONNode.VALUE_STRING_NODE:
                String value = ((IJSONValue) node).getSimpleValue();
                if (value != null) {
                    styledString.append(" : ");
                    Styler styler = fAdapterFactory.getStyler(((IJSONValue) node).getValueRegionType());
                    styledString.append(value, styler);
                }
                break;
        }
    }
    return styledString;
}
Also used : IJSONPair(org.eclipse.wst.json.core.document.IJSONPair) IJSONValue(org.eclipse.wst.json.core.document.IJSONValue) StyledString(org.eclipse.jface.viewers.StyledString) StyledString(org.eclipse.jface.viewers.StyledString) Styler(org.eclipse.jface.viewers.StyledString.Styler) IJSONNode(org.eclipse.wst.json.core.document.IJSONNode)

Example 15 with IJSONPair

use of org.eclipse.wst.json.core.document.IJSONPair in project webtools.sourceediting by eclipse.

the class AbstractJSONCompletionProposalComputer method computeObjectKeyProposals.

private ContentAssistRequest computeObjectKeyProposals(String matchString, ITextRegion completionRegion, IJSONNode nodeAtOffset, IJSONNode node, CompletionProposalInvocationContext context) {
    int documentPosition = context.getInvocationOffset();
    ContentAssistRequest contentAssistRequest = null;
    IStructuredDocumentRegion sdRegion = getStructuredDocumentRegion(documentPosition);
    int replaceLength = 0;
    int begin = documentPosition;
    if (completionRegion.getType() == JSONRegionContexts.JSON_OBJECT_KEY || completionRegion.getType() == JSONRegionContexts.JSON_UNKNOWN) {
        replaceLength = completionRegion.getTextLength();
        // value region not the entire container
        if (completionRegion instanceof ITextRegionContainer) {
            ITextRegion openRegion = ((ITextRegionContainer) completionRegion).getFirstRegion();
            ITextRegion closeRegion = ((ITextRegionContainer) completionRegion).getLastRegion();
            if (openRegion.getType() != closeRegion.getType()) {
                replaceLength = openRegion.getTextLength();
            }
        }
        begin = sdRegion.getStartOffset(completionRegion);
    }
    if (isPairValue(context, nodeAtOffset)) {
        IJSONPair pair = (IJSONPair) nodeAtOffset;
        IJSONValue value = pair.getValue();
        if (value != null) {
            try {
                begin = value.getStartOffset();
                String valueText = getNodeText(value);
                valueText = valueText.trim();
                replaceLength = valueText.length();
                if (valueText.startsWith(QUOTE)) {
                    begin = begin + 1;
                    replaceLength = replaceLength - 1;
                }
                if (valueText.endsWith(QUOTE)) {
                    replaceLength = replaceLength - 1;
                }
            } catch (BadLocationException e) {
            // ignore
            }
        }
    } else if (nodeAtOffset instanceof IJSONPair) {
        IJSONPair pair = (IJSONPair) nodeAtOffset;
        try {
            begin = pair.getStartOffset();
            String text = getNodeText(pair);
            text = text.trim();
            replaceLength = pair.getName().length();
            if (text.startsWith(QUOTE)) {
                begin = begin + 1;
            }
        } catch (BadLocationException e) {
        // ignore
        }
    }
    contentAssistRequest = new ContentAssistRequest(nodeAtOffset, node.getParentNode(), sdRegion, completionRegion, begin, replaceLength, matchString);
    addObjectKeyProposals(contentAssistRequest, context);
    return contentAssistRequest;
}
Also used : IStructuredDocumentRegion(org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion) IJSONPair(org.eclipse.wst.json.core.document.IJSONPair) ITextRegion(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion) ITextRegionContainer(org.eclipse.wst.sse.core.internal.provisional.text.ITextRegionContainer) IJSONValue(org.eclipse.wst.json.core.document.IJSONValue) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

IJSONPair (org.eclipse.wst.json.core.document.IJSONPair)16 IJSONValue (org.eclipse.wst.json.core.document.IJSONValue)10 IJSONNode (org.eclipse.wst.json.core.document.IJSONNode)9 HashSet (java.util.HashSet)4 IJSONObject (org.eclipse.wst.json.core.document.IJSONObject)4 JsonArray (org.eclipse.json.provisonnal.com.eclipsesource.json.JsonArray)3 JsonValue (org.eclipse.json.provisonnal.com.eclipsesource.json.JsonValue)3 IJSONArray (org.eclipse.wst.json.core.document.IJSONArray)3 IStructuredDocumentRegion (org.eclipse.wst.sse.core.internal.provisional.text.IStructuredDocumentRegion)3 JsonObject (org.eclipse.json.provisonnal.com.eclipsesource.json.JsonObject)2 IJSONSchemaProperty (org.eclipse.json.schema.IJSONSchemaProperty)2 IJSONDocument (org.eclipse.wst.json.core.document.IJSONDocument)2 ITextRegion (org.eclipse.wst.sse.core.internal.provisional.text.ITextRegion)2 IOException (java.io.IOException)1 MalformedURLException (java.net.MalformedURLException)1 URL (java.net.URL)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 DocumentRewriteSession (org.eclipse.jface.text.DocumentRewriteSession)1