1. Project Clover database Sat Feb 2 2019 06:45:20 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
167
69
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: e01b33738655f73ab7d11f031974f4ea4010efd8 $
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  78 toggle public ParametersPrinter(char escapeChar, String... escapedStrings)
65    {
66  78 StringBuilder builder = new StringBuilder();
67   
68  78 builder.append(Pattern.quote(QUOTE));
69  78 for (String str : escapedStrings) {
70  48 builder.append('|');
71  48 builder.append(Pattern.quote(str));
72    }
73   
74  78 this.escapedStrings = builder.toString();
75   
76  78 setEscapeChar(escapeChar);
77    }
78   
 
79  90 toggle private void setEscapeChar(char escapeChar)
80    {
81  90 this.escapeChar = escapeChar;
82   
83  90 StringBuilder replacementBuilder = new StringBuilder();
84  90 replacementBuilder.append(Matcher.quoteReplacement(String.valueOf(escapeChar)));
85  90 replacementBuilder.append("$0");
86  90 this.replacement = replacementBuilder.toString();
87   
88  90 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 setEscapeChar(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  95 toggle public String print(Map<String, String> parameters)
116    {
117  95 StringBuilder builder = new StringBuilder();
118   
119  95 for (Map.Entry<String, String> entry : parameters.entrySet()) {
120  115 String value = entry.getValue();
121  115 String key = entry.getKey();
122   
123  115 if (key != null && value != null) {
124  115 if (builder.length() > 0) {
125  20 builder.append(' ');
126    }
127  115 builder.append(print(key, value));
128    }
129    }
130   
131  95 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 @Deprecated
144    public String print(String parameterName, String parameterValue, char escapeChar)
145    {
146  10 setEscapeChar(escapeChar);
147   
148  10 return print(parameterName, parameterValue);
149    }
150   
151    /**
152    * Print a parameter as a String.
153    *
154    * @param parameterName the name of the parameter to print
155    * @param parameterValue the value of the parameter to print
156    * @return the printed parameter
157    * @since 7.4.5
158    * @since 8.2RC1
159    */
 
160  126 toggle public String print(String parameterName, String parameterValue)
161    {
162    // escape meaningfull strings
163  126 String value = this.escaped.matcher(parameterValue).replaceAll(this.replacement);
164   
165  126 return parameterName + "=" + QUOTE + value + QUOTE;
166    }
167    }