Search in sources :

Example 1 with UndefinedFile

use of org.eclipse.winery.model.converter.support.exception.UndefinedFile in project winery by eclipse.

the class YamlReader 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.model.converter.support.exception.UndefinedFile) InputStreamReader(java.io.InputStreamReader) Matcher(java.util.regex.Matcher) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) FileNotFoundException(java.io.FileNotFoundException) IOException(java.io.IOException) MultiException(org.eclipse.winery.model.converter.support.exception.MultiException) FileInputStream(java.io.FileInputStream)

Aggregations

BufferedReader (java.io.BufferedReader)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InputStreamReader (java.io.InputStreamReader)1 Matcher (java.util.regex.Matcher)1 MultiException (org.eclipse.winery.model.converter.support.exception.MultiException)1 UndefinedFile (org.eclipse.winery.model.converter.support.exception.UndefinedFile)1