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

File AbstractMacro.java

 

Coverage histogram

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

Code metrics

2
20
12
1
243
92
13
0.65
1.67
12
1.08

Classes

Class Line # Actions
AbstractMacro 40 20 0% 13 2
0.941176594.1%
 

Contributing tests

This file is covered by 253 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;
21   
22    import javax.inject.Inject;
23   
24    import org.xwiki.component.descriptor.ComponentDescriptor;
25    import org.xwiki.component.phase.Initializable;
26    import org.xwiki.component.phase.InitializationException;
27    import org.xwiki.properties.BeanManager;
28    import org.xwiki.rendering.macro.descriptor.AbstractMacroDescriptor;
29    import org.xwiki.rendering.macro.descriptor.ContentDescriptor;
30    import org.xwiki.rendering.macro.descriptor.DefaultMacroDescriptor;
31    import org.xwiki.rendering.macro.descriptor.MacroDescriptor;
32   
33    /**
34    * Helper to implement Macro, providing some default implementation. We recommend Macro writers to extend this class.
35    *
36    * @param <P> the type of the macro parameters bean
37    * @version $Id: 8395f9155b506abcd75eb66ce7d02867b2e58de9 $
38    * @since 1.5M2
39    */
 
40    public abstract class AbstractMacro<P> implements Macro<P>, Initializable
41    {
42    /**
43    * "Formatting" default macro category.
44    */
45    public static final String DEFAULT_CATEGORY_FORMATTING = "Formatting";
46   
47    /**
48    * "Development" default macro category.
49    */
50    public static final String DEFAULT_CATEGORY_DEVELOPMENT = "Development";
51   
52    /**
53    * "Content" default macro category.
54    */
55    public static final String DEFAULT_CATEGORY_CONTENT = "Content";
56   
57    /**
58    * "Navigation" default macro category.
59    */
60    public static final String DEFAULT_CATEGORY_NAVIGATION = "Navigation";
61   
62    /**
63    * "Internal" default macro category.
64    */
65    public static final String DEFAULT_CATEGORY_INTERNAL = "Internal";
66   
67    /**
68    * The {@link BeanManager} component.
69    */
70    @Inject
71    protected BeanManager beanManager;
72   
73    @Inject
74    private ComponentDescriptor<Macro> componentDescriptor;
75   
76    /**
77    * The human-readable macro name (eg "Table of Contents" for the TOC macro).
78    */
79    private String name;
80   
81    /**
82    * Macro description used to generate the macro descriptor.
83    */
84    private String description;
85   
86    /**
87    * Content descriptor used to generate the macro descriptor.
88    */
89    private ContentDescriptor contentDescriptor;
90   
91    /**
92    * Parameter bean class used to generate the macro descriptor.
93    */
94    private Class<?> parametersBeanClass;
95   
96    /**
97    * The descriptor of the macro.
98    */
99    private MacroDescriptor macroDescriptor;
100   
101    /**
102    * @see Macro#getPriority()
103    */
104    private int priority = 1000;
105   
106    /**
107    * The default category under which this macro should be listed.
108    */
109    private String defaultCategory;
110   
111    /**
112    * Creates a new {@link Macro} instance.
113    *
114    * @param name the name of the macro (eg "Table Of Contents" for the TOC macro)
115    * @since 2.0M3
116    */
 
117  100 toggle public AbstractMacro(String name)
118    {
119  100 this(name, null);
120    }
121   
122    /**
123    * Creates a new {@link Macro} instance.
124    *
125    * @param name the name of the macro (eg "Table Of Contents" for the TOC macro)
126    * @param description a string describing this macro.
127    * @since 2.0M3
128    */
 
129  100 toggle public AbstractMacro(String name, String description)
130    {
131  100 this(name, description, null, Object.class);
132    }
133   
134    /**
135    * Creates a new {@link Macro} instance.
136    *
137    * @param name the name of the macro (eg "Table Of Contents" for the TOC macro)
138    * @param description a string describing this macro.
139    * @param contentDescriptor {@link ContentDescriptor} for this macro.
140    * @since 2.0M3
141    */
 
142  35 toggle public AbstractMacro(String name, String description, ContentDescriptor contentDescriptor)
143    {
144  35 this(name, description, contentDescriptor, Object.class);
145    }
146   
147    /**
148    * Creates a new {@link Macro} instance.
149    *
150    * @param name the name of the macro (eg "Table Of Contents" for the TOC macro)
151    * @param description a string describing this macro.
152    * @param parametersBeanClass class of the parameters bean of this macro.
153    * @since 2.0M3
154    */
 
155  95 toggle public AbstractMacro(String name, String description, Class<?> parametersBeanClass)
156    {
157  95 this(name, description, null, parametersBeanClass);
158    }
159   
160    /**
161    * Creates a new {@link Macro} instance.
162    *
163    * @param name the name of the macro (eg "Table Of Contents" for the TOC macro)
164    * @param description string describing this macro.
165    * @param contentDescriptor the {@link ContentDescriptor} describing the content of this macro.
166    * @param parametersBeanClass class of the parameters bean.
167    * @since 2.0M3
168    */
 
169  480 toggle public AbstractMacro(String name, String description, ContentDescriptor contentDescriptor,
170    Class<?> parametersBeanClass)
171    {
172  480 this.name = name;
173  480 this.description = description;
174  480 this.contentDescriptor = contentDescriptor;
175  480 this.parametersBeanClass = parametersBeanClass;
176    }
177   
 
178  464 toggle @Override
179    public void initialize() throws InitializationException
180    {
181  464 MacroId macroId = new MacroId(this.componentDescriptor.getRoleHint());
182   
183  464 DefaultMacroDescriptor descriptor = new DefaultMacroDescriptor(macroId, this.name, this.description,
184    this.contentDescriptor, this.beanManager.getBeanDescriptor(this.parametersBeanClass));
185  464 descriptor.setDefaultCategory(this.defaultCategory);
186  464 setDescriptor(descriptor);
187    }
188   
 
189  120401 toggle @Override
190    public int getPriority()
191    {
192  120401 return this.priority;
193    }
194   
195    /**
196    * @param priority the macro priority to use (lower means execute before others)
197    */
 
198  85 toggle public void setPriority(int priority)
199    {
200  85 this.priority = priority;
201    }
202   
 
203  35227 toggle @Override
204    public MacroDescriptor getDescriptor()
205    {
206  35226 return this.macroDescriptor;
207    }
208   
 
209  59409 toggle @Override
210    public int compareTo(Macro<?> macro)
211    {
212  59409 return getPriority() - macro.getPriority();
213    }
214   
215    /**
216    * Allows macro classes extending other macro classes to override the macro descriptor with their own.
217    *
218    * @param descriptor the overriding descriptor to set
219    */
 
220  464 toggle protected void setDescriptor(MacroDescriptor descriptor)
221    {
222  464 this.macroDescriptor = descriptor;
223    }
224   
225    /**
226    * Allows sub classes to set the default macro category. This method only has an effect if the internal
227    * {@link MacroDescriptor} is of type {@link AbstractMacroDescriptor}.
228    *
229    * @param defaultCategory the default macro category to be set.
230    */
 
231  396 toggle protected void setDefaultCategory(String defaultCategory)
232    {
233    // If setDefaultCategory() method is invoked before macro initialization, this will make sure the macro will
234    // have correct default category after initialization.
235  396 this.defaultCategory = defaultCategory;
236   
237    // In case if setDefaultCategory() is invoked after macro initialization. Only works if the internal
238    // MacroDescriptor is of type AbstractMacroDescriptor.
239  396 if (getDescriptor() instanceof AbstractMacroDescriptor) {
240  0 ((AbstractMacroDescriptor) getDescriptor()).setDefaultCategory(defaultCategory);
241    }
242    }
243    }