use of org.eclipse.winery.yaml.common.reader.yaml.Reader in project winery by eclipse.
the class ImportVisitor method visit.
@Override
public Result visit(TServiceTemplate node, Parameter parameter) {
Reader reader = Reader.getReader();
if (!this.namespace.equals(Namespaces.TOSCA_NS)) {
Set<String> typeDefinitions = new HashSet<>(Arrays.asList(Defaults.TOSCA_NORMATIVE_TYPES, Defaults.TOSCA_NONNORMATIVE_TYPES));
String tmpNamespace = this.namespace;
this.namespace = Namespaces.TOSCA_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);
TServiceTemplate serviceTemplate = reader.parseSkipTest(outFilePath, Namespaces.TOSCA_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;
}
use of org.eclipse.winery.yaml.common.reader.yaml.Reader in project winery by eclipse.
the class Converter method convertY2X.
public void convertY2X(InputStream zip) throws MultiException {
Path path = Utils.unzipFile(zip);
LOGGER.debug("Unzip path: {}", path);
PathMatcher matcher = FileSystems.getDefault().getPathMatcher("glob:*.{yml,yaml}");
MultiException exception = Arrays.stream(Optional.ofNullable(path.toFile().listFiles()).orElse(new File[] {})).map(File::toPath).filter(file -> matcher.matches(file.getFileName())).map(file -> {
Reader reader = Reader.getReader();
try {
String id = file.getFileName().toString().substring(0, file.getFileName().toString().lastIndexOf("."));
Path fileName = file.subpath(path.getNameCount(), file.getNameCount());
Path fileOutPath = path.resolve("tmp");
String namespace = reader.getNamespace(path, fileName);
TServiceTemplate serviceTemplate = reader.parse(path, fileName);
LOGGER.debug("Convert filePath = {}, fileName = {}, id = {}, namespace = {}, fileOutPath = {}", path, fileName, id, namespace, fileOutPath);
this.convertY2X(serviceTemplate, id, namespace, path, fileOutPath);
} catch (MultiException e) {
return e;
}
return null;
}).filter(Objects::nonNull).reduce(MultiException::add).orElse(new MultiException());
if (exception.hasException())
throw exception;
}
use of org.eclipse.winery.yaml.common.reader.yaml.Reader 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;
}
use of org.eclipse.winery.yaml.common.reader.yaml.Reader in project winery by eclipse.
the class ImportVisitor method visit.
@Override
public Result visit(TImportDefinition node, Parameter parameter) {
Reader reader = Reader.getReader();
String importNamespace = node.getNamespaceUri() == null ? this.namespace : node.getNamespaceUri();
try {
TServiceTemplate 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;
}
use of org.eclipse.winery.yaml.common.reader.yaml.Reader in project winery by eclipse.
the class Converter method convertX2Y.
public InputStream convertX2Y(InputStream csar) {
Path filePath = Utils.unzipFile(csar);
Path fileOutPath = filePath.resolve("tmp");
try {
TOSCAMetaFileParser parser = new TOSCAMetaFileParser();
TOSCAMetaFile metaFile = parser.parse(filePath.resolve("TOSCA-Metadata").resolve("TOSCA.meta"));
org.eclipse.winery.yaml.common.reader.xml.Reader reader = new org.eclipse.winery.yaml.common.reader.xml.Reader();
try {
String fileName = metaFile.getEntryDefinitions();
Definitions definitions = reader.parse(filePath, Paths.get(fileName));
this.convertX2Y(definitions, fileOutPath);
} catch (MultiException e) {
LOGGER.error("Convert TOSCA XML to TOSCA YAML error", e);
}
return Utils.zipPath(fileOutPath);
} catch (Exception e) {
LOGGER.error("Error", e);
throw new AssertionError();
}
}
Aggregations