If someone can be of interest I wrote a method to overwrite a text
content with an additional odt.
private void callExample() throws Exception {
File file1 = new File("resources/source.odt");
ODSingleXMLDocument source =
ODSingleXMLDocument.createFromFile(file1);
replaceStringWithDocument(source, "[TEMPLATE:INCLUDE
FILE=TOINCLUDE.ODT]", "resources/toInclude.odt");
source.saveAs(new File("resources/result.odt"));
}
private void replaceStringWithDocument(ODSingleXMLDocument source,
String searchString, String includedDocumentName) throws Exception {
if (!(source == null || searchString.equals("") ||
includedDocumentName.equals(""))) {
Element body = source.getBody();
Element toReplaceElement = getElement(body, searchString);
if (toReplaceElement != null) {
File includedDocumentFile = new
File(includedDocumentName);
ODSingleXMLDocument includedDocument =
ODSingleXMLDocument.createFromFile(includedDocumentFile);
source.replace(toReplaceElement, includedDocument);
}
}
}
private Element getElement(Element el, String searchString) {
List contents = el.getContent();
Object loopObj;
Element recursiveEl;
for (int i = 0; i < contents.size(); i++) {
loopObj = contents.get(i);
if (loopObj instanceof Text) {
if (!((Text) loopObj).getValue().trim().equals("")) {
String pippo = "";
}
}
if ((loopObj instanceof Text) && (((Text)
loopObj).getValue().equals(searchString))) {
return el;
} else if ((loopObj instanceof Element) && (((Element)
loopObj).getContent() != null)) {
recursiveEl = getElement((Element) loopObj,
searchString);
if (recursiveEl != null) {
return recursiveEl;
}
}
}
return null;
}
I recursively look for the input string in the xml body tree, then i
use the method "replace" of ODSingleXMLDocument.
Now I'm trying to do the same thing with the items in the header and
footer. The problem is that these are contained in the office:master-
styles element in the styles.xml file.
In your opinion, is it possible?
Thanks,
Gabriele