1
|
/**
|
2
|
* Copyright (C) 2007 EDIT
|
3
|
* European Distributed Institute of Taxonomy
|
4
|
* http://www.e-taxonomy.eu
|
5
|
*
|
6
|
* The contents of this file are subject to the Mozilla Public License Version 1.1
|
7
|
* See LICENSE.TXT at the top of this package for the full license terms.
|
8
|
*/
|
9
|
package eu.etaxonomy.cdm.print.out.odf;
|
10
|
|
11
|
import org.apache.logging.log4j.LogManager;
|
12
|
import org.apache.logging.log4j.Logger;
|
13
|
import org.jdom.Document;
|
14
|
import org.jdom.JDOMException;
|
15
|
import org.jdom.output.DOMOutputter;
|
16
|
import org.odftoolkit.odfdom.doc.OdfTextDocument;
|
17
|
import org.odftoolkit.odfdom.dom.element.office.OfficeTextElement;
|
18
|
import org.odftoolkit.odfdom.incubator.doc.office.OdfOfficeAutomaticStyles;
|
19
|
import org.odftoolkit.odfdom.pkg.OdfFileDom;
|
20
|
import org.w3c.dom.Node;
|
21
|
import org.w3c.dom.NodeList;
|
22
|
|
23
|
/**
|
24
|
* @author n.hoffmann
|
25
|
* @since Apr 20, 2010
|
26
|
*/
|
27
|
public class DocumentCreator {
|
28
|
|
29
|
private static final Logger logger = LogManager.getLogger(DocumentCreator.class);
|
30
|
|
31
|
private OdfTextDocument outputDocument;
|
32
|
private OdfFileDom contentDom; // the document object model for content.xml
|
33
|
private OdfFileDom stylesDom; // the document object model for styles.xml
|
34
|
|
35
|
// the office:automatic-styles element in content.xml
|
36
|
private OdfOfficeAutomaticStyles contentAutoStyles;
|
37
|
|
38
|
// the office:styles element in styles.xml
|
39
|
private StylesFactory stylesFactory;
|
40
|
|
41
|
// the office:text element in the content.xml file
|
42
|
private OfficeTextElement officeText;
|
43
|
|
44
|
public OdfTextDocument create(Document document) throws JDOMException {
|
45
|
if (setupOutputDocument()) {
|
46
|
|
47
|
cleanOutDocument();
|
48
|
|
49
|
stylesFactory = new StylesFactory(outputDocument);
|
50
|
|
51
|
DOMOutputter domOutputter = new DOMOutputter();
|
52
|
org.w3c.dom.Document output;
|
53
|
output = domOutputter.output(document);
|
54
|
|
55
|
Node firstChild = output.getFirstChild();
|
56
|
|
57
|
org.w3c.dom.Document officeDocument = officeText.getOwnerDocument();
|
58
|
|
59
|
|
60
|
Node node = officeDocument.importNode(firstChild, true);
|
61
|
NodeList childNodes = node.getChildNodes();
|
62
|
|
63
|
for (int i = 0; i < childNodes.getLength(); i++){
|
64
|
officeText.appendChild(childNodes.item(i));
|
65
|
}
|
66
|
|
67
|
return outputDocument;
|
68
|
}
|
69
|
return null;
|
70
|
}
|
71
|
|
72
|
private boolean setupOutputDocument() {
|
73
|
|
74
|
try {
|
75
|
outputDocument = OdfTextDocument.newTextDocument();
|
76
|
contentDom = outputDocument.getContentDom();
|
77
|
stylesDom = outputDocument.getStylesDom();
|
78
|
|
79
|
officeText = outputDocument.getContentRoot();
|
80
|
|
81
|
contentAutoStyles = contentDom.getOrCreateAutomaticStyles();
|
82
|
|
83
|
return true;
|
84
|
} catch (Exception e) {
|
85
|
logger.error("Unable to create output document.", e);
|
86
|
outputDocument = null;
|
87
|
}
|
88
|
return false;
|
89
|
}
|
90
|
|
91
|
|
92
|
|
93
|
/*
|
94
|
* The default document has some content in it already (in the case of a
|
95
|
* text document, a <text:p>. Clean out all the old stuff.
|
96
|
*/
|
97
|
void cleanOutDocument() {
|
98
|
Node childNode;
|
99
|
|
100
|
childNode = officeText.getFirstChild();
|
101
|
while (childNode != null) {
|
102
|
officeText.removeChild(childNode);
|
103
|
childNode = officeText.getFirstChild();
|
104
|
}
|
105
|
}
|
106
|
}
|