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

File DefaultMacroCategoryManager.java

 

Coverage histogram

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

Code metrics

8
22
7
2
152
86
11
0.5
3.14
3.5
1.57

Classes

Class Line # Actions
DefaultMacroCategoryManager 48 22 0% 11 1
0.97297397.3%
DefaultMacroCategoryManager.MacroMatcher 65 0 - 0 0
-1.0 -
 

Contributing tests

This file is covered by 3 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.internal.macro;
21   
22    import java.util.Collections;
23    import java.util.HashMap;
24    import java.util.HashSet;
25    import java.util.Map;
26    import java.util.Properties;
27    import java.util.Set;
28   
29    import javax.inject.Inject;
30    import javax.inject.Singleton;
31   
32    import org.xwiki.component.annotation.Component;
33    import org.xwiki.rendering.macro.MacroCategoryManager;
34    import org.xwiki.rendering.macro.MacroId;
35    import org.xwiki.rendering.macro.MacroLookupException;
36    import org.xwiki.rendering.macro.MacroManager;
37    import org.xwiki.rendering.syntax.Syntax;
38    import org.xwiki.rendering.transformation.macro.MacroTransformationConfiguration;
39   
40    /**
41    * Default implementation of {@link org.xwiki.rendering.macro.MacroCategoryManager}.
42    *
43    * @version $Id: 2f82765e335a6eb8519029daf82ee7ba61707706 $
44    * @since 2.0M3
45    */
46    @Component
47    @Singleton
 
48    public class DefaultMacroCategoryManager implements MacroCategoryManager
49    {
50    /**
51    * Used to get macro categories defined by the user (if any).
52    */
53    @Inject
54    private MacroTransformationConfiguration configuration;
55   
56    /**
57    * Macro manager component used to check the existence of macros.
58    */
59    @Inject
60    private MacroManager macroManager;
61   
62    /**
63    * Internal help class to be able to search Macros matching a Macro Id.
64    */
 
65    private interface MacroMatcher
66    {
67    /**
68    * @param macroId the macro Id to match
69    * @return true if the concerned macro matches the macro Id
70    */
71    boolean match(MacroId macroId);
72    }
73   
 
74  1 toggle @Override
75    public Set<String> getMacroCategories() throws MacroLookupException
76    {
77  1 return getMacroCategories(null);
78    }
79   
 
80  1 toggle @Override
81    public Set<String> getMacroCategories(final Syntax syntax) throws MacroLookupException
82    {
83  1 Set<String> categories = getMacroIdsByCategory(new MacroMatcher()
84    {
 
85  8 toggle @Override
86    public boolean match(MacroId macroId)
87    {
88    // True if the macroId has no syntax or if it has one it has to match the passed syntax
89  8 return syntax == null || macroId.getSyntax() == null || macroId.getSyntax() == syntax;
90    }
91    }).keySet();
92  1 return Collections.unmodifiableSet(categories);
93    }
94   
 
95  2 toggle @Override
96    public Set<MacroId> getMacroIds(String category) throws MacroLookupException
97    {
98  2 return getMacroIds(category, null);
99    }
100   
 
101  5 toggle @Override
102    public Set<MacroId> getMacroIds(String category, final Syntax syntax) throws MacroLookupException
103    {
104  5 Set<MacroId> macros = getMacroIdsByCategory(new MacroMatcher()
105    {
 
106  47 toggle @Override
107    public boolean match(MacroId macroId)
108    {
109    // True if the macroId has no syntax or if it has one it has to match the passed syntax
110  47 return syntax == null || macroId.getSyntax() == null || macroId.getSyntax().equals(syntax);
111    }
112    }).get(category);
113  5 return (null != macros) ? Collections.unmodifiableSet(macros) : Collections.<MacroId>emptySet();
114    }
115   
116    /**
117    * @param matcher a macro name matcher to be able to filter macros, used to filter macros for a given syntax
118    * @return macro names grouped by category, including the 'null' macro category.
119    * @exception MacroLookupException if any error occurs when getting macros ids by category
120    */
 
121  6 toggle private Map<String, Set<MacroId>> getMacroIdsByCategory(MacroMatcher matcher) throws MacroLookupException
122    {
123  6 Map<String, Set<MacroId>> result = new HashMap<String, Set<MacroId>>();
124   
125    // Find all registered macro ids
126  6 Set<MacroId> macroIds = this.macroManager.getMacroIds();
127   
128    // Loop through all the macro ids and categorize them.
129  6 Properties categories = this.configuration.getCategories();
130  6 for (MacroId macroId : macroIds) {
131  55 if (matcher.match(macroId)) {
132    // Check if this macro's category has been overwritten.
133  54 String category = categories.getProperty(macroId.toString());
134   
135    // If not, use the default category set by macro author.
136  54 if (category == null) {
137  46 category = this.macroManager.getMacro(macroId).getDescriptor().getDefaultCategory();
138    }
139   
140    // Add to category. Note the category can also be null.
141  54 Set<MacroId> ids = result.get(category);
142  54 if (ids == null) {
143  18 ids = new HashSet<MacroId>();
144    }
145  54 ids.add(macroId);
146  54 result.put(category, ids);
147    }
148    }
149   
150  6 return result;
151    }
152    }