Search in sources :

Example 1 with MarkedYAMLException

use of org.yaml.snakeyaml.error.MarkedYAMLException in project be5 by DevelopmentOnTheEdge.

the class Templates method getTemplatesProject.

public static Project getTemplatesProject() throws ReadException {
    Project prj = new Project(TEMPLATES_PROJECT_NAME, true);
    LoadContext lc = new LoadContext();
    for (String template : TEMPLATES) {
        URL url = Templates.class.getResource("templates/" + template + ".yaml");
        Node content;
        try (InputStream is = url.openStream()) {
            content = new Yaml().compose(new InputStreamReader(is, StandardCharsets.UTF_8));
        } catch (MarkedYAMLException e) {
            throw new ReadException(new Exception((e.getProblemMark().getLine() + 1) + ":" + (e.getProblemMark().getColumn() + 1) + ": " + e.getMessage()), getPath(url), ReadException.LEE_INVALID_STRUCTURE);
        } catch (YAMLException | IOException e) {
            throw new ReadException(new Exception(e.getMessage()), getPath(url), ReadException.LEE_INVALID_STRUCTURE);
        }
        try {
            Object obj = Serialization.derepresent(content);
            @SuppressWarnings("unchecked") Map<String, Object> root = (Map<String, Object>) obj;
            @SuppressWarnings("unchecked") Map<String, Object> entityContent = (Map<String, Object>) root.get(template);
            DataElementUtils.saveQuiet(YamlDeserializer.readEntity(lc, template, entityContent, prj.getApplication()));
        } catch (RuntimeException e) {
            throw new ReadException(e, getPath(url), ReadException.LEE_INTERNAL_ERROR);
        }
        lc.check();
    }
    return prj;
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) Node(org.yaml.snakeyaml.nodes.Node) MarkedYAMLException(org.yaml.snakeyaml.error.MarkedYAMLException) YAMLException(org.yaml.snakeyaml.error.YAMLException) MarkedYAMLException(org.yaml.snakeyaml.error.MarkedYAMLException) IOException(java.io.IOException) URL(java.net.URL) Yaml(org.yaml.snakeyaml.Yaml) FileSystemNotFoundException(java.nio.file.FileSystemNotFoundException) URISyntaxException(java.net.URISyntaxException) IOException(java.io.IOException) ReadException(com.developmentontheedge.be5.metadata.exception.ReadException) MarkedYAMLException(org.yaml.snakeyaml.error.MarkedYAMLException) YAMLException(org.yaml.snakeyaml.error.YAMLException) ReadException(com.developmentontheedge.be5.metadata.exception.ReadException) LoadContext(com.developmentontheedge.be5.metadata.serialization.LoadContext) Map(java.util.Map)

Example 2 with MarkedYAMLException

use of org.yaml.snakeyaml.error.MarkedYAMLException 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)

Aggregations

MarkedYAMLException (org.yaml.snakeyaml.error.MarkedYAMLException)2 Node (org.yaml.snakeyaml.nodes.Node)2 ReadException (com.developmentontheedge.be5.metadata.exception.ReadException)1 LoadContext (com.developmentontheedge.be5.metadata.serialization.LoadContext)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 URISyntaxException (java.net.URISyntaxException)1 URL (java.net.URL)1 FileSystemNotFoundException (java.nio.file.FileSystemNotFoundException)1 Map (java.util.Map)1 Yaml (org.yaml.snakeyaml.Yaml)1 Composer (org.yaml.snakeyaml.composer.Composer)1 Mark (org.yaml.snakeyaml.error.Mark)1 YAMLException (org.yaml.snakeyaml.error.YAMLException)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