|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
AbstractBoxMacro | Line # 53 | 35 | 0% | 14 | 6 | 89.3% |
0.89285713
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
(16) | |||
Result | |||
0.73214287
|
org.xwiki.rendering.test.integration.RenderingTest.execute
![]() |
1 PASS | |
0.73214287
|
org.xwiki.rendering.test.integration.RenderingTest.execute
![]() |
1 PASS | |
0.73214287
|
org.xwiki.rendering.test.integration.RenderingTest.execute
![]() |
1 PASS | |
0.6785714
|
org.xwiki.rendering.test.integration.RenderingTest.execute
![]() |
1 PASS | |
0.6785714
|
org.xwiki.rendering.test.integration.RenderingTest.execute
![]() |
1 PASS | |
0.6785714
|
org.xwiki.rendering.test.integration.RenderingTest.execute
![]() |
1 PASS | |
0.625
|
org.xwiki.rendering.test.integration.RenderingTest.execute
![]() |
1 PASS | |
0.625
|
org.xwiki.rendering.test.integration.RenderingTest.execute
![]() |
1 PASS | |
0.625
|
org.xwiki.rendering.test.integration.RenderingTest.execute
![]() |
1 PASS | |
0.625
|
org.xwiki.rendering.test.integration.RenderingTest.execute
![]() |
1 PASS | |
0.625
|
org.xwiki.rendering.test.integration.RenderingTest.execute
![]() |
1 PASS | |
0.60714287
|
org.xwiki.rendering.test.integration.RenderingTest.execute
![]() |
1 PASS | |
0.5714286
|
org.xwiki.rendering.test.integration.RenderingTest.execute
![]() |
1 PASS | |
0.5714286
|
org.xwiki.rendering.test.integration.RenderingTest.execute
![]() |
1 PASS | |
0.5535714
|
org.xwiki.rendering.test.integration.RenderingTest.execute
![]() |
1 PASS | |
0.5535714
|
org.xwiki.rendering.test.integration.RenderingTest.execute
![]() |
1 PASS | |
1 | /* | |
2 | * See the NOTICE file distributed with this work for additional | |
3 | * information regarding copyright ownership. | |
4 | * | |
5 | * This is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU Lesser General Public License as | |
7 | * published by the Free Software Foundation; either version 2.1 of | |
8 | * the License, or (at your option) any later version. | |
9 | * | |
10 | * This software is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * Lesser General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU Lesser General Public | |
16 | * License along with this software; if not, write to the Free | |
17 | * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | |
18 | * 02110-1301 USA, or see the FSF site: http://www.fsf.org. | |
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 | * Draw a box around provided content. | |
48 | * | |
49 | * @param <P> the type of macro parameters bean. | |
50 | * @version $Id: a21d9f81699beba5a13fb04dfd7a7bfc4b26c066 $ | |
51 | * @since 1.7 | |
52 | */ | |
53 | public abstract class AbstractBoxMacro<P extends BoxMacroParameters> extends AbstractMacro<P> | |
54 | { | |
55 | /** | |
56 | * Parses untyped image references. | |
57 | */ | |
58 | @Inject | |
59 | @Named("image/untyped") | |
60 | private ResourceReferenceParser untypedImageReferenceParser; | |
61 | ||
62 | /** | |
63 | * The parser used to parse box content and box title parameter. | |
64 | */ | |
65 | @Inject | |
66 | private MacroContentParser contentParser; | |
67 | ||
68 | /** | |
69 | * Creates a new box macro. | |
70 | * | |
71 | * @param name the name of the macro | |
72 | * @param description string describing this macro. | |
73 | * @param contentDescriptor the {@link ContentDescriptor} describing the content of this macro. | |
74 | * @param parametersBeanClass class of the parameters bean. | |
75 | */ | |
76 | 17 |
![]() |
77 | Class< ? > parametersBeanClass) | |
78 | { | |
79 | 17 | super(name, description, contentDescriptor, parametersBeanClass); |
80 | } | |
81 | ||
82 | 5 |
![]() |
83 | public boolean supportsInlineMode() | |
84 | { | |
85 | 5 | return true; |
86 | } | |
87 | ||
88 | 19 |
![]() |
89 | public List<Block> execute(P parameters, String content, MacroTransformationContext context) | |
90 | throws MacroExecutionException | |
91 | { | |
92 | // TODO: Refactor this when it'll possible to have a specific converter associated to a macro parameter. | |
93 | 19 | ResourceReference imageReference = parameters.getImage(); |
94 | // If the image reference is unknown then resolve it with the untyped resource reference parser | |
95 | // (this happens when the user doesn't specify a type for the image reference). | |
96 | 19 | if (imageReference != null && imageReference.getType().equals(ResourceType.UNKNOWN)) { |
97 | 0 | imageReference = this.untypedImageReferenceParser.parse(imageReference.getReference()); |
98 | } | |
99 | ||
100 | 19 | String titleParameter = parameters.getTitle(); |
101 | 19 | List< ? extends Block> titleBlockList = parameters.getBlockTitle(); |
102 | ||
103 | // Use a linked hashmap to keep the parameters in the same order as we create them when they are retrieved | |
104 | // by renderers. This is useful for example in the Event renderer to control the order in which the params | |
105 | // are displayed. | |
106 | 19 | Map<String, String> boxParameters = new LinkedHashMap<String, String>(); |
107 | 19 | String classParameter = parameters.getCssClass(); |
108 | 19 | String cssClass = |
109 | 19 | StringUtils.isEmpty(classParameter) ? getClassProperty() : getClassProperty() + " " + classParameter; |
110 | 19 | boxParameters.put("class", cssClass); |
111 | ||
112 | 19 | if (!StringUtils.isEmpty(parameters.getWidth())) { |
113 | 1 | boxParameters.put("style", "width:" + parameters.getWidth()); |
114 | } | |
115 | ||
116 | 19 | Block boxBlock; |
117 | ||
118 | 19 | if (content != null) { |
119 | 19 | if (context.isInline()) { |
120 | 5 | List<Block> contentBlocks = parseContent(parameters, content, context); |
121 | 5 | FormatBlock spanBlock = new FormatBlock(contentBlocks, Format.NONE); |
122 | 5 | spanBlock.setParameters(boxParameters); |
123 | 5 | boxBlock = spanBlock; |
124 | } else { | |
125 | 14 | boxBlock = new GroupBlock(boxParameters); |
126 | ||
127 | // we add the image, if there is one | |
128 | 14 | if (imageReference != null) { |
129 | 3 | Block imageBlock = new ImageBlock(imageReference, true); |
130 | 3 | boxBlock.addChild(imageBlock); |
131 | 3 | boxBlock.addChild(new NewLineBlock()); |
132 | } | |
133 | // we add the title, if there is one | |
134 | 14 | if (!StringUtils.isEmpty(titleParameter)) { |
135 | // Don't execute transformations explicitly. They'll be executed on the generated content later on. | |
136 | 7 | boxBlock.addChildren(this.contentParser.parse(titleParameter, context, false, true).getChildren()); |
137 | } | |
138 | 14 | if (titleBlockList != null) { |
139 | 0 | boxBlock.addChildren(titleBlockList); |
140 | } | |
141 | 14 | List<Block> contentBlocks = parseContent(parameters, content, context); |
142 | 14 | boxBlock.addChildren(contentBlocks); |
143 | } | |
144 | ||
145 | 19 | return Collections.singletonList(boxBlock); |
146 | } else { | |
147 | 0 | return Collections.emptyList(); |
148 | } | |
149 | } | |
150 | ||
151 | /** | |
152 | * Execute macro content and return the result. This methods is separated form | |
153 | * {@link #execute(BoxMacroParameters, String, MacroTransformationContext)} to be able to overwrite it in macro | |
154 | * which need boxes. | |
155 | * | |
156 | * @param parameters the parameters of the macro. | |
157 | * @param content the content of the macro. | |
158 | * @param context the context if the macros transformation. | |
159 | * @return the result of the macro execution. | |
160 | * @throws MacroExecutionException error when executing the macro. | |
161 | */ | |
162 | protected abstract List<Block> parseContent(P parameters, String content, MacroTransformationContext context) | |
163 | throws MacroExecutionException; | |
164 | ||
165 | /** | |
166 | * @return the name of the CSS class to use when rendering, in case no cssClass parameter is specified. | |
167 | */ | |
168 | 19 |
![]() |
169 | { | |
170 | 19 | return "box"; |
171 | } | |
172 | ||
173 | /** | |
174 | * @return the macro content parser to use to parse content in wiki syntax | |
175 | */ | |
176 | 17 |
![]() |
177 | { | |
178 | 17 | return this.contentParser; |
179 | } | |
180 | } |
|