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

File MacroBlock.java

 

Coverage histogram

../../../../img/srcFileCovDistChart8.png
54% of files have more coverage

Code metrics

4
24
8
1
156
64
11
0.46
3
8
1.38

Classes

Class Line # Actions
MacroBlock 38 24 0% 11 7
0.805555680.6%
 

Contributing tests

This file is covered by 322 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.apache.commons.lang3.builder.EqualsBuilder;
25    import org.apache.commons.lang3.builder.HashCodeBuilder;
26    import org.xwiki.rendering.listener.Listener;
27   
28    /**
29    * Represents a Macro (standalone or inline) defined in a page.
30    * <p>
31    * Note: You can get macro parameters using {@link #getParameters()} for example. Macro block is reusing Block standard
32    * custom parameters API since macro by definition already have parameters and don't need also block parameters. So in
33    * this case MacroBlock parameters and Block parameters are the same thing.
34    *
35    * @version $Id: 2921463a9cf077b05945ebe1c5168a88ffad463d $
36    * @since 1.8M2
37    */
 
38    public class MacroBlock extends AbstractBlock
39    {
40    /**
41    * @see #getId
42    */
43    private String id;
44   
45    /**
46    * The macro content for macro that have content. Otherwise it's null.
47    */
48    private String content;
49   
50    /**
51    * The macro is located in a inline content (like paragraph, etc.).
52    */
53    private boolean inline;
54   
55    /**
56    * @param id the id of the macro
57    * @param parameters the parameters of the macro
58    * @param isInline indicate if the macro is located in a inline content (like paragraph, etc.)
59    */
 
60  78 toggle public MacroBlock(String id, Map<String, String> parameters, boolean isInline)
61    {
62  78 this(id, parameters, null, isInline);
63    }
64   
65    /**
66    * @param id the id of the macro
67    * @param parameters the parameters of the macro
68    * @param content the content of the macro. Null if the macro does not have content
69    * @param isInline indicate if the macro is located in a inline content (like paragraph, etc.)
70    */
 
71  11335 toggle public MacroBlock(String id, Map<String, String> parameters, String content, boolean isInline)
72    {
73  11334 super(parameters);
74   
75  11336 this.id = id;
76  11336 this.content = content;
77  11335 this.inline = isInline;
78    }
79   
80    /**
81    * @return the macro id (eg "toc" for the TOC Macro).
82    * @since 2.0M3
83    */
 
84  168537 toggle public String getId()
85    {
86  168535 return this.id;
87    }
88   
89    /**
90    * @return the macro content.
91    */
 
92  37676 toggle public String getContent()
93    {
94  37676 return this.content;
95    }
96   
97    /**
98    * @return if true the macro is located in a inline content (like paragraph, etc.).
99    */
 
100  35951 toggle public boolean isInline()
101    {
102  35951 return this.inline;
103    }
104   
 
105  418 toggle @Override
106    public void traverse(Listener listener)
107    {
108    // Don't do anything here since we want the Macro Transformer component to take in charge
109    // Macro execution. This is because Macro execution is a complex process that involves:
110    // * computing the order in which the macros should be evaluated. For example the TOC macro
111    // should evaluate last since other macros can contribute headers/sections blocks.
112    // * some macros need to modify blocks in the XDOM object
113    // * macro execution is a multi-pass process
114    // In essence the Macro Transformer will replace all MacroBlock blocks with other Blocks
115    // generated from the execution of the Macros when XDOM.traverse() is called there
116    // won't be any MacroBlock.traverse() method called at all.
117   
118    // Note: We're calling the event to let other listener downstream decide what to do with it.
119    // In practice as described above this method will never get called when the whole rendering
120    // process is executed. This does get called during our unit tests though.
121  418 listener.onMacro(getId(), getParameters(), getContent(), isInline());
122    }
123   
 
124  12 toggle @Override
125    public boolean equals(Object obj)
126    {
127  12 if (obj == this) {
128  9 return true;
129    }
130   
131  3 if (obj instanceof MacroBlock && super.equals(obj)) {
132  1 EqualsBuilder builder = new EqualsBuilder();
133   
134  1 builder.append(getContent(), ((MacroBlock) obj).getContent());
135  1 builder.append(getId(), ((MacroBlock) obj).getId());
136  1 builder.append(isInline(), ((MacroBlock) obj).isInline());
137   
138  1 return builder.isEquals();
139    }
140   
141  2 return false;
142    }
143   
 
144  0 toggle @Override
145    public int hashCode()
146    {
147  0 HashCodeBuilder builder = new HashCodeBuilder();
148   
149  0 builder.appendSuper(super.hashCode());
150  0 builder.append(getContent());
151  0 builder.append(getId());
152  0 builder.append(isInline());
153   
154  0 return builder.toHashCode();
155    }
156    }