Java Code Examples for org.yaml.snakeyaml.nodes.Node#getStartMark()

The following examples show how to use org.yaml.snakeyaml.nodes.Node#getStartMark() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: BaseConstructor.java    From onedev with MIT License 6 votes vote down vote up
protected Object constructObjectNoCheck(Node node) {
    if (recursiveObjects.contains(node)) {
        throw new ConstructorException(null, null, "found unconstructable recursive node",
                node.getStartMark());
    }
    recursiveObjects.add(node);
    Construct constructor = getConstructor(node);
    Object data = (constructedObjects.containsKey(node)) ? constructedObjects.get(node)
            : constructor.construct(node);

    finalizeConstruction(node, data);
    constructedObjects.put(node, data);
    recursiveObjects.remove(node);
    if (node.isTwoStepsConstruction()) {
        constructor.construct2ndStep(node, data);
    }
    return data;
}
 
Example 2
Source File: Composer.java    From orion.server with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Reads a document from a source that contains only one document.
 * <p>
 * If the stream contains more than one document an exception is thrown.
 * </p>
 * 
 * @return The root node of the document or <code>null</code> if no document
 *         is available.
 */
public Node getSingleNode() {
    // Drop the STREAM-START event.
    parser.getEvent();
    // Compose a document if the stream is not empty.
    Node document = null;
    if (!parser.checkEvent(Event.ID.StreamEnd)) {
        document = composeDocument();
    }
    // Ensure that the stream contains no more documents.
    if (!parser.checkEvent(Event.ID.StreamEnd)) {
        Event event = parser.getEvent();
        throw new ComposerException("expected a single document in the stream",
                document.getStartMark(), "but found another document", event.getStartMark());
    }
    // Drop the STREAM-END event.
    parser.getEvent();
    return document;
}
 
Example 3
Source File: BaseConstructor.java    From orion.server with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * Construct object from the specified Node. Return existing instance if the
 * node is already constructed.
 * 
 * @param node
 *            Node to be constructed
 * @return Java instance
 */
protected Object constructObject(Node node) {
    if (constructedObjects.containsKey(node)) {
        return constructedObjects.get(node);
    }
    if (recursiveObjects.contains(node)) {
        throw new ConstructorException(null, null, "found unconstructable recursive node",
                node.getStartMark());
    }
    recursiveObjects.add(node);
    Construct constructor = getConstructor(node);
    Object data = constructor.construct(node);
    constructedObjects.put(node, data);
    recursiveObjects.remove(node);
    if (node.isTwoStepsConstruction()) {
        constructor.construct2ndStep(node, data);
    }
    return data;
}
 
Example 4
Source File: BaseConstructor.java    From snake-yaml with Apache License 2.0 6 votes vote down vote up
/**
 * Construct object from the specified Node. Return existing instance if the
 * node is already constructed.
 *
 * @param node
 *            Node to be constructed
 * @return Java instance
 */
protected Object constructObject(Node node) {
    if (constructedObjects.containsKey(node)) {
        return constructedObjects.get(node);
    }
    if (recursiveObjects.contains(node)) {
        throw new ConstructorException(null, null, "found unconstructable recursive node",
                node.getStartMark());
    }
    recursiveObjects.add(node);
    Construct constructor = getConstructor(node);
    Object data = constructor.construct(node);
    constructedObjects.put(node, data);
    recursiveObjects.remove(node);
    if (node.isTwoStepsConstruction()) {
        constructor.construct2ndStep(node, data);
    }
    return data;
}
 
Example 5
Source File: Composer.java    From snake-yaml with Apache License 2.0 6 votes vote down vote up
/**
 * Reads a document from a source that contains only one document.
 * <p>
 * If the stream contains more than one document an exception is thrown.
 * </p>
 * 
 * @return The root node of the document or <code>null</code> if no document
 *         is available.
 */
public Node getSingleNode() {
    // Drop the STREAM-START event.
    parser.getEvent();
    // Compose a document if the stream is not empty.
    Node document = null;
    if (!parser.checkEvent(Event.ID.StreamEnd)) {
        document = composeDocument();
    }
    // Ensure that the stream contains no more documents.
    if (!parser.checkEvent(Event.ID.StreamEnd)) {
        Event event = parser.getEvent();
        throw new ComposerException("expected a single document in the stream",
                document.getStartMark(), "but found another document", event.getStartMark());
    }
    // Drop the STREAM-END event.
    parser.getEvent();
    return document;
}
 
Example 6
Source File: YamlParameterizedConstructor.java    From digdag with Apache License 2.0 5 votes vote down vote up
private String validateScalar(Node node)
{
    if (node.isTwoStepsConstruction()) {
        throw new TagException("'"+node.getTag()+"' cannot be recursive.",
                node.getStartMark());
    }
    if (!node.getNodeId().equals(NodeId.scalar)) {
        throw new TagException("'"+node.getTag()+"' must be a string.",
                node.getStartMark());
    }
    return ((ScalarNode) node).getValue().toString();
}
 
Example 7
Source File: YamlParameterizedConstructor.java    From digdag with Apache License 2.0 5 votes vote down vote up
public Object construct(Node node)
{
    switch (node.getTag().getValue()) {
    case "!include":
        // TODO use implicit resolver (Resolver.addImplicitResolver) to convert "<include: path.dig" to new Tag("!include")?
        return "!include:" + java.util.UUID.randomUUID().toString();
    }
    throw new TagException(
            "could not determine a constructor for the tag " + node.getTag(),
            node.getStartMark());
}
 
Example 8
Source File: YamlTester.java    From sql-layer with GNU Affero General Public License v3.0 5 votes vote down vote up
@Override
protected Object constructObject(Node node) {
    if (recursing)
        return super.constructObject(node);
    else {
        recursing = true;
        Object o = super.constructObject(node);
        recursing = false;
        return new LinedObject(o, node.getStartMark());
    }
}
 
Example 9
Source File: SafeConstructor.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
public Object construct(Node node) {
    // Note: we do not check for duplicate keys, because it's too
    // CPU-expensive.
    if (!(node instanceof SequenceNode)) {
        throw new ConstructorException("while constructing pairs", node.getStartMark(),
                "expected a sequence, but found " + node.getNodeId(), node.getStartMark());
    }
    SequenceNode snode = (SequenceNode) node;
    List<Object[]> pairs = new ArrayList<Object[]>(snode.getValue().size());
    for (Node subnode : snode.getValue()) {
        if (!(subnode instanceof MappingNode)) {
            throw new ConstructorException("while constructingpairs", node.getStartMark(),
                    "expected a mapping of length 1, but found " + subnode.getNodeId(),
                    subnode.getStartMark());
        }
        MappingNode mnode = (MappingNode) subnode;
        if (mnode.getValue().size() != 1) {
            throw new ConstructorException("while constructing pairs", node.getStartMark(),
                    "expected a single mapping item, but found " + mnode.getValue().size()
                            + " items", mnode.getStartMark());
        }
        Node keyNode = mnode.getValue().get(0).getKeyNode();
        Node valueNode = mnode.getValue().get(0).getValueNode();
        Object key = constructObject(keyNode);
        Object value = constructObject(valueNode);
        pairs.add(new Object[] { key, value });
    }
    return pairs;
}
 
Example 10
Source File: SafeConstructor.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
public Object construct(Node node) {
    // Note: we do not check for duplicate keys, because it's too
    // CPU-expensive.
    Map<Object, Object> omap = new LinkedHashMap<Object, Object>();
    if (!(node instanceof SequenceNode)) {
        throw new ConstructorException("while constructing an ordered map",
                node.getStartMark(), "expected a sequence, but found " + node.getNodeId(),
                node.getStartMark());
    }
    SequenceNode snode = (SequenceNode) node;
    for (Node subnode : snode.getValue()) {
        if (!(subnode instanceof MappingNode)) {
            throw new ConstructorException("while constructing an ordered map",
                    node.getStartMark(), "expected a mapping of length 1, but found "
                            + subnode.getNodeId(), subnode.getStartMark());
        }
        MappingNode mnode = (MappingNode) subnode;
        if (mnode.getValue().size() != 1) {
            throw new ConstructorException("while constructing an ordered map",
                    node.getStartMark(), "expected a single mapping item, but found "
                            + mnode.getValue().size() + " items", mnode.getStartMark());
        }
        Node keyNode = mnode.getValue().get(0).getKeyNode();
        Node valueNode = mnode.getValue().get(0).getValueNode();
        Object key = constructObject(keyNode);
        Object value = constructObject(valueNode);
        omap.put(key, value);
    }
    return omap;
}
 
Example 11
Source File: Constructor.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
public void construct2ndStep(Node node, Object object) {
    try {
        getConstructor(node).construct2ndStep(node, object);
    } catch (Exception e) {
        throw new ConstructorException(null, null,
                "Can't construct a second step for a java object for " + node.getTag()
                        + "; exception=" + e.getMessage(), node.getStartMark(), e);
    }
}
 
Example 12
Source File: SafeConstructor.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
public Object construct(Node node) {
    // Note: we do not check for duplicate keys, because it's too
    // CPU-expensive.
    if (!(node instanceof SequenceNode)) {
        throw new ConstructorException("while constructing pairs", node.getStartMark(),
                "expected a sequence, but found " + node.getNodeId(), node.getStartMark());
    }
    SequenceNode snode = (SequenceNode) node;
    List<Object[]> pairs = new ArrayList<Object[]>(snode.getValue().size());
    for (Node subnode : snode.getValue()) {
        if (!(subnode instanceof MappingNode)) {
            throw new ConstructorException("while constructingpairs", node.getStartMark(),
                    "expected a mapping of length 1, but found " + subnode.getNodeId(),
                    subnode.getStartMark());
        }
        MappingNode mnode = (MappingNode) subnode;
        if (mnode.getValue().size() != 1) {
            throw new ConstructorException("while constructing pairs", node.getStartMark(),
                    "expected a single mapping item, but found " + mnode.getValue().size()
                            + " items", mnode.getStartMark());
        }
        Node keyNode = mnode.getValue().get(0).getKeyNode();
        Node valueNode = mnode.getValue().get(0).getValueNode();
        Object key = constructObject(keyNode);
        Object value = constructObject(valueNode);
        pairs.add(new Object[] { key, value });
    }
    return pairs;
}
 
Example 13
Source File: SafeConstructor.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
public Object construct(Node node) {
    // Note: we do not check for duplicate keys, because it's too
    // CPU-expensive.
    Map<Object, Object> omap = new LinkedHashMap<Object, Object>();
    if (!(node instanceof SequenceNode)) {
        throw new ConstructorException("while constructing an ordered map",
                node.getStartMark(), "expected a sequence, but found " + node.getNodeId(),
                node.getStartMark());
    }
    SequenceNode snode = (SequenceNode) node;
    for (Node subnode : snode.getValue()) {
        if (!(subnode instanceof MappingNode)) {
            throw new ConstructorException("while constructing an ordered map",
                    node.getStartMark(), "expected a mapping of length 1, but found "
                            + subnode.getNodeId(), subnode.getStartMark());
        }
        MappingNode mnode = (MappingNode) subnode;
        if (mnode.getValue().size() != 1) {
            throw new ConstructorException("while constructing an ordered map",
                    node.getStartMark(), "expected a single mapping item, but found "
                            + mnode.getValue().size() + " items", mnode.getStartMark());
        }
        Node keyNode = mnode.getValue().get(0).getKeyNode();
        Node valueNode = mnode.getValue().get(0).getValueNode();
        Object key = constructObject(keyNode);
        Object value = constructObject(valueNode);
        omap.put(key, value);
    }
    return omap;
}
 
Example 14
Source File: Constructor.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
public void construct2ndStep(Node node, Object object) {
    try {
        getConstructor(node).construct2ndStep(node, object);
    } catch (Exception e) {
        throw new ConstructorException(null, null,
                "Can't construct a second step for a java object for " + node.getTag()
                        + "; exception=" + e.getMessage(), node.getStartMark(), e);
    }
}
 
Example 15
Source File: SafeConstructor.java    From orion.server with Eclipse Public License 1.0 4 votes vote down vote up
public Object construct(Node node) {
    throw new ConstructorException(null, null,
            "could not determine a constructor for the tag " + node.getTag(),
            node.getStartMark());
}
 
Example 16
Source File: SafeConstructor.java    From snake-yaml with Apache License 2.0 4 votes vote down vote up
/**
 * Does merge for supplied mapping node.
 * 
 * @param node
 *            where to merge
 * @param isPreffered
 *            true if keys of node should take precedence over others...
 * @param key2index
 *            maps already merged keys to index from values
 * @param values
 *            collects merged NodeTuple
 * @return list of the merged NodeTuple (to be set as value for the
 *         MappingNode)
 */
private List<NodeTuple> mergeNode(MappingNode node, boolean isPreffered,
        Map<Object, Integer> key2index, List<NodeTuple> values) {
    List<NodeTuple> nodeValue = node.getValue();
    // reversed for http://code.google.com/p/snakeyaml/issues/detail?id=139
    Collections.reverse(nodeValue);
    for (Iterator<NodeTuple> iter = nodeValue.iterator(); iter.hasNext();) {
        final NodeTuple nodeTuple = iter.next();
        final Node keyNode = nodeTuple.getKeyNode();
        final Node valueNode = nodeTuple.getValueNode();
        if (keyNode.getTag().equals(Tag.MERGE)) {
            iter.remove();
            switch (valueNode.getNodeId()) {
            case mapping:
                MappingNode mn = (MappingNode) valueNode;
                mergeNode(mn, false, key2index, values);
                break;
            case sequence:
                SequenceNode sn = (SequenceNode) valueNode;
                List<Node> vals = sn.getValue();
                for (Node subnode : vals) {
                    if (!(subnode instanceof MappingNode)) {
                        throw new ConstructorException("while constructing a mapping",
                                node.getStartMark(),
                                "expected a mapping for merging, but found "
                                        + subnode.getNodeId(), subnode.getStartMark());
                    }
                    MappingNode mnode = (MappingNode) subnode;
                    mergeNode(mnode, false, key2index, values);
                }
                break;
            default:
                throw new ConstructorException("while constructing a mapping",
                        node.getStartMark(),
                        "expected a mapping or list of mappings for merging, but found "
                                + valueNode.getNodeId(), valueNode.getStartMark());
            }
        } else {
            // we need to construct keys to avoid duplications
            Object key = constructObject(keyNode);
            if (!key2index.containsKey(key)) { // 1st time merging key
                values.add(nodeTuple);
                // keep track where tuple for the key is
                key2index.put(key, values.size() - 1);
            } else if (isPreffered) { // there is value for the key, but we
                                      // need to override it
                // change value for the key using saved position
                values.set(key2index.get(key), nodeTuple);
            }
        }
    }
    return values;
}
 
Example 17
Source File: SafeConstructor.java    From snake-yaml with Apache License 2.0 4 votes vote down vote up
public Object construct(Node node) {
    throw new ConstructorException(null, null,
            "could not determine a constructor for the tag " + node.getTag(),
            node.getStartMark());
}
 
Example 18
Source File: SafeConstructor.java    From orion.server with Eclipse Public License 1.0 4 votes vote down vote up
/**
 * Does merge for supplied mapping node.
 * 
 * @param node
 *            where to merge
 * @param isPreffered
 *            true if keys of node should take precedence over others...
 * @param key2index
 *            maps already merged keys to index from values
 * @param values
 *            collects merged NodeTuple
 * @return list of the merged NodeTuple (to be set as value for the
 *         MappingNode)
 */
private List<NodeTuple> mergeNode(MappingNode node, boolean isPreffered,
        Map<Object, Integer> key2index, List<NodeTuple> values) {
    List<NodeTuple> nodeValue = node.getValue();
    // reversed for http://code.google.com/p/snakeyaml/issues/detail?id=139
    Collections.reverse(nodeValue);
    for (Iterator<NodeTuple> iter = nodeValue.iterator(); iter.hasNext();) {
        final NodeTuple nodeTuple = iter.next();
        final Node keyNode = nodeTuple.getKeyNode();
        final Node valueNode = nodeTuple.getValueNode();
        if (keyNode.getTag().equals(Tag.MERGE)) {
            iter.remove();
            switch (valueNode.getNodeId()) {
            case mapping:
                MappingNode mn = (MappingNode) valueNode;
                mergeNode(mn, false, key2index, values);
                break;
            case sequence:
                SequenceNode sn = (SequenceNode) valueNode;
                List<Node> vals = sn.getValue();
                for (Node subnode : vals) {
                    if (!(subnode instanceof MappingNode)) {
                        throw new ConstructorException("while constructing a mapping",
                                node.getStartMark(),
                                "expected a mapping for merging, but found "
                                        + subnode.getNodeId(), subnode.getStartMark());
                    }
                    MappingNode mnode = (MappingNode) subnode;
                    mergeNode(mnode, false, key2index, values);
                }
                break;
            default:
                throw new ConstructorException("while constructing a mapping",
                        node.getStartMark(),
                        "expected a mapping or list of mappings for merging, but found "
                                + valueNode.getNodeId(), valueNode.getStartMark());
            }
        } else {
            // we need to construct keys to avoid duplications
            Object key = constructObject(keyNode);
            if (!key2index.containsKey(key)) { // 1st time merging key
                values.add(nodeTuple);
                // keep track where tuple for the key is
                key2index.put(key, values.size() - 1);
            } else if (isPreffered) { // there is value for the key, but we
                                      // need to override it
                // change value for the key using saved position
                values.set(key2index.get(key), nodeTuple);
            }
        }
    }
    return values;
}
 
Example 19
Source File: ModelConstructor.java    From configuration-as-code-plugin with MIT License 4 votes vote down vote up
private static Source getSource(Node node) {
    final Mark mark = node.getStartMark();
    return new Source(mark.getName(), mark.getLine()+ 1);
}