use of org.odftoolkit.simple.TextDocument in project structr by structr.
the class ODTExporter method exportAttributes.
static void exportAttributes(final ODTExporter thisNode, final String uuid) throws FrameworkException {
final SecurityContext securityContext = thisNode.getSecurityContext();
final File output = thisNode.getResultDocument();
final VirtualType transformation = thisNode.getTransformationProvider();
try {
final App app = StructrApp.getInstance();
final Result result = app.nodeQuery(AbstractNode.class).and(GraphObject.id, uuid).getResult();
final Result transformedResult = transformation.transformOutput(securityContext, AbstractNode.class, result);
Map<String, Object> nodeProperties = new HashMap<>();
GraphObjectMap node = (GraphObjectMap) transformedResult.get(0);
node.getPropertyKeys(null).forEach(p -> nodeProperties.put(p.dbName(), node.getProperty(p)));
TextDocument text = TextDocument.loadDocument(output.getFileOnDisk().getAbsolutePath());
NodeList nodes = text.getContentRoot().getElementsByTagName(ODT_FIELD_TAG_NAME);
for (int i = 0; i < nodes.getLength(); i++) {
Node currentNode = nodes.item(i);
NamedNodeMap attrs = currentNode.getAttributes();
Node fieldName = attrs.getNamedItem(ODT_FIELD_ATTRIBUTE_NAME);
Object nodeFieldValue = nodeProperties.get(fieldName.getNodeValue());
Node currentContent = attrs.getNamedItem(ODT_FIELD_ATTRIBUTE_VALUE);
if (nodeFieldValue != null) {
if (nodeFieldValue instanceof String[]) {
String[] arr = (String[]) nodeFieldValue;
List<String> list = new ArrayList<>(Arrays.asList(arr));
StringBuilder sb = new StringBuilder();
list.forEach(s -> sb.append(s + "\n"));
currentContent.setNodeValue(sb.toString());
} else if (nodeFieldValue instanceof Collection) {
Collection col = (Collection) nodeFieldValue;
StringBuilder sb = new StringBuilder();
col.forEach(s -> sb.append(s + "\n"));
currentContent.setNodeValue(sb.toString());
} else {
currentContent.setNodeValue(nodeFieldValue.toString());
}
}
}
text.save(output.getFileOnDisk().getAbsolutePath());
text.close();
} catch (Exception e) {
logger.error("Error while exporting to ODT", e);
}
}
Aggregations