1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.rendering.internal.macro.chart.source.table; |
21 |
|
|
22 |
|
import java.util.List; |
23 |
|
|
24 |
|
import javax.inject.Inject; |
25 |
|
import javax.inject.Named; |
26 |
|
|
27 |
|
import org.slf4j.Logger; |
28 |
|
import org.xwiki.bridge.DocumentAccessBridge; |
29 |
|
import org.xwiki.bridge.DocumentModelBridge; |
30 |
|
import org.xwiki.component.annotation.Component; |
31 |
|
import org.xwiki.component.annotation.InstantiationStrategy; |
32 |
|
import org.xwiki.display.internal.DocumentDisplayer; |
33 |
|
import org.xwiki.display.internal.DocumentDisplayerParameters; |
34 |
|
import org.xwiki.model.reference.DocumentReference; |
35 |
|
import org.xwiki.model.reference.DocumentReferenceResolver; |
36 |
|
import org.xwiki.rendering.block.Block; |
37 |
|
import org.xwiki.rendering.block.MacroBlock; |
38 |
|
import org.xwiki.rendering.block.MetaDataBlock; |
39 |
|
import org.xwiki.rendering.block.TableBlock; |
40 |
|
import org.xwiki.rendering.block.XDOM; |
41 |
|
import org.xwiki.rendering.block.match.ClassBlockMatcher; |
42 |
|
import org.xwiki.rendering.block.match.MetadataBlockMatcher; |
43 |
|
import org.xwiki.rendering.listener.MetaData; |
44 |
|
import org.xwiki.rendering.macro.MacroExecutionException; |
45 |
|
import org.xwiki.rendering.transformation.MacroTransformationContext; |
46 |
|
import org.xwiki.security.authorization.AuthorizationManager; |
47 |
|
import org.xwiki.security.authorization.Right; |
48 |
|
|
49 |
|
import static org.xwiki.component.descriptor.ComponentInstantiationStrategy.PER_LOOKUP; |
50 |
|
|
51 |
|
|
52 |
|
@link |
53 |
|
|
54 |
|
@version |
55 |
|
@since |
56 |
|
|
57 |
|
@Component |
58 |
|
@Named("xdom") |
59 |
|
@InstantiationStrategy(PER_LOOKUP) |
|
|
| 90.4% |
Uncovered Elements: 8 (83) |
Complexity: 21 |
Complexity Density: 0.41 |
|
60 |
|
public class DocumentTableBlockDataSource extends AbstractTableBlockDataSource |
61 |
|
{ |
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
private static final String DOCUMENT_PARAM = "document"; |
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
private static final String TABLE_PARAM = "table"; |
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
|
75 |
|
|
76 |
|
private DocumentReference documentReference; |
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
|
81 |
|
private String tableId; |
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
@Inject |
87 |
|
private Logger logger; |
88 |
|
|
89 |
|
|
90 |
|
@link |
91 |
|
|
92 |
|
@Inject |
93 |
|
private DocumentDisplayer documentDisplayer; |
94 |
|
|
95 |
|
|
96 |
|
@link |
97 |
|
|
98 |
|
@Inject |
99 |
|
private DocumentAccessBridge docBridge; |
100 |
|
|
101 |
|
|
102 |
|
|
103 |
|
|
104 |
|
@Inject |
105 |
|
private DocumentReferenceResolver<String> documentReferenceResolver; |
106 |
|
|
107 |
|
|
108 |
|
|
109 |
|
|
110 |
|
@Inject |
111 |
|
private AuthorizationManager authorizationManager; |
112 |
|
|
|
|
| 86.4% |
Uncovered Elements: 3 (22) |
Complexity: 6 |
Complexity Density: 0.43 |
|
113 |
10 |
@Override... |
114 |
|
protected TableBlock getTableBlock(String macroContent, MacroTransformationContext context) |
115 |
|
throws MacroExecutionException |
116 |
|
{ |
117 |
10 |
XDOM xdom = computeXDOM(context); |
118 |
|
|
119 |
|
|
120 |
10 |
List<TableBlock> tableBlocks = xdom.getBlocks(new ClassBlockMatcher(TableBlock.class), Block.Axes.DESCENDANT); |
121 |
10 |
TableBlock result = null; |
122 |
10 |
this.logger.debug("Table id is [{}], there are [{}] tables in the document [{}]", |
123 |
|
new Object[]{this.tableId, tableBlocks.size(), this.documentReference}); |
124 |
10 |
if (null != tableId) { |
125 |
1 |
for (TableBlock tableBlock : tableBlocks) { |
126 |
2 |
String id = tableBlock.getParameter("id"); |
127 |
2 |
if (null != id && id.equals(this.tableId)) { |
128 |
1 |
result = tableBlock; |
129 |
1 |
break; |
130 |
|
} |
131 |
|
} |
132 |
|
} else { |
133 |
9 |
result = (tableBlocks.size() > 0) ? tableBlocks.get(0) : null; |
134 |
|
} |
135 |
|
|
136 |
10 |
if (null == result) { |
137 |
0 |
throw new MacroExecutionException("Unable to find a matching data table."); |
138 |
|
} |
139 |
|
|
140 |
10 |
return result; |
141 |
|
} |
142 |
|
|
143 |
|
|
144 |
|
|
145 |
|
|
146 |
|
@param |
147 |
|
@return |
148 |
|
@throws |
149 |
|
|
|
|
| 92.3% |
Uncovered Elements: 1 (13) |
Complexity: 3 |
Complexity Density: 0.27 |
|
150 |
10 |
private XDOM computeXDOM(MacroTransformationContext context) throws MacroExecutionException... |
151 |
|
{ |
152 |
10 |
XDOM xdom; |
153 |
|
|
154 |
|
|
155 |
|
|
156 |
|
|
157 |
10 |
if (isDefinedChartSourceTheCurrentDocument(context.getCurrentMacroBlock())) { |
158 |
2 |
xdom = context.getXDOM(); |
159 |
|
} else { |
160 |
8 |
try { |
161 |
8 |
DocumentModelBridge document = this.docBridge.getDocument(this.documentReference); |
162 |
8 |
DocumentDisplayerParameters parameters = new DocumentDisplayerParameters(); |
163 |
8 |
parameters.setContentTranslated(true); |
164 |
8 |
parameters.setTargetSyntax(context.getTransformationContext().getTargetSyntax()); |
165 |
8 |
xdom = this.documentDisplayer.display(document, parameters); |
166 |
|
} catch (Exception e) { |
167 |
0 |
throw new MacroExecutionException(String.format("Error getting Chart table from document [%s]", |
168 |
|
this.documentReference, e)); |
169 |
|
} |
170 |
|
} |
171 |
10 |
return xdom; |
172 |
|
} |
173 |
|
|
174 |
|
|
175 |
|
@param |
176 |
|
@return |
177 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (12) |
Complexity: 3 |
Complexity Density: 0.38 |
|
178 |
11 |
protected boolean isDefinedChartSourceTheCurrentDocument(MacroBlock currentMacroBlock)... |
179 |
|
{ |
180 |
11 |
boolean result; |
181 |
11 |
if (this.documentReference == null) { |
182 |
2 |
result = true; |
183 |
|
} else { |
184 |
9 |
String sourceReference = extractSourceContentReference(currentMacroBlock); |
185 |
9 |
if (this.documentReferenceResolver.resolve(sourceReference, |
186 |
|
this.docBridge.getCurrentDocumentReference()).equals(this.documentReference)) |
187 |
|
{ |
188 |
1 |
result = true; |
189 |
|
} else { |
190 |
8 |
result = false; |
191 |
|
} |
192 |
|
} |
193 |
|
|
194 |
11 |
return result; |
195 |
|
} |
196 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (11) |
Complexity: 3 |
Complexity Density: 0.43 |
|
197 |
82 |
@Override... |
198 |
|
protected boolean setParameter(String key, String value) throws MacroExecutionException |
199 |
|
{ |
200 |
82 |
if (DOCUMENT_PARAM.equals(key)) { |
201 |
9 |
this.documentReference |
202 |
|
= this.documentReferenceResolver.resolve(value, docBridge.getCurrentDocumentReference()); |
203 |
9 |
return true; |
204 |
|
} |
205 |
|
|
206 |
73 |
if (TABLE_PARAM.equals(key)) { |
207 |
1 |
this.tableId = value; |
208 |
1 |
return true; |
209 |
|
} |
210 |
|
|
211 |
72 |
return super.setParameter(key, value); |
212 |
|
} |
213 |
|
|
|
|
| 66.7% |
Uncovered Elements: 4 (12) |
Complexity: 4 |
Complexity Density: 0.67 |
|
214 |
10 |
@Override... |
215 |
|
protected void validateParameters() throws MacroExecutionException |
216 |
|
{ |
217 |
10 |
super.validateParameters(); |
218 |
|
|
219 |
10 |
if (this.documentReference != null) { |
220 |
8 |
if (!authorizationManager.hasAccess(Right.VIEW, this.docBridge.getCurrentUserReference(), |
221 |
|
this.documentReference)) |
222 |
|
{ |
223 |
0 |
throw new MacroExecutionException("You do not have permission to view the document."); |
224 |
|
} |
225 |
|
|
226 |
8 |
if (!this.docBridge.exists(this.documentReference)) { |
227 |
0 |
throw new MacroExecutionException( |
228 |
|
String.format("Document [%s] does not exist.", this.documentReference)); |
229 |
|
} |
230 |
|
} |
231 |
|
} |
232 |
|
|
233 |
|
|
234 |
|
@param |
235 |
|
@return |
236 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (7) |
Complexity: 2 |
Complexity Density: 0.4 |
|
237 |
9 |
private String extractSourceContentReference(Block source)... |
238 |
|
{ |
239 |
9 |
String contentSource = null; |
240 |
9 |
MetaDataBlock metaDataBlock = |
241 |
|
source.getFirstBlock(new MetadataBlockMatcher(MetaData.SOURCE), Block.Axes.ANCESTOR); |
242 |
9 |
if (metaDataBlock != null) { |
243 |
8 |
contentSource = (String) metaDataBlock.getMetaData().getMetaData(MetaData.SOURCE); |
244 |
|
} |
245 |
9 |
return contentSource; |
246 |
|
} |
247 |
|
} |