Java Code Examples for org.yaml.snakeyaml.nodes.NodeId#scalar()

The following examples show how to use org.yaml.snakeyaml.nodes.NodeId#scalar() . 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: ConfigurationRepresenter.java    From ServerListPlus with GNU General Public License v3.0 6 votes vote down vote up
@Override // Skip null values for configuration generating
protected NodeTuple representJavaBeanProperty(Object javaBean, Property property, Object value, Tag customTag) {
    if (value != null) {
        NodeTuple tuple = super.representJavaBeanProperty(javaBean, property, value, customTag);
        Node valueNode = tuple.getValueNode();

        // Avoid using tags for enums
        if (customTag == null && valueNode.getNodeId() == NodeId.scalar && value instanceof Enum<?>) {
            valueNode.setTag(Tag.STR);
        }

        return tuple;
    } else {
        return null;
    }
}
 
Example 2
Source File: Representer.java    From onedev with MIT License 5 votes vote down vote up
/**
 * Represent one JavaBean property.
 *
 * @param javaBean
 *            - the instance to be represented
 * @param property
 *            - the property of the instance
 * @param propertyValue
 *            - value to be represented
 * @param customTag
 *            - user defined Tag
 * @return NodeTuple to be used in a MappingNode. Return null to skip the
 *         property
 */
protected NodeTuple representJavaBeanProperty(Object javaBean, Property property,
        Object propertyValue, Tag customTag) {
    ScalarNode nodeKey = (ScalarNode) representData(property.getName());
    // the first occurrence of the node must keep the tag
    boolean hasAlias = this.representedObjects.containsKey(propertyValue);

    Node nodeValue = representData(propertyValue);

    if (propertyValue != null && !hasAlias) {
        NodeId nodeId = nodeValue.getNodeId();
        if (customTag == null) {
            if (nodeId == NodeId.scalar) {
                if (property.getType() == propertyValue.getClass()) {
                    if (propertyValue instanceof Enum<?>) {
                        nodeValue.setTag(Tag.STR);
                    }
                }
            } else {
                if (nodeId == NodeId.mapping) {
                    if (property.getType() == propertyValue.getClass()) {
                        if (!(propertyValue instanceof Map<?, ?>)) {
                            if (!nodeValue.getTag().equals(Tag.SET)) {
                                nodeValue.setTag(Tag.MAP);
                            }
                        }
                    }
                }
                checkGlobalTag(property, nodeValue, propertyValue);
            }
        }
    }

    return new NodeTuple(nodeKey, nodeValue);
}
 
Example 3
Source File: Representer.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Represent one JavaBean property.
 * 
 * @param javaBean
 *            - the instance to be represented
 * @param property
 *            - the property of the instance
 * @param propertyValue
 *            - value to be represented
 * @param customTag
 *            - user defined Tag
 * @return NodeTuple to be used in a MappingNode. Return null to skip the
 *         property
 */
protected NodeTuple representJavaBeanProperty(Object javaBean, Property property,
        Object propertyValue, Tag customTag) {
    ScalarNode nodeKey = (ScalarNode) representData(property.getName());
    // the first occurrence of the node must keep the tag
    boolean hasAlias = this.representedObjects.containsKey(propertyValue);

    Node nodeValue = representData(propertyValue);

    if (propertyValue != null && !hasAlias) {
        NodeId nodeId = nodeValue.getNodeId();
        if (customTag == null) {
            if (nodeId == NodeId.scalar) {
                if (propertyValue instanceof Enum<?>) {
                    nodeValue.setTag(Tag.STR);
                }
            } else {
                if (nodeId == NodeId.mapping) {
                    if (property.getType() == propertyValue.getClass()) {
                        if (!(propertyValue instanceof Map<?, ?>)) {
                            if (!nodeValue.getTag().equals(Tag.SET)) {
                                nodeValue.setTag(Tag.MAP);
                            }
                        }
                    }
                }
                checkGlobalTag(property, nodeValue, propertyValue);
            }
        }
    }

    return new NodeTuple(nodeKey, nodeValue);
}
 
Example 4
Source File: ManifestParser.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
private void addChild(NodeTuple tuple, ManifestParseTree parent) throws ParserException {
	ManifestParseTree keyNode = new ManifestParseTree();
	Node tupleKeyNode = tuple.getKeyNode();
	int lineNumber = tupleKeyNode.getStartMark().getLine() + 1;
	if (tupleKeyNode.getNodeId() != NodeId.scalar) {
		throw new ParserException(NLS.bind(ManifestConstants.UNSUPPORTED_TOKEN_ERROR, lineNumber), lineNumber);
	}
	keyNode.setLabel(((ScalarNode) tupleKeyNode).getValue());
	keyNode.setLineNumber(lineNumber);
	keyNode.setParent(parent);
	parent.getChildren().add(keyNode);
	addChild(tuple.getValueNode(), keyNode);
}
 
Example 5
Source File: Representer.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
/**
 * Represent one JavaBean property.
 *
 * @param javaBean
 *            - the instance to be represented
 * @param property
 *            - the property of the instance
 * @param propertyValue
 *            - value to be represented
 * @param customTag
 *            - user defined Tag
 * @return NodeTuple to be used in a MappingNode. Return null to skip the
 *         property
 */
protected NodeTuple representJavaBeanProperty(Object javaBean, Property property,
        Object propertyValue, Tag customTag) {
    ScalarNode nodeKey = (ScalarNode) representData(property.getName());
    // the first occurrence of the node must keep the tag
    boolean hasAlias = this.representedObjects.containsKey(propertyValue);

    Node nodeValue = representData(propertyValue);

    if (propertyValue != null && !hasAlias) {
        NodeId nodeId = nodeValue.getNodeId();
        if (customTag == null) {
            if (nodeId == NodeId.scalar) {
                if (propertyValue instanceof Enum<?>) {
                    nodeValue.setTag(Tag.STR);
                }
            } else {
                if (nodeId == NodeId.mapping) {
                    if (property.getType() == propertyValue.getClass()) {
                        if (!(propertyValue instanceof Map<?, ?>)) {
                            if (!nodeValue.getTag().equals(Tag.SET)) {
                                nodeValue.setTag(Tag.MAP);
                            }
                        }
                    }
                }
                checkGlobalTag(property, nodeValue, propertyValue);
            }
        }
    }

    return new NodeTuple(nodeKey, nodeValue);
}
 
Example 6
Source File: BeanConstructor.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
@Override
public Object construct(Node node) {
    if (node.getNodeId() == NodeId.scalar) {
        ScalarNode n = (ScalarNode) node;
        String value = n.getValue();
        int i = 3;
        if (value.length() != 0) {
            i = Integer.parseInt(value);
        }
        return new BeanHolder(new Bean1(i));
    } else {
        return new BeanHolder();
    }
}
 
Example 7
Source File: JsonReference.java    From KaiZen-OpenAPI-Editor with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Returns true if the argument can be identified as a JSON reference node.
 * 
 * @param tuple
 * @return true if a reference node
 */
public static boolean isReference(NodeTuple tuple) {
    if (tuple.getKeyNode().getNodeId() == NodeId.scalar) {
        String value = ((ScalarNode) tuple.getKeyNode()).getValue();

        return JsonReference.PROPERTY.equals(value) && tuple.getValueNode().getNodeId() == NodeId.scalar;
    }
    return false;
}
 
Example 8
Source File: YamlUtils.java    From configuration-as-code-plugin with MIT License 4 votes vote down vote up
private static void merge(Node root, Node node, String source) throws ConfiguratorException {
    if (root.getNodeId() != node.getNodeId()) {
        // means one of those yaml file doesn't conform to JCasC schema
        throw new ConfiguratorException(
                String.format("Found incompatible configuration elements %s %s", source, node.getStartMark()));
    }

    switch (root.getNodeId()) {
        case sequence:
            SequenceNode seq = (SequenceNode) root;
            SequenceNode seq2 = (SequenceNode) node;
            seq.getValue().addAll(seq2.getValue());
            return;
        case mapping:
            MappingNode map = (MappingNode) root;
            MappingNode map2 = (MappingNode) node;
            // merge common entries
            final Iterator<NodeTuple> it = map2.getValue().iterator();
            while (it.hasNext()) {
                NodeTuple t2 = it.next();
                for (NodeTuple tuple : map.getValue()) {

                    final Node key = tuple.getKeyNode();
                    final Node key2 = t2.getKeyNode();
                    if (key.getNodeId() == NodeId.scalar) {
                        // We dont support merge for more complex cases (yet)
                        if (((ScalarNode) key).getValue().equals(((ScalarNode) key2).getValue())) {
                            merge(tuple.getValueNode(), t2.getValueNode(), source);
                            it.remove();
                        }
                    } else {
                        throw new ConfiguratorException(
                                String.format("Found unmergeable configuration keys %s %s)", source, node.getEndMark()));
                    }
                }
            }
            // .. and add others
            map.getValue().addAll(map2.getValue());
            return;
        default:
            throw new ConfiguratorException(
                    String.format("Found conflicting configuration at %s %s", source, node.getStartMark()));
    }

}
 
Example 9
Source File: Representer.java    From Diorite with MIT License 4 votes vote down vote up
/**
 * Represent one JavaBean property.
 *
 * @param javaBean
 *         - the instance to be represented
 * @param property
 *         - the property of the instance
 * @param propertyValue
 *         - value to be represented
 * @param customTag
 *         - user defined Tag
 *
 * @return NodeTuple to be used in a MappingNode. Return null to skip the property
 */
@Nullable
protected NodeTuple representJavaBeanProperty(Object javaBean, Property property, @Nullable Object propertyValue, @Nullable Tag customTag)
{
    ScalarNode nodeKey = (ScalarNode) this.representData(property.getName());
    // the first occurrence of the node must keep the tag
    boolean hasAlias = this.representedObjects.containsKey(propertyValue);

    Node nodeValue = this.representData(propertyValue);

    if ((propertyValue != null) && ! hasAlias)
    {
        NodeId nodeId = nodeValue.getNodeId();
        if (customTag == null)
        {
            if (nodeId == NodeId.scalar)
            {
                if (propertyValue instanceof Enum<?>)
                {
                    nodeValue.setTag(Tag.STR);
                }
            }
            else
            {
                if (nodeId == NodeId.mapping)
                {
                    if (property.getType() == propertyValue.getClass())
                    {
                        if (! (propertyValue instanceof Map<?, ?>))
                        {
                            if (! nodeValue.getTag().equals(Tag.SET))
                            {
                                nodeValue.setTag(Tag.MAP);
                            }
                        }
                    }
                }
                this.checkGlobalTag(property, nodeValue, propertyValue);
            }
        }
    }

    return new NodeTuple(nodeKey, nodeValue);
}
 
Example 10
Source File: Yamls.java    From brooklyn-server with Apache License 2.0 4 votes vote down vote up
private static void findTextOfYamlAtPath(YamlExtract result, int pathIndex, Object ...path) {
    if (pathIndex>=path.length) {
        // we're done
        return;
    }
    
    Object pathItem = path[pathIndex];
    Node node = result.focus;
    
    if (node.getNodeId()==NodeId.mapping && pathItem instanceof String) {
        // find key
        Iterator<NodeTuple> ti = ((MappingNode)node).getValue().iterator();
        while (ti.hasNext()) {
            NodeTuple t = ti.next();
            Node key = t.getKeyNode();
            if (key.getNodeId()==NodeId.scalar) {
                if (pathItem.equals( ((ScalarNode)key).getValue() )) {
                    result.key = key;
                    result.focus = t.getValueNode();
                    if (pathIndex+1<path.length) {
                        // there are more path items, so the key here is a previous node
                        result.prev = key;
                    } else {
                        result.focusTuple = t;
                    }
                    findTextOfYamlAtPath(result, pathIndex+1, path);
                    if (result.next==null) {
                        if (ti.hasNext()) result.next = ti.next().getKeyNode();
                    }
                    return;
                } else {
                    result.prev = t.getValueNode();
                }
            } else {
                throw new IllegalStateException("Key "+key+" is not a scalar, searching for "+pathItem+" at depth "+pathIndex+" of "+Arrays.asList(path));
            }
        }
        throw new IllegalStateException("Did not find "+pathItem+" in "+node+" at depth "+pathIndex+" of "+Arrays.asList(path));
        
    } else if (node.getNodeId()==NodeId.sequence && pathItem instanceof Number) {
        // find list item
        List<Node> nl = ((SequenceNode)node).getValue();
        int i = ((Number)pathItem).intValue();
        if (i>=nl.size()) 
            throw new IllegalStateException("Index "+i+" is out of bounds in "+node+", searching for "+pathItem+" at depth "+pathIndex+" of "+Arrays.asList(path));
        if (i>0) result.prev = nl.get(i-1);
        result.key = null;
        result.focus = nl.get(i);
        findTextOfYamlAtPath(result, pathIndex+1, path);
        if (result.next==null) {
            if (nl.size()>i+1) result.next = nl.get(i+1);
        }
        return;
        
    } else {
        throw new IllegalStateException("Node "+node+" does not match selector "+pathItem+" at depth "+pathIndex+" of "+Arrays.asList(path));
    }
    
    // unreachable
}