Search in sources :

Example 1 with UndefinedFile

use of org.eclipse.winery.yaml.common.exception.UndefinedFile in project winery by eclipse.

the class Reader method readMetadataObject.

/**
 * Uses snakeyaml to convert the part of an file containing metadata into an Object
 *
 * @return Object (Lists, Maps, Strings, Integers, Dates)
 * @throws UndefinedFile if the file could not be found.
 */
private Object readMetadataObject(Path path) throws MultiException {
    try (InputStream inputStream = new FileInputStream(path.toFile())) {
        BufferedReader buffer = new BufferedReader(new InputStreamReader(inputStream));
        String metadata = buffer.lines().collect(Collectors.joining("\n"));
        Matcher matcher = Pattern.compile("\\nmetadata:").matcher(metadata);
        // No metadata return null
        if (!matcher.find())
            return null;
        // Prevent index out of bound
        int index = matcher.start() + 1;
        if (index >= metadata.length())
            return null;
        // Get file string starting with "metadata:"
        metadata = metadata.substring(matcher.start() + 1);
        matcher = Pattern.compile(("\\n[^ ]")).matcher(metadata);
        if (matcher.find()) {
            // Cut of the part of the file after metadata (indicated by newline and a key)
            metadata = metadata.substring(0, matcher.start());
        }
        return this.yaml.load(metadata);
    } catch (FileNotFoundException e) {
        throw new MultiException().add(new UndefinedFile("The file '{}' is missing", path).setFileContext(path));
    } catch (IOException e) {
        logger.error("Could not read from inputstream", e);
        return null;
    }
}
Also used : UndefinedFile(org.eclipse.winery.yaml.common.exception.UndefinedFile) Matcher(java.util.regex.Matcher) MultiException(org.eclipse.winery.yaml.common.exception.MultiException)

Aggregations

Matcher (java.util.regex.Matcher)1 MultiException (org.eclipse.winery.yaml.common.exception.MultiException)1 UndefinedFile (org.eclipse.winery.yaml.common.exception.UndefinedFile)1