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

The following examples show how to use org.yaml.snakeyaml.nodes.NodeId#mapping() . 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: 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 2
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 3
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 4
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 5
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
}