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

File AbstractContainerMacro.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart7.png
64% of files have more coverage

Code metrics

6
17
5
1
148
64
9
0.53
3.4
5
1.8

Classes

Class Line # Actions
AbstractContainerMacro 49 17 0% 9 10
0.6428571364.3%
 

Contributing tests

This file is covered by 2 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.container;
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   
29    import org.apache.commons.lang3.StringUtils;
30    import org.xwiki.component.manager.ComponentLookupException;
31    import org.xwiki.component.manager.ComponentManager;
32    import org.xwiki.rendering.block.Block;
33    import org.xwiki.rendering.block.GroupBlock;
34    import org.xwiki.rendering.macro.AbstractMacro;
35    import org.xwiki.rendering.macro.MacroExecutionException;
36    import org.xwiki.rendering.macro.descriptor.ContentDescriptor;
37    import org.xwiki.rendering.transformation.MacroTransformationContext;
38   
39    /**
40    * Abstract container macro to hold a list groups and style them together, for example laying them out as indicated by
41    * the styleLayout parameter. For the moment this macro handles only the layouting, and only the columns layout. When it
42    * will be enhanced with other layout styles, it should be split in multiple classes, one to handle each. This is
43    * abstract to allow subclasses to provide the content of the macro.
44    *
45    * @param <P> the macro parameters bean
46    * @version $Id: 2b01f65d33aa2d397b5fa0a79969e48d7d5f3667 $
47    * @since 3.0M1
48    */
 
49    public abstract class AbstractContainerMacro<P extends ContainerMacroParameters> extends AbstractMacro<P>
50    {
51    /**
52    * The name of the parameter to convey style information to the HTML (html style attribute).
53    */
54    private static final String CLASS_ATTRIBUTE = "class";
55   
56    /**
57    * The component manager used to dynamically fetch components (syntax parsers, in this case).
58    */
59    @Inject
60    private ComponentManager componentManager;
61   
62    /**
63    * Builds a container macro.
64    *
65    * @param name the name of the macro
66    * @param description the description of the macro
67    * @param contentDescriptor the descriptor of the content of this macro
68    * @param parametersBeanClass the type of parameters of this macro
69    */
 
70  3 toggle protected AbstractContainerMacro(String name, String description, ContentDescriptor contentDescriptor,
71    Class< ? > parametersBeanClass)
72    {
73  3 super(name, description, contentDescriptor, parametersBeanClass);
74    }
75   
 
76  3 toggle @Override
77    public List<Block> execute(P parameters, String content, MacroTransformationContext context)
78    throws MacroExecutionException
79    {
80    // TODO: include here container CSS. FTM the only rule, the one about justified text, is in the columns.css, in
81    // which case the justification makes the most sense.
82    // transform the container in a group, with appropriate parameters
83  3 Map<String, String> containerParameters = new HashMap<String, String>();
84  3 if (parameters.isJustify()) {
85  0 containerParameters.put(CLASS_ATTRIBUTE, "container-justified");
86    }
87   
88    // create the root block for the container macro, as a group block, and add all the blocks resulted from parsing
89    // its content
90  3 GroupBlock containerRoot = new GroupBlock(containerParameters);
91  3 containerRoot.addChildren(getContent(parameters, content, context));
92   
93    // grab the layout manager to layout this container
94  3 LayoutManager layoutManager = getLayoutManager(parameters.getLayoutStyle());
95    // if a suitable layout manager was found, layout this container
96  3 if (layoutManager != null) {
97  3 layoutManager.layoutContainer(containerRoot);
98    }
99   
100    // add the css class, if any, to the container root
101  3 if (StringUtils.isNotEmpty(parameters.getCssClass())) {
102  0 containerRoot.setParameter(CLASS_ATTRIBUTE, parameters.getCssClass());
103    }
104   
105    // and finally return the styled container root
106  3 return Collections.<Block> singletonList(containerRoot);
107    }
108   
109    /**
110    * @param layoutStyle the style passed to the container component
111    * @return the layout manager to do the layouting according to the specified layout style
112    */
 
113  3 toggle protected LayoutManager getLayoutManager(String layoutStyle)
114    {
115  3 try {
116  3 return getComponentManager().getInstance(LayoutManager.class, layoutStyle);
117    } catch (ComponentLookupException e) {
118    // TODO: maybe should log?
119  0 return null;
120    }
121    }
122   
123    /**
124    * Returns the content of this macro, as blocks, either after parsing the passed {@code content} or by other means.
125    *
126    * @param parameters the parameters of this macro
127    * @param content the content of the macro
128    * @param context the macro transformation context
129    * @return a list of blocks representing the content of this macro, as blocks
130    * @throws MacroExecutionException in case anything wrong happens during parsing the content
131    */
132    protected abstract List<Block> getContent(P parameters, String content, MacroTransformationContext context)
133    throws MacroExecutionException;
134   
135    /**
136    * @return the componentManager
137    */
 
138  0 toggle protected ComponentManager getComponentManager()
139    {
140  0 return this.componentManager;
141    }
142   
 
143  0 toggle @Override
144    public boolean supportsInlineMode()
145    {
146  0 return false;
147    }
148    }