Search in sources :

Example 1 with ScannerException

use of org.yaml.snakeyaml.scanner.ScannerException in project winery by eclipse.

the class Reader method readServiceTemplate.

/**
 * Reads a file and converts it to a ServiceTemplate
 *
 * @return ServiceTemplate
 * @throws MultiException the ServiceTemplate or the file is invalid.
 */
private TServiceTemplate readServiceTemplate(Path path, Path file, String namespace) throws MultiException {
    Path filePath;
    if (Objects.isNull(path)) {
        filePath = file;
    } else {
        filePath = path.resolve(file);
    }
    // 
    if (!fileChanged(filePath)) {
        if (exceptionBuffer.containsKey(filePath)) {
            throw exceptionBuffer.get(filePath);
        }
        if (serviceTemplateBuffer.containsKey(filePath)) {
            return serviceTemplateBuffer.get(filePath);
        }
    }
    logger.debug("Read Service Template: {}", filePath);
    try {
        // pre parse checking
        try {
            ObjectValidator objectValidator = new ObjectValidator();
            objectValidator.validateObject(readObject(filePath));
        } catch (ConstructorException e) {
            ExceptionInterpreter interpreter = new ExceptionInterpreter();
            throw new MultiException().add(interpreter.interpret(e));
        } catch (ScannerException e) {
            ExceptionInterpreter interpreter = new ExceptionInterpreter();
            throw new MultiException().add(interpreter.interpret(e));
        } catch (YAMLParserException e) {
            throw new MultiException().add(e);
        }
        // parse checking
        TServiceTemplate result = buildServiceTemplate(readObject(filePath), namespace);
        // post parse checking
        Validator validator = new Validator(path);
        validator.validate(result, namespace);
        serviceTemplateBuffer.put(filePath, result);
        return result;
    } catch (MultiException e) {
        exceptionBuffer.put(filePath, e);
        throw e.add(file.toString());
    }
}
Also used : Path(java.nio.file.Path) ScannerException(org.yaml.snakeyaml.scanner.ScannerException) ExceptionInterpreter(org.eclipse.winery.yaml.common.validator.support.ExceptionInterpreter) YAMLParserException(org.eclipse.winery.yaml.common.exception.YAMLParserException) ObjectValidator(org.eclipse.winery.yaml.common.validator.ObjectValidator) ConstructorException(org.yaml.snakeyaml.constructor.ConstructorException) MultiException(org.eclipse.winery.yaml.common.exception.MultiException) ObjectValidator(org.eclipse.winery.yaml.common.validator.ObjectValidator) Validator(org.eclipse.winery.yaml.common.validator.Validator) TServiceTemplate(org.eclipse.winery.model.tosca.yaml.TServiceTemplate)

Example 2 with ScannerException

use of org.yaml.snakeyaml.scanner.ScannerException in project google-cloud-intellij by GoogleCloudPlatform.

the class DefaultAppEngineProjectService method getValueFromAppYaml.

/**
 * Returns the value of a key-value pair for a given {@code key}, on the file located at {@code
 * appYamlPathString}.
 *
 * @return a String with the value, or an empty Optional if app.yaml isn't a regular file, or if
 *     there is any error getting the value
 * @throws MalformedYamlFileException when an app.yaml isn't syntactically well formed
 */
private Optional<String> getValueFromAppYaml(@NotNull String appYamlPathString, @NotNull String key) throws MalformedYamlFileException {
    Yaml yamlParser = new Yaml();
    Path appYamlPath = Paths.get(appYamlPathString);
    if (!Files.isRegularFile(appYamlPath)) {
        return Optional.empty();
    }
    try (BufferedReader reader = Files.newBufferedReader(appYamlPath, Charset.defaultCharset())) {
        Object parseResult = yamlParser.load(reader);
        if (!(parseResult instanceof Map)) {
            return Optional.empty();
        }
        // It's possible to get rid of this unchecked cast using a loadAs(file,
        // AppEngineYamlWebApp.class) sort of approach.
        Map<String, String> yamlMap = (Map<String, String>) parseResult;
        return yamlMap.containsKey(key) ? Optional.of(yamlMap.get(key)) : Optional.empty();
    } catch (ScannerException se) {
        throw new MalformedYamlFileException(se);
    } catch (InvalidPathException | IOException ioe) {
        return Optional.empty();
    }
}
Also used : Path(java.nio.file.Path) ScannerException(org.yaml.snakeyaml.scanner.ScannerException) BufferedReader(java.io.BufferedReader) IOException(java.io.IOException) Map(java.util.Map) Yaml(org.yaml.snakeyaml.Yaml) InvalidPathException(java.nio.file.InvalidPathException)

Example 3 with ScannerException

use of org.yaml.snakeyaml.scanner.ScannerException in project jPOS by jpos.

the class Environment method readYAML.

private boolean readYAML(String n, Properties properties) throws IOException {
    errorString = null;
    File f = new File(envDir + "/" + n + ".yml");
    if (f.exists() && f.canRead()) {
        try (InputStream fis = new FileInputStream(f)) {
            Yaml yaml = new Yaml();
            Iterable<Object> document = yaml.loadAll(fis);
            document.forEach(d -> {
                flat(properties, null, (Map<String, Object>) d, false);
            });
            propRef.set(properties);
            return true;
        } catch (ScannerException e) {
            errorString = "Environment (" + getName() + ") error " + e.getMessage();
        }
    }
    return false;
}
Also used : ScannerException(org.yaml.snakeyaml.scanner.ScannerException) Yaml(org.yaml.snakeyaml.Yaml)

Example 4 with ScannerException

use of org.yaml.snakeyaml.scanner.ScannerException in project winery by eclipse.

the class YamlReader method readServiceTemplate.

/**
 * Reads a file and converts it to a ServiceTemplate
 *
 * @return ServiceTemplate
 * @throws MultiException the ServiceTemplate or the file is invalid.
 */
private YTServiceTemplate readServiceTemplate(InputStream inputStream, String namespace) throws MultiException {
    Object object = null;
    // pre parse checking
    try {
        object = readObjectFromInputStream(inputStream);
        ObjectValidator objectValidator = new ObjectValidator();
        objectValidator.validateObject(object);
    } catch (ConstructorException e) {
        ExceptionInterpreter interpreter = new ExceptionInterpreter();
        throw new MultiException().add(interpreter.interpret(e));
    } catch (ScannerException e) {
        ExceptionInterpreter interpreter = new ExceptionInterpreter();
        throw new MultiException().add(interpreter.interpret(e));
    } catch (InvalidToscaSyntax invalidToscaSyntax) {
        invalidToscaSyntax.printStackTrace();
    }
    // parse checking
    return buildServiceTemplate(object, namespace);
}
Also used : ScannerException(org.yaml.snakeyaml.scanner.ScannerException) ExceptionInterpreter(org.eclipse.winery.repository.converter.validator.support.ExceptionInterpreter) InvalidToscaSyntax(org.eclipse.winery.model.converter.support.exception.InvalidToscaSyntax) ObjectValidator(org.eclipse.winery.repository.converter.validator.ObjectValidator) ConstructorException(org.yaml.snakeyaml.constructor.ConstructorException) MultiException(org.eclipse.winery.model.converter.support.exception.MultiException)

Example 5 with ScannerException

use of org.yaml.snakeyaml.scanner.ScannerException in project halyard by spinnaker.

the class HalconfigParser method getHalconfig.

/**
 * Returns the current halconfig stored at the halconfigPath.
 *
 * @return the fully parsed halconfig.
 * @see Halconfig
 */
public Halconfig getHalconfig() {
    Halconfig local = (Halconfig) DaemonTaskHandler.getContext();
    if (local == null) {
        try {
            InputStream is = getHalconfigStream();
            local = parseHalconfig(is);
        } catch (FileNotFoundException ignored) {
        // leave res as `null`
        } catch (ParserException e) {
            throw new ParseConfigException(e);
        } catch (ScannerException e) {
            throw new ParseConfigException(e);
        } catch (IllegalArgumentException e) {
            throw new ParseConfigException(e);
        }
    }
    local = transformHalconfig(local);
    DaemonTaskHandler.setContext(local);
    return local;
}
Also used : ScannerException(org.yaml.snakeyaml.scanner.ScannerException) ParserException(org.yaml.snakeyaml.parser.ParserException) ParseConfigException(com.netflix.spinnaker.halyard.config.error.v1.ParseConfigException) Halconfig(com.netflix.spinnaker.halyard.config.model.v1.node.Halconfig) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) FileNotFoundException(java.io.FileNotFoundException)

Aggregations

ScannerException (org.yaml.snakeyaml.scanner.ScannerException)7 Path (java.nio.file.Path)3 ConstructorException (org.yaml.snakeyaml.constructor.ConstructorException)3 MultiException (org.eclipse.winery.model.converter.support.exception.MultiException)2 ObjectValidator (org.eclipse.winery.repository.converter.validator.ObjectValidator)2 ExceptionInterpreter (org.eclipse.winery.repository.converter.validator.support.ExceptionInterpreter)2 Yaml (org.yaml.snakeyaml.Yaml)2 ParserException (org.yaml.snakeyaml.parser.ParserException)2 ParseConfigException (com.netflix.spinnaker.halyard.config.error.v1.ParseConfigException)1 Halconfig (com.netflix.spinnaker.halyard.config.model.v1.node.Halconfig)1 BufferedReader (java.io.BufferedReader)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 InvalidPathException (java.nio.file.InvalidPathException)1 Map (java.util.Map)1 InvalidToscaSyntax (org.eclipse.winery.model.converter.support.exception.InvalidToscaSyntax)1 YAMLParserException (org.eclipse.winery.model.converter.support.exception.YAMLParserException)1