1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.officeimporter.internal.builder; |
21 |
|
|
22 |
|
import java.io.InputStream; |
23 |
|
import java.io.StringReader; |
24 |
|
import java.io.UnsupportedEncodingException; |
25 |
|
import java.net.URLEncoder; |
26 |
|
import java.util.Collections; |
27 |
|
import java.util.HashMap; |
28 |
|
import java.util.Map; |
29 |
|
|
30 |
|
import javax.inject.Inject; |
31 |
|
import javax.inject.Named; |
32 |
|
import javax.inject.Provider; |
33 |
|
import javax.inject.Singleton; |
34 |
|
|
35 |
|
import org.apache.commons.lang3.StringUtils; |
36 |
|
import org.w3c.dom.Document; |
37 |
|
import org.xwiki.bridge.DocumentAccessBridge; |
38 |
|
import org.xwiki.component.annotation.Component; |
39 |
|
import org.xwiki.component.manager.ComponentManager; |
40 |
|
import org.xwiki.model.reference.DocumentReference; |
41 |
|
import org.xwiki.model.reference.EntityReferenceSerializer; |
42 |
|
import org.xwiki.officeimporter.OfficeImporterException; |
43 |
|
import org.xwiki.officeimporter.builder.PresentationBuilder; |
44 |
|
import org.xwiki.officeimporter.converter.OfficeConverterException; |
45 |
|
import org.xwiki.officeimporter.document.XDOMOfficeDocument; |
46 |
|
import org.xwiki.officeimporter.server.OfficeServer; |
47 |
|
import org.xwiki.rendering.block.Block; |
48 |
|
import org.xwiki.rendering.block.ExpandedMacroBlock; |
49 |
|
import org.xwiki.rendering.block.XDOM; |
50 |
|
import org.xwiki.rendering.listener.MetaData; |
51 |
|
import org.xwiki.rendering.parser.Parser; |
52 |
|
import org.xwiki.rendering.renderer.BlockRenderer; |
53 |
|
import org.xwiki.xml.html.HTMLCleaner; |
54 |
|
import org.xwiki.xml.html.HTMLCleanerConfiguration; |
55 |
|
import org.xwiki.xml.html.HTMLUtils; |
56 |
|
|
57 |
|
|
58 |
|
@link |
59 |
|
|
60 |
|
@version |
61 |
|
@since |
62 |
|
|
63 |
|
@Component |
64 |
|
@Singleton |
|
|
| 93.9% |
Uncovered Elements: 3 (49) |
Complexity: 9 |
Complexity Density: 0.21 |
|
65 |
|
public class DefaultPresentationBuilder implements PresentationBuilder |
66 |
|
{ |
67 |
|
|
68 |
|
@link |
69 |
|
|
70 |
|
@Inject |
71 |
|
@Named("context") |
72 |
|
private Provider<ComponentManager> contextComponentManagerProvider; |
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
@Inject |
78 |
|
private OfficeServer officeServer; |
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
|
83 |
|
@Inject |
84 |
|
private DocumentAccessBridge documentAccessBridge; |
85 |
|
|
86 |
|
|
87 |
|
|
88 |
|
|
89 |
|
@Inject |
90 |
|
private EntityReferenceSerializer<String> entityReferenceSerializer; |
91 |
|
|
92 |
|
|
93 |
|
|
94 |
|
|
95 |
|
@Inject |
96 |
|
@Named("openoffice") |
97 |
|
private HTMLCleaner officeHTMLCleaner; |
98 |
|
|
99 |
|
|
100 |
|
|
101 |
|
|
102 |
|
@Inject |
103 |
|
@Named("xhtml/1.0") |
104 |
|
private Parser xhtmlParser; |
105 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
106 |
1 |
@Override... |
107 |
|
public XDOMOfficeDocument build(InputStream officeFileStream, String officeFileName, |
108 |
|
DocumentReference documentReference) throws OfficeImporterException |
109 |
|
{ |
110 |
|
|
111 |
1 |
Map<String, byte[]> artifacts = importPresentation(officeFileStream, officeFileName); |
112 |
|
|
113 |
|
|
114 |
1 |
String html = buildPresentationHTML(artifacts, StringUtils.substringBeforeLast(officeFileName, ".")); |
115 |
|
|
116 |
|
|
117 |
1 |
html = cleanPresentationHTML(html, documentReference); |
118 |
|
|
119 |
|
|
120 |
1 |
XDOM xdom = buildPresentationXDOM(html, documentReference); |
121 |
|
|
122 |
1 |
return new XDOMOfficeDocument(xdom, artifacts, this.contextComponentManagerProvider.get()); |
123 |
|
} |
124 |
|
|
125 |
|
|
126 |
|
|
127 |
|
|
128 |
|
|
129 |
|
@param |
130 |
|
@param |
131 |
|
@return |
132 |
|
@throws |
133 |
|
|
|
|
| 66.7% |
Uncovered Elements: 2 (6) |
Complexity: 2 |
Complexity Density: 0.33 |
|
134 |
1 |
protected Map<String, byte[]> importPresentation(InputStream officeFileStream, String officeFileName)... |
135 |
|
throws OfficeImporterException |
136 |
|
{ |
137 |
1 |
Map<String, InputStream> inputStreams = new HashMap<String, InputStream>(); |
138 |
1 |
inputStreams.put(officeFileName, officeFileStream); |
139 |
1 |
try { |
140 |
|
|
141 |
|
|
142 |
|
|
143 |
|
|
144 |
|
|
145 |
1 |
return this.officeServer.getConverter().convert(inputStreams, officeFileName, "img0.html"); |
146 |
|
} catch (OfficeConverterException e) { |
147 |
0 |
String message = "Error while converting document [%s] into html."; |
148 |
0 |
throw new OfficeImporterException(String.format(message, officeFileName), e); |
149 |
|
} |
150 |
|
} |
151 |
|
|
152 |
|
|
153 |
|
|
154 |
|
|
155 |
|
|
156 |
|
|
157 |
|
|
158 |
|
@param |
159 |
|
|
160 |
|
@param |
161 |
|
@return |
162 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (17) |
Complexity: 3 |
Complexity Density: 0.2 |
|
163 |
1 |
protected String buildPresentationHTML(Map<String, byte[]> presentationArtifacts, String nameSpace)... |
164 |
|
{ |
165 |
1 |
StringBuilder presentationHTML = new StringBuilder(); |
166 |
|
|
167 |
|
|
168 |
1 |
int i = 0; |
169 |
1 |
String slideImageKeyFormat = "img%s.jpg"; |
170 |
1 |
byte[] slideImage = presentationArtifacts.remove(String.format(slideImageKeyFormat, i)); |
171 |
3 |
while (slideImage != null) { |
172 |
|
|
173 |
|
|
174 |
2 |
presentationArtifacts.remove(String.format("img%s.html", i)); |
175 |
|
|
176 |
2 |
presentationArtifacts.remove(String.format("text%s.html", i)); |
177 |
|
|
178 |
|
|
179 |
2 |
String slideImageName = String.format("%s-slide%s.jpg", nameSpace, i); |
180 |
2 |
presentationArtifacts.put(slideImageName, slideImage); |
181 |
|
|
182 |
|
|
183 |
2 |
String slideImageURL = null; |
184 |
2 |
try { |
185 |
|
|
186 |
2 |
slideImageURL = URLEncoder.encode(slideImageName, "UTF-8"); |
187 |
|
} catch (UnsupportedEncodingException e) { |
188 |
|
|
189 |
|
} |
190 |
2 |
presentationHTML.append(String.format("<p><img src=\"%s\"/></p>", slideImageURL)); |
191 |
|
|
192 |
|
|
193 |
2 |
slideImage = presentationArtifacts.remove(String.format(slideImageKeyFormat, ++i)); |
194 |
|
} |
195 |
|
|
196 |
1 |
return presentationHTML.toString(); |
197 |
|
} |
198 |
|
|
199 |
|
|
200 |
|
|
201 |
|
|
202 |
|
|
203 |
|
@param |
204 |
|
@param |
205 |
|
@return |
206 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (5) |
Complexity: 1 |
Complexity Density: 0.2 |
|
207 |
1 |
protected String cleanPresentationHTML(String dirtyHTML, DocumentReference targetDocumentReference)... |
208 |
|
{ |
209 |
1 |
HTMLCleanerConfiguration configuration = this.officeHTMLCleaner.getDefaultConfiguration(); |
210 |
1 |
configuration.setParameters(Collections.singletonMap("targetDocument", |
211 |
|
this.entityReferenceSerializer.serialize(targetDocumentReference))); |
212 |
1 |
Document xhtmlDocument = this.officeHTMLCleaner.clean(new StringReader(dirtyHTML), configuration); |
213 |
1 |
HTMLUtils.stripHTMLEnvelope(xhtmlDocument); |
214 |
1 |
return HTMLUtils.toString(xhtmlDocument); |
215 |
|
} |
216 |
|
|
217 |
|
|
218 |
|
|
219 |
|
|
220 |
|
@param |
221 |
|
@param |
222 |
|
|
223 |
|
|
224 |
|
@return |
225 |
|
@throws |
226 |
|
|
|
|
| 90.9% |
Uncovered Elements: 1 (11) |
Complexity: 2 |
Complexity Density: 0.18 |
|
227 |
1 |
protected XDOM buildPresentationXDOM(String html, DocumentReference targetDocumentReference)... |
228 |
|
throws OfficeImporterException |
229 |
|
{ |
230 |
1 |
try { |
231 |
1 |
ComponentManager contextComponentManager = this.contextComponentManagerProvider.get(); |
232 |
1 |
String syntaxId = this.documentAccessBridge.getDocument(targetDocumentReference).getSyntax().toIdString(); |
233 |
1 |
BlockRenderer renderer = contextComponentManager.getInstance(BlockRenderer.class, syntaxId); |
234 |
|
|
235 |
1 |
Map<String, String> galleryParameters = Collections.emptyMap(); |
236 |
1 |
ExpandedMacroBlock gallery = |
237 |
|
new ExpandedMacroBlock("gallery", galleryParameters, renderer, false, contextComponentManager); |
238 |
1 |
gallery.addChild(this.xhtmlParser.parse(new StringReader(html))); |
239 |
|
|
240 |
1 |
XDOM xdom = new XDOM(Collections.singletonList((Block) gallery)); |
241 |
|
|
242 |
1 |
xdom.getMetaData().addMetaData(MetaData.BASE, entityReferenceSerializer.serialize(targetDocumentReference)); |
243 |
1 |
return xdom; |
244 |
|
} catch (Exception e) { |
245 |
0 |
throw new OfficeImporterException("Failed to build presentation XDOM.", e); |
246 |
|
} |
247 |
|
} |
248 |
|
} |