Today I have a question how to get the table that is placed somewhere in a document using a title of the table?
I can make no assumptions about where the table I used to find is placed in the document, except that the table is not placed within a comment.
Do I have to investigate all child objects of a document and for any child object iterate over their child objects (recursively)?
Here is my proposed solution:
- Code: Select all
private Table getTableFromPlaceholder(final Document document, final String placeholder) {
Stack<DocumentObject> documentObjects = new Stack<>();
documentObjects.addAll(asList(document.getChildObjects()));
while (!documentObjects.isEmpty()) {
DocumentObject documentObject = documentObjects.pop();
if (documentObject instanceof Table) {
Table table = (Table) documentObject;
if (Objects.equals(table.getTitle(), placeholder)) {
return table;
}
}
documentObjects.addAll(asList(documentObject.getChildObjects()));
}
return null;
}
Is there another more efficient way?
Kind regards,
Marcus