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

File ContentMacro.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

0
9
4
1
112
57
6
0.67
2.25
4
1.5

Classes

Class Line # Actions
ContentMacro 52 9 0% 6 3
0.769230876.9%
 

Contributing tests

This file is covered by 2 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.content;
21   
22    import java.io.StringReader;
23    import java.util.List;
24   
25    import javax.inject.Inject;
26    import javax.inject.Named;
27    import javax.inject.Singleton;
28   
29    import org.xwiki.component.annotation.Component;
30    import org.xwiki.component.manager.ComponentLookupException;
31    import org.xwiki.component.manager.ComponentManager;
32    import org.xwiki.rendering.block.Block;
33    import org.xwiki.rendering.macro.AbstractMacro;
34    import org.xwiki.rendering.macro.MacroExecutionException;
35    import org.xwiki.rendering.macro.content.ContentMacroParameters;
36    import org.xwiki.rendering.macro.descriptor.DefaultContentDescriptor;
37    import org.xwiki.rendering.parser.ParseException;
38    import org.xwiki.rendering.parser.Parser;
39    import org.xwiki.rendering.syntax.Syntax;
40    import org.xwiki.rendering.transformation.MacroTransformationContext;
41   
42    /**
43    * Allows writing content in any syntax (wiki syntax, HTML, etc). This allows to have to main content in a given syntax
44    * and mix content written in another syntax in it.
45    *
46    * @version $Id: 857c76de1fbb2112e8046355667409338d0ac1f3 $
47    * @since 4.2M3
48    */
49    @Component
50    @Named("content")
51    @Singleton
 
52    public class ContentMacro extends AbstractMacro<ContentMacroParameters>
53    {
54    /**
55    * The description of the macro.
56    */
57    private static final String DESCRIPTION = "Allows writing content in any wiki markup";
58   
59    /**
60    * The description of the macro content.
61    */
62    private static final String CONTENT_DESCRIPTION = "The content to execute";
63   
64    /**
65    * Used to find the Parser corresponding to the user-specified syntax for the Macro.
66    */
67    @Inject
68    private ComponentManager componentManager;
69   
70    /**
71    * Create and initialize the descriptor of the macro.
72    */
 
73  2 toggle public ContentMacro()
74    {
75  2 super("Content", DESCRIPTION, new DefaultContentDescriptor(CONTENT_DESCRIPTION), ContentMacroParameters.class);
76  2 setDefaultCategory(DEFAULT_CATEGORY_CONTENT);
77    }
78   
 
79  0 toggle @Override
80    public boolean supportsInlineMode()
81    {
82  0 return false;
83    }
84   
 
85  2 toggle @Override
86    public List<Block> execute(ContentMacroParameters parameters, String content, MacroTransformationContext context)
87    throws MacroExecutionException
88    {
89  2 try {
90  2 return getSyntaxParser(parameters.getSyntax()).parse(new StringReader(content)).getChildren();
91    } catch (ParseException e) {
92  0 throw new MacroExecutionException(
93    String.format("Failed to parse macro content in syntax [%s]", parameters.getSyntax()), e);
94    }
95    }
96   
97    /**
98    * Get the parser for the passed Syntax.
99    *
100    * @param syntax the Syntax for which to find the Parser
101    * @return the matching Parser that can be used to parse content in the passed Syntax
102    * @throws MacroExecutionException if there's no Parser in the system for the passed Syntax
103    */
 
104  2 toggle protected Parser getSyntaxParser(Syntax syntax) throws MacroExecutionException
105    {
106  2 try {
107  2 return this.componentManager.getInstance(Parser.class, syntax.toIdString());
108    } catch (ComponentLookupException e) {
109  1 throw new MacroExecutionException(String.format("Cannot find Parser for syntax [%s]", syntax.toIdString()));
110    }
111    }
112    }