Search in sources :

Example 1 with ComposerException

use of org.yaml.snakeyaml.composer.ComposerException in project toolkit by googleapis.

the class ConfigYamlReader method generateConfigNode.

public FieldConfigNode generateConfigNode(File file, ConfigHelper helper) {
    int initialErrorCount = helper.getErrorCount();
    String input;
    try {
        input = Files.toString(file, Charsets.UTF_8);
    } catch (IOException e) {
        helper.error("Cannot read configuration file: %s", e.getMessage());
        return null;
    }
    if (input.trim().isEmpty()) {
        helper.error("Empty YAML document");
        return null;
    }
    Node tree;
    try {
        tree = new Yaml().compose(new StringReader(input));
    } catch (ComposerException e) {
        helper.error(e.getProblemMark(), "Parsing error: %s", e.getMessage());
        return null;
    } catch (Exception e) {
        helper.error("Parsing error: %s", e.getMessage());
        return null;
    }
    if (tree == null) {
        helper.error("Parsing error");
        return null;
    }
    if (!(tree instanceof MappingNode)) {
        helper.error(tree, "Expected a map as a root object.");
        return null;
    }
    List<String> lines = Splitter.on(System.lineSeparator()).splitToList(input);
    ConfigNode configNode = new ConfigYamlNodeReader(lines, helper).readMessageNode((MappingNode) tree, ConfigProto.getDescriptor());
    return helper.getErrorCount() == initialErrorCount ? new FieldConfigNode(0, "").setChild(configNode) : null;
}
Also used : MappingNode(org.yaml.snakeyaml.nodes.MappingNode) ConfigNode(com.google.api.codegen.configgen.nodes.ConfigNode) Node(org.yaml.snakeyaml.nodes.Node) FieldConfigNode(com.google.api.codegen.configgen.nodes.FieldConfigNode) IOException(java.io.IOException) Yaml(org.yaml.snakeyaml.Yaml) ComposerException(org.yaml.snakeyaml.composer.ComposerException) IOException(java.io.IOException) FieldConfigNode(com.google.api.codegen.configgen.nodes.FieldConfigNode) MappingNode(org.yaml.snakeyaml.nodes.MappingNode) StringReader(java.io.StringReader) ComposerException(org.yaml.snakeyaml.composer.ComposerException) ConfigNode(com.google.api.codegen.configgen.nodes.ConfigNode) FieldConfigNode(com.google.api.codegen.configgen.nodes.FieldConfigNode)

Aggregations

ConfigNode (com.google.api.codegen.configgen.nodes.ConfigNode)1 FieldConfigNode (com.google.api.codegen.configgen.nodes.FieldConfigNode)1 IOException (java.io.IOException)1 StringReader (java.io.StringReader)1 Yaml (org.yaml.snakeyaml.Yaml)1 ComposerException (org.yaml.snakeyaml.composer.ComposerException)1 MappingNode (org.yaml.snakeyaml.nodes.MappingNode)1 Node (org.yaml.snakeyaml.nodes.Node)1