Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
com.regnosys.rosetta.generators.excel.ExcelGenerator Maven / Gradle / Ivy
package com.regnosys.rosetta.generators.excel;
import com.google.common.collect.Iterables;
import com.regnosys.rosetta.generator.external.AbstractExternalGenerator;
import com.regnosys.rosetta.rosetta.RosettaEnumValue;
import com.regnosys.rosetta.rosetta.RosettaEnumeration;
import com.regnosys.rosetta.rosetta.RosettaModel;
import com.regnosys.rosetta.rosetta.RosettaRootElement;
import com.regnosys.rosetta.rosetta.simple.Attribute;
import com.regnosys.rosetta.rosetta.simple.Data;
import java.io.ByteArrayOutputStream;
import java.util.ArrayList;
import java.util.Base64;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.function.Consumer;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.eclipse.emf.common.util.EList;
import org.eclipse.emf.ecore.EObject;
import org.eclipse.emf.ecore.resource.Resource;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.xtext.nodemodel.ICompositeNode;
import org.eclipse.xtext.nodemodel.ILeafNode;
import org.eclipse.xtext.nodemodel.util.NodeModelUtils;
import org.eclipse.xtext.xbase.lib.CollectionLiterals;
import org.eclipse.xtext.xbase.lib.Exceptions;
import org.eclipse.xtext.xbase.lib.Functions.Function1;
import org.eclipse.xtext.xbase.lib.IterableExtensions;
import org.eclipse.xtext.xbase.lib.ListExtensions;
import org.eclipse.xtext.xbase.lib.Procedures.Procedure2;
@SuppressWarnings("all")
public class ExcelGenerator extends AbstractExternalGenerator {
public ExcelGenerator() {
super("Excel");
}
@Override
public Map generate(final Resource resource, final RosettaModel model, final String version) {
return CollectionLiterals.newHashMap();
}
@Override
public Map afterAllGenerate(final ResourceSet set, final Collection extends RosettaModel> models, final String version) {
try {
final XSSFWorkbook workbook = new XSSFWorkbook();
final XSSFSheet typesSheet = workbook.createSheet("types");
final XSSFRow typesHeader = typesSheet.createRow(0);
this.populateRow(typesHeader, this.dataTypeHeader());
final Function1> _function = (RosettaModel it) -> {
return it.getElements();
};
final Function1>> _function_1 = (Data it) -> {
return this.dataAndAttributes(it);
};
final Procedure2, Integer> _function_2 = (List row, Integer index) -> {
final XSSFRow typesRow = typesSheet.createRow(((index).intValue() + 1));
this.populateRow(typesRow, row);
};
IterableExtensions.>forEach(IterableExtensions.>flatMap(Iterables.filter(IterableExtensions.flatMap(models, _function), Data.class), _function_1), _function_2);
final XSSFSheet enumsSheet = workbook.createSheet("enums");
final XSSFRow enumsHeader = enumsSheet.createRow(0);
this.populateRow(enumsHeader, this.enumHeader());
final Function1> _function_3 = (RosettaModel it) -> {
return it.getElements();
};
final Function1>> _function_4 = (RosettaEnumeration it) -> {
return this.enumAndValues(it);
};
final Procedure2, Integer> _function_5 = (List row, Integer index) -> {
final XSSFRow enumsRow = enumsSheet.createRow(((index).intValue() + 1));
this.populateRow(enumsRow, row);
};
IterableExtensions.>forEach(IterableExtensions.>flatMap(Iterables.filter(IterableExtensions.flatMap(models, _function_3), RosettaEnumeration.class), _function_4), _function_5);
final ByteArrayOutputStream baos = new ByteArrayOutputStream();
workbook.write(baos);
workbook.close();
final HashMap res = CollectionLiterals.newHashMap();
res.put("model.xlsx", Base64.getEncoder().encodeToString(baos.toByteArray()));
return res;
} catch (Throwable _e) {
throw Exceptions.sneakyThrow(_e);
}
}
public void populateRow(final Row row, final List cells) {
final Procedure2 _function = (String element, Integer index) -> {
final Cell headerCell = row.createCell((index).intValue());
headerCell.setCellValue(element);
};
IterableExtensions.forEach(cells, _function);
}
public List dataTypeHeader() {
return Collections.unmodifiableList(CollectionLiterals.newArrayList("Category", "Namespace", "Type Name", "Attribute Name", "Type", "Cardinality", "Definition"));
}
public List enumHeader() {
return Collections.unmodifiableList(CollectionLiterals.newArrayList("Category", "Namespace", "Enum Name", "Enum Value", "Definition"));
}
public ArrayList> dataAndAttributes(final Data it) {
final ArrayList> dataLines = CollectionLiterals.>newArrayList();
dataLines.add(this.dataType(it));
final Function1> _function = (Attribute it_1) -> {
return this.dataAttribute(it_1);
};
final Consumer> _function_1 = (List it_1) -> {
dataLines.add(it_1);
};
ListExtensions.>map(it.getAttributes(), _function).forEach(_function_1);
return dataLines;
}
public ArrayList> enumAndValues(final RosettaEnumeration _enum) {
final ArrayList> enumLines = CollectionLiterals.>newArrayList();
enumLines.add(this.enumType(_enum));
final Function1> _function = (RosettaEnumValue it) -> {
return this.enumValue(it);
};
final Consumer> _function_1 = (List it) -> {
enumLines.add(it);
};
ListExtensions.>map(_enum.getEnumValues(), _function).forEach(_function_1);
return enumLines;
}
public List dataType(final Data data) {
List _xblockexpression = null;
{
final String category = "data type";
EObject _eContainer = data.eContainer();
final String namespace = ((RosettaModel) _eContainer).getName();
final String typeName = data.getName();
final String definition = data.getDefinition();
_xblockexpression = Collections.unmodifiableList(CollectionLiterals.newArrayList(category, namespace, typeName, "", "", "", definition));
}
return _xblockexpression;
}
public List dataAttribute(final Attribute attribute) {
List _xblockexpression = null;
{
final String category = "attribute";
EObject _eContainer = attribute.eContainer().eContainer();
final String namespace = ((RosettaModel) _eContainer).getName();
EObject _eContainer_1 = attribute.eContainer();
final String typeName = ((Data) _eContainer_1).getName();
final String attributeName = attribute.getName();
final String attributeType = attribute.getTypeCall().getType().getName();
final String cardinality = this.extractGrammarText(attribute.getCard());
final String definition = attribute.getDefinition();
_xblockexpression = Collections.unmodifiableList(CollectionLiterals.newArrayList(category, namespace, typeName, attributeName, attributeType, cardinality, definition));
}
return _xblockexpression;
}
public List enumType(final RosettaEnumeration _enum) {
List _xblockexpression = null;
{
final String category = "enum";
EObject _eContainer = _enum.eContainer();
final String namespace = ((RosettaModel) _eContainer).getName();
final String enumName = _enum.getName();
final String definition = _enum.getDefinition();
_xblockexpression = Collections.unmodifiableList(CollectionLiterals.newArrayList(category, namespace, enumName, "", definition));
}
return _xblockexpression;
}
public List enumValue(final RosettaEnumValue enumValue) {
List _xblockexpression = null;
{
final String category = "enum value";
EObject _eContainer = enumValue.eContainer().eContainer();
final String namespace = ((RosettaModel) _eContainer).getName();
EObject _eContainer_1 = enumValue.eContainer();
final String enumName = ((RosettaEnumeration) _eContainer_1).getName();
final String enumValueName = enumValue.getName();
final String definition = enumValue.getDefinition();
_xblockexpression = Collections.unmodifiableList(CollectionLiterals.newArrayList(category, namespace, enumName, enumValueName, definition));
}
return _xblockexpression;
}
public String extractGrammarText(final EObject object) {
final ICompositeNode node = NodeModelUtils.getNode(object);
if ((node == null)) {
return null;
}
String _xifexpression = null;
if ((node instanceof ILeafNode)) {
_xifexpression = node.getText().trim();
} else {
String _xblockexpression = null;
{
int _max = Math.max(node.getTotalLength(), 1);
final StringBuilder builder = new StringBuilder(_max);
Iterable _leafNodes = node.getLeafNodes();
for (final ILeafNode leaf : _leafNodes) {
builder.append(leaf.getText());
}
_xblockexpression = builder.toString().trim();
}
_xifexpression = _xblockexpression;
}
return _xifexpression;
}
}