Search in sources :

Example 6 with Model

use of com.reprezen.swagedit.core.model.Model in project KaiZen-OpenAPI-Editor by RepreZen.

the class JsonContentOutlinePage method update.

protected void update() {
    final IDocument document = documentProvider.getDocument(currentInput);
    if (document instanceof JsonDocument) {
        final Model model = ((JsonDocument) document).getModel();
        if (model == null) {
            return;
        }
        final TreeViewer viewer = getTreeViewer();
        if (viewer != null && viewer.getControl() != null && !viewer.getControl().isDisposed()) {
            // we keep all elements that have been previously expanded
            // so the tree stay in the same state between updates.
            final Object[] expandedElements = viewer.getExpandedElements();
            final List<Object> elements = expandedElements != null ? Arrays.asList(expandedElements) : null;
            viewer.setInput(model);
            if (elements != null && !elements.isEmpty()) {
                List<AbstractNode> newElements = new ArrayList<>();
                model.allNodes().forEach(node -> {
                    if (elements.contains(node))
                        newElements.add(node);
                });
                viewer.setExpandedElements(newElements.toArray());
            }
        }
    }
}
Also used : AbstractNode(com.reprezen.swagedit.core.model.AbstractNode) TreeViewer(org.eclipse.jface.viewers.TreeViewer) Model(com.reprezen.swagedit.core.model.Model) ArrayList(java.util.ArrayList) IDocument(org.eclipse.jface.text.IDocument) JsonDocument(com.reprezen.swagedit.core.editor.JsonDocument)

Example 7 with Model

use of com.reprezen.swagedit.core.model.Model in project KaiZen-OpenAPI-Editor by RepreZen.

the class SecuritySchemeHyperlinkDetector method doDetect.

@Override
protected IHyperlink[] doDetect(JsonDocument doc, ITextViewer viewer, HyperlinkInfo info, JsonPointer pointer) {
    Matcher matcher = PATTERN.matcher(pointer.toString());
    String link = matcher.find() ? matcher.group(1) : null;
    if (link != null) {
        Model model = doc.getModel();
        AbstractNode securityScheme = model.find("/components/securitySchemes/" + link);
        if (securityScheme != null) {
            IRegion target = doc.getRegion(securityScheme.getPointer());
            if (target != null) {
                return new IHyperlink[] { new SwaggerHyperlink(info.text, viewer, info.region, target) };
            }
        }
    }
    return null;
}
Also used : SwaggerHyperlink(com.reprezen.swagedit.core.hyperlinks.SwaggerHyperlink) Matcher(java.util.regex.Matcher) AbstractNode(com.reprezen.swagedit.core.model.AbstractNode) IHyperlink(org.eclipse.jface.text.hyperlink.IHyperlink) Model(com.reprezen.swagedit.core.model.Model) IRegion(org.eclipse.jface.text.IRegion)

Example 8 with Model

use of com.reprezen.swagedit.core.model.Model in project KaiZen-OpenAPI-Editor by RepreZen.

the class Validator method validate.

public Set<SwaggerError> validate(JsonDocument document, URI baseURI) {
    Set<SwaggerError> errors = new HashSet<>();
    JsonNode jsonContent = null;
    try {
        jsonContent = document.asJson();
    } catch (Exception e) {
        jsonContent = null;
    }
    if (jsonContent != null) {
        Node yaml = document.getYaml();
        Model model = document.getModel();
        if (yaml != null && model != null) {
            errors.addAll(getSchemaValidator().validate(document));
            errors.addAll(validateDocument(baseURI, document));
            errors.addAll(checkDuplicateKeys(yaml));
            errors.addAll(getReferenceValidator().validate(baseURI, document, model));
        }
    }
    return errors;
}
Also used : SequenceNode(org.yaml.snakeyaml.nodes.SequenceNode) ObjectNode(com.reprezen.swagedit.core.model.ObjectNode) ValueNode(com.reprezen.swagedit.core.model.ValueNode) MappingNode(org.yaml.snakeyaml.nodes.MappingNode) ArrayNode(com.reprezen.swagedit.core.model.ArrayNode) Node(org.yaml.snakeyaml.nodes.Node) JsonNode(com.fasterxml.jackson.databind.JsonNode) AbstractNode(com.reprezen.swagedit.core.model.AbstractNode) ScalarNode(org.yaml.snakeyaml.nodes.ScalarNode) Model(com.reprezen.swagedit.core.model.Model) JsonNode(com.fasterxml.jackson.databind.JsonNode) IOException(java.io.IOException) ParserException(org.yaml.snakeyaml.parser.ParserException) HashSet(java.util.HashSet)

Example 9 with Model

use of com.reprezen.swagedit.core.model.Model in project KaiZen-OpenAPI-Editor by RepreZen.

the class JsonReferenceFactory method createSimpleReference.

/**
 * Returns a simple reference if the value node points to a definition inside the same document.
 *
 * @param baseURI
 * @param value
 * @return reference
 */
public JsonReference createSimpleReference(URI baseURI, AbstractNode valueNode) {
    if (valueNode == null || valueNode.isArray() || valueNode.isObject()) {
        return null;
    }
    final Object value = valueNode.asValue().getValue();
    if (!(value instanceof String)) {
        return null;
    }
    String stringValue = (String) value;
    if (StringUtils.emptyToNull(stringValue) == null || stringValue.startsWith("#") || stringValue.contains("/")) {
        return null;
    }
    final Model model = valueNode.getModel();
    if (model != null) {
        JsonPointer ptr = JsonPointer.compile("/definitions/" + value);
        AbstractNode target = model.find(ptr);
        if (target != null) {
            return new JsonReference.SimpleReference(baseURI, ptr, valueNode);
        }
    }
    return null;
}
Also used : AbstractNode(com.reprezen.swagedit.core.model.AbstractNode) Model(com.reprezen.swagedit.core.model.Model) JsonPointer(com.fasterxml.jackson.core.JsonPointer)

Aggregations

Model (com.reprezen.swagedit.core.model.Model)9 AbstractNode (com.reprezen.swagedit.core.model.AbstractNode)8 IRegion (org.eclipse.jface.text.IRegion)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 SwaggerHyperlink (com.reprezen.swagedit.core.hyperlinks.SwaggerHyperlink)2 HashSet (java.util.HashSet)2 IHyperlink (org.eclipse.jface.text.hyperlink.IHyperlink)2 JsonPointer (com.fasterxml.jackson.core.JsonPointer)1 JsonDocument (com.reprezen.swagedit.core.editor.JsonDocument)1 ArrayNode (com.reprezen.swagedit.core.model.ArrayNode)1 ObjectNode (com.reprezen.swagedit.core.model.ObjectNode)1 ValueNode (com.reprezen.swagedit.core.model.ValueNode)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Matcher (java.util.regex.Matcher)1 IFile (org.eclipse.core.resources.IFile)1 IDocument (org.eclipse.jface.text.IDocument)1 Position (org.eclipse.jface.text.Position)1 Region (org.eclipse.jface.text.Region)1 TreeViewer (org.eclipse.jface.viewers.TreeViewer)1