1. Project Clover database Sat Feb 2 2019 06:45:20 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
154
98
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 274 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: 5cdde881a631ac1f6e1420cc459a85624cc2f6b5 $
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  116 toggle @Override
82    public Set<MacroId> getMacroIds(Syntax syntax) throws MacroLookupException
83    {
84    // Lookup all registered macros
85  116 Map<String, Macro> allMacros;
86  116 try {
87  116 allMacros = this.componentManager.get().getInstanceMap(Macro.class);
88    } catch (ComponentLookupException e) {
89  0 throw new MacroLookupException("Failed to lookup Macros", e);
90    }
91   
92  116 Set<MacroId> result = new HashSet<MacroId>();
93   
94    // Loop through all the macros and filter those macros that will work with the given syntax.
95  116 for (Map.Entry<String, Macro> entry : allMacros.entrySet()) {
96  5627 MacroId macroId;
97  5627 try {
98  5627 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(
103    "Invalid Macro descriptor format for hint [{}]. "
104    + "The hint should contain either the macro name only or the macro name followed by "
105    + "the syntax for which it is valid. In that case the macro name should be followed by a "
106    + "\"/\" followed by the syntax name followed by another \"/\" followed by the syntax version. "
107    + "For example \"html/xwiki/2.0\". This macro will not be available in the system.",
108    entry.getKey());
109  1 continue;
110    }
111  5626 if (syntax == null || macroId.getSyntax() == null || syntax.equals(macroId.getSyntax())) {
112  5626 result.add(macroId);
113    }
114    }
115   
116  116 return result;
117    }
118   
 
119  634354 toggle @Override
120    public Macro<?> getMacro(MacroId macroId) throws MacroLookupException
121    {
122    // First search for a macro registered for the passed macro id.
123  634428 String macroHint = macroId.toString();
124  634665 ComponentManager cm = this.componentManager.get();
125  634557 try {
126  634614 if (cm.hasComponent(Macro.class, macroHint)) {
127  28762 return cm.getInstance(Macro.class, macroHint);
128    } else {
129    // Now search explicitly for a macro registered for all syntaxes.
130  605890 if (cm.hasComponent(Macro.class, macroId.getId())) {
131  605610 return cm.getInstance(Macro.class, macroId.getId());
132    }
133   
134  213 throw new MacroNotFoundException(String.format("No macro [%s] could be found.", macroId.toString()));
135    }
136    } catch (ComponentLookupException e) {
137  1 throw new MacroLookupException(String.format("Macro [%s] failed to be instantiated.", macroId.toString()),
138    e);
139    }
140    }
141   
 
142  6 toggle @Override
143    public boolean exists(MacroId macroId)
144    {
145  6 String macroHint = macroId.toString();
146  6 boolean hasMacro = true;
147  6 try {
148  6 this.componentManager.get().getInstance(Macro.class, macroHint);
149    } catch (ComponentLookupException ex) {
150  2 hasMacro = false;
151    }
152  6 return hasMacro;
153    }
154    }