OfflineImporter.java
6.16 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
153
154
155
156
157
158
159
160
161
162
163
164
165
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* com.day.cq.commons.jcr.JcrUtil
* javax.jcr.Binary
* javax.jcr.Node
* javax.jcr.Property
* javax.jcr.RepositoryException
* javax.jcr.Session
* javax.jcr.ValueFactory
* org.apache.sling.api.request.RequestParameter
* org.apache.sling.api.resource.ValueMap
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.wcm.offline;
import com.day.cq.commons.jcr.JcrUtil;
import com.day.cq.wcm.offline.DocImporter;
import com.day.cq.wcm.offline.DocxImporter;
import com.day.cq.wcm.offline.Paragraph;
import com.day.cq.wcm.offline.Picture;
import com.day.cq.wcm.offline.TextDocumentImporter;
import com.day.cq.wcm.offline.TextImportException;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Calendar;
import java.util.List;
import javax.jcr.Binary;
import javax.jcr.Node;
import javax.jcr.Property;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.jcr.ValueFactory;
import org.apache.sling.api.request.RequestParameter;
import org.apache.sling.api.resource.ValueMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class OfflineImporter {
private static final Logger log = LoggerFactory.getLogger(OfflineImporter.class);
private final Node parent;
private final TextDocumentImporter importer;
private String filename;
private String resourceType;
private String template;
private ValueMap config;
public OfflineImporter(Node parent, InputStream stream) throws IOException, TextImportException {
this.parent = parent;
this.importer = new DocImporter(stream);
}
public OfflineImporter(Node parent, RequestParameter rp) throws IOException, TextImportException {
this.parent = parent;
this.importer = OfflineImporter.getImporter(rp);
}
private static TextDocumentImporter getImporter(RequestParameter rp) throws IOException, TextImportException {
try {
return new DocImporter(rp.getInputStream());
}
catch (TextImportException ex) {
return new DocxImporter(rp.getInputStream());
}
}
public void setFilename(String filename) {
this.filename = filename;
}
public void setResourceType(String resourceType) {
this.resourceType = resourceType;
}
public void setTemplate(String template) {
this.template = template;
}
public void importDocument() throws RepositoryException {
String title = this.getTitle(this.filename);
Node node = this.parent.addNode(this.getNodeName(this.filename), "cq:Page");
Node content = node.addNode("jcr:content", "cq:PageContent");
if (this.resourceType != null) {
content.setProperty("sling:resourceType", this.resourceType);
}
if (this.template != null) {
content.setProperty("cq:template", this.template);
}
content.setProperty("jcr:title", title);
content.setProperty("pageTitle", title);
Node parsys = content.addNode("par", "nt:unstructured");
parsys.setProperty("sling:resourceType", "foundation/components/parsys");
for (int i = 0; i < this.importer.getNumberOfParagraphs(); ++i) {
Node paragraph = parsys.addNode("par" + i, "nt:unstructured");
Paragraph p = this.importer.getParagraph(i);
String html = p.getHTML();
List<Picture> pics = p.getPictures();
if (pics.size() == 0) {
this.importTextParagraph(paragraph, p);
continue;
}
if (html.length() == 0) {
this.importImageParagraph(paragraph, p);
continue;
}
this.importTextImageParagraph(paragraph, p);
}
}
private void importTextParagraph(Node node, Paragraph paragraph) throws RepositoryException {
node.setProperty("sling:resourceType", (String)this.config.get("text", (Object)"foundation/components/text"));
node.setProperty("textIsRich", "true");
node.setProperty("text", paragraph.getHTML());
}
private void importTextImageParagraph(Node node, Paragraph paragraph) throws RepositoryException {
node.setProperty("sling:resourceType", (String)this.config.get("textimage", (Object)"foundation/components/textimage"));
node.setProperty("textIsRich", "true");
Node image = node.addNode("image", "nt:unstructured");
this.importImageParagraph(image, paragraph);
node.setProperty("text", paragraph.getHTML());
}
private void importImageParagraph(Node node, Paragraph paragraph) throws RepositoryException {
List<Picture> pics = paragraph.getPictures();
if (pics.size() == 0) {
throw new RepositoryException("Paragraoh does not contain any pictures");
}
if (pics.size() > 1) {
log.debug("paragraph contains multiple pictures, all but last are dropped");
}
node.setProperty("sling:resourceType", (String)this.config.get("image", (Object)"foundation/components/image"));
Node file = node.addNode("file", "nt:resource");
file.setProperty("jcr:lastModified", Calendar.getInstance());
file.setProperty("jcr:mimeType", pics.get(0).getMediaType());
file.setProperty("jcr:data", node.getSession().getValueFactory().createBinary((InputStream)new ByteArrayInputStream(pics.get(0).getBytes())));
}
private String getTitle(String filename) {
String title = this.importer.getTitle();
return title != null ? title : this.trimFilename(filename);
}
private String trimFilename(String filename) {
if (filename.endsWith(".doc")) {
return filename.substring(0, filename.length() - ".doc".length());
}
if (filename.endsWith(".docx")) {
return filename.substring(0, filename.length() - ".docx".length());
}
return filename;
}
private String getNodeName(String filename) {
return JcrUtil.createValidName((String)this.trimFilename(filename));
}
public void setConfig(ValueMap config) {
this.config = config;
}
}