All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.raml.parser.utils.NodeUtils Maven / Gradle / Ivy

There is a newer version: 0.8.40
Show newest version
/*
 * Copyright 2016 (c) MuleSoft, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
 * either express or implied. See the License for the specific
 * language governing permissions and limitations under the License.
 */
package org.raml.parser.utils;

import static org.raml.parser.tagresolver.IncludeResolver.INCLUDE_APPLIED_TAG;
import static org.yaml.snakeyaml.nodes.Tag.STR;

import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.yaml.snakeyaml.nodes.MappingNode;
import org.yaml.snakeyaml.nodes.Node;
import org.yaml.snakeyaml.nodes.NodeTuple;
import org.yaml.snakeyaml.nodes.ScalarNode;
import org.yaml.snakeyaml.nodes.SequenceNode;
import org.yaml.snakeyaml.nodes.Tag;


public class NodeUtils
{

    private static Set STANDARD_TAGS = new HashSet(
            Arrays.asList(new Tag[] {
                    Tag.YAML,
                    Tag.MERGE,
                    Tag.SET,
                    Tag.PAIRS,
                    Tag.OMAP,
                    Tag.BINARY,
                    Tag.INT,
                    Tag.FLOAT,
                    Tag.TIMESTAMP,
                    Tag.BOOL,
                    Tag.NULL,
                    Tag.STR,
                    Tag.SEQ,
                    Tag.MAP,
                    new Tag(Tag.PREFIX + "value")
            }));

    public static Object getNodeValue(Node node)
    {
        Object value = null;
        if (node instanceof ScalarNode)
        {
            value = ((ScalarNode) node).getValue();
        }
        else if (node instanceof MappingNode)
        {
            List nodeTuples = ((MappingNode) node).getValue();
            if (!nodeTuples.isEmpty())
            {
                value = getNodeValue(nodeTuples.get(0).getKeyNode());
            }
        }
        else if (node instanceof SequenceNode)
        {
            List nodeList = ((SequenceNode) node).getValue();
            if (!nodeList.isEmpty())
            {
                value = getNodeValue(nodeList.get(0));
            }
        }
        return value;
    }

    public static boolean isStandardTag(Tag tag)
    {
        return STANDARD_TAGS.contains(tag);
    }

    public static boolean isNonStringTag(Tag tag)
    {
        return tag != null && !STR.equals(tag) && !tag.startsWith(INCLUDE_APPLIED_TAG);
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy