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

File ParametersPrinter.java

 

Coverage histogram

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

Code metrics

4
29
7
1
166
68
10
0.34
4.14
7
1.43

Classes

Class Line # Actions
ParametersPrinter 32 29 0% 10 3
0.92592.5%
 

Contributing tests

This file is covered by 58 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.renderer;
21   
22    import java.util.Map;
23    import java.util.regex.Matcher;
24    import java.util.regex.Pattern;
25   
26    /**
27    * Generates syntax for a parameters group like macros and links.
28    *
29    * @version $Id: 8bdc2112ab5837a49f2ea7dfdaa94d34ff9b54e3 $
30    * @since 1.9RC2
31    */
 
32    public class ParametersPrinter
33    {
34    /**
35    * Quote character.
36    */
37    private static final String QUOTE = "\"";
38   
39    private final String escapedStrings;
40   
41    private char escapeChar;
42   
43    private Pattern escaped;
44   
45    private String replacement;
46   
47    /**
48    * Default constructor.
49    *
50    * @deprecated since 7.4.5 and 8.2RC1, use {@link #ParametersPrinter(char, String...) instead
51    */
 
52  0 toggle @Deprecated
53    public ParametersPrinter()
54    {
55  0 this.escapedStrings = Pattern.quote(QUOTE);
56    }
57   
58    /**
59    * @param escapeChar the character used to escape a meaningful string
60    * @param escapedStrings the meaningful strings to escape
61    * @since 7.4.5
62    * @since 8.2RC1
63    */
 
64  48 toggle public ParametersPrinter(char escapeChar, String... escapedStrings)
65    {
66  48 StringBuilder builder = new StringBuilder();
67   
68  48 builder.append(Pattern.quote(QUOTE));
69  48 for (String str : escapedStrings) {
70  45 builder.append('|');
71  45 builder.append(Pattern.quote(str));
72    }
73   
74  48 this.escapedStrings = builder.toString();
75   
76  48 setEscapeChar(escapeChar);
77    }
78   
 
79  48 toggle private void setEscapeChar(char escapeChar)
80    {
81  48 this.escapeChar = escapeChar;
82   
83  48 StringBuilder replacementBuilder = new StringBuilder();
84  48 replacementBuilder.append(Matcher.quoteReplacement(String.valueOf(escapeChar)));
85  48 replacementBuilder.append("$0");
86  48 this.replacement = replacementBuilder.toString();
87   
88  48 this.escaped = Pattern.compile(Pattern.quote(String.valueOf(this.escapeChar)) + '|' + this.escapedStrings);
89    }
90   
91    /**
92    * Print the parameters as a String.
93    *
94    * @param parameters the parameters to print
95    * @param escapeChar the character used in front of a special character when need to escape it
96    * @return the printed parameters
97    * @deprecated since 7.4.5 and 8.2RC1, use {@link #print(Map)} instead
98    */
 
99  2 toggle @Deprecated
100    public String print(Map<String, String> parameters, char escapeChar)
101    {
102  2 this.escapeChar = escapeChar;
103   
104  2 return print(parameters);
105    }
106   
107    /**
108    * Print the parameters as a String.
109    *
110    * @param parameters the parameters to print
111    * @return the printed parameters
112    * @since 7.4.5
113    * @since 8.2RC1
114    */
 
115  45 toggle public String print(Map<String, String> parameters)
116    {
117  45 StringBuilder builder = new StringBuilder();
118   
119  45 for (Map.Entry<String, String> entry : parameters.entrySet()) {
120  61 String value = entry.getValue();
121  61 String key = entry.getKey();
122   
123  61 if (key != null && value != null) {
124  61 if (builder.length() > 0) {
125  16 builder.append(' ');
126    }
127  61 builder.append(print(key, value));
128    }
129    }
130   
131  45 return builder.toString();
132    }
133   
134    /**
135    * Print a parameter as a String.
136    *
137    * @param parameterName the name of the parameter to print
138    * @param parameterValue the value of the parameter to print
139    * @param escapeChar the character used in front of a special character when need to escape it
140    * @return the printed parameter
141    * @deprecated since 7.4.5 and 8.2RC1, use {@link #print(String, String)} instead
142    */
 
143  10 toggle public String print(String parameterName, String parameterValue, char escapeChar)
144    {
145  10 this.escapeChar = escapeChar;
146   
147  10 return print(parameterName, parameterValue);
148    }
149   
150    /**
151    * Print a parameter as a String.
152    *
153    * @param parameterName the name of the parameter to print
154    * @param parameterValue the value of the parameter to print
155    * @return the printed parameter
156    * @since 7.4.5
157    * @since 8.2RC1
158    */
 
159  72 toggle public String print(String parameterName, String parameterValue)
160    {
161    // escape meaningfull strings
162  72 String value = this.escaped.matcher(parameterValue).replaceAll(this.replacement);
163   
164  72 return parameterName + "=" + QUOTE + value + QUOTE;
165    }
166    }