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

com.xceptance.xlt.nocoding.parser.yaml.YamlParserUtils Maven / Gradle / Ivy

/*
 * Copyright (c) 2013-2023 Xceptance Software Technologies GmbH
 *
 * 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 com.xceptance.xlt.nocoding.parser.yaml;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.htmlunit.util.NameValuePair;
import org.yaml.snakeyaml.error.Mark;
import org.yaml.snakeyaml.nodes.MappingNode;
import org.yaml.snakeyaml.nodes.Node;
import org.yaml.snakeyaml.nodes.ScalarNode;
import org.yaml.snakeyaml.nodes.SequenceNode;
import org.yaml.snakeyaml.parser.ParserException;

/**
 * Utility methods for parsing.
 *
 * @author ckeiner
 */
public class YamlParserUtils
{
    /**
     * Converts a {@link SequenceNode} consisting of {@link MappingNode}s with simple Key/Value-Pairs to a List of
     * {@link NameValuePair}s
     *
     * @param context
     *            The {@link Mark} of the surrounding {@link Node}/context.
     * @param node
     *            The SequenceNode consisting of MappingNodes
     * @return A list of NameValuePairs that describe the assignments
     */
    public static List getSequenceNodeAsNameValuePair(final Mark context, final Node node)
    {
        // Verify node is an array
        if (!(node instanceof SequenceNode))
        {
            throw new ParserException("Node at", context, " is " + node.getNodeId() + " but needs to be an array", node.getStartMark());
        }
        final List nvp = new ArrayList<>();
        // For each item of the array
        ((SequenceNode) node).getValue().forEach(assignment -> {
            if (assignment instanceof MappingNode)
            {
                // For each single item
                ((MappingNode) assignment).getValue().forEach(pair -> {
                    // Scalars are expected and they are checked in the transform-method
                    final String name = transformScalarNodeToString(assignment.getStartMark(), pair.getKeyNode());
                    final String value = transformScalarNodeToString(assignment.getStartMark(), pair.getValueNode());
                    nvp.add(new NameValuePair(name, value));
                });
            }
            else
            {
                throw new ParserException("Node", node.getStartMark(),
                                          " contains a " + assignment.getNodeId() + " but it must contain a mapping",
                                          assignment.getStartMark());
            }
        });
        return nvp;
    }

    /**
     * Converts a {@link SequenceNode} consisting of {@link MappingNode}s with simple Key/Value-Pairs to a Map of
     * Strings.
     *
     * @param context
     *            The {@link Mark} of the surrounding {@link Node}/context.
     * @param node
     *            The SequenceNode consisting of MappingNodes
     * @return A Map of Strings that describe the assignments.
     */
    public static Map getSequenceNodeAsMap(final Mark context, final Node node)
    {
        // Verify node is an array
        if (!(node instanceof SequenceNode))
        {
            throw new ParserException("Node", context, " contains a " + context + " but it must contain an array", node.getStartMark());
        }
        final Map map = new HashMap<>();
        // For each item of the array
        ((SequenceNode) node).getValue().forEach(assignment -> {
            if (assignment instanceof MappingNode)
            {
                // For each single item
                ((MappingNode) assignment).getValue().forEach(pair -> {
                    // Scalars are expected and they are checked in the transform-method
                    final String name = transformScalarNodeToString(assignment.getStartMark(), pair.getKeyNode());
                    final String value = transformScalarNodeToString(assignment.getStartMark(), pair.getValueNode());
                    map.put(name, value);
                });
            }
            else
            {
                throw new ParserException("Node", node.getStartMark(),
                                          " contains a " + assignment.getNodeId() + " but it must contain a mapping",
                                          assignment.getStartMark());
            }
        });
        return map;
    }

    /**
     * Converts a {@link SequenceNode} consisting of {@link ScalarNode}s with a string element to a List of Strings
     *
     * @param context
     *            The {@link Mark} of the surrounding {@link Node}/context.
     * @param node
     *            The SequenceNode consisting of ScalarNodes
     * @return A list of Strings that describe the element of the sequence node
     */
    public static List getSequenceNodeAsStringList(final Mark context, final Node node)
    {
        // Verify node is an array
        if (!(node instanceof SequenceNode))
        {
            throw new ParserException("Node", context, " contains a " + node.getNodeId() + " but it must contain an array",
                                      node.getStartMark());
        }
        final List singleElement = new ArrayList<>();
        // For each item of the array
        ((SequenceNode) node).getValue().forEach(assignment -> {
            // Transform the scalar to a String and add it to the list
            singleElement.add(transformScalarNodeToString(node.getStartMark(), assignment));
        });
        return singleElement;
    }

    /**
     * Verifies the {@link Node} is a {@link ScalarNode} and returns its value as String.
     * 
     * @param context
     *            The {@link Mark} of the surrounding {@link Node}/context.
     * @param node
     *            The Node from which the value should be read.
     * @return The String value of the node.
     */
    public static String transformScalarNodeToString(final Mark context, final Node node)
    {
        if (!(node instanceof ScalarNode))
        {
            throw new ParserException("Node", context, " contains a " + node.getNodeId() + " but it must contain a scalar",
                                      node.getStartMark());
        }

        return ((ScalarNode) node).getValue();
    }

}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy