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

File DefaultMacroManager.java

 

Coverage histogram

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

Code metrics

6
30
4
1
152
96
13
0.43
7.5
4
3.25

Classes

Class Line # Actions
DefaultMacroManager 53 30 0% 13 2
0.9595%
 

Contributing tests

This file is covered by 240 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.HashSet;
23    import java.util.Map;
24    import java.util.Set;
25   
26    import javax.inject.Inject;
27    import javax.inject.Named;
28    import javax.inject.Provider;
29    import javax.inject.Singleton;
30   
31    import org.slf4j.Logger;
32    import org.xwiki.component.annotation.Component;
33    import org.xwiki.component.manager.ComponentLookupException;
34    import org.xwiki.component.manager.ComponentManager;
35    import org.xwiki.rendering.macro.Macro;
36    import org.xwiki.rendering.macro.MacroId;
37    import org.xwiki.rendering.macro.MacroIdFactory;
38    import org.xwiki.rendering.macro.MacroLookupException;
39    import org.xwiki.rendering.macro.MacroManager;
40    import org.xwiki.rendering.macro.MacroNotFoundException;
41    import org.xwiki.rendering.parser.ParseException;
42    import org.xwiki.rendering.syntax.Syntax;
43   
44    /**
45    * Default {@link MacroManager} implementation, retrieves all {@link Macro} implementations that are registered against
46    * XWiki's component manager.
47    *
48    * @version $Id: c2a0f42f514a8c0f76bbbe0bf3fb912bc7c4d7da $
49    * @since 1.9M1
50    */
51    @Component
52    @Singleton
 
53    public class DefaultMacroManager implements MacroManager
54    {
55    /**
56    * Allows transforming a macro id specified as text into a {@link MacroId} object.
57    */
58    @Inject
59    private MacroIdFactory macroIdFactory;
60   
61    /**
62    * We use the Context Component Manager (if it exists) to lookup macro implementations registered as components.
63    * Note that Context Component Manager allows Macros to be registered for a specific user, for a specific wiki, etc.
64    */
65    @Inject
66    @Named("context")
67    private Provider<ComponentManager> componentManager;
68   
69    /**
70    * The logger to log.
71    */
72    @Inject
73    private Logger logger;
74   
 
75  6 toggle @Override
76    public Set<MacroId> getMacroIds() throws MacroLookupException
77    {
78  6 return getMacroIds(null);
79    }
80   
 
81  7 toggle @Override
82    public Set<MacroId> getMacroIds(Syntax syntax) throws MacroLookupException
83    {
84  7 Set<MacroId> result = new HashSet<MacroId>();
85   
86    // Lookup all registered macros
87  7 Map<String, Macro> allMacros;
88  7 try {
89  7 allMacros = this.componentManager.get().getInstanceMap(Macro.class);
90    } catch (ComponentLookupException e) {
91  0 throw new MacroLookupException("Failed to lookup Macros", e);
92    }
93   
94    // Loop through all the macros and filter those macros that will work with the given syntax.
95  7 for (Map.Entry<String, Macro> entry : allMacros.entrySet()) {
96  56 MacroId macroId;
97  56 try {
98  56 macroId = this.macroIdFactory.createMacroId(entry.getKey());
99    } catch (ParseException e) {
100    // One of the macros is registered against the component manager with an invalid macro id, ignore it
101    // but log a warning.
102  1 this.logger.warn("Invalid Macro descriptor format for hint [" + entry.getKey()
103    + "]. The hint should contain either the macro name only or the macro name followed by "
104    + "the syntax for which it is valid. In that case the macro name should be followed by a "
105    + "\"/\" followed by the syntax name followed by another \"/\" followed by the syntax version. "
106    + "For example \"html/xwiki/2.0\". This macro will not be available in the system.");
107  1 continue;
108    }
109  55 if (syntax == null || macroId.getSyntax() == null || syntax == macroId.getSyntax()) {
110  55 result.add(macroId);
111    }
112    }
113   
114  7 return result;
115    }
116   
 
117  22628 toggle @Override
118    public Macro<?> getMacro(MacroId macroId) throws MacroLookupException
119    {
120    // First search for a macro registered for the passed macro id.
121  22628 String macroHint = macroId.toString();
122  22629 ComponentManager cm = this.componentManager.get();
123  22629 try {
124  22629 if (cm.hasComponent(Macro.class, macroHint)) {
125  1193 return cm.getInstance(Macro.class, macroHint);
126    } else {
127    // Now search explicitly for a macro registered for all syntaxes.
128  21436 if (cm.hasComponent(Macro.class, macroId.getId())) {
129  21422 return cm.getInstance(Macro.class, macroId.getId());
130    }
131   
132  14 throw new MacroNotFoundException(String.format("No macro [%s] could be found.", macroId.toString()));
133    }
134    } catch (ComponentLookupException e) {
135  1 throw new MacroLookupException(String.format("Macro [%s] failed to be instantiated.", macroId.toString()),
136    e);
137    }
138    }
139   
 
140  6 toggle @Override
141    public boolean exists(MacroId macroId)
142    {
143  6 String macroHint = macroId.toString();
144  6 boolean hasMacro = true;
145  6 try {
146  6 this.componentManager.get().getInstance(Macro.class, macroHint);
147    } catch (ComponentLookupException ex) {
148  2 hasMacro = false;
149    }
150  6 return hasMacro;
151    }
152    }