Search in sources :

Example 1 with YTPropertyDefinition

use of org.eclipse.winery.model.tosca.yaml.YTPropertyDefinition in project winery by eclipse.

the class SchemaVisitor method visit.

@Override
public Result visit(YTEntityType node, Parameter parameter) {
    if (node instanceof YTDataType || node.getProperties().isEmpty()) {
        return null;
    }
    // BuildPlan for assignments
    Map<String, QName> plan = new LinkedHashMap<>();
    String name = parameter.getKey() + "_Properties";
    SchemaBuilder builder = new SchemaBuilder(parameter.getNamespace());
    Map<String, String> imports = new LinkedHashMap<>();
    for (Map.Entry<String, YTPropertyDefinition> entry : node.getProperties().entrySet()) {
        builder.addElements(entry.getKey(), entry.getValue());
        QName type = entry.getValue().getType();
        if (type.getNamespaceURI() != null && !type.getNamespaceURI().equals(Namespaces.YAML_NS)) {
            imports.put(type.getNamespaceURI(), type.getLocalPart());
        }
        plan.put(entry.getKey(), entry.getValue().getType());
    }
    imports.forEach((key, value) -> builder.addImports(key, getRelativeFileName(key)));
    Document document = builder.buildComplexType(name, true).build();
    WriterUtils.saveType(document, parameter.getPath(), parameter.getNamespace(), name);
    // FIXME this needs access to a repository
    WriterUtils.storeTypes(null, parameter.getPath(), name, parameter.getNamespace(), name);
    this.propertyDefinition.put(new QName(parameter.getNamespace(), name), plan);
    return null;
}
Also used : YTPropertyDefinition(org.eclipse.winery.model.tosca.yaml.YTPropertyDefinition) YTDataType(org.eclipse.winery.model.tosca.yaml.YTDataType) QName(javax.xml.namespace.QName) Document(org.w3c.dom.Document) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) AbstractMap(java.util.AbstractMap) LinkedHashMap(java.util.LinkedHashMap)

Example 2 with YTPropertyDefinition

use of org.eclipse.winery.model.tosca.yaml.YTPropertyDefinition 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)

Aggregations

AbstractMap (java.util.AbstractMap)2 LinkedHashMap (java.util.LinkedHashMap)2 Map (java.util.Map)2 QName (javax.xml.namespace.QName)2 YTPropertyDefinition (org.eclipse.winery.model.tosca.yaml.YTPropertyDefinition)2 MultiException (org.eclipse.winery.model.converter.support.exception.MultiException)1 YTDataType (org.eclipse.winery.model.tosca.yaml.YTDataType)1 YTImportDefinition (org.eclipse.winery.model.tosca.yaml.YTImportDefinition)1 YTServiceTemplate (org.eclipse.winery.model.tosca.yaml.YTServiceTemplate)1 YamlReader (org.eclipse.winery.repository.converter.reader.YamlReader)1 Document (org.w3c.dom.Document)1