Clover Coverage Report - XWiki Rendering - Parent POM 4.0-SNAPSHOT (Aggregated)
Coverage timestamp: Mon Mar 12 2012 18:03:13 CET
../../../../img/srcFileCovDistChart8.png 68% of files have more coverage
33   267   22   2.54
10   111   0.67   13
13     1.69  
1    
 
  AbstractMacro       Line # 42 33 0% 22 13 76.8% 0.76785713
 
  (104)
 
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;
21   
22    import javax.inject.Inject;
23    import javax.inject.Named;
24   
25    import org.apache.commons.lang3.StringUtils;
26    import org.xwiki.component.annotation.Component;
27    import org.xwiki.component.phase.Initializable;
28    import org.xwiki.component.phase.InitializationException;
29    import org.xwiki.properties.BeanManager;
30    import org.xwiki.rendering.macro.descriptor.AbstractMacroDescriptor;
31    import org.xwiki.rendering.macro.descriptor.ContentDescriptor;
32    import org.xwiki.rendering.macro.descriptor.DefaultMacroDescriptor;
33    import org.xwiki.rendering.macro.descriptor.MacroDescriptor;
34   
35    /**
36    * Helper to implement Macro, providing some default implementation. We recommend Macro writers to extend this class.
37    *
38    * @param <P> the type of the macro parameters bean
39    * @version $Id: 655d80c40a6a50a3496cf3f15c45321f8070b6f3 $
40    * @since 1.5M2
41    */
 
42    public abstract class AbstractMacro<P> implements Macro<P>, Initializable
43    {
44    /**
45    * "Formatting" default macro category.
46    */
47    public static final String DEFAULT_CATEGORY_FORMATTING = "Formatting";
48   
49    /**
50    * "Development" default macro category.
51    */
52    public static final String DEFAULT_CATEGORY_DEVELOPMENT = "Development";
53   
54    /**
55    * "Content" default macro category.
56    */
57    public static final String DEFAULT_CATEGORY_CONTENT = "Content";
58   
59    /**
60    * "Navigation" default macro category.
61    */
62    public static final String DEFAULT_CATEGORY_NAVIGATION = "Navigation";
63   
64    /**
65    * The {@link BeanManager} component.
66    */
67    @Inject
68    protected BeanManager beanManager;
69   
70    /**
71    * The human-readable macro name (eg "Table of Contents" for the TOC macro).
72    */
73    private String name;
74   
75    /**
76    * Macro description used to generate the macro descriptor.
77    */
78    private String description;
79   
80    /**
81    * Content descriptor used to generate the macro descriptor.
82    */
83    private ContentDescriptor contentDescriptor;
84   
85    /**
86    * Parameter bean class used to generate the macro descriptor.
87    */
88    private Class< ? > parametersBeanClass;
89   
90    /**
91    * The descriptor of the macro.
92    */
93    private MacroDescriptor macroDescriptor;
94   
95    /**
96    * @see Macro#getPriority()
97    */
98    private int priority = 1000;
99   
100    /**
101    * The default category under which this macro should be listed.
102    */
103    private String defaultCategory;
104   
105    /**
106    * Creates a new {@link Macro} instance.
107    *
108    * @param name the name of the macro (eg "Table Of Contents" for the TOC macro)
109    * @since 2.0M3
110    */
 
111  78 toggle public AbstractMacro(String name)
112    {
113  78 this(name, null);
114    }
115   
116    /**
117    * Creates a new {@link Macro} instance.
118    *
119    * @param name the name of the macro (eg "Table Of Contents" for the TOC macro)
120    * @param description a string describing this macro.
121    * @since 2.0M3
122    */
 
123  78 toggle public AbstractMacro(String name, String description)
124    {
125  78 this(name, description, null, Object.class);
126    }
127   
128    /**
129    * Creates a new {@link Macro} instance.
130    *
131    * @param name the name of the macro (eg "Table Of Contents" for the TOC macro)
132    * @param description a string describing this macro.
133    * @param contentDescriptor {@link ContentDescriptor} for this macro.
134    * @since 2.0M3
135    */
 
136  10 toggle public AbstractMacro(String name, String description, ContentDescriptor contentDescriptor)
137    {
138  10 this(name, description, contentDescriptor, Object.class);
139    }
140   
141    /**
142    * Creates a new {@link Macro} instance.
143    *
144    * @param name the name of the macro (eg "Table Of Contents" for the TOC macro)
145    * @param description a string describing this macro.
146    * @param parametersBeanClass class of the parameters bean of this macro.
147    * @since 2.0M3
148    */
 
149  23 toggle public AbstractMacro(String name, String description, Class< ? > parametersBeanClass)
150    {
151  23 this(name, description, null, parametersBeanClass);
152    }
153   
154    /**
155    * Creates a new {@link Macro} instance.
156    *
157    * @param name the name of the macro (eg "Table Of Contents" for the TOC macro)
158    * @param description string describing this macro.
159    * @param contentDescriptor the {@link ContentDescriptor} describing the content of this macro.
160    * @param parametersBeanClass class of the parameters bean.
161    * @since 2.0M3
162    */
 
163  156 toggle public AbstractMacro(String name, String description, ContentDescriptor contentDescriptor,
164    Class< ? > parametersBeanClass)
165    {
166  156 this.name = name;
167  156 this.description = description;
168  156 this.contentDescriptor = contentDescriptor;
169  156 this.parametersBeanClass = parametersBeanClass;
170    }
171   
 
172  154 toggle @Override
173    public void initialize() throws InitializationException
174    {
175  154 MacroId macroId = null;
176  154 String hint = extractMacroComponentHint();
177  154 if (hint != null && !"default".equals(hint) && !"".equals(hint)) {
178  154 macroId = new MacroId(hint);
179    }
180   
181  154 DefaultMacroDescriptor descriptor = new DefaultMacroDescriptor(macroId, this.name, this.description,
182    this.contentDescriptor, this.beanManager.getBeanDescriptor(this.parametersBeanClass));
183  154 descriptor.setDefaultCategory(this.defaultCategory);
184  154 setDescriptor(descriptor);
185    }
186   
187    /**
188    * Extract the Macro Component Hint from the Macro annotation (from the {@link Named} annotation or from the
189    * older {@link Component} annotation which is kept for backward compatibility only).
190    *
191    * @return the Macro component hint under whicht the macro is registered
192    * since 3.1M2
193    */
 
194  154 toggle private String extractMacroComponentHint()
195    {
196  154 String hint = null;
197  154 Named named = this.getClass().getAnnotation(Named.class);
198  154 if (named != null) {
199  154 hint = named.value();
200    } else {
201    // Kept for backward-compatibility to continue supporting the declaration of the Macro hint in the
202    // Component annotation.
203  0 Component component = this.getClass().getAnnotation(Component.class);
204  0 if (component != null && component.hints().length > 0) {
205  0 hint = component.hints()[0];
206  0 } else if (component != null && StringUtils.isNotBlank(component.value())) {
207  0 hint = component.value().trim();
208    }
209    }
210  154 return hint;
211    }
212   
 
213  44 toggle @Override
214    public int getPriority()
215    {
216  44 return this.priority;
217    }
218   
219    /**
220    * @param priority the macro priority to use (lower means execute before others)
221    */
 
222  25 toggle public void setPriority(int priority)
223    {
224  25 this.priority = priority;
225    }
226   
 
227  1286 toggle @Override
228    public MacroDescriptor getDescriptor()
229    {
230  1286 return this.macroDescriptor;
231    }
232   
 
233  22 toggle @Override
234    public int compareTo(Macro< ? > macro)
235    {
236  22 return getPriority() - macro.getPriority();
237    }
238   
239    /**
240    * Allows macro classes extending other macro classes to override the macro descriptor with their own.
241    *
242    * @param descriptor the overriding descriptor to set
243    */
 
244  154 toggle protected void setDescriptor(MacroDescriptor descriptor)
245    {
246  154 this.macroDescriptor = descriptor;
247    }
248   
249    /**
250    * Allows sub classes to set the default macro category. This method only has an effect if the internal
251    * {@link MacroDescriptor} is of type {@link AbstractMacroDescriptor}.
252    *
253    * @param defaultCategory the default macro category to be set.
254    */
 
255  100 toggle protected void setDefaultCategory(String defaultCategory)
256    {
257    // If setDefaultCategory() method is invoked before macro initialization, this will make sure the macro will
258    // have correct default category after initialization.
259  100 this.defaultCategory = defaultCategory;
260   
261    // In case if setDefaultCategory() is invoked after macro initialization. Only works if the internal
262    // MacroDescriptor is of type AbstractMacroDescriptor.
263  100 if (getDescriptor() instanceof AbstractMacroDescriptor) {
264  0 ((AbstractMacroDescriptor) getDescriptor()).setDefaultCategory(defaultCategory);
265    }
266    }
267    }