Clover Coverage Report - XWiki Rendering - Parent POM 4.0-SNAPSHOT (Aggregated)
Coverage timestamp: Mon Mar 12 2012 18:03:13 CET
../../../../../../img/srcFileCovDistChart10.png 0% of files have more coverage
51   167   26   4.25
24   112   0.51   12
12     2.17  
1    
 
  AbstractVelocityMacroConverter       Line # 36 51 0% 26 6 93.1% 0.9310345
 
  (11)
 
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.parser.xwiki10.macro;
21   
22    import java.util.ArrayList;
23    import java.util.LinkedHashMap;
24    import java.util.List;
25    import java.util.Map;
26   
27    import org.apache.commons.lang3.StringUtils;
28    import org.xwiki.rendering.parser.xwiki10.FilterContext;
29   
30    /**
31    * Base class for Velocity macros converters.
32    *
33    * @version $Id: f2205a283fc97fe9df6069abf383cbcf510523a3 $
34    * @since 1.8M1
35    */
 
36    public abstract class AbstractVelocityMacroConverter implements VelocityMacroConverter
37    {
38    private String name;
39   
40    private List<String> parameterNameList = new ArrayList<String>();
41   
 
42  10 toggle protected AbstractVelocityMacroConverter()
43    {
44   
45    }
46   
 
47  7 toggle protected AbstractVelocityMacroConverter(String name)
48    {
49  7 this.name = name;
50    }
51   
 
52  22 toggle protected void addParameterName(String parameterName)
53    {
54  22 this.parameterNameList.add(parameterName);
55    }
56   
 
57  30 toggle @Override
58    public boolean protectResult()
59    {
60  30 return true;
61    }
62   
 
63  55 toggle @Override
64    public boolean isInline()
65    {
66  55 return true;
67    }
68   
 
69  47 toggle protected String convertName(String name)
70    {
71  47 return this.name == null ? name : this.name;
72    }
73   
 
74  31 toggle protected Map<String, String> convertParameters(List<String> parameters)
75    {
76  31 Map<String, String> parameters20 = new LinkedHashMap<String, String>(parameters.size());
77   
78  72 for (int index = 0; index < parameters.size(); ++index) {
79  41 convertParameter(parameters20, index, parameters.get(index));
80    }
81   
82  31 return parameters20;
83    }
84   
 
85  41 toggle protected void convertParameter(Map<String, String> parameters20, int index, String value)
86    {
87  41 if (this.parameterNameList.size() > index) {
88  25 String key = this.parameterNameList.get(index);
89  25 if (key != null) {
90  25 parameters20.put(this.parameterNameList.get(index), value);
91    }
92    }
93    }
94   
 
95  15 toggle protected String convertContent(List<String> parameters, FilterContext context)
96    {
97  15 return null;
98    }
99   
 
100  16 toggle protected String cleanQuotes(String value)
101    {
102  16 String cleaned = value;
103   
104  16 if (!StringUtils.isEmpty(cleaned)) {
105  16 char firstChar = value.charAt(0);
106  16 char lastChar = value.charAt(value.length() - 1);
107   
108  16 if ((firstChar == '"' || firstChar == '\'') && firstChar == lastChar) {
109  16 cleaned = cleaned.substring(1, cleaned.length() - 1);
110    }
111    }
112   
113  16 return cleaned;
114    }
115   
 
116  31 toggle @Override
117    public String convert(String name, List<String> parameters, FilterContext context)
118    {
119  31 StringBuffer begin = new StringBuffer();
120  31 String content = convertContent(parameters, context);
121  31 Map<String, String> params = convertParameters(parameters);
122   
123  31 begin.append("{{");
124  31 begin.append(convertName(name));
125  31 if (params.size() > 0) {
126  14 begin.append(' ');
127  14 appendParameters(begin, params);
128    }
129   
130  31 StringBuffer result = new StringBuffer();
131   
132  31 if (content != null) {
133  16 begin.append("}}");
134  16 result.append(!protectResult() ? context.addProtectedContent(begin.toString(), isInline()) : begin);
135   
136  16 result.append(content);
137   
138  16 StringBuffer end = new StringBuffer();
139  16 end.append("{{/");
140  16 end.append(convertName(name));
141  16 end.append("}}");
142  16 result.append(!protectResult() ? context.addProtectedContent(end.toString(), isInline()) : end);
143    } else {
144  15 begin.append("/");
145  15 begin.append("}}");
146   
147  15 result.append(!protectResult() ? context.addProtectedContent(begin.toString(), isInline()) : begin);
148    }
149   
150  31 return result.toString();
151    }
152   
 
153  14 toggle protected void appendParameters(StringBuffer result, Map<String, String> parameters)
154    {
155  14 StringBuffer parametersSB = new StringBuffer();
156  14 for (Map.Entry<String, String> parameter : parameters.entrySet()) {
157  26 if (parametersSB.length() > 0) {
158  12 parametersSB.append(" ");
159    }
160  26 parametersSB.append(parameter.getKey());
161  26 parametersSB.append("=");
162  26 parametersSB.append(parameter.getValue());
163    }
164   
165  14 result.append(parametersSB);
166    }
167    }