Search in sources :

Example 1 with Composer

use of org.yaml.snakeyaml.composer.Composer in project alien4cloud by alien4cloud.

the class YamlParser method parseFile.

/**
 * Parse a yaml file into the given T instance.
 *
 * @param yamlStream Input stream that contains the yaml.
 * @param instance The instance to parse.
 * @return A parsing result that contains the parsing errors as well as the created instance.
 * @throws ParsingException In case there is a blocking issue while parsing the definition.
 */
public ParsingResult<T> parseFile(String filePath, String fileName, InputStream yamlStream, T instance) throws ParsingException {
    StreamReader sreader = new StreamReader(new UnicodeReader(yamlStream));
    Composer composer = new Composer(new ParserImpl(sreader), new Resolver());
    Node rootNode = null;
    try {
        rootNode = composer.getSingleNode();
        if (rootNode == null) {
            throw new ParsingException(fileName, new ParsingError(ErrorCode.SYNTAX_ERROR, "Empty file.", new Mark("root", 0, 0, 0, null, 0), "No yaml content found in file.", new Mark("root", 0, 0, 0, null, 0), filePath));
        }
    } catch (MarkedYAMLException exception) {
        throw new ParsingException(fileName, new ParsingError(ErrorCode.INVALID_YAML, exception));
    }
    try {
        return doParsing(fileName, rootNode, instance);
    } catch (ParsingException e) {
        e.setFileName(fileName);
        throw e;
    }
}
Also used : Composer(org.yaml.snakeyaml.composer.Composer) StreamReader(org.yaml.snakeyaml.reader.StreamReader) Resolver(org.yaml.snakeyaml.resolver.Resolver) Node(org.yaml.snakeyaml.nodes.Node) ParserImpl(org.yaml.snakeyaml.parser.ParserImpl) Mark(org.yaml.snakeyaml.error.Mark) UnicodeReader(org.yaml.snakeyaml.reader.UnicodeReader) MarkedYAMLException(org.yaml.snakeyaml.error.MarkedYAMLException)

Example 2 with Composer

use of org.yaml.snakeyaml.composer.Composer in project cassandra by apache.

the class YamlConfigurationLoader method fromMap.

// getSingleData returns Object, not T
@SuppressWarnings("unchecked")
public static <T> T fromMap(Map<String, Object> map, boolean shouldCheck, Class<T> klass) {
    Constructor constructor = new YamlConfigurationLoader.CustomConstructor(klass, klass.getClassLoader());
    Map<Class<?>, Map<String, Replacement>> replacements = getNameReplacements(Config.class);
    YamlConfigurationLoader.PropertiesChecker propertiesChecker = new YamlConfigurationLoader.PropertiesChecker(replacements);
    constructor.setPropertyUtils(propertiesChecker);
    Yaml yaml = new Yaml(constructor);
    Node node = yaml.represent(map);
    constructor.setComposer(new Composer(null, null) {

        @Override
        public Node getSingleNode() {
            return node;
        }
    });
    T value = (T) constructor.getSingleData(klass);
    if (shouldCheck)
        propertiesChecker.check();
    return value;
}
Also used : Composer(org.yaml.snakeyaml.composer.Composer) CustomClassLoaderConstructor(org.yaml.snakeyaml.constructor.CustomClassLoaderConstructor) Constructor(org.yaml.snakeyaml.constructor.Constructor) Node(org.yaml.snakeyaml.nodes.Node) Yaml(org.yaml.snakeyaml.Yaml) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Composer (org.yaml.snakeyaml.composer.Composer)2 Node (org.yaml.snakeyaml.nodes.Node)2 HashMap (java.util.HashMap)1 Map (java.util.Map)1 Yaml (org.yaml.snakeyaml.Yaml)1 Constructor (org.yaml.snakeyaml.constructor.Constructor)1 CustomClassLoaderConstructor (org.yaml.snakeyaml.constructor.CustomClassLoaderConstructor)1 Mark (org.yaml.snakeyaml.error.Mark)1 MarkedYAMLException (org.yaml.snakeyaml.error.MarkedYAMLException)1 ParserImpl (org.yaml.snakeyaml.parser.ParserImpl)1 StreamReader (org.yaml.snakeyaml.reader.StreamReader)1 UnicodeReader (org.yaml.snakeyaml.reader.UnicodeReader)1 Resolver (org.yaml.snakeyaml.resolver.Resolver)1