1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.rendering.macro

File AbstractMacro.java

 

Coverage histogram

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

Code metrics

4
29
13
1
284
115
15
0.52
2.23
13
1.15

Classes

Class Line # Actions
AbstractMacro 46 29 0% 15 2
0.9565217595.7%
 

Contributing tests

This file is covered by 291 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 java.lang.reflect.Type;
23   
24    import javax.inject.Inject;
25   
26    import org.xwiki.component.descriptor.ComponentDescriptor;
27    import org.xwiki.component.phase.Initializable;
28    import org.xwiki.component.phase.InitializationException;
29    import org.xwiki.properties.BeanManager;
30    import org.xwiki.properties.ConverterManager;
31    import org.xwiki.rendering.listener.MetaData;
32    import org.xwiki.rendering.macro.descriptor.AbstractMacroDescriptor;
33    import org.xwiki.rendering.macro.descriptor.ContentDescriptor;
34    import org.xwiki.rendering.macro.descriptor.DefaultContentDescriptor;
35    import org.xwiki.rendering.macro.descriptor.DefaultMacroDescriptor;
36    import org.xwiki.rendering.macro.descriptor.MacroDescriptor;
37    import org.xwiki.stability.Unstable;
38   
39    /**
40    * Helper to implement Macro, providing some default implementation. We recommend Macro writers to extend this class.
41    *
42    * @param <P> the type of the macro parameters bean
43    * @version $Id: b24182575a563a3f5550bfd830d6b82252063910 $
44    * @since 1.5M2
45    */
 
46    public abstract class AbstractMacro<P> implements Macro<P>, Initializable
47    {
48    /**
49    * "Formatting" default macro category.
50    */
51    public static final String DEFAULT_CATEGORY_FORMATTING = "Formatting";
52   
53    /**
54    * "Development" default macro category.
55    */
56    public static final String DEFAULT_CATEGORY_DEVELOPMENT = "Development";
57   
58    /**
59    * "Content" default macro category.
60    */
61    public static final String DEFAULT_CATEGORY_CONTENT = "Content";
62   
63    /**
64    * "Navigation" default macro category.
65    */
66    public static final String DEFAULT_CATEGORY_NAVIGATION = "Navigation";
67   
68    /**
69    * "Internal" default macro category.
70    */
71    public static final String DEFAULT_CATEGORY_INTERNAL = "Internal";
72   
73    /**
74    * "Layout" default macro category.
75    */
76    public static final String DEFAULT_CATEGORY_LAYOUT = "Layout";
77   
78    /**
79    * The {@link BeanManager} component.
80    */
81    @Inject
82    protected BeanManager beanManager;
83   
84    @Inject
85    private ComponentDescriptor<Macro> componentDescriptor;
86   
87    @Inject
88    private ConverterManager converterManager;
89   
90    /**
91    * The human-readable macro name (eg "Table of Contents" for the TOC macro).
92    */
93    private String name;
94   
95    /**
96    * Macro description used to generate the macro descriptor.
97    */
98    private String description;
99   
100    /**
101    * Content descriptor used to generate the macro descriptor.
102    */
103    private ContentDescriptor contentDescriptor;
104   
105    /**
106    * Parameter bean class used to generate the macro descriptor.
107    */
108    private Class<?> parametersBeanClass;
109   
110    /**
111    * The descriptor of the macro.
112    */
113    private MacroDescriptor macroDescriptor;
114   
115    /**
116    * @see Macro#getPriority()
117    */
118    private int priority = 1000;
119   
120    /**
121    * The default category under which this macro should be listed.
122    */
123    private String defaultCategory;
124   
125    /**
126    * Creates a new {@link Macro} instance.
127    *
128    * @param name the name of the macro (eg "Table Of Contents" for the TOC macro)
129    * @since 2.0M3
130    */
 
131  112 toggle public AbstractMacro(String name)
132    {
133  112 this(name, null);
134    }
135   
136    /**
137    * Creates a new {@link Macro} instance.
138    *
139    * @param name the name of the macro (eg "Table Of Contents" for the TOC macro)
140    * @param description a string describing this macro.
141    * @since 2.0M3
142    */
 
143  112 toggle public AbstractMacro(String name, String description)
144    {
145  112 this(name, description, null, Object.class);
146    }
147   
148    /**
149    * Creates a new {@link Macro} instance.
150    *
151    * @param name the name of the macro (eg "Table Of Contents" for the TOC macro)
152    * @param description a string describing this macro.
153    * @param contentDescriptor {@link ContentDescriptor} for this macro.
154    * @since 2.0M3
155    */
 
156  33 toggle public AbstractMacro(String name, String description, ContentDescriptor contentDescriptor)
157    {
158  33 this(name, description, contentDescriptor, Object.class);
159    }
160   
161    /**
162    * Creates a new {@link Macro} instance.
163    *
164    * @param name the name of the macro (eg "Table Of Contents" for the TOC macro)
165    * @param description a string describing this macro.
166    * @param parametersBeanClass class of the parameters bean of this macro.
167    * @since 2.0M3
168    */
 
169  137 toggle public AbstractMacro(String name, String description, Class<?> parametersBeanClass)
170    {
171  137 this(name, description, null, parametersBeanClass);
172    }
173   
174    /**
175    * Creates a new {@link Macro} instance.
176    *
177    * @param name the name of the macro (eg "Table Of Contents" for the TOC macro)
178    * @param description string describing this macro.
179    * @param contentDescriptor the {@link ContentDescriptor} describing the content of this macro.
180    * @param parametersBeanClass class of the parameters bean.
181    * @since 2.0M3
182    */
 
183  818 toggle public AbstractMacro(String name, String description, ContentDescriptor contentDescriptor,
184    Class<?> parametersBeanClass)
185    {
186  818 this.name = name;
187  818 this.description = description;
188  818 this.contentDescriptor = contentDescriptor;
189  818 this.parametersBeanClass = parametersBeanClass;
190    }
191   
 
192  639 toggle @Override
193    public void initialize() throws InitializationException
194    {
195  639 MacroId macroId = new MacroId(this.componentDescriptor.getRoleHint());
196   
197  639 DefaultMacroDescriptor descriptor = new DefaultMacroDescriptor(macroId, this.name, this.description,
198    this.contentDescriptor, this.beanManager.getBeanDescriptor(this.parametersBeanClass));
199  639 descriptor.setDefaultCategory(this.defaultCategory);
200  639 descriptor.setSupportsInlineMode(this.supportsInlineMode());
201  639 setDescriptor(descriptor);
202    }
203   
 
204  683416 toggle @Override
205    public int getPriority()
206    {
207  683458 return this.priority;
208    }
209   
210    /**
211    * @param priority the macro priority to use (lower means execute before others)
212    */
 
213  123 toggle public void setPriority(int priority)
214    {
215  123 this.priority = priority;
216    }
217   
 
218  1050406 toggle @Override
219    public MacroDescriptor getDescriptor()
220    {
221  1050603 return this.macroDescriptor;
222    }
223   
 
224  334007 toggle @Override
225    public int compareTo(Macro<?> macro)
226    {
227  334022 return getPriority() - macro.getPriority();
228    }
229   
230    /**
231    * Allows macro classes extending other macro classes to override the macro descriptor with their own.
232    *
233    * @param descriptor the overriding descriptor to set
234    */
 
235  639 toggle protected void setDescriptor(MacroDescriptor descriptor)
236    {
237  639 this.macroDescriptor = descriptor;
238    }
239   
240    /**
241    * Allows sub classes to set the default macro category. This method only has an effect if the internal
242    * {@link MacroDescriptor} is of type {@link AbstractMacroDescriptor}.
243    *
244    * @param defaultCategory the default macro category to be set.
245    */
 
246  553 toggle protected void setDefaultCategory(String defaultCategory)
247    {
248    // If setDefaultCategory() method is invoked before macro initialization, this will make sure the macro will
249    // have correct default category after initialization.
250  553 this.defaultCategory = defaultCategory;
251   
252    // In case if setDefaultCategory() is invoked after macro initialization. Only works if the internal
253    // MacroDescriptor is of type AbstractMacroDescriptor.
254  553 if (getDescriptor() instanceof AbstractMacroDescriptor) {
255  0 ((AbstractMacroDescriptor) getDescriptor()).setDefaultCategory(defaultCategory);
256    }
257    }
258   
259    /**
260    * Helper to get the proper metadata for non generated content (i.e. content that has not gone through a
261    * Transformation). This content can be used for inline editing.
262    *
263    * @return the new metadata with the content type for the content represented as a string (e.g.
264    * {@code java.util.List< org.xwiki.rendering.block.Block >} for content of type {@code List<Block>}
265    * @since 10.10
266    */
 
267  6823 toggle @Unstable
268    protected MetaData getNonGeneratedContentMetaData()
269    {
270  6825 MetaData metaData = new MetaData();
271  6824 Type contentType;
272   
273  6825 if (this.contentDescriptor != null) {
274  6823 contentType = this.contentDescriptor.getType();
275    } else {
276  1 contentType = DefaultContentDescriptor.DEFAULT_CONTENT_TYPE;
277    }
278   
279  6823 String converted = this.converterManager.convert(String.class, contentType);
280   
281  6825 metaData.addMetaData(MetaData.NON_GENERATED_CONTENT, converted);
282  6825 return metaData;
283    }
284    }