Project

General

Profile

Download (2.6 KB) Statistics
| Branch: | Tag: | Revision:
1
package eu.etaxonomy.cdm.print.out.odf;
2

    
3
import org.apache.log4j.Logger;
4
import org.jdom.Document;
5
import org.jdom.JDOMException;
6
import org.jdom.output.DOMOutputter;
7
import org.odftoolkit.odfdom.pkg.OdfFileDom;
8
import org.odftoolkit.odfdom.doc.OdfTextDocument;
9
import org.odftoolkit.odfdom.dom.element.office.OfficeTextElement;
10
import org.odftoolkit.odfdom.incubator.doc.office.OdfOfficeAutomaticStyles;
11
import org.w3c.dom.Node;
12
import org.w3c.dom.NodeList;
13

    
14
/**
15
 * 
16
 * @author n.hoffmann
17
 * @since Apr 20, 2010
18
 * @version 1.0
19
 */
20
public class DocumentCreator {
21

    
22
	private static final Logger logger = Logger
23
			.getLogger(DocumentCreator.class);
24
	
25
	private OdfTextDocument outputDocument;
26
	private OdfFileDom contentDom; // the document object model for content.xml
27
	private OdfFileDom stylesDom; // the document object model for styles.xml
28

    
29
	// the office:automatic-styles element in content.xml
30
	private OdfOfficeAutomaticStyles contentAutoStyles;
31

    
32
	// the office:styles element in styles.xml
33
	private StylesFactory stylesFactory;
34

    
35
	// the office:text element in the content.xml file
36
	private OfficeTextElement officeText;
37

    
38
	public OdfTextDocument create(Document document) throws JDOMException {
39
		if (setupOutputDocument()) {
40

    
41
			cleanOutDocument();
42

    
43
			stylesFactory = new StylesFactory(outputDocument);
44

    
45
			DOMOutputter domOutputter = new DOMOutputter();
46
			org.w3c.dom.Document output;
47
			output = domOutputter.output(document);
48
			
49
			Node firstChild = output.getFirstChild();
50
			
51
			org.w3c.dom.Document officeDocument = officeText.getOwnerDocument();
52
			
53
			
54
			Node node = officeDocument.importNode(firstChild, true);
55
			NodeList childNodes = node.getChildNodes();
56
			
57
			for (int i = 0; i < childNodes.getLength(); i++){
58
				officeText.appendChild(childNodes.item(i));
59
			}
60
			
61
			return outputDocument;
62
		}
63
		return null;
64
	}
65

    
66
	private boolean setupOutputDocument() {
67

    
68
		try {
69
			outputDocument = OdfTextDocument.newTextDocument();
70
			contentDom = outputDocument.getContentDom();
71
			stylesDom = outputDocument.getStylesDom();
72
			
73
			officeText = outputDocument.getContentRoot();
74
			
75
			contentAutoStyles = contentDom.getOrCreateAutomaticStyles();
76

    
77
			return true;
78
		} catch (Exception e) {
79
			logger.error("Unable to create output document.", e);
80
			outputDocument = null;
81
		}
82
		return false;
83
	}
84

    
85
	
86

    
87
	/*
88
	 * The default document has some content in it already (in the case of a
89
	 * text document, a <text:p>. Clean out all the old stuff.
90
	 */
91
	void cleanOutDocument() {
92
		Node childNode;
93

    
94
		childNode = officeText.getFirstChild();
95
		while (childNode != null) {
96
			officeText.removeChild(childNode);
97
			childNode = officeText.getFirstChild();
98
		}
99
	}
100
}
(1-1/4)