Clover Coverage Report - XWiki Rendering - Parent POM 4.0-SNAPSHOT (Aggregated)
Coverage timestamp: Mon Mar 12 2012 18:03:13 CET
../../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
32   168   14   6.4
2   97   0.44   5
5     2.8  
1    
 
  DefaultMacroManager       Line # 50 32 0% 14 2 94.9% 0.94871795
 
  (109)
 
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.Map;
23    import java.util.Set;
24    import java.util.HashSet;
25   
26    import javax.inject.Inject;
27    import javax.inject.Singleton;
28   
29    import org.slf4j.Logger;
30    import org.xwiki.component.annotation.Component;
31    import org.xwiki.component.manager.ComponentLookupException;
32    import org.xwiki.component.manager.ComponentManager;
33    import org.xwiki.rendering.macro.Macro;
34    import org.xwiki.rendering.macro.MacroLookupException;
35    import org.xwiki.rendering.macro.MacroManager;
36    import org.xwiki.rendering.macro.MacroId;
37    import org.xwiki.rendering.macro.MacroIdFactory;
38    import org.xwiki.rendering.parser.ParseException;
39    import org.xwiki.rendering.syntax.Syntax;
40   
41    /**
42    * Default {@link MacroManager} implementation, retrieves all {@link Macro} implementations that are registered against
43    * XWiki's component manager.
44    *
45    * @version $Id: e1ecc9cbec2868544ee32aec1207523ce49668b3 $
46    * @since 1.9M1
47    */
48    @Component
49    @Singleton
 
50    public class DefaultMacroManager implements MacroManager
51    {
52    /**
53    * Allows transforming a macro id specified as text into a {@link MacroId} object.
54    */
55    @Inject
56    private MacroIdFactory macroIdFactory;
57   
58    /**
59    * The Root Component Manager we use find the Context Component Manager (if it exists) to lookup macro
60    * implementations registered as components. Note that Context Component Manager allows Macros to be
61    * registered for a specific user, for a specific wiki, etc.
62    */
63    @Inject
64    private ComponentManager rootComponentManager;
65   
66    /**
67    * The logger to log.
68    */
69    @Inject
70    private Logger logger;
71   
 
72  6 toggle @Override
73    public Set<MacroId> getMacroIds() throws MacroLookupException
74    {
75  6 return getMacroIds(null);
76    }
77   
 
78  7 toggle @Override
79    public Set<MacroId> getMacroIds(Syntax syntax) throws MacroLookupException
80    {
81  7 Set<MacroId> result = new HashSet<MacroId>();
82   
83    // Lookup all registered macros
84  7 Map<String, Macro> allMacros;
85  7 try {
86  7 allMacros = getComponentManager().lookupMap(Macro.class);
87    } catch (ComponentLookupException e) {
88  0 throw new MacroLookupException("Failed to lookup Macros", e);
89    }
90   
91    // Loop through all the macros and filter those macros that will work with the given syntax.
92  7 for (Map.Entry<String, Macro> entry : allMacros.entrySet()) {
93  56 MacroId macroId;
94  56 try {
95  56 macroId = this.macroIdFactory.createMacroId(entry.getKey());
96    } catch (ParseException e) {
97    // One of the macros is registered against the component manager with an invalid macro id, ignore it
98    // but log a warning.
99  1 this.logger.warn("Invalid Macro descriptor format for hint [" + entry.getKey()
100    + "]. The hint should contain either the macro name only or the macro name followed by "
101    + "the syntax for which it is valid. In that case the macro name should be followed by a "
102    + "\"/\" followed by the syntax name followed by another \"/\" followed by the syntax version. "
103    + "For example \"html/xwiki/2.0\". This macro will not be available in the system.");
104  1 continue;
105    }
106  55 if (syntax == null || macroId.getSyntax() == null || syntax == macroId.getSyntax()) {
107  55 result.add(macroId);
108    }
109    }
110   
111  7 return result;
112    }
113   
 
114  1218 toggle @Override
115    public Macro< ? > getMacro(MacroId macroId) throws MacroLookupException
116    {
117    // First search for a macro registered for the passed macro id.
118  1218 String macroHint = macroId.toString();
119  1218 try {
120  1218 return getComponentManager().lookup(Macro.class, macroHint);
121    } catch (ComponentLookupException ex1) {
122    // Now search explicitly for a macro registered for all syntaxes.
123  1169 try {
124  1169 return getComponentManager().lookup(Macro.class, macroId.getId());
125    } catch (ComponentLookupException ex2) {
126    // TODO: Improve this since it's possible the macro wasn't found because it contains some invalid
127    // requirement and since we're not passing the raised exception it's hard to know why the macro
128    // couldn't be found.
129  6 throw new MacroLookupException(String.format("No macro [%s] could be found.", macroId.toString()));
130    }
131    }
132    }
133   
 
134  4 toggle @Override
135    public boolean exists(MacroId macroId)
136    {
137  4 String macroHint = macroId.toString();
138  4 boolean hasMacro = true;
139  4 try {
140  4 getComponentManager().lookup(Macro.class, macroHint);
141    } catch (ComponentLookupException ex) {
142  1 hasMacro = false;
143    }
144  4 return hasMacro;
145    }
146   
147    /**
148    * @return the Component Manager to use to lookup Macros. If the Context Component Manager is available
149    * we use it thus allowing Macros to be registered only for a given Wiki or for a given User (for example).
150    * @since 2.2M1
151    */
 
152  2398 toggle private ComponentManager getComponentManager()
153    {
154  2398 ComponentManager componentManagerToUse;
155   
156    // Look for the Context Component Manager so that Macros can be registered for a specific user, for a
157    // specific wiki, etc. If it's not found use the Root Component Manager. This allows the Rendering module
158    // to work outside of XWiki when there's no notion of Execution Context and Wiki Model for example.
159  2398 try {
160  2398 componentManagerToUse = this.rootComponentManager.lookup(ComponentManager.class, "context");
161    } catch (ComponentLookupException e) {
162    // This means the Context CM doesn't exist, use the Root CM.
163  2397 componentManagerToUse = this.rootComponentManager;
164    }
165   
166  2398 return componentManagerToUse;
167    }
168    }