Clover Coverage Report - XWiki Rendering - Parent POM 4.0-SNAPSHOT (Aggregated)
Coverage timestamp: Mon Mar 12 2012 18:03:13 CET
../../../../../img/srcFileCovDistChart8.png 68% of files have more coverage
29   145   13   9.67
12   87   0.45   3
3     4.33  
1    
 
  DefaultMacroContentParser       Line # 57 29 0% 13 12 72.7% 0.72727275
 
  (24)
 
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.listener.MetaData;
41    import org.xwiki.rendering.macro.MacroContentParser;
42    import org.xwiki.rendering.macro.MacroExecutionException;
43    import org.xwiki.rendering.parser.Parser;
44    import org.xwiki.rendering.syntax.Syntax;
45    import org.xwiki.rendering.transformation.MacroTransformationContext;
46    import org.xwiki.rendering.transformation.TransformationContext;
47    import org.xwiki.rendering.util.ParserUtils;
48   
49    /**
50    * Default implementation for {@link org.xwiki.rendering.macro.MacroContentParser}.
51    *
52    * @version $Id: 7a351dc95dc74690aabfff4e1cfbb3e3128f582c $
53    * @since 3.0M1
54    */
55    @Component
56    @Singleton
 
57    public class DefaultMacroContentParser implements MacroContentParser
58    {
59    /**
60    * Used to look up the syntax parser to use for parsing the content.
61    */
62    @Inject
63    private ComponentManager componentManager;
64   
65    /**
66    * Utility to remove the top level paragraph.
67    */
68    private ParserUtils parserUtils = new ParserUtils();
69   
 
70  35 toggle @Override
71    public XDOM parse(String content, MacroTransformationContext macroContext, boolean transform,
72    boolean removeTopLevelParagraph) throws MacroExecutionException
73    {
74    // If the content is empty return an empty list
75  35 if (StringUtils.isEmpty(content)) {
76  4 return new XDOM(Collections.<Block> emptyList());
77    }
78   
79  31 Syntax syntax = getCurrentSyntax(macroContext);
80   
81    // If there's no syntax specified in the Transformation throw an error
82  31 if (syntax == null) {
83  0 throw new MacroExecutionException("Invalid Transformation: missing Syntax");
84    }
85   
86  31 try {
87  31 XDOM result = getSyntaxParser(syntax).parse(new StringReader(content));
88   
89  31 if (transform && macroContext.getTransformation() != null) {
90  0 TransformationContext txContext = new TransformationContext(result, syntax);
91  0 txContext.setId(macroContext.getId());
92  0 try {
93  0 macroContext.getTransformation().transform(result, txContext);
94    } catch (Exception e) {
95  0 throw new MacroExecutionException("Failed to perform transformation", e);
96    }
97    }
98   
99  31 if (removeTopLevelParagraph) {
100  12 List<Block> children = new ArrayList<Block>(result.getChildren());
101  12 this.parserUtils.removeTopLevelParagraph(children);
102  12 result.setChildren(children);
103    }
104   
105  31 return result;
106    } catch (Exception e) {
107  0 throw new MacroExecutionException("Failed to parse content [" + content + "]", e);
108    }
109    }
110   
111    /**
112    * Get the parser for the current syntax.
113    *
114    * @param syntax the current syntax of the title content
115    * @return the parser for the current syntax
116    * @throws org.xwiki.rendering.macro.MacroExecutionException Failed to find source parser.
117    */
 
118  31 toggle private Parser getSyntaxParser(Syntax syntax) throws MacroExecutionException
119    {
120  31 try {
121  31 return this.componentManager.lookup(Parser.class, syntax.toIdString());
122    } catch (ComponentLookupException e) {
123  0 throw new MacroExecutionException("Failed to find source parser for syntax [" + syntax + "]", e);
124    }
125    }
126   
 
127  31 toggle @Override
128    public Syntax getCurrentSyntax(MacroTransformationContext context)
129    {
130  31 Syntax currentSyntax = context.getSyntax();
131   
132  31 MacroBlock currentMacroBlock = context.getCurrentMacroBlock();
133   
134  31 if (currentMacroBlock != null) {
135  31 MetaDataBlock metaDataBlock =
136    currentMacroBlock.getFirstBlock(new MetadataBlockMatcher(MetaData.SYNTAX), Axes.ANCESTOR_OR_SELF);
137   
138  31 if (metaDataBlock != null) {
139  31 currentSyntax = (Syntax) metaDataBlock.getMetaData().getMetaData(MetaData.SYNTAX);
140    }
141    }
142   
143  31 return currentSyntax;
144    }
145    }