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

File MacroErrorManager.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

2
14
5
1
116
53
7
0.5
2.8
5
1.4

Classes

Class Line # Actions
MacroErrorManager 40 14 0% 7 0
1.0100%
 

Contributing tests

This file is covered by 444 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.transformation.macro;
21   
22    import java.util.List;
23   
24    import org.xwiki.rendering.block.Block;
25    import org.xwiki.rendering.block.FormatBlock;
26    import org.xwiki.rendering.block.GroupBlock;
27    import org.xwiki.rendering.block.MacroBlock;
28    import org.xwiki.rendering.block.MacroMarkerBlock;
29    import org.xwiki.rendering.block.XDOM;
30    import org.xwiki.rendering.block.match.ClassBlockMatcher;
31    import org.xwiki.rendering.block.match.OrBlockMatcher;
32    import org.xwiki.rendering.util.ErrorBlockGenerator;
33   
34    /**
35    * Generates Blocks to signify that a Macro has failed to execute.
36    *
37    * @version $Id: e07040b762952ff9bd1bc6eee667f5cc7596e687 $
38    * @since 4.3M2
39    */
 
40    public class MacroErrorManager
41    {
42    private ErrorBlockGenerator errorBlockGenerator;
43   
44    /**
45    * @param errorBlockGenerator the error generator to use to generate the error blocks
46    */
 
47  489 toggle public MacroErrorManager(ErrorBlockGenerator errorBlockGenerator)
48    {
49  489 this.errorBlockGenerator = errorBlockGenerator;
50    }
51   
52    /**
53    * Generates Blocks to signify that the passed Macro Block has failed to execute.
54    *
55    * @param macroToReplace the block for the macro that failed to execute and that we'll replace with Block
56    * showing to the user that macro has failed
57    * @param message the message to display to the user in place of the macro result
58    * @param description the long description of the error to display to the user in place of the macro result
59    */
 
60  218 toggle public void generateError(MacroBlock macroToReplace, String message, String description)
61    {
62  218 List<Block> errorBlocks =
63    this.errorBlockGenerator.generateErrorBlocks(message, description, macroToReplace.isInline());
64  218 macroToReplace.getParent().replaceChild(wrapInMacroMarker(macroToReplace, errorBlocks), macroToReplace);
65    }
66   
67    /**
68    * Generates Blocks to signify that the passed Macro Block has failed to execute.
69    *
70    * @param macroToReplace the block for the macro that failed to execute and that we'll replace with Block
71    * showing to the user that macro has failed
72    * @param message the message to display to the user in place of the macro result
73    * @param throwable the exception for the failed macro execution to display to the user in place of the macro result
74    */
 
75  56 toggle public void generateError(MacroBlock macroToReplace, String message, Throwable throwable)
76    {
77  56 List<Block> errorBlocks =
78    this.errorBlockGenerator.generateErrorBlocks(message, throwable, macroToReplace.isInline());
79  55 macroToReplace.getParent().replaceChild(wrapInMacroMarker(macroToReplace, errorBlocks), macroToReplace);
80    }
81   
82    /**
83    * @param xdom the XDOM on which to check if there's a macro error
84    * @return true if the passed XDOM contains a macro error or false otherwise
85    */
 
86  3 toggle public boolean containsError(XDOM xdom)
87    {
88  3 boolean foundError = false;
89  3 List<Block> groupAndFormatBlocks = xdom.getBlocks(
90    new OrBlockMatcher(
91    new ClassBlockMatcher(GroupBlock.class),
92    new ClassBlockMatcher(FormatBlock.class)),
93    Block.Axes.DESCENDANT);
94  3 for (Block block : groupAndFormatBlocks) {
95  3 String classParameter = block.getParameters().get(ErrorBlockGenerator.CLASS_ATTRIBUTE_NAME);
96  3 if (classParameter != null && classParameter.contains(ErrorBlockGenerator.CLASS_ATTRIBUTE_MESSAGE_VALUE)) {
97  1 foundError = true;
98  1 break;
99    }
100    }
101  3 return foundError;
102    }
103   
104    /**
105    * Wrap the output of a macro block with a {@link org.xwiki.rendering.block.MacroMarkerBlock}.
106    *
107    * @param macroBlockToWrap the block that should be replaced
108    * @param newBlocks list of blocks to wrap
109    * @return the wrapper
110    */
 
111  273 toggle private Block wrapInMacroMarker(MacroBlock macroBlockToWrap, List<Block> newBlocks)
112    {
113  273 return new MacroMarkerBlock(macroBlockToWrap.getId(), macroBlockToWrap.getParameters(),
114    macroBlockToWrap.getContent(), newBlocks, macroBlockToWrap.isInline());
115    }
116    }