OfflineExporter.java
5.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.Node
* javax.jcr.NodeIterator
* javax.jcr.Property
* javax.jcr.RepositoryException
* org.apache.poi.hpsf.SummaryInformation
* org.apache.poi.hwpf.HWPFDocument
* org.apache.poi.hwpf.model.StyleDescription
* org.apache.poi.hwpf.model.StyleSheet
* org.apache.poi.hwpf.usermodel.CharacterRun
* org.apache.poi.hwpf.usermodel.Paragraph
* org.apache.poi.hwpf.usermodel.ParagraphProperties
* org.apache.poi.hwpf.usermodel.Range
*/
package com.day.cq.wcm.offline;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import org.apache.poi.hpsf.SummaryInformation;
import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.model.StyleDescription;
import org.apache.poi.hwpf.model.StyleSheet;
import org.apache.poi.hwpf.usermodel.CharacterRun;
import org.apache.poi.hwpf.usermodel.Paragraph;
import org.apache.poi.hwpf.usermodel.ParagraphProperties;
import org.apache.poi.hwpf.usermodel.Range;
public class OfflineExporter {
private final HWPFDocument document;
private final Range range;
private final Map<String, Style> styles;
private int maxLevel;
public static void export(Node node, int maxLevel, OutputStream stream) throws IOException, RepositoryException {
OfflineExporter exporter = new OfflineExporter(maxLevel);
if (node.hasProperty("jcr:content/jcr:title")) {
exporter.document.getSummaryInformation().setTitle(node.getProperty("jcr:content/jcr:title").toString());
}
exporter.exportPage(node, 0);
exporter.document.write(stream);
}
/*
* WARNING - Removed try catching itself - possible behaviour change.
*/
private OfflineExporter(int maxLevel) throws IOException {
this.styles = new HashMap<String, Style>();
this.maxLevel = 0;
InputStream stream = OfflineExporter.class.getResourceAsStream("export.doc");
try {
this.document = new HWPFDocument(stream);
this.range = this.document.getRange();
this.maxLevel = maxLevel;
StyleSheet stylesheet = this.document.getStyleSheet();
for (int i = 0; i < stylesheet.numStyles(); ++i) {
StyleDescription desc = stylesheet.getStyleDescription(i);
if (desc == null) continue;
this.styles.put(desc.getName(), new Style(i, desc));
}
}
finally {
stream.close();
}
}
private void exportPage(Node page, int level) throws RepositoryException {
if (page.hasNode("jcr:content")) {
this.exportContent(page.getNode("jcr:content"), level);
}
if (level < this.maxLevel) {
NodeIterator iterator = page.getNodes();
while (iterator.hasNext()) {
Node child = iterator.nextNode();
if (!child.isNodeType("cq:Page")) continue;
this.exportPage(child, level + 1);
}
}
}
private void exportContent(Node node, int level) throws RepositoryException {
if (node.hasNode("par")) {
NodeIterator iterator = node.getNode("par").getNodes();
while (iterator.hasNext()) {
Node child = iterator.nextNode();
if (!child.hasProperty("sling:resourceType")) continue;
String type = child.getProperty("sling:resourceType").getString();
if (type.equals("foundation/components/title")) {
this.exportTitle(child, level);
continue;
}
if (type.equals("foundation/components/text")) {
this.exportText(child);
continue;
}
if (type.equals("foundation/components/text")) {
this.exportText(child);
continue;
}
if (!type.equals("foundation/components/textimage")) continue;
this.exportText(child);
}
}
}
private void exportTitle(Node node, int level) throws RepositoryException {
Style style;
if (node.hasProperty("type") && "small".equals(node.getProperty("type").getString())) {
++level;
}
if ((style = this.styles.get("Heading")) != null && node.hasProperty("jcr:title")) {
Paragraph paragraph = style.addParagraph();
paragraph.insertAfter(node.getProperty("jcr:title").getString());
}
}
private void exportText(Node node) throws RepositoryException {
if (node.hasProperty("text") && this.styles.containsKey("Text body")) {
Paragraph paragraph = this.styles.get("Text body").addParagraph();
if (node.hasProperty("textIsRich") && node.getProperty("textIsRich").getBoolean()) {
paragraph.insertAfter(node.getProperty("text").getString());
} else {
paragraph.insertAfter(node.getProperty("text").getString());
}
}
}
private class Style {
private int index;
private StyleDescription description;
public Style(int index, StyleDescription description) {
this.index = index;
this.description = description;
}
public Paragraph addParagraph() {
return OfflineExporter.this.range.insertAfter(this.description.getPAP(), this.index);
}
}
}