Search in sources :

Example 1 with SchemaBuilder

use of org.eclipse.winery.yaml.converter.yaml.support.SchemaBuilder in project winery by eclipse.

the class SchemaVisitor method visit.

@Override
public Result visit(TDataType 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, TPropertyDefinition> 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, TImportDefinition> importDefinition : imports.get(type.getNamespaceURI())) {
                try {
                    Reader reader = Reader.getReader();
                    TServiceTemplate 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) Reader(org.eclipse.winery.yaml.common.reader.yaml.Reader) SchemaBuilder(org.eclipse.winery.yaml.converter.yaml.support.SchemaBuilder) Parameter(org.eclipse.winery.yaml.converter.yaml.visitors.support.Parameter) MultiException(org.eclipse.winery.yaml.common.exception.MultiException)

Example 2 with SchemaBuilder

use of org.eclipse.winery.yaml.converter.yaml.support.SchemaBuilder in project winery by eclipse.

the class SchemaVisitor method visit.

@Override
public Result visit(TEntityType node, Parameter parameter) {
    if (node instanceof TDataType || 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, TPropertyDefinition> 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);
    WriterUtils.storeTypes(parameter.getPath(), name, parameter.getNamespace(), name);
    this.propertyDefinition.put(new QName(parameter.getNamespace(), name), plan);
    return null;
}
Also used : QName(javax.xml.namespace.QName) SchemaBuilder(org.eclipse.winery.yaml.converter.yaml.support.SchemaBuilder) Document(org.w3c.dom.Document)

Example 3 with SchemaBuilder

use of org.eclipse.winery.yaml.converter.yaml.support.SchemaBuilder in project winery by eclipse.

the class SchemaVisitor method visit.

@Override
public Result visit(TServiceTemplate node, Parameter parameter) {
    Map<String, TDataType> tmpDataTypes = this.data_types;
    this.data_types = node.getDataTypes();
    for (TMapImportDefinition map : node.getImports()) {
        for (Map.Entry<String, TImportDefinition> entry : map.entrySet()) {
            addImport(entry.getValue().getNamespaceUri(), new AbstractMap.SimpleEntry<>(entry));
        }
    }
    // init localDataTypesList
    node.getDataTypes().forEach((key, value) -> this.localDatatypeNames.add(key));
    // Default: parameter.datatype is not set -> visit all datatype nodes
    QName type = parameter.getDatatype();
    if (type == null) {
        for (Map.Entry<String, TDataType> entry : node.getDataTypes().entrySet()) {
            if (entry.getValue() != null) {
                entry.getValue().accept(this, parameter.copy().addContext("datatypes", entry.getKey()));
            }
        }
        visitChildes(node, parameter);
    } else // Optimized: parameter.datatype is set and defined in this service template
    if (type.getNamespaceURI().equals(parameter.getNamespace()) && this.localDatatypeNames.contains(type.getLocalPart())) {
        node.getDataTypes().get(parameter.getDatatype().getLocalPart()).accept(this, parameter.copy().addContext("datatypes", parameter.getDatatype().getLocalPart()));
    }
    if (parameter.getBuildSchema()) {
        for (Map.Entry<String, SchemaBuilder> entry : schemaBuilders.entrySet()) {
            Document document = entry.getValue().build();
            WriterUtils.saveType(document, parameter.getPath(), parameter.getNamespace(), entry.getValue().getNamespace());
        }
    }
    this.data_types = tmpDataTypes;
    return null;
}
Also used : TMapImportDefinition(org.eclipse.winery.model.tosca.yaml.support.TMapImportDefinition) QName(javax.xml.namespace.QName) Document(org.w3c.dom.Document) SchemaBuilder(org.eclipse.winery.yaml.converter.yaml.support.SchemaBuilder)

Aggregations

QName (javax.xml.namespace.QName)3 SchemaBuilder (org.eclipse.winery.yaml.converter.yaml.support.SchemaBuilder)3 Document (org.w3c.dom.Document)2 TMapImportDefinition (org.eclipse.winery.model.tosca.yaml.support.TMapImportDefinition)1 MultiException (org.eclipse.winery.yaml.common.exception.MultiException)1 Reader (org.eclipse.winery.yaml.common.reader.yaml.Reader)1 Parameter (org.eclipse.winery.yaml.converter.yaml.visitors.support.Parameter)1