Search in sources :

Example 1 with NodeTuple

use of org.yaml.snakeyaml.nodes.NodeTuple in project KaiZen-OpenAPI-Editor by RepreZen.

the class ValidationUtil method findNode.

/*
     * Returns the yaml node that matches the given path.
     * 
     * The path is given as a list of String. The Node matching the path is found by traversing the children of the node
     * pass as first parameter.
     */
private static Node findNode(MappingNode root, List<String> paths) {
    if (paths.isEmpty())
        return root;
    String path = paths.get(0);
    if (path.startsWith("/")) {
        path = path.substring(1, path.length());
    }
    final List<String> next = paths.subList(1, paths.size());
    // ~1 is use to escape /
    if (path.contains("~1")) {
        path = path.replaceAll("~1", "/");
    }
    for (NodeTuple child : root.getValue()) {
        if (child.getKeyNode() instanceof ScalarNode) {
            ScalarNode scalar = (ScalarNode) child.getKeyNode();
            if (scalar.getValue().equals(path)) {
                return findNode(child, next);
            }
        }
    }
    return root;
}
Also used : ScalarNode(org.yaml.snakeyaml.nodes.ScalarNode) NodeTuple(org.yaml.snakeyaml.nodes.NodeTuple)

Example 2 with NodeTuple

use of org.yaml.snakeyaml.nodes.NodeTuple in project KaiZen-OpenAPI-Editor by RepreZen.

the class JsonReconcilingStrategy method calculatePositions.

protected List<Position> calculatePositions(MappingNode mapping) {
    List<Position> positions = new ArrayList<>();
    int start;
    int end = -1;
    for (NodeTuple tuple : mapping.getValue()) {
        start = tuple.getKeyNode().getStartMark().getLine();
        end = tuple.getValueNode().getEndMark().getLine();
        if ((end - start) > 0) {
            try {
                int startOffset = document.getLineOffset(start);
                int endOffset = document.getLineOffset(end);
                positions.add(new Position(startOffset, (endOffset - startOffset)));
            } catch (BadLocationException e) {
            }
        }
        if (tuple.getValueNode() instanceof MappingNode) {
            positions.addAll(calculatePositions((MappingNode) tuple.getValueNode()));
        }
    }
    return positions;
}
Also used : Position(org.eclipse.jface.text.Position) MappingNode(org.yaml.snakeyaml.nodes.MappingNode) ArrayList(java.util.ArrayList) NodeTuple(org.yaml.snakeyaml.nodes.NodeTuple) BadLocationException(org.eclipse.jface.text.BadLocationException)

Aggregations

NodeTuple (org.yaml.snakeyaml.nodes.NodeTuple)2 ArrayList (java.util.ArrayList)1 BadLocationException (org.eclipse.jface.text.BadLocationException)1 Position (org.eclipse.jface.text.Position)1 MappingNode (org.yaml.snakeyaml.nodes.MappingNode)1 ScalarNode (org.yaml.snakeyaml.nodes.ScalarNode)1