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

The following examples show how to use org.yaml.snakeyaml.nodes.Node#isTwoStepsConstruction() . 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: BaseConstructor.java    From snake-yaml with Apache License 2.0 6 votes vote down vote up
protected void constructSet2ndStep(MappingNode node, Set<Object> set) {
    List<NodeTuple> nodeValue = (List<NodeTuple>) node.getValue();
    for (NodeTuple tuple : nodeValue) {
        Node keyNode = tuple.getKeyNode();
        Object key = constructObject(keyNode);
        if (key != null) {
            try {
                key.hashCode();// check circular dependencies
            } catch (Exception e) {
                throw new ConstructorException("while constructing a Set", node.getStartMark(),
                        "found unacceptable key " + key, tuple.getKeyNode().getStartMark(), e);
            }
        }
        if (keyNode.isTwoStepsConstruction()) {
            /*
             * if keyObject is created it 2 steps we should postpone putting
             * it into the set because it may have different hash after
             * initialization compared to clean just created one. And set of
             * course does not observe value hashCode changes.
             */
            sets2fill.add(0, new RecursiveTuple<Set<Object>, Object>(set, key));
        } else {
            set.add(key);
        }
    }
}
 
Example 3
Source File: SkriptYamlConstructor.java    From skript-yaml with MIT License 6 votes vote down vote up
@Override
public Object construct(Node node) {
	if (node.isTwoStepsConstruction()) {
		throw new YAMLException("Unexpected referential mapping structure. Node: " + node);
	}

	Map<?, ?> raw = (Map<?, ?>) super.construct(node);

	if (raw.containsKey(ConfigurationSerialization.SERIALIZED_TYPE_KEY)) {
		Map<String, Object> typed = new LinkedHashMap<String, Object>(raw.size());
		for (Map.Entry<?, ?> entry : raw.entrySet()) {
			typed.put(entry.getKey().toString(), entry.getValue());
		}

		try {
			return ConfigurationSerialization.deserializeObject(typed);
		} catch (IllegalArgumentException ex) {
			throw new YAMLException("Could not deserialize object", ex);
		}
	}

	return raw;
}
 
Example 4
Source File: BaseConstructor.java    From orion.server with Eclipse Public License 1.0 6 votes vote down vote up
protected void constructSet2ndStep(MappingNode node, Set<Object> set) {
    List<NodeTuple> nodeValue = (List<NodeTuple>) node.getValue();
    for (NodeTuple tuple : nodeValue) {
        Node keyNode = tuple.getKeyNode();
        Object key = constructObject(keyNode);
        if (key != null) {
            try {
                key.hashCode();// check circular dependencies
            } catch (Exception e) {
                throw new ConstructorException("while constructing a Set", node.getStartMark(),
                        "found unacceptable key " + key, tuple.getKeyNode().getStartMark(), e);
            }
        }
        if (keyNode.isTwoStepsConstruction()) {
            /*
             * if keyObject is created it 2 steps we should postpone putting
             * it into the set because it may have different hash after
             * initialization compared to clean just created one. And set of
             * course does not observe value hashCode changes.
             */
            sets2fill.add(0, new RecursiveTuple<Set<Object>, Object>(set, key));
        } else {
            set.add(key);
        }
    }
}
 
Example 5
Source File: BaseConstructor.java    From onedev with MIT License 6 votes vote down vote up
protected void constructSet2ndStep(MappingNode node, Set<Object> set) {
    List<NodeTuple> nodeValue = node.getValue();
    for (NodeTuple tuple : nodeValue) {
        Node keyNode = tuple.getKeyNode();
        Object key = constructObject(keyNode);
        if (key != null) {
            try {
                key.hashCode();// check circular dependencies
            } catch (Exception e) {
                throw new ConstructorException("while constructing a Set", node.getStartMark(),
                        "found unacceptable key " + key, tuple.getKeyNode().getStartMark(), e);
            }
        }
        if (keyNode.isTwoStepsConstruction()) {
            /*
             * if keyObject is created it 2 steps we should postpone putting
             * it into the set because it may have different hash after
             * initialization compared to clean just created one. And set of
             * course does not observe value hashCode changes.
             */
            sets2fill.add(0, new RecursiveTuple<Set<Object>, Object>(set, key));
        } else {
            set.add(key);
        }
    }
}
 
Example 6
Source File: SafeConstructor.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
public Object construct(Node node) {
    SequenceNode seqNode = (SequenceNode) node;
    if (node.isTwoStepsConstruction()) {
        return createDefaultList(seqNode.getValue().size());
    } else {
        return constructSequence(seqNode);
    }
}
 
Example 7
Source File: AbstractConstruct.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
/**
 * Fail with a reminder to provide the seconds step for a recursive
 * structure
 * 
 * @see org.yaml.snakeyaml.constructor.Construct#construct2ndStep(org.yaml.snakeyaml.nodes.Node,
 *      java.lang.Object)
 */
public void construct2ndStep(Node node, Object data) {
    if (node.isTwoStepsConstruction()) {
        throw new IllegalStateException("Not Implemented in " + getClass().getName());
    } else {
        throw new YAMLException("Unexpected recursive structure for Node: " + node);
    }
}
 
Example 8
Source File: BaseConstructor.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
protected void constructMapping2ndStep(MappingNode node, Map<Object, Object> mapping) {
    List<NodeTuple> nodeValue = (List<NodeTuple>) node.getValue();
    for (NodeTuple tuple : nodeValue) {
        Node keyNode = tuple.getKeyNode();
        Node valueNode = tuple.getValueNode();
        Object key = constructObject(keyNode);
        if (key != null) {
            try {
                key.hashCode();// check circular dependencies
            } catch (Exception e) {
                throw new ConstructorException("while constructing a mapping",
                        node.getStartMark(), "found unacceptable key " + key, tuple
                                .getKeyNode().getStartMark(), e);
            }
        }
        Object value = constructObject(valueNode);
        if (keyNode.isTwoStepsConstruction()) {
            /*
             * if keyObject is created it 2 steps we should postpone putting
             * it in map because it may have different hash after
             * initialization compared to clean just created one. And map of
             * course does not observe key hashCode changes.
             */
            maps2fill.add(0,
                    new RecursiveTuple<Map<Object, Object>, RecursiveTuple<Object, Object>>(
                            mapping, new RecursiveTuple<Object, Object>(key, value)));
        } else {
            mapping.put(key, value);
        }
    }
}
 
Example 9
Source File: AbstractConstruct.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Fail with a reminder to provide the seconds step for a recursive
 * structure
 * 
 * @see org.yaml.snakeyaml.constructor.Construct#construct2ndStep(org.yaml.snakeyaml.nodes.Node,
 *      java.lang.Object)
 */
public void construct2ndStep(Node node, Object data) {
    if (node.isTwoStepsConstruction()) {
        throw new IllegalStateException("Not Implemented in " + getClass().getName());
    } else {
        throw new YAMLException("Unexpected recursive structure for Node: " + node);
    }
}
 
Example 10
Source File: SafeConstructor.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
public Object construct(Node node) {
    if (node.isTwoStepsConstruction()) {
        return createDefaultSet();
    } else {
        return constructSet((MappingNode) node);
    }
}
 
Example 11
Source File: SafeConstructor.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
public Object construct(Node node) {
    if (node.isTwoStepsConstruction()) {
        return createDefaultSet();
    } else {
        return constructSet((MappingNode) node);
    }
}
 
Example 12
Source File: SafeConstructor.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
public Object construct(Node node) {
    if (node.isTwoStepsConstruction()) {
        return createDefaultMap();
    } else {
        return constructMapping((MappingNode) node);
    }
}
 
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) {
    if (node.isTwoStepsConstruction()) {
        return createDefaultMap();
    } else {
        return constructMapping((MappingNode) node);
    }
}
 
Example 14
Source File: SafeConstructor.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public void construct2ndStep(Node node, Object object) {
    if (node.isTwoStepsConstruction()) {
        constructMapping2ndStep((MappingNode) node, (Map<Object, Object>) object);
    } else {
        throw new YAMLException("Unexpected recursive mapping structure. Node: " + node);
    }
}
 
Example 15
Source File: BaseConstructor.java    From onedev with MIT License 5 votes vote down vote up
protected void constructMapping2ndStep(MappingNode node, Map<Object, Object> mapping) {
    List<NodeTuple> nodeValue = node.getValue();
    for (NodeTuple tuple : nodeValue) {
        Node keyNode = tuple.getKeyNode();
        Node valueNode = tuple.getValueNode();
        Object key = constructObject(keyNode);
        if (key != null) {
            try {
                key.hashCode();// check circular dependencies
            } catch (Exception e) {
                throw new ConstructorException("while constructing a mapping",
                        node.getStartMark(), "found unacceptable key " + key,
                        tuple.getKeyNode().getStartMark(), e);
            }
        }
        Object value = constructObject(valueNode);
        if (keyNode.isTwoStepsConstruction()) {
            /*
             * if keyObject is created it 2 steps we should postpone putting
             * it in map because it may have different hash after
             * initialization compared to clean just created one. And map of
             * course does not observe key hashCode changes.
             */
            maps2fill.add(0,
                    new RecursiveTuple<Map<Object, Object>, RecursiveTuple<Object, Object>>(
                            mapping, new RecursiveTuple<Object, Object>(key, value)));
        } else {
            mapping.put(key, value);
        }
    }
}
 
Example 16
Source File: SafeConstructor.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
public void construct2ndStep(Node node, Object object) {
    if (node.isTwoStepsConstruction()) {
        constructSet2ndStep((MappingNode) node, (Set<Object>) object);
    } else {
        throw new YAMLException("Unexpected recursive set structure. Node: " + node);
    }
}
 
Example 17
Source File: BaseConstructor.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
protected void constructMapping2ndStep(MappingNode node, Map<Object, Object> mapping) {
    List<NodeTuple> nodeValue = (List<NodeTuple>) node.getValue();
    for (NodeTuple tuple : nodeValue) {
        Node keyNode = tuple.getKeyNode();
        Node valueNode = tuple.getValueNode();
        Object key = constructObject(keyNode);
        if (key != null) {
            try {
                key.hashCode();// check circular dependencies
            } catch (Exception e) {
                throw new ConstructorException("while constructing a mapping",
                        node.getStartMark(), "found unacceptable key " + key, tuple
                                .getKeyNode().getStartMark(), e);
            }
        }
        Object value = constructObject(valueNode);
        if (keyNode.isTwoStepsConstruction()) {
            /*
             * if keyObject is created it 2 steps we should postpone putting
             * it in map because it may have different hash after
             * initialization compared to clean just created one. And map of
             * course does not observe key hashCode changes.
             */
            maps2fill.add(0,
                    new RecursiveTuple<Map<Object, Object>, RecursiveTuple<Object, Object>>(
                            mapping, new RecursiveTuple<Object, Object>(key, value)));
        } else {
            mapping.put(key, value);
        }
    }
}
 
Example 18
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 19
Source File: YamlConstructMapping.java    From Diorite with MIT License 4 votes vote down vote up
/**
 * Construct JavaBean. If type safe collections are used please look at
 * <code>TypeDescription</code>.
 *
 * @param node
 *         node where the keys are property names (they can only be <code>String</code>s) and values are objects to be created
 *
 * @return constructed JavaBean
 */
@SuppressWarnings({"unchecked", "rawtypes"})
@Override
public Object construct(Node node)
{
    MappingNode mnode = (MappingNode) node;
    Class<?> nodeType = node.getType();
    if (Map.class.isAssignableFrom(nodeType) || Collection.class.isAssignableFrom(nodeType))
    {
        if (Config.class.isAssignableFrom(nodeType))
        {
            ConfigManager configManager = ConfigManager.get();
            ConfigTemplate configTemplate = configManager.getConfigFile((Class) nodeType);
            Config config = configTemplate.create();
            this.yamlConstructor.constructMapping2ndStep(mnode, (Map) config);
            return config;
        }
        Object created = YamlCollectionCreator.createCollection(nodeType, mnode.getValue().size());
        if (Properties.class.isAssignableFrom(nodeType))
        {
            if (node.isTwoStepsConstruction())
            {
                throw new YAMLException("Properties must not be recursive.");
            }
            this.yamlConstructor.constructMapping2ndStep(mnode, (Map<Object, Object>) created);
            return created;
        }
        if (Map.class.isAssignableFrom(nodeType))
        {
            if (node.isTwoStepsConstruction())
            {
                return created;
            }
            this.yamlConstructor.constructMapping2ndStep(mnode, (Map<Object, Object>) created);
            return created;
        }
        if (Set.class.isAssignableFrom(nodeType))
        {
            if (node.isTwoStepsConstruction())
            {
                return created;
            }
            this.yamlConstructor.constructSet2ndStep(mnode, (Set<Object>) created);
            return created;
        }
        if (Collection.class.isAssignableFrom(nodeType))
        {
            Set<Object> collection = new LinkedHashSet<>((Collection<?>) created);
            if (node.isTwoStepsConstruction())
            {
                return collection;
            }
            this.yamlConstructor.constructSet2ndStep(mnode, collection);
            return collection;
        }
        else
        {
            throw new YAMLException("Unknown type: " + nodeType);
        }
    }
    else
    {
        if (node.isTwoStepsConstruction())
        {
            return this.createEmptyJavaBean(mnode);
        }
        else
        {
            return this.constructJavaBean2ndStep(mnode, this.createEmptyJavaBean(mnode));
        }
    }
}
 
Example 20
Source File: Constructor.java    From snake-yaml with Apache License 2.0 4 votes vote down vote up
/**
 * Construct JavaBean. If type safe collections are used please look at
 * <code>TypeDescription</code>.
 *
 * @param node
 *            node where the keys are property names (they can only be
 *            <code>String</code>s) and values are objects to be created
 * @return constructed JavaBean
 */
public Object construct(Node node) {
    MappingNode mnode = (MappingNode) node;
    if (Properties.class.isAssignableFrom(node.getType())) {
        Properties properties = new Properties();
        if (!node.isTwoStepsConstruction()) {
            constructMapping2ndStep(mnode, properties);
        } else {
            throw new YAMLException("Properties must not be recursive.");
        }
        return properties;
    } else if (SortedMap.class.isAssignableFrom(node.getType())) {
        SortedMap<Object, Object> map = new TreeMap<Object, Object>();
        if (!node.isTwoStepsConstruction()) {
            constructMapping2ndStep(mnode, map);
        }
        return map;
    } else if (Map.class.isAssignableFrom(node.getType())) {
        if (node.isTwoStepsConstruction()) {
            return createDefaultMap();
        } else {
            return constructMapping(mnode);
        }
    } else if (SortedSet.class.isAssignableFrom(node.getType())) {
        SortedSet<Object> set = new TreeSet<Object>();
        // XXX why this is not used ?
        // if (!node.isTwoStepsConstruction()) {
        constructSet2ndStep(mnode, set);
        // }
        return set;
    } else if (Collection.class.isAssignableFrom(node.getType())) {
        if (node.isTwoStepsConstruction()) {
            return createDefaultSet();
        } else {
            return constructSet(mnode);
        }
    } else {
        if (node.isTwoStepsConstruction()) {
            return createEmptyJavaBean(mnode);
        } else {
            return constructJavaBean2ndStep(mnode, createEmptyJavaBean(mnode));
        }
    }
}