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

File CodeMacro.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

10
26
3
1
170
96
10
0.38
8.67
3
3.33

Classes

Class Line # Actions
CodeMacro 60 26 0% 10 4
0.897435989.7%
 

Contributing tests

This file is covered by 15 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.code;
21   
22    import java.io.StringReader;
23    import java.util.Arrays;
24    import java.util.Collections;
25    import java.util.LinkedHashMap;
26    import java.util.List;
27    import java.util.Map;
28   
29    import javax.inject.Inject;
30    import javax.inject.Named;
31    import javax.inject.Singleton;
32   
33    import org.apache.commons.lang3.StringUtils;
34    import org.slf4j.Logger;
35    import org.xwiki.component.annotation.Component;
36    import org.xwiki.component.manager.ComponentLookupException;
37    import org.xwiki.component.manager.ComponentManager;
38    import org.xwiki.rendering.block.Block;
39    import org.xwiki.rendering.block.FormatBlock;
40    import org.xwiki.rendering.block.GroupBlock;
41    import org.xwiki.rendering.listener.Format;
42    import org.xwiki.rendering.macro.MacroExecutionException;
43    import org.xwiki.rendering.macro.box.AbstractBoxMacro;
44    import org.xwiki.rendering.macro.code.CodeMacroParameters;
45    import org.xwiki.rendering.macro.descriptor.DefaultContentDescriptor;
46    import org.xwiki.rendering.parser.HighlightParser;
47    import org.xwiki.rendering.parser.ParseException;
48    import org.xwiki.rendering.parser.Parser;
49    import org.xwiki.rendering.transformation.MacroTransformationContext;
50   
51    /**
52    * Highlight provided content depending of the content syntax.
53    *
54    * @version $Id: 41031c46c4ea35309d561f7bfb16602a6670bd1d $
55    * @since 1.7RC1
56    */
57    @Component
58    @Named("code")
59    @Singleton
 
60    public class CodeMacro extends AbstractBoxMacro<CodeMacroParameters>
61    {
62    /**
63    * The description of the macro.
64    */
65    private static final String DESCRIPTION = "Highlights code snippets of various programming languages";
66   
67    /**
68    * Used to indicate that content should not be highlighted.
69    */
70    private static final String LANGUAGE_NONE = "none";
71   
72    /**
73    * The description of the macro content.
74    */
75    private static final String CONTENT_DESCRIPTION = "the content to highlight";
76   
77    /**
78    * Used to parse content when language="none".
79    */
80    @Inject
81    @Named("plain/1.0")
82    private Parser plainTextParser;
83   
84    /**
85    * Used to lookup highlight parsers.
86    */
87    @Inject
88    private ComponentManager componentManager;
89   
90    /**
91    * The logger to log.
92    */
93    @Inject
94    private Logger logger;
95   
96    /**
97    * Create and initialize the descriptor of the macro.
98    */
 
99  15 toggle public CodeMacro()
100    {
101  15 super("Code", DESCRIPTION, new DefaultContentDescriptor(CONTENT_DESCRIPTION), CodeMacroParameters.class);
102  15 setDefaultCategory(DEFAULT_CATEGORY_FORMATTING);
103    }
104   
 
105  16 toggle @Override
106    protected List<Block> parseContent(CodeMacroParameters parameters, String content,
107    MacroTransformationContext context) throws MacroExecutionException
108    {
109  16 List<Block> result;
110  16 try {
111  16 if (LANGUAGE_NONE.equalsIgnoreCase(parameters.getLanguage())) {
112  3 if (StringUtils.isEmpty(content)) {
113  0 result = Collections.emptyList();
114    } else {
115  3 result = this.plainTextParser.parse(new StringReader(content)).getChildren().get(0).getChildren();
116    }
117    } else {
118  13 result = highlight(parameters, content);
119    }
120    } catch (Exception e) {
121  0 throw new MacroExecutionException("Failed to highlight content", e);
122    }
123   
124  16 Map<String, String> formatParameters = new LinkedHashMap<String, String>();
125  16 formatParameters.put("class", "code");
126   
127  16 if (context.isInline()) {
128  2 result = Arrays.<Block> asList(new FormatBlock(result, Format.NONE, formatParameters));
129    } else {
130  14 result = Arrays.<Block> asList(new GroupBlock(result, formatParameters));
131    }
132   
133  16 return result;
134    }
135   
136    /**
137    * Return a highlighted version of the provided content.
138    *
139    * @param parameters the code macro parameters.
140    * @param content the content to highlight.
141    * @return the highlighted version of the provided content.
142    * @throws ParseException the highlight parser failed.
143    * @throws ComponentLookupException failed to find highlight parser for provided language.
144    */
 
145  13 toggle protected List<Block> highlight(CodeMacroParameters parameters, String content) throws ParseException,
146    ComponentLookupException
147    {
148  13 HighlightParser parser;
149   
150  13 if (parameters.getLanguage() != null) {
151  11 if (this.componentManager.hasComponent(HighlightParser.class, parameters.getLanguage())) {
152  1 try {
153  1 parser = this.componentManager.getInstance(HighlightParser.class, parameters.getLanguage());
154  1 return parser.highlight(parameters.getLanguage(), new StringReader(content));
155    } catch (ComponentLookupException e) {
156  0 this.logger.error("Faild to load highlighting parser for language [{}]", parameters.getLanguage(),
157    e);
158    }
159    }
160    }
161   
162  12 this.logger.debug(
163    "Can't find any specific highlighting parser for language [{}]. Trying the default highlighting parser.",
164    parameters.getLanguage());
165   
166  12 parser = this.componentManager.getInstance(HighlightParser.class, "default");
167   
168  12 return parser.highlight(parameters.getLanguage(), new StringReader(content));
169    }
170    }