1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.wysiwyg.server.internal.plugin.importer; |
21 |
|
|
22 |
|
import java.util.Collections; |
23 |
|
import java.util.HashMap; |
24 |
|
import java.util.Map; |
25 |
|
|
26 |
|
import org.xwiki.bridge.DocumentAccessBridge; |
27 |
|
import org.xwiki.bridge.DocumentModelBridge; |
28 |
|
import org.xwiki.component.manager.ComponentLookupException; |
29 |
|
import org.xwiki.component.manager.ComponentManager; |
30 |
|
import org.xwiki.model.reference.AttachmentReference; |
31 |
|
import org.xwiki.model.reference.EntityReferenceSerializer; |
32 |
|
import org.xwiki.rendering.block.Block; |
33 |
|
import org.xwiki.rendering.block.MacroBlock; |
34 |
|
import org.xwiki.rendering.block.XDOM; |
35 |
|
import org.xwiki.rendering.internal.transformation.MutableRenderingContext; |
36 |
|
import org.xwiki.rendering.listener.MetaData; |
37 |
|
import org.xwiki.rendering.renderer.BlockRenderer; |
38 |
|
import org.xwiki.rendering.renderer.printer.DefaultWikiPrinter; |
39 |
|
import org.xwiki.rendering.renderer.printer.WikiPrinter; |
40 |
|
import org.xwiki.rendering.transformation.RenderingContext; |
41 |
|
import org.xwiki.rendering.transformation.Transformation; |
42 |
|
import org.xwiki.rendering.transformation.TransformationContext; |
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
@version |
49 |
|
|
|
|
| 63.3% |
Uncovered Elements: 11 (30) |
Complexity: 6 |
Complexity Density: 0.24 |
|
50 |
|
public class OfficeMacroImporter |
51 |
|
{ |
52 |
|
|
53 |
|
|
54 |
|
|
55 |
|
private final RenderingContext renderingContext; |
56 |
|
|
57 |
|
|
58 |
|
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
@see |
65 |
|
|
66 |
|
|
67 |
|
private final Transformation macroTransformation; |
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
|
72 |
|
private final BlockRenderer xhtmlRenderer; |
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
|
77 |
|
private final EntityReferenceSerializer<String> entityReferenceSerializer; |
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
|
82 |
|
private final DocumentAccessBridge documentAccessBridge; |
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
|
87 |
|
@param |
88 |
|
|
|
|
| 85.7% |
Uncovered Elements: 1 (7) |
Complexity: 2 |
Complexity Density: 0.29 |
|
89 |
1 |
public OfficeMacroImporter(ComponentManager componentManager)... |
90 |
|
{ |
91 |
1 |
try { |
92 |
1 |
renderingContext = componentManager.getInstance(RenderingContext.class); |
93 |
1 |
macroTransformation = componentManager.getInstance(Transformation.class, "macro"); |
94 |
1 |
xhtmlRenderer = componentManager.getInstance(BlockRenderer.class, "annotatedxhtml/1.0"); |
95 |
1 |
entityReferenceSerializer = componentManager.getInstance(EntityReferenceSerializer.TYPE_STRING); |
96 |
1 |
documentAccessBridge = componentManager.getInstance(DocumentAccessBridge.class); |
97 |
|
} catch (ComponentLookupException e) { |
98 |
0 |
throw new RuntimeException("Failed to initialize the office importer based on office macro.", e); |
99 |
|
} |
100 |
|
} |
101 |
|
|
102 |
|
|
103 |
|
|
104 |
|
|
105 |
|
@param |
106 |
|
@param |
107 |
|
@return |
108 |
|
|
|
|
| 78.6% |
Uncovered Elements: 3 (14) |
Complexity: 3 |
Complexity Density: 0.25 |
|
109 |
1 |
public XDOM buildXDOM(AttachmentReference attachmentReference, boolean filterStyles)... |
110 |
|
{ |
111 |
1 |
Map<String, String> macroParams = new HashMap<String, String>(); |
112 |
1 |
macroParams.put("attachment", attachmentReference.getName()); |
113 |
1 |
if (!filterStyles) { |
114 |
0 |
macroParams.put("filterStyles", "false"); |
115 |
|
} |
116 |
1 |
MacroBlock officeMacro = new MacroBlock("office", macroParams, false); |
117 |
|
|
118 |
1 |
XDOM xdom = new XDOM(Collections.<Block> singletonList(officeMacro)); |
119 |
|
|
120 |
|
|
121 |
|
|
122 |
|
|
123 |
1 |
xdom.getMetaData().addMetaData(MetaData.BASE, |
124 |
|
entityReferenceSerializer.serialize(attachmentReference.getDocumentReference())); |
125 |
|
|
126 |
|
|
127 |
1 |
try { |
128 |
1 |
DocumentModelBridge document = documentAccessBridge.getDocument(attachmentReference.getDocumentReference()); |
129 |
1 |
xdom.getMetaData().addMetaData(MetaData.SYNTAX, document.getSyntax()); |
130 |
|
} catch (Exception e) { |
131 |
0 |
throw new RuntimeException(String.format( |
132 |
|
"Failed to compute Syntax for the document containing attachment [%s]", attachmentReference), e); |
133 |
|
} |
134 |
|
|
135 |
1 |
return xdom; |
136 |
|
} |
137 |
|
|
138 |
|
|
139 |
|
|
140 |
|
|
141 |
|
@param |
142 |
|
@return |
143 |
|
@throws |
144 |
|
|
|
|
| 0% |
Uncovered Elements: 6 (6) |
Complexity: 1 |
Complexity Density: 0.17 |
|
145 |
0 |
public String render(XDOM xdom) throws Exception... |
146 |
|
{ |
147 |
0 |
TransformationContext txContext = new TransformationContext(); |
148 |
0 |
txContext.setXDOM(xdom); |
149 |
0 |
((MutableRenderingContext) renderingContext).transformInContext(macroTransformation, txContext, xdom); |
150 |
|
|
151 |
0 |
WikiPrinter printer = new DefaultWikiPrinter(); |
152 |
0 |
xhtmlRenderer.render(xdom, printer); |
153 |
|
|
154 |
0 |
return printer.toString(); |
155 |
|
} |
156 |
|
} |