Search in sources :

Example 1 with MultiException

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

the class YamlRepository method artifactTemplateExistsInType.

/**
 * Checks if artifact templates exists in type
 *
 * @param targetPath target path of requested type
 * @param qName      target QName
 * @return boolean if it was found
 */
private boolean artifactTemplateExistsInType(Path targetPath, QName qName) {
    try {
        TDefinitions xmlDefinitions = convertToDefinitions(targetPath, getNameOfTypeFromArtifactName(qName.getLocalPart()), qName.getNamespaceURI());
        List<TArtifactTemplate> artifacts = xmlDefinitions.getArtifactTemplates();
        for (TArtifactTemplate artifact : artifacts) {
            if (artifact.getId().equalsIgnoreCase(getNameOfArtifactFromArtifactName(qName.getLocalPart()))) {
                return true;
            }
        }
    } catch (IOException | MultiException e) {
        LOGGER.debug("Internal error", e);
    }
    return false;
}
Also used : TArtifactTemplate(org.eclipse.winery.model.tosca.TArtifactTemplate) IOException(java.io.IOException) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions) MultiException(org.eclipse.winery.model.converter.support.exception.MultiException)

Example 2 with MultiException

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

the class YamlRepository method definitionsFromRef.

/**
 * Gets yaml service template from ref and converts it to xml definitions
 *
 * @param ref Repository File Reference
 * @return xml definitions
 */
@Override
public TDefinitions definitionsFromRef(RepositoryFileReference ref) throws IOException {
    Path targetPath = this.ref2AbsolutePath(ref);
    if (ref.getParent() instanceof DefinitionsChildId) {
        try {
            QName name = ((DefinitionsChildId) ref.getParent()).getQName();
            TDefinitions definitions = convertToDefinitions(targetPath, name.getLocalPart(), name.getNamespaceURI());
            return getRequestedDefinition((DefinitionsChildId) ref.getParent(), definitions);
        } catch (MultiException e) {
            LOGGER.warn("Internal error", e);
        }
    }
    return null;
}
Also used : Path(java.nio.file.Path) DefinitionsChildId(org.eclipse.winery.model.ids.definitions.DefinitionsChildId) QName(javax.xml.namespace.QName) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions) MultiException(org.eclipse.winery.model.converter.support.exception.MultiException)

Example 3 with MultiException

use of org.eclipse.winery.model.converter.support.exception.MultiException 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 4 with MultiException

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

the class ImportVisitor method visit.

@Override
public Result visit(YTServiceTemplate node, Parameter parameter) {
    YamlReader reader = new YamlReader();
    if (!this.namespace.equals(Namespaces.TOSCA_YAML_NS)) {
        Set<String> typeDefinitions = new HashSet<>(Arrays.asList(Defaults.TOSCA_NORMATIVE_TYPES, Defaults.TOSCA_NONNORMATIVE_TYPES));
        String tmpNamespace = this.namespace;
        this.namespace = Namespaces.TOSCA_YAML_NS;
        Path tmpDir = Utils.getTmpDir(Paths.get("types"));
        for (String typeDefinition : typeDefinitions) {
            try {
                Path outFilePath = tmpDir.resolve(typeDefinition);
                InputStream inputStream = this.getClass().getResourceAsStream(// Do not use File.separator here (https://stackoverflow.com/a/41677152/8235252)
                "/".concat(typeDefinition));
                Files.copy(inputStream, outFilePath, StandardCopyOption.REPLACE_EXISTING);
                YTServiceTemplate serviceTemplate = reader.parseSkipTest(outFilePath, Namespaces.TOSCA_YAML_NS);
                if (Objects.nonNull(serviceTemplate)) {
                    serviceTemplate.accept(this, new Parameter());
                }
            } catch (MultiException e) {
                setException(e);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        this.namespace = tmpNamespace;
    }
    super.visit(node, parameter);
    return null;
}
Also used : Path(java.nio.file.Path) InputStream(java.io.InputStream) YTServiceTemplate(org.eclipse.winery.model.tosca.yaml.YTServiceTemplate) Parameter(org.eclipse.winery.repository.converter.validator.support.Parameter) YamlReader(org.eclipse.winery.repository.converter.reader.YamlReader) MultiException(org.eclipse.winery.model.converter.support.exception.MultiException) MultiException(org.eclipse.winery.model.converter.support.exception.MultiException) HashSet(java.util.HashSet)

Example 5 with MultiException

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

the class ImportVisitor method visit.

@Override
public Result visit(YTImportDefinition node, Parameter parameter) {
    YamlReader reader = new YamlReader();
    String importNamespace = node.getNamespaceUri() == null ? this.namespace : node.getNamespaceUri();
    try {
        YTServiceTemplate serviceTemplate = reader.parse(node, path, importNamespace);
        if (serviceTemplate != null) {
            String tmpNamespace = this.namespace;
            this.namespace = importNamespace;
            this.visit(serviceTemplate, new Parameter());
            this.namespace = tmpNamespace;
        }
        super.visit(node, parameter);
    } catch (MultiException e) {
        this.setException(e);
    }
    return null;
}
Also used : YTServiceTemplate(org.eclipse.winery.model.tosca.yaml.YTServiceTemplate) Parameter(org.eclipse.winery.repository.converter.validator.support.Parameter) YamlReader(org.eclipse.winery.repository.converter.reader.YamlReader) MultiException(org.eclipse.winery.model.converter.support.exception.MultiException)

Aggregations

MultiException (org.eclipse.winery.model.converter.support.exception.MultiException)11 YTServiceTemplate (org.eclipse.winery.model.tosca.yaml.YTServiceTemplate)6 YamlReader (org.eclipse.winery.repository.converter.reader.YamlReader)5 IOException (java.io.IOException)3 Path (java.nio.file.Path)3 TDefinitions (org.eclipse.winery.model.tosca.TDefinitions)3 FileInputStream (java.io.FileInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 InputStream (java.io.InputStream)2 AbstractMap (java.util.AbstractMap)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 QName (javax.xml.namespace.QName)2 TArtifactTemplate (org.eclipse.winery.model.tosca.TArtifactTemplate)2 YTImportDefinition (org.eclipse.winery.model.tosca.yaml.YTImportDefinition)2 ObjectValidator (org.eclipse.winery.repository.converter.validator.ObjectValidator)2 ExceptionInterpreter (org.eclipse.winery.repository.converter.validator.support.ExceptionInterpreter)2 Parameter (org.eclipse.winery.repository.converter.validator.support.Parameter)2 ConstructorException (org.yaml.snakeyaml.constructor.ConstructorException)2 ScannerException (org.yaml.snakeyaml.scanner.ScannerException)2