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; |
21 |
|
|
22 |
|
import java.util.Collections; |
23 |
|
import java.util.HashMap; |
24 |
|
import java.util.List; |
25 |
|
import java.util.Map; |
26 |
|
|
27 |
|
import javax.inject.Inject; |
28 |
|
import javax.inject.Named; |
29 |
|
import javax.inject.Singleton; |
30 |
|
|
31 |
|
import org.apache.commons.lang3.StringUtils; |
32 |
|
import org.xwiki.chart.ChartGenerator; |
33 |
|
import org.xwiki.chart.ChartGeneratorException; |
34 |
|
import org.xwiki.component.annotation.Component; |
35 |
|
import org.xwiki.component.manager.ComponentLookupException; |
36 |
|
import org.xwiki.component.manager.ComponentManager; |
37 |
|
import org.xwiki.rendering.block.Block; |
38 |
|
import org.xwiki.rendering.block.ImageBlock; |
39 |
|
import org.xwiki.rendering.block.LinkBlock; |
40 |
|
import org.xwiki.rendering.block.ParagraphBlock; |
41 |
|
import org.xwiki.rendering.internal.macro.chart.source.DataSource; |
42 |
|
import org.xwiki.rendering.listener.reference.ResourceReference; |
43 |
|
import org.xwiki.rendering.listener.reference.ResourceType; |
44 |
|
import org.xwiki.rendering.macro.AbstractMacro; |
45 |
|
import org.xwiki.rendering.macro.MacroExecutionException; |
46 |
|
import org.xwiki.rendering.macro.chart.ChartMacroParameters; |
47 |
|
import org.xwiki.rendering.macro.descriptor.DefaultContentDescriptor; |
48 |
|
import org.xwiki.rendering.transformation.MacroTransformationContext; |
49 |
|
|
50 |
|
|
51 |
|
|
52 |
|
|
53 |
|
@version |
54 |
|
@since |
55 |
|
|
56 |
|
@Component |
57 |
|
@Named("chart") |
58 |
|
@Singleton |
|
|
| 93.8% |
Uncovered Elements: 4 (65) |
Complexity: 13 |
Complexity Density: 0.27 |
|
59 |
|
public class ChartMacro extends AbstractMacro<ChartMacroParameters> |
60 |
|
{ |
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
private static final String DESCRIPTION = "Displays a graphical chart generated from miscellaneous data sources"; |
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
|
69 |
|
private static final String CONTENT_DESCRIPTION = "Input data for the chart macro (Ex. for 'inline' source mode)"; |
70 |
|
|
71 |
|
|
72 |
|
|
73 |
|
|
74 |
|
@Inject |
75 |
|
private ChartGenerator chartGenerator; |
76 |
|
|
77 |
|
|
78 |
|
|
79 |
|
|
80 |
|
@Inject |
81 |
|
private ComponentManager componentManager; |
82 |
|
|
83 |
|
|
84 |
|
|
85 |
|
|
86 |
|
@Inject |
87 |
|
@Named("tmp") |
88 |
|
private ChartImageWriter imageWriter; |
89 |
|
|
90 |
|
|
91 |
|
|
92 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (2) |
Complexity: 1 |
Complexity Density: 0.5 |
|
93 |
9 |
public ChartMacro()... |
94 |
|
{ |
95 |
9 |
super("Chart", DESCRIPTION, new DefaultContentDescriptor(CONTENT_DESCRIPTION, false), |
96 |
|
ChartMacroParameters.class); |
97 |
9 |
setDefaultCategory(DEFAULT_CATEGORY_CONTENT); |
98 |
|
} |
99 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
100 |
1 |
@Override... |
101 |
|
public boolean supportsInlineMode() |
102 |
|
{ |
103 |
1 |
return true; |
104 |
|
} |
105 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (15) |
Complexity: 2 |
Complexity Density: 0.15 |
|
106 |
15 |
@Override... |
107 |
|
public List<Block> execute(ChartMacroParameters macroParams, String content, MacroTransformationContext context) |
108 |
|
throws MacroExecutionException |
109 |
|
{ |
110 |
|
|
111 |
15 |
generateChart(macroParams, content, context); |
112 |
|
|
113 |
15 |
String imageLocation = this.imageWriter.getURL(new ImageId(macroParams)); |
114 |
15 |
String title = macroParams.getTitle(); |
115 |
15 |
ResourceReference reference = new ResourceReference(imageLocation, ResourceType.URL); |
116 |
15 |
ImageBlock imageBlock = new ImageBlock(new ResourceReference(imageLocation, ResourceType.URL), true); |
117 |
15 |
imageBlock.setParameter("alt", title); |
118 |
15 |
LinkBlock linkBlock = new LinkBlock(Collections.singletonList((Block) imageBlock), reference, true); |
119 |
15 |
linkBlock.setParameter("title", title); |
120 |
|
|
121 |
|
|
122 |
15 |
Block resultBlock; |
123 |
15 |
if (context.isInline()) { |
124 |
1 |
resultBlock = linkBlock; |
125 |
|
} else { |
126 |
14 |
resultBlock = new ParagraphBlock(Collections.singletonList((Block) linkBlock)); |
127 |
|
} |
128 |
|
|
129 |
15 |
return Collections.singletonList(resultBlock); |
130 |
|
} |
131 |
|
|
132 |
|
|
133 |
|
|
134 |
|
|
135 |
|
@param |
136 |
|
@param |
137 |
|
@param |
138 |
|
@throws |
139 |
|
|
|
|
| 80% |
Uncovered Elements: 2 (10) |
Complexity: 3 |
Complexity Density: 0.3 |
|
140 |
15 |
private void generateChart(ChartMacroParameters parameters, String content, MacroTransformationContext context)... |
141 |
|
throws MacroExecutionException |
142 |
|
{ |
143 |
15 |
String source = computeSource(parameters.getSource(), content); |
144 |
|
|
145 |
15 |
DataSource dataSource; |
146 |
15 |
try { |
147 |
15 |
dataSource = this.componentManager.getInstance(DataSource.class, source); |
148 |
|
} catch (ComponentLookupException e) { |
149 |
0 |
throw new MacroExecutionException(String.format("Invalid source parameter [%s]", |
150 |
|
parameters.getSource()), e); |
151 |
|
} |
152 |
|
|
153 |
15 |
Map<String, String> sourceParameters = getSourceParameters(parameters, source); |
154 |
|
|
155 |
15 |
dataSource.buildDataset(content, sourceParameters, context); |
156 |
|
|
157 |
15 |
try { |
158 |
15 |
this.imageWriter.writeImage(new ImageId(parameters), |
159 |
|
this.chartGenerator.generate(dataSource.getChartModel(), sourceParameters)); |
160 |
|
} catch (ChartGeneratorException e) { |
161 |
0 |
throw new MacroExecutionException("Error while rendering chart", e); |
162 |
|
} |
163 |
|
} |
164 |
|
|
165 |
|
|
166 |
|
|
167 |
|
|
168 |
|
|
169 |
|
@param |
170 |
|
@param |
171 |
|
@return@link |
172 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (10) |
Complexity: 3 |
Complexity Density: 0.5 |
|
173 |
15 |
private String computeSource(String userDefinedSource, String content)... |
174 |
|
{ |
175 |
15 |
String source = userDefinedSource; |
176 |
15 |
if (source == null) { |
177 |
6 |
if (StringUtils.isEmpty(content)) { |
178 |
2 |
source = "xdom"; |
179 |
|
} else { |
180 |
4 |
source = "inline"; |
181 |
|
} |
182 |
|
} |
183 |
15 |
return source; |
184 |
|
} |
185 |
|
|
186 |
|
|
187 |
|
|
188 |
|
|
189 |
|
@param |
190 |
|
@param |
191 |
|
@return |
192 |
|
|
|
|
| 90.5% |
Uncovered Elements: 2 (21) |
Complexity: 3 |
Complexity Density: 0.18 |
|
193 |
15 |
private Map<String, String> getSourceParameters(ChartMacroParameters chartMacroParameters, String sourceHint)... |
194 |
|
{ |
195 |
15 |
Map<String, String> parameters = new HashMap<String, String>(); |
196 |
15 |
parameters.put(ChartGenerator.TITLE_PARAM, chartMacroParameters.getTitle()); |
197 |
15 |
parameters.put(ChartGenerator.WIDTH_PARAM, String.valueOf(chartMacroParameters.getWidth())); |
198 |
15 |
parameters.put(ChartGenerator.HEIGHT_PARAM, String.valueOf(chartMacroParameters.getHeight())); |
199 |
15 |
parameters.put(ChartGenerator.TYPE_PARAM, chartMacroParameters.getType()); |
200 |
15 |
parameters.put(DataSource.SOURCE_PARAM, sourceHint); |
201 |
15 |
parameters.put(DataSource.PARAMS_PARAM, chartMacroParameters.getParams()); |
202 |
|
|
203 |
15 |
String sourceParameters = chartMacroParameters.getParams(); |
204 |
|
|
205 |
15 |
if (null != sourceParameters) { |
206 |
12 |
String[] segments = sourceParameters.split(";"); |
207 |
12 |
for (String segment : segments) { |
208 |
57 |
String[] keyValue = segment.split(":", 2); |
209 |
57 |
String key = StringUtils.trim(keyValue[0]); |
210 |
57 |
if (keyValue.length == 2) { |
211 |
57 |
parameters.put(key, keyValue[1]); |
212 |
|
} else { |
213 |
0 |
parameters.put(key, null); |
214 |
|
} |
215 |
|
} |
216 |
|
} |
217 |
|
|
218 |
15 |
return parameters; |
219 |
|
} |
220 |
|
} |