org.yaml.snakeyaml.nodes.NodeId Java Examples

The following examples show how to use org.yaml.snakeyaml.nodes.NodeId. 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: ConfigViewFactory.java    From thorntail with Apache License 2.0 7 votes vote down vote up
private static Yaml newYaml(Map<String, String> environment) {
    return new Yaml(new EnvironmentConstructor(environment),
                    new Representer(),
                    new DumperOptions(),
                    new Resolver() {
                        @Override
                        public Tag resolve(NodeId kind, String value, boolean implicit) {
                            if (value != null) {
                                if (value.startsWith("${env.")) {
                                    return new Tag("!env");
                                }
                                if (value.equalsIgnoreCase("on") ||
                                        value.equalsIgnoreCase("off") ||
                                        value.equalsIgnoreCase("yes") ||
                                        value.equalsIgnoreCase("no")) {
                                    return Tag.STR;
                                }
                            }
                            return super.resolve(kind, value, implicit);
                        }
                    });
}
 
Example #2
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 #3
Source File: SafeConstructor.java    From orion.server with Eclipse Public License 1.0 6 votes vote down vote up
public SafeConstructor() {
    this.yamlConstructors.put(Tag.NULL, new ConstructYamlNull());
    this.yamlConstructors.put(Tag.BOOL, new ConstructYamlBool());
    this.yamlConstructors.put(Tag.INT, new ConstructYamlInt());
    this.yamlConstructors.put(Tag.FLOAT, new ConstructYamlFloat());
    this.yamlConstructors.put(Tag.BINARY, new ConstructYamlBinary());
    this.yamlConstructors.put(Tag.TIMESTAMP, new ConstructYamlTimestamp());
    this.yamlConstructors.put(Tag.OMAP, new ConstructYamlOmap());
    this.yamlConstructors.put(Tag.PAIRS, new ConstructYamlPairs());
    this.yamlConstructors.put(Tag.SET, new ConstructYamlSet());
    this.yamlConstructors.put(Tag.STR, new ConstructYamlStr());
    this.yamlConstructors.put(Tag.SEQ, new ConstructYamlSeq());
    this.yamlConstructors.put(Tag.MAP, new ConstructYamlMap());
    this.yamlConstructors.put(null, undefinedConstructor);
    this.yamlClassConstructors.put(NodeId.scalar, undefinedConstructor);
    this.yamlClassConstructors.put(NodeId.sequence, undefinedConstructor);
    this.yamlClassConstructors.put(NodeId.mapping, undefinedConstructor);
}
 
Example #4
Source File: SafeConstructor.java    From snake-yaml with Apache License 2.0 6 votes vote down vote up
public SafeConstructor() {
    this.yamlConstructors.put(Tag.NULL, new ConstructYamlNull());
    this.yamlConstructors.put(Tag.BOOL, new ConstructYamlBool());
    this.yamlConstructors.put(Tag.INT, new ConstructYamlInt());
    this.yamlConstructors.put(Tag.FLOAT, new ConstructYamlFloat());
    this.yamlConstructors.put(Tag.BINARY, new ConstructYamlBinary());
    this.yamlConstructors.put(Tag.TIMESTAMP, new ConstructYamlTimestamp());
    this.yamlConstructors.put(Tag.OMAP, new ConstructYamlOmap());
    this.yamlConstructors.put(Tag.PAIRS, new ConstructYamlPairs());
    this.yamlConstructors.put(Tag.SET, new ConstructYamlSet());
    this.yamlConstructors.put(Tag.STR, new ConstructYamlStr());
    this.yamlConstructors.put(Tag.SEQ, new ConstructYamlSeq());
    this.yamlConstructors.put(Tag.MAP, new ConstructYamlMap());
    this.yamlConstructors.put(null, undefinedConstructor);
    this.yamlClassConstructors.put(NodeId.scalar, undefinedConstructor);
    this.yamlClassConstructors.put(NodeId.sequence, undefinedConstructor);
    this.yamlClassConstructors.put(NodeId.mapping, undefinedConstructor);
}
 
Example #5
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 #6
Source File: ComposerImplTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
public void testComposeBean() {
    String data = "!!org.yaml.snakeyaml.composer.ComposerImplTest$BeanToCompose {name: Bill, age: 18}";
    Yaml yaml = new Yaml();
    Node node = yaml.compose(new StringReader(data));
    assertNotNull(node);
    assertTrue(node instanceof MappingNode);
    assertEquals(
            "tag:yaml.org,2002:org.yaml.snakeyaml.composer.ComposerImplTest$BeanToCompose",
            node.getTag().getValue());
    assertEquals(NodeId.mapping, node.getNodeId());
    assertEquals(Object.class, node.getType());
}
 
Example #7
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 #8
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 #9
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 #10
Source File: BundleCacher.java    From APICloud-Studio with GNU General Public License v3.0 5 votes vote down vote up
public Object construct(Node node)
{
	node.setType(BuildPathElement.class);

	String path = getPath(node);
	String buildPath = getPath(node, "buildPath"); //$NON-NLS-1$
	BuildPathElement bpe = new BuildPathElement(path);
	Construct mappingConstruct = yamlClassConstructors.get(NodeId.mapping);
	mappingConstruct.construct2ndStep(node, bpe);
	bpe.setPath(path);
	bpe.setBuildPath(buildPath);
	return bpe;
}
 
Example #11
Source File: BundleCacher.java    From APICloud-Studio with GNU General Public License v3.0 5 votes vote down vote up
public Object construct(Node node)
{
	node.setType(BundleElement.class);
	String path = getPath(node);
	BundleElement be = new BundleElement(path);
	Construct mappingConstruct = yamlClassConstructors.get(NodeId.mapping);
	mappingConstruct.construct2ndStep(node, be);
	be.setPath(path);
	return be;
}
 
Example #12
Source File: BundleCacher.java    From APICloud-Studio with GNU General Public License v3.0 5 votes vote down vote up
public Object construct(Node node)
{
	node.setType(CommandElement.class);
	String path = getPath(node);
	CommandElement be = new LazyCommandElement(path);
	Construct mappingConstruct = yamlClassConstructors.get(NodeId.mapping);
	mappingConstruct.construct2ndStep(node, be);
	be.setPath(path);
	setPrefixTriggers(node, be);
	return be;
}
 
Example #13
Source File: BundleCacher.java    From APICloud-Studio with GNU General Public License v3.0 5 votes vote down vote up
public Object construct(Node node)
{
	node.setType(SnippetElement.class);
	String path = getPath(node);
	SnippetElement be = new SnippetElement(path);
	Construct mappingConstruct = yamlClassConstructors.get(NodeId.mapping);
	mappingConstruct.construct2ndStep(node, be);
	be.setPath(path);
	setPrefixTriggers(node, be);
	return be;
}
 
Example #14
Source File: BundleCacher.java    From APICloud-Studio with GNU General Public License v3.0 5 votes vote down vote up
public Object construct(Node node)
{
	node.setType(SnippetCategoryElement.class);
	String path = getPath(node);
	SnippetCategoryElement sce = new SnippetCategoryElement(path);
	Construct mappingConstruct = yamlClassConstructors.get(NodeId.mapping);
	mappingConstruct.construct2ndStep(node, sce);
	sce.setPath(path);
	return sce;
}
 
Example #15
Source File: BundleCacher.java    From APICloud-Studio with GNU General Public License v3.0 5 votes vote down vote up
public Object construct(Node node)
{
	node.setType(MenuElement.class);
	String path = getPath(node);
	MenuElement be = new MenuElement(path);
	Construct mappingConstruct = yamlClassConstructors.get(NodeId.mapping);
	mappingConstruct.construct2ndStep(node, be);
	be.setPath(path);
	forcePathsOfChildren(be.getChildren());
	return be;
}
 
Example #16
Source File: BundleCacher.java    From APICloud-Studio with GNU General Public License v3.0 5 votes vote down vote up
public Object construct(Node node)
{
	node.setType(ProjectTemplateElement.class);
	String path = getPath(node);
	ProjectTemplateElement be = new ProjectTemplateElement(path);
	Construct mappingConstruct = yamlClassConstructors.get(NodeId.mapping);
	mappingConstruct.construct2ndStep(node, be);
	be.setPath(path);
	return be;
}
 
Example #17
Source File: BundleCacher.java    From APICloud-Studio with GNU General Public License v3.0 5 votes vote down vote up
public Object construct(Node node)
{
	node.setType(ProjectSampleElement.class);
	String path = getPath(node);
	ProjectSampleElement be = new ProjectSampleElement(path);
	Construct mappingConstruct = yamlClassConstructors.get(NodeId.mapping);
	mappingConstruct.construct2ndStep(node, be);
	be.setPath(path);
	return be;
}
 
Example #18
Source File: BundleCacher.java    From APICloud-Studio with GNU General Public License v3.0 5 votes vote down vote up
public Object construct(Node node)
{
	node.setType(EnvironmentElement.class);
	String path = getPath(node);
	EnvironmentElement be = new LazyEnvironmentElement(path);
	Construct mappingConstruct = yamlClassConstructors.get(NodeId.mapping);
	mappingConstruct.construct2ndStep(node, be);
	be.setPath(path);
	return be;
}
 
Example #19
Source File: BundleCacher.java    From APICloud-Studio with GNU General Public License v3.0 5 votes vote down vote up
public Object construct(Node node)
{
	node.setType(TemplateElement.class);
	String path = getPath(node);
	TemplateElement be = new LazyTemplateElement(path);
	Construct mappingConstruct = yamlClassConstructors.get(NodeId.mapping);
	mappingConstruct.construct2ndStep(node, be);
	be.setPath(path);
	setPrefixTriggers(node, be);
	return be;
}
 
Example #20
Source File: BundleCacher.java    From APICloud-Studio with GNU General Public License v3.0 5 votes vote down vote up
public Object construct(Node node)
{
	node.setType(ContentAssistElement.class);
	String path = getPath(node);
	ContentAssistElement be = new LazyContentAssistElement(path);
	Construct mappingConstruct = yamlClassConstructors.get(NodeId.mapping);
	mappingConstruct.construct2ndStep(node, be);
	be.setPath(path);
	setPrefixTriggers(node, be);
	return be;
}
 
Example #21
Source File: YamlComposeTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
public void testComposeAllFromReader() {
    Yaml yaml = new Yaml();
    boolean first = true;
    for (Node node : yaml.composeAll(new StringReader("abc: 56\n---\n123\n---\n456"))) {
        if (first) {
            assertEquals(NodeId.mapping, node.getNodeId());
        } else {
            assertEquals(NodeId.scalar, node.getNodeId());
        }
        first = false;
    }
}
 
Example #22
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 #23
Source File: Constructor.java    From orion.server with Eclipse Public License 1.0 5 votes vote down vote up
public Constructor(TypeDescription theRoot) {
    if (theRoot == null) {
        throw new NullPointerException("Root type must be provided.");
    }
    this.yamlConstructors.put(null, new ConstructYamlObject());
    if (!Object.class.equals(theRoot.getType())) {
        rootTag = new Tag(theRoot.getType());
    }
    typeTags = new HashMap<Tag, Class<? extends Object>>();
    typeDefinitions = new HashMap<Class<? extends Object>, TypeDescription>();
    yamlClassConstructors.put(NodeId.scalar, new ConstructScalar());
    yamlClassConstructors.put(NodeId.mapping, new ConstructMapping());
    yamlClassConstructors.put(NodeId.sequence, new ConstructSequence());
    addTypeDescription(theRoot);
}
 
Example #24
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 #25
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 #26
Source File: Constructor.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
public Constructor(TypeDescription theRoot) {
    if (theRoot == null) {
        throw new NullPointerException("Root type must be provided.");
    }
    this.yamlConstructors.put(null, new ConstructYamlObject());
    if (!Object.class.equals(theRoot.getType())) {
        rootTag = new Tag(theRoot.getType());
    }
    typeTags = new HashMap<Tag, Class<? extends Object>>();
    typeDefinitions = new HashMap<Class<? extends Object>, TypeDescription>();
    yamlClassConstructors.put(NodeId.scalar, new ConstructScalar());
    yamlClassConstructors.put(NodeId.mapping, new ConstructMapping());
    yamlClassConstructors.put(NodeId.sequence, new ConstructSequence());
    addTypeDescription(theRoot);
}
 
Example #27
Source File: JodaTimeExampleTest.java    From snake-yaml with Apache License 2.0 5 votes vote down vote up
public JodaTimeConstructor() {
    javaDateConstruct = new ConstructYamlTimestamp();
    jodaDateConstruct = new ConstructJodaTimestamp();
    // Whenever we see an explicit timestamp tag, make a Joda Date
    // instead
    yamlConstructors.put(Tag.TIMESTAMP, jodaDateConstruct);
    // See
    // We need this to work around implicit construction.
    yamlClassConstructors.put(NodeId.scalar, new TimeStampConstruct());
}
 
Example #28
Source File: YamlParser.java    From nexus-repository-helm with Eclipse Public License 1.0 4 votes vote down vote up
public JodaPropertyConstructor() {
  yamlClassConstructors.put(NodeId.scalar, new TimeStampConstruct());
}
 
Example #29
Source File: YamlSortedSetTest.java    From snake-yaml with Apache License 2.0 4 votes vote down vote up
public SetContructor() {
    yamlClassConstructors.put(NodeId.sequence, new ConstructSetFromSequence());
}
 
Example #30
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
}