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

File ExpandedMacroBlock.java

 

Coverage histogram

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

Code metrics

8
19
4
1
118
56
10
0.53
4.75
4
2.5

Classes

Class Line # Actions
ExpandedMacroBlock 40 19 0% 10 2
0.935483993.5%
 

Contributing tests

This file is covered by 6 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.block;
21   
22    import java.util.Map;
23   
24    import org.xwiki.component.manager.ComponentLookupException;
25    import org.xwiki.component.manager.ComponentManager;
26    import org.xwiki.rendering.block.match.MetadataBlockMatcher;
27    import org.xwiki.rendering.listener.MetaData;
28    import org.xwiki.rendering.renderer.BlockRenderer;
29    import org.xwiki.rendering.renderer.printer.DefaultWikiPrinter;
30    import org.xwiki.rendering.renderer.printer.WikiPrinter;
31    import org.xwiki.rendering.syntax.Syntax;
32   
33    /**
34    * A {@link MacroBlock} whose content is expanded. The macro content can be accessed and modified through the child
35    * blocks. This type of macro block is suited only for macros whose content can be parsed into a list of block nodes.
36    *
37    * @since 3.0M3
38    * @version $Id: f913c5a7e3ef4d829f2c706a31a617aec1a8d128 $
39    */
 
40    public class ExpandedMacroBlock extends MacroBlock
41    {
42    /**
43    * The component used to reconstruct the macro content from the child blocks.
44    */
45    private final BlockRenderer contentRenderer;
46   
47    /**
48    * Used to get the {@link #contentRenderer} corresponding to the syntax specified by an ancestor meta data block.
49    */
50    private final ComponentManager componentManager;
51   
52    /**
53    * Creates a new expanded macro block.
54    *
55    * @param id the id of the macro
56    * @param parameters the parameters of the macro
57    * @param contentRenderer the component used to reconstruct the macro content from the child blocks
58    * @param inline indicates if the macro is located in an in-line content (e.g. inside a paragraph)
59    */
 
60  4 toggle public ExpandedMacroBlock(String id, Map<String, String> parameters, BlockRenderer contentRenderer, boolean inline)
61    {
62  4 this(id, parameters, contentRenderer, inline, null);
63    }
64   
65    /**
66    * Creates a new expanded macro block.
67    *
68    * @param id the id of the macro
69    * @param parameters the parameters of the macro
70    * @param contentRenderer the component used to reconstruct the macro content from the child blocks
71    * @param inline indicates if the macro is located in an in-line content (e.g. inside a paragraph)
72    * @param componentManager allows the expanded macro block to use a different syntax for its content if one of its
73    * ancestor meta data blocks specify the syntax
74    */
 
75  6 toggle public ExpandedMacroBlock(String id, Map<String, String> parameters, BlockRenderer contentRenderer, boolean inline,
76    ComponentManager componentManager)
77    {
78  6 super(id, parameters, inline);
79  6 this.contentRenderer = contentRenderer;
80  6 this.componentManager = componentManager;
81    }
82   
 
83  3 toggle @Override
84    public String getContent()
85    {
86    // Keep the macro content synchronized with the child blocks.
87  3 WikiPrinter printer = new DefaultWikiPrinter();
88  3 XDOM xdom;
89    // We need the begin/endDocument events so we wrap the child blocks in a XDOM block.
90  3 if (getChildren().size() == 1 && getChildren().get(0) instanceof XDOM) {
91  1 xdom = (XDOM) getChildren().get(0);
92    } else {
93  2 xdom = new XDOM(getChildren());
94    }
95  3 getContentRenderer().render(xdom, printer);
96  3 return printer.toString();
97    }
98   
 
99  3 toggle private BlockRenderer getContentRenderer()
100    {
101  3 if (this.componentManager != null) {
102  1 MetaDataBlock metaDataBlock =
103    getFirstBlock(new MetadataBlockMatcher(MetaData.SYNTAX), Axes.ANCESTOR_OR_SELF);
104  1 if (metaDataBlock != null) {
105  1 Syntax syntax = (Syntax) metaDataBlock.getMetaData().getMetaData(MetaData.SYNTAX);
106  1 if (this.componentManager.hasComponent(BlockRenderer.class, syntax.toIdString())) {
107  1 try {
108  1 return this.componentManager.getInstance(BlockRenderer.class, syntax.toIdString());
109    } catch (ComponentLookupException e) {
110    // Fall-back on the default content renderer;
111    }
112    }
113    }
114    }
115   
116  2 return this.contentRenderer;
117    }
118    }