1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.rendering.macro.box

File AbstractBoxMacro.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

16
35
5
1
180
96
14
0.4
7
5
2.8

Classes

Class Line # Actions
AbstractBoxMacro 53 35 0% 14 0
1.0100%
 

Contributing tests

This file is covered by 38 tests. .

Source view

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: c1fcf6b776bd34880a3758e72a643e7b4eb89448 $
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  53 toggle protected AbstractBoxMacro(String name, String description, ContentDescriptor contentDescriptor,
77    Class<?> parametersBeanClass)
78    {
79  53 super(name, description, contentDescriptor, parametersBeanClass);
80    }
81   
 
82  7 toggle @Override
83    public boolean supportsInlineMode()
84    {
85  7 return true;
86    }
87   
 
88  195 toggle @Override
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  195 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  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    // 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  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    // we add the image, if there is one
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    // we add the title, if there is one
134  183 if (!StringUtils.isEmpty(titleParameter)) {
135    // Don't execute transformations explicitly. They'll be executed on the generated content later on.
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    * 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  195 toggle protected String getClassProperty()
169    {
170  195 return "box";
171    }
172   
173    /**
174    * @return the macro content parser to use to parse content in wiki syntax
175    */
 
176  176 toggle protected MacroContentParser getMacroContentParser()
177    {
178  176 return this.contentParser;
179    }
180    }