1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
|
19 |
|
|
20 |
|
package org.xwiki.rendering.macro.box; |
21 |
|
|
22 |
|
import java.util.Collections; |
23 |
|
import java.util.LinkedHashMap; |
24 |
|
import java.util.List; |
25 |
|
import java.util.Map; |
26 |
|
|
27 |
|
import javax.inject.Inject; |
28 |
|
import javax.inject.Named; |
29 |
|
|
30 |
|
import org.apache.commons.lang3.StringUtils; |
31 |
|
import org.xwiki.rendering.block.Block; |
32 |
|
import org.xwiki.rendering.block.FormatBlock; |
33 |
|
import org.xwiki.rendering.block.GroupBlock; |
34 |
|
import org.xwiki.rendering.block.ImageBlock; |
35 |
|
import org.xwiki.rendering.block.NewLineBlock; |
36 |
|
import org.xwiki.rendering.listener.Format; |
37 |
|
import org.xwiki.rendering.listener.reference.ResourceReference; |
38 |
|
import org.xwiki.rendering.listener.reference.ResourceType; |
39 |
|
import org.xwiki.rendering.macro.AbstractMacro; |
40 |
|
import org.xwiki.rendering.macro.MacroContentParser; |
41 |
|
import org.xwiki.rendering.macro.MacroExecutionException; |
42 |
|
import org.xwiki.rendering.macro.descriptor.ContentDescriptor; |
43 |
|
import org.xwiki.rendering.parser.ResourceReferenceParser; |
44 |
|
import org.xwiki.rendering.transformation.MacroTransformationContext; |
45 |
|
|
46 |
|
|
47 |
|
|
48 |
|
|
49 |
|
@param |
50 |
|
@version |
51 |
|
@since |
52 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (56) |
Complexity: 14 |
Complexity Density: 0.4 |
|
53 |
|
public abstract class AbstractBoxMacro<P extends BoxMacroParameters> extends AbstractMacro<P> |
54 |
|
{ |
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
@Inject |
59 |
|
@Named("image/untyped") |
60 |
|
private ResourceReferenceParser untypedImageReferenceParser; |
61 |
|
|
62 |
|
|
63 |
|
|
64 |
|
|
65 |
|
@Inject |
66 |
|
private MacroContentParser contentParser; |
67 |
|
|
68 |
|
|
69 |
|
|
70 |
|
|
71 |
|
@param |
72 |
|
@param |
73 |
|
@param@link |
74 |
|
@param |
75 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
76 |
53 |
protected AbstractBoxMacro(String name, String description, ContentDescriptor contentDescriptor,... |
77 |
|
Class<?> parametersBeanClass) |
78 |
|
{ |
79 |
53 |
super(name, description, contentDescriptor, parametersBeanClass); |
80 |
|
} |
81 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
82 |
7 |
@Override... |
83 |
|
public boolean supportsInlineMode() |
84 |
|
{ |
85 |
7 |
return true; |
86 |
|
} |
87 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (47) |
Complexity: 10 |
Complexity Density: 0.32 |
|
88 |
195 |
@Override... |
89 |
|
public List<Block> execute(P parameters, String content, MacroTransformationContext context) |
90 |
|
throws MacroExecutionException |
91 |
|
{ |
92 |
|
|
93 |
195 |
ResourceReference imageReference = parameters.getImage(); |
94 |
|
|
95 |
|
|
96 |
195 |
if (imageReference != null && imageReference.getType().equals(ResourceType.UNKNOWN)) { |
97 |
3 |
imageReference = this.untypedImageReferenceParser.parse(imageReference.getReference()); |
98 |
|
} |
99 |
|
|
100 |
195 |
String titleParameter = parameters.getTitle(); |
101 |
195 |
List<? extends Block> titleBlockList = parameters.getBlockTitle(); |
102 |
|
|
103 |
|
|
104 |
|
|
105 |
|
|
106 |
195 |
Map<String, String> boxParameters = new LinkedHashMap<String, String>(); |
107 |
195 |
String classParameter = parameters.getCssClass(); |
108 |
195 |
String cssClass = |
109 |
195 |
StringUtils.isEmpty(classParameter) ? getClassProperty() : getClassProperty() + " " + classParameter; |
110 |
195 |
boxParameters.put("class", cssClass); |
111 |
|
|
112 |
195 |
if (!StringUtils.isEmpty(parameters.getWidth())) { |
113 |
1 |
boxParameters.put("style", "width:" + parameters.getWidth()); |
114 |
|
} |
115 |
|
|
116 |
195 |
Block boxBlock; |
117 |
|
|
118 |
195 |
if (content != null) { |
119 |
194 |
if (context.isInline()) { |
120 |
11 |
List<Block> contentBlocks = parseContent(parameters, content, context); |
121 |
11 |
FormatBlock spanBlock = new FormatBlock(contentBlocks, Format.NONE); |
122 |
11 |
spanBlock.setParameters(boxParameters); |
123 |
11 |
boxBlock = spanBlock; |
124 |
|
} else { |
125 |
183 |
boxBlock = new GroupBlock(boxParameters); |
126 |
|
|
127 |
|
|
128 |
183 |
if (imageReference != null) { |
129 |
5 |
Block imageBlock = new ImageBlock(imageReference, true); |
130 |
5 |
boxBlock.addChild(imageBlock); |
131 |
5 |
boxBlock.addChild(new NewLineBlock()); |
132 |
|
} |
133 |
|
|
134 |
183 |
if (!StringUtils.isEmpty(titleParameter)) { |
135 |
|
|
136 |
133 |
boxBlock.addChildren(this.contentParser.parse(titleParameter, context, false, true).getChildren()); |
137 |
|
} |
138 |
183 |
if (titleBlockList != null) { |
139 |
4 |
boxBlock.addChildren(titleBlockList); |
140 |
|
} |
141 |
183 |
List<Block> contentBlocks = parseContent(parameters, content, context); |
142 |
183 |
boxBlock.addChildren(contentBlocks); |
143 |
|
} |
144 |
|
|
145 |
194 |
return Collections.singletonList(boxBlock); |
146 |
|
} else { |
147 |
1 |
return Collections.emptyList(); |
148 |
|
} |
149 |
|
} |
150 |
|
|
151 |
|
|
152 |
|
|
153 |
|
@link |
154 |
|
|
155 |
|
|
156 |
|
@param |
157 |
|
@param |
158 |
|
@param |
159 |
|
@return |
160 |
|
@throws |
161 |
|
|
162 |
|
protected abstract List<Block> parseContent(P parameters, String content, MacroTransformationContext context) |
163 |
|
throws MacroExecutionException; |
164 |
|
|
165 |
|
|
166 |
|
@return |
167 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
168 |
195 |
protected String getClassProperty()... |
169 |
|
{ |
170 |
195 |
return "box"; |
171 |
|
} |
172 |
|
|
173 |
|
|
174 |
|
@return |
175 |
|
|
|
|
| 100% |
Uncovered Elements: 0 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
176 |
176 |
protected MacroContentParser getMacroContentParser()... |
177 |
|
{ |
178 |
176 |
return this.contentParser; |
179 |
|
} |
180 |
|
} |