1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.rendering.internal.macro

File DefaultMacroContentParser.java

 

Coverage histogram

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

Code metrics

22
42
7
1
210
127
22
0.52
6
7
3.14

Classes

Class Line # Actions
DefaultMacroContentParser 60 42 0% 22 8
0.887323988.7%
 

Contributing tests

This file is covered by 106 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.io.StringReader;
23    import java.util.ArrayList;
24    import java.util.Collections;
25    import java.util.List;
26   
27    import javax.inject.Inject;
28    import javax.inject.Singleton;
29   
30    import org.apache.commons.lang3.StringUtils;
31    import org.xwiki.component.annotation.Component;
32    import org.xwiki.component.manager.ComponentLookupException;
33    import org.xwiki.component.manager.ComponentManager;
34    import org.xwiki.rendering.block.Block;
35    import org.xwiki.rendering.block.Block.Axes;
36    import org.xwiki.rendering.block.MacroBlock;
37    import org.xwiki.rendering.block.MetaDataBlock;
38    import org.xwiki.rendering.block.XDOM;
39    import org.xwiki.rendering.block.match.MetadataBlockMatcher;
40    import org.xwiki.rendering.internal.transformation.MutableRenderingContext;
41    import org.xwiki.rendering.listener.MetaData;
42    import org.xwiki.rendering.macro.MacroContentParser;
43    import org.xwiki.rendering.macro.MacroExecutionException;
44    import org.xwiki.rendering.parser.Parser;
45    import org.xwiki.rendering.syntax.Syntax;
46    import org.xwiki.rendering.transformation.MacroTransformationContext;
47    import org.xwiki.rendering.transformation.RenderingContext;
48    import org.xwiki.rendering.transformation.Transformation;
49    import org.xwiki.rendering.transformation.TransformationContext;
50    import org.xwiki.rendering.util.ParserUtils;
51   
52    /**
53    * Default implementation for {@link org.xwiki.rendering.macro.MacroContentParser}.
54    *
55    * @version $Id: 89bab5d5e73ce7c45d3e732bcceda129e959de9f $
56    * @since 3.0M1
57    */
58    @Component
59    @Singleton
 
60    public class DefaultMacroContentParser implements MacroContentParser
61    {
62    /**
63    * Used to look up the syntax parser to use for parsing the content.
64    */
65    @Inject
66    private ComponentManager componentManager;
67   
68    /**
69    * Used to update rendering context during content transformation.
70    */
71    @Inject
72    private RenderingContext renderingContext;
73   
74    /**
75    * Utility to remove the top level paragraph.
76    */
77    private ParserUtils parserUtils = new ParserUtils();
78   
 
79  241951 toggle @Override
80    public XDOM parse(String content, MacroTransformationContext macroContext, boolean transform, boolean inline)
81    throws MacroExecutionException
82    {
83  241955 return parse(content, macroContext, transform, null, inline);
84    }
85   
 
86  241954 toggle @Override
87    public XDOM parse(String content, MacroTransformationContext macroContext, boolean transform, MetaData metadata,
88    boolean inline) throws MacroExecutionException
89    {
90    // If the content is empty return an empty list
91  241957 if (StringUtils.isEmpty(content)) {
92  28507 return new XDOM(Collections.<Block>emptyList(), metadata != null ? metadata : MetaData.EMPTY);
93    }
94   
95  213444 Syntax syntax = getCurrentSyntax(macroContext);
96   
97    // If there's no syntax specified in the Transformation throw an error
98  213448 if (syntax == null) {
99  0 throw new MacroExecutionException("Invalid Transformation: missing Syntax");
100    }
101   
102  213450 return createXDOM(content, macroContext, transform,
103    metadata, inline, syntax);
104    }
105   
106    /**
107    * creates XDOM.
108    */
 
109  213445 toggle private XDOM createXDOM(String content, MacroTransformationContext macroContext, boolean transform,
110    MetaData metadata, boolean inline, Syntax syntax) throws MacroExecutionException
111    {
112  213449 try {
113  213451 XDOM result = getSyntaxParser(syntax).parse(new StringReader(content));
114   
115  213450 if (metadata != null) {
116  1 result.getMetaData().addMetaData(metadata);
117    }
118   
119  213448 if (transform && macroContext.getTransformation() != null) {
120  30 TransformationContext txContext = new TransformationContext(result, syntax);
121  30 txContext.setId(macroContext.getId());
122  30 performTransformation((MutableRenderingContext) this.renderingContext,
123    macroContext.getTransformation(), txContext, result);
124    }
125   
126  213448 if (inline) {
127  1468 result = convertToInline(result);
128    }
129   
130  213449 return result;
131    } catch (Exception e) {
132  0 throw new MacroExecutionException("Failed to parse content [" + content + "]", e);
133    }
134    }
135   
136    /**
137    * Calls transformInContext on renderingContext.
138    */
 
139  30 toggle private void performTransformation(MutableRenderingContext renderingContext, Transformation transformation,
140    TransformationContext context, Block block) throws MacroExecutionException
141    {
142  30 try {
143  30 renderingContext.transformInContext(transformation, context, block);
144    } catch (Exception e) {
145  0 throw new MacroExecutionException("Failed to perform transformation", e);
146    }
147    }
148   
149    /**
150    * @param xdom the {@link XDOM} to convert
151    * @return an inline version of the passed {@link XDOM}
152    */
 
153  1468 toggle private XDOM convertToInline(XDOM xdom)
154    {
155  1468 List<Block> blocks = new ArrayList<Block>(xdom.getChildren());
156   
157    // TODO: use inline parser instead
158  1468 if (!blocks.isEmpty()) {
159  1468 this.parserUtils.removeTopLevelParagraph(blocks);
160   
161    // Make sure included macro is inline when script macro itself is inline
162  1468 Block block = blocks.get(0);
163  1468 if (block instanceof MacroBlock) {
164  16 MacroBlock macro = (MacroBlock) block;
165  16 if (!macro.isInline()) {
166  16 blocks.set(0, new MacroBlock(macro.getId(), macro.getParameters(), macro.getContent(), true));
167    }
168    }
169   
170  1468 xdom.setChildren(blocks);
171    }
172   
173  1468 return xdom;
174    }
175   
176    /**
177    * Get the parser for the current syntax.
178    *
179    * @param syntax the current syntax of the title content
180    * @return the parser for the current syntax
181    * @throws org.xwiki.rendering.macro.MacroExecutionException Failed to find source parser.
182    */
 
183  213446 toggle private Parser getSyntaxParser(Syntax syntax) throws MacroExecutionException
184    {
185  213446 try {
186  213447 return this.componentManager.getInstance(Parser.class, syntax.toIdString());
187    } catch (ComponentLookupException e) {
188  0 throw new MacroExecutionException("Failed to find source parser for syntax [" + syntax + "]", e);
189    }
190    }
191   
 
192  213445 toggle @Override
193    public Syntax getCurrentSyntax(MacroTransformationContext context)
194    {
195  213444 Syntax currentSyntax = context.getSyntax();
196   
197  213449 MacroBlock currentMacroBlock = context.getCurrentMacroBlock();
198   
199  213447 if (currentMacroBlock != null) {
200  213437 MetaDataBlock metaDataBlock =
201    currentMacroBlock.getFirstBlock(new MetadataBlockMatcher(MetaData.SYNTAX), Axes.ANCESTOR_OR_SELF);
202   
203  213441 if (metaDataBlock != null) {
204  213427 currentSyntax = (Syntax) metaDataBlock.getMetaData().getMetaData(MetaData.SYNTAX);
205    }
206    }
207   
208  213450 return currentSyntax;
209    }
210    }