Search in sources :

Example 6 with MultiException

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

the class YamlRepository method getAllArtifactNamesFromType.

/**
 * Gets all artifact names from targeted type
 *
 * @param target          target path of service template
 * @param idClass         id Class of requested Definition
 * @param targetNamespace targetNamespace of requested Definition
 * @return list of strings
 */
private <T extends DefinitionsChildId> List<String> getAllArtifactNamesFromType(Path target, Class<T> idClass, String targetNamespace) {
    List<String> output = new ArrayList<>();
    try {
        String fileName = BackendUtils.getFileNameOfDefinitions(idClass);
        String id = target.getFileName().toString();
        target = target.resolve(fileName);
        TDefinitions definitions = convertToDefinitions(target, id, targetNamespace);
        List<TArtifactTemplate> artifactTemplates = definitions.getArtifactTemplates();
        for (TArtifactTemplate artifactTemplate : artifactTemplates) {
            output.add(artifactTemplate.getId() + "@" + id);
        }
    } catch (MultiException | IOException e) {
        LOGGER.debug("Internal error", e);
    }
    return output;
}
Also used : TArtifactTemplate(org.eclipse.winery.model.tosca.TArtifactTemplate) ArrayList(java.util.ArrayList) IOException(java.io.IOException) TDefinitions(org.eclipse.winery.model.tosca.TDefinitions) MultiException(org.eclipse.winery.model.converter.support.exception.MultiException)

Example 7 with MultiException

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

the class SchemaVisitor method visit.

public Map<QName, Map<String, QName>> visit(YTServiceTemplate node, Path path, Path outpath, String namespace) {
    for (YTMapImportDefinition map : node.getImports()) {
        for (Map.Entry<String, YTImportDefinition> entry : map.entrySet()) {
            YamlReader reader = new YamlReader();
            try {
                YTServiceTemplate serviceTemplate = reader.parse(entry.getValue(), path, entry.getValue().getNamespaceUri());
                visit(serviceTemplate, new Parameter(outpath, entry.getValue().getNamespaceUri()).setBuildSchema(false));
            } catch (MultiException e) {
                setException(e);
            }
        }
    }
    this.visit(node, new Parameter(outpath, namespace));
    return propertyDefinition;
}
Also used : YTServiceTemplate(org.eclipse.winery.model.tosca.yaml.YTServiceTemplate) YTMapImportDefinition(org.eclipse.winery.model.tosca.yaml.support.YTMapImportDefinition) YTImportDefinition(org.eclipse.winery.model.tosca.yaml.YTImportDefinition) YamlReader(org.eclipse.winery.repository.converter.reader.YamlReader) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) AbstractMap(java.util.AbstractMap) MultiException(org.eclipse.winery.model.converter.support.exception.MultiException)

Example 8 with MultiException

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

the class SchemaVisitor method visit.

@Override
public Result visit(YTDataType node, Parameter parameter) {
    SchemaBuilder builder;
    if (this.schemaBuilders.containsKey(parameter.getNamespace())) {
        builder = this.schemaBuilders.get(parameter.getNamespace());
    } else {
        builder = new SchemaBuilder(parameter.getNamespace());
        this.schemaBuilders.put(parameter.getNamespace(), builder);
    }
    Map<String, QName> buildPlan = new LinkedHashMap<>();
    for (Map.Entry<String, YTPropertyDefinition> entry : node.getProperties().entrySet()) {
        QName type = entry.getValue().getType();
        buildPlan.put(entry.getKey(), entry.getValue().getType());
        // Add default YAML types
        if (type.getNamespaceURI().equals(Namespaces.YAML_NS)) {
            builder.addElements(entry.getKey(), entry.getValue());
        } else // if parameter.datatype is not defined and property type is defined in this schema -> add
        if (parameter.getDatatype() == null && type.getNamespaceURI().equals(parameter.getNamespace()) && this.localDatatypeNames.contains(type.getLocalPart())) {
            builder.addElements(entry.getKey(), entry.getValue());
        } else // if parameter.datatype is defined and property type is not in this service template
        if (!type.getNamespaceURI().equals(parameter.getNamespace()) || !this.localDatatypeNames.contains(type.getLocalPart())) {
            for (Map.Entry<String, YTImportDefinition> importDefinition : imports.get(type.getNamespaceURI())) {
                try {
                    YamlReader reader = new YamlReader();
                    YTServiceTemplate serviceTemplate = reader.parse(importDefinition.getValue(), parameter.getPath(), importDefinition.getValue().getNamespaceUri());
                    visit(serviceTemplate, new Parameter(parameter.getPath(), type.getNamespaceURI()).setDatatype(type).setBuildSchema(false));
                    // TODO getAbsoluteFilePath
                    builder.addImports(importDefinition.getValue().getNamespaceUri(), getRelativeFileName(importDefinition.getValue().getNamespaceUri()));
                    builder.addElements(entry.getKey(), entry.getValue());
                } catch (MultiException e) {
                    setException(e);
                }
            }
        } else // if parameter.datatype is defined and property type is in this service template but not read
        if (!(this.schemaTypes.containsKey(parameter.getNamespace()) && this.schemaTypes.get(parameter.getNamespace()).contains(type.getLocalPart()))) {
            this.data_types.get(type.getLocalPart()).accept(this, parameter.copy().addContext(type.getLocalPart()).setDatatype(type));
            builder.addElements(entry.getKey(), entry.getValue());
        }
    }
    this.addSchemaTypes(parameter.getNamespace(), parameter.getKey());
    this.propertyDefinition.put(new QName(parameter.getNamespace(), parameter.getKey()), buildPlan);
    if (parameter.getBuildComplexType()) {
        builder.buildComplexType(parameter.getKey(), false);
    }
    return null;
}
Also used : QName(javax.xml.namespace.QName) YTServiceTemplate(org.eclipse.winery.model.tosca.yaml.YTServiceTemplate) YamlReader(org.eclipse.winery.repository.converter.reader.YamlReader) LinkedHashMap(java.util.LinkedHashMap) YTPropertyDefinition(org.eclipse.winery.model.tosca.yaml.YTPropertyDefinition) YTImportDefinition(org.eclipse.winery.model.tosca.yaml.YTImportDefinition) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) AbstractMap(java.util.AbstractMap) MultiException(org.eclipse.winery.model.converter.support.exception.MultiException)

Example 9 with MultiException

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

the class YamlCsarImporter method parseDefinitionsElement.

@Override
protected Optional<TDefinitions> parseDefinitionsElement(Path entryDefinitionsPath, final List<String> errors) {
    YamlReader reader = new YamlReader();
    YTServiceTemplate serviceTemplate;
    try {
        serviceTemplate = reader.parse(new FileInputStream(entryDefinitionsPath.toFile()));
        String name = serviceTemplate.getMetadata().get("name");
        if (name == null) {
            // fallback to filename
            name = entryDefinitionsPath.toString().substring(entryDefinitionsPath.toString().indexOf("__") + 2, entryDefinitionsPath.toString().indexOf(".tosca"));
        }
        ToCanonical converter = new ToCanonical(targetRepository);
        return Optional.of(converter.convert(serviceTemplate, name, serviceTemplate.getMetadata().get("targetNamespace"), true));
    } catch (MultiException | FileNotFoundException e) {
        e.printStackTrace();
        LOGGER.error("Could not read the given entry definition " + e.getMessage());
    }
    return Optional.empty();
}
Also used : YTServiceTemplate(org.eclipse.winery.model.tosca.yaml.YTServiceTemplate) FileNotFoundException(java.io.FileNotFoundException) YamlReader(org.eclipse.winery.repository.converter.reader.YamlReader) ToCanonical(org.eclipse.winery.repository.yaml.converter.ToCanonical) MultiException(org.eclipse.winery.model.converter.support.exception.MultiException) FileInputStream(java.io.FileInputStream)

Example 10 with MultiException

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

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