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

File WikiPageUtil.java

 

Coverage histogram

../../../../img/srcFileCovDistChart5.png
74% of files have more coverage

Code metrics

34
66
11
1
312
180
42
0.64
6
11
3.82

Classes

Class Line # Actions
WikiPageUtil 31 66 0% 42 64
0.423423442.3%
 

Contributing tests

This file is covered by 348 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.wikimodel;
21   
22    import java.util.Arrays;
23   
24    /**
25    * This class contains some utility methods used for escaping xml strings as
26    * well as for encoding/decoding http parameters.
27    *
28    * @version $Id: 09da175c854238cedd21d5601b4f1cc2b4d4a3e3 $
29    * @since 4.0M1
30    */
 
31    public class WikiPageUtil
32    {
33    /**
34    * Reserved symbols - see RFC 2396 (http://www.ietf.org/rfc/rfc2396.txt)
35    */
36    private static final char[] HTTP_RESERVED_SYMBOLS = {
37    ';',
38    '/',
39    '?',
40    ':',
41    '@',
42    '&',
43    '=',
44    '+',
45    '$',
46    ','};
47   
48    /**
49    * Unreserved symbols - see RFC 2396 (http://www.ietf.org/rfc/rfc2396.txt)
50    */
51    private static final char[] HTTP_UNRESERVED_SYMBOLS = {
52    '-',
53    '_',
54    '.',
55    '!',
56    '~',
57    '*',
58    '\'',
59    '(',
60    ')',
61    '#'};
62   
 
63  12 toggle static {
64  12 Arrays.sort(HTTP_RESERVED_SYMBOLS);
65  12 Arrays.sort(HTTP_UNRESERVED_SYMBOLS);
66    }
67   
68    /**
69    * Returns the decoded http string - all special symbols, replaced by
70    * replaced by the %[HEX HEX] sequence, where [HEX HEX] is the hexadecimal
71    * code of the escaped symbol will be restored to its original characters
72    * (see RFC-2616 http://www.w3.org/Protocols/rfc2616/).
73    *
74    * @param str the string to decode
75    * @return the decoded string.
76    */
 
77  0 toggle public static String decodeHttpParams(String str)
78    {
79  0 if (str == null) {
80  0 return "";
81    }
82  0 StringBuffer buf = new StringBuffer();
83  0 char[] array = str.toCharArray();
84  0 for (int i = 0; i < array.length; i++) {
85  0 char ch = array[i];
86  0 if (ch == '%') {
87  0 if (i + 2 >= array.length) {
88  0 break;
89    }
90  0 int val = (array[++i] - '0');
91  0 val <<= 4;
92  0 val |= (array[++i] - '0');
93  0 ch = (char) val;
94    }
95  0 buf.append(ch);
96    }
97  0 return buf.toString();
98    }
99   
100    /**
101    * Returns the encoded string - all special symbols will be replaced by
102    * %[HEX HEX] sequence, where [HEX HEX] is the hexadecimal code of the
103    * escaped symbol (see RFC-2616
104    * http://www.w3.org/Protocols/rfc2616/rfc2616.html).
105    *
106    * @param str the string to encode
107    * @return the encoded string.
108    */
 
109  0 toggle public static String encodeHttpParams(String str)
110    {
111  0 if (str == null) {
112  0 return "";
113    }
114  0 StringBuffer buf = new StringBuffer();
115  0 char[] array = str.toCharArray();
116  0 for (int i = 0; i < array.length; i++) {
117  0 char ch = array[i];
118  0 if ((ch >= 'a' && ch <= 'z')
119    || (ch >= 'A' && ch <= 'Z')
120    || (ch >= '0' && ch <= '9')
121    || Character.isDigit(ch)
122    || Arrays.binarySearch(HTTP_RESERVED_SYMBOLS, ch) >= 0
123    || Arrays.binarySearch(HTTP_UNRESERVED_SYMBOLS, ch) >= 0)
124    {
125  0 buf.append(array[i]);
126    } else {
127  0 buf.append("%" + Integer.toHexString(array[i]));
128    }
129    }
130  0 return buf.toString();
131    }
132   
133    /**
134    * Returns the escaped attribute string.
135    *
136    * @param str the string to escape
137    * @return the escaped string.
138    */
 
139  743 toggle public static String escapeXmlAttribute(String str)
140    {
141  743 return escapeXmlString(str, true);
142    }
143   
144    /**
145    * Returns the escaped string.
146    *
147    * @param str the string to escape
148    * @return the escaped string.
149    */
 
150  5501 toggle public static String escapeXmlString(String str)
151    {
152  5501 return escapeXmlString(str, false);
153    }
154   
155    /**
156    * Returns the escaped string.
157    *
158    * @param str the string to escape
159    * @param escapeQuots if this flag is <code>true</code> then "'" and "\""
160    * symbols also will be escaped
161    * @return the escaped string.
162    */
 
163  6244 toggle public static String escapeXmlString(String str, boolean escapeQuots)
164    {
165  6244 if (str == null) {
166  3 return "";
167    }
168  6241 StringBuffer buf = new StringBuffer();
169  6241 char[] array = str.toCharArray();
170  38380 for (int i = 0; i < array.length; i++) {
171  32139 if (array[i] == '>'
172    || array[i] == '&'
173    || array[i] == '<'
174    || (escapeQuots && (array[i] == '\'' || array[i] == '"')))
175    {
176  14 buf.append("&#x" + Integer.toHexString(array[i]) + ";");
177    } else {
178  32125 buf.append(array[i]);
179    }
180    }
181  6241 return buf.toString();
182    }
183   
184    /**
185    * @return CDATA block corresponding to the given text
186    */
 
187  0 toggle public static String getCDATA(String text)
188    {
189  0 StringBuffer buf = new StringBuffer();
190  0 buf.append("<![CDATA[");
191  0 int startPos = 0;
192  0 while (startPos >= 0 && startPos < text.length()) {
193  0 int id = text.indexOf("]]>", startPos);
194  0 if (id >= 0) {
195  0 buf.append(text.substring(startPos, id));
196  0 buf.append("]]]]><![CDATA[>");
197  0 startPos += id + "]]>".length();
198    } else {
199  0 buf.append(text.substring(startPos));
200  0 startPos = -1;
201    }
202    }
203  0 buf.append("]]>");
204  0 return buf.toString();
205    }
206   
207    /**
208    * Returns <code>true</code> if the given value is a valid XML character.
209    * <p>
210    * See http://www.w3.org/TR/xml/#charsets
211    * </p>
212    *
213    * @param ch the value to check
214    * @return <code>true</code> if the given value is a valid XML character.
215    */
 
216  59 toggle public static boolean isValidXmlChar(int ch)
217    {
218  59 return (ch == 0x9)
219    || (ch == 0xA)
220    || (ch == 0xD)
221    || (ch >= 0x20 && ch <= 0xD7FF)
222    || (ch >= 0xE000 && ch <= 0xFFFD)
223    || (ch >= 0x10000 && ch <= 0x10FFFF);
224    }
225   
226    /**
227    * This method checks the given string and returns <code>true</code> if it is a valid XML name
228    * <p>
229    * See http://www.w3.org/TR/xml/#NT-Name.
230    * </p>
231    *
232    * @param tagName the name to check
233    * @param colonEnabled if this flag is <code>true</code> then this method accepts the ':' symbol in the name
234    * @return <code>true</code> if the given string is a valid XML name
235    */
 
236  160 toggle public static boolean isValidXmlName(String tagName, boolean colonEnabled)
237    {
238  160 boolean valid = false;
239  160 int len = tagName != null ? tagName.length() : 0;
240  1225 for (int i = 0; i < len; i++) {
241  1065 char ch = tagName.charAt(i);
242  1065 if (i == 0) {
243  160 valid = isValidXmlNameStartChar(ch, colonEnabled);
244    } else {
245  905 valid = isValidXmlNameChar(ch, colonEnabled);
246    }
247  1065 if (!valid) {
248  0 break;
249    }
250    }
251  160 return valid;
252    }
253   
254    /**
255    * Returns <code>true</code> if the given value is a valid XML name
256    * character.
257    * <p>
258    * See http://www.w3.org/TR/xml/#NT-NameChar.
259    * </p>
260    *
261    * @param ch the character to check
262    * @param colonEnabled if this flag is <code>true</code> then this method
263    * accepts the ':' symbol.
264    * @return <code>true</code> if the given value is a valid XML name
265    * character
266    */
 
267  905 toggle public static boolean isValidXmlNameChar(char ch, boolean colonEnabled)
268    {
269  905 return isValidXmlNameStartChar(ch, colonEnabled)
270    || (ch == '-')
271    || (ch == '.')
272    || (ch >= '0' && ch <= '9')
273    || (ch == 0xB7)
274    || (ch >= 0x0300 && ch <= 0x036F)
275    || (ch >= 0x203F && ch <= 0x2040);
276    }
277   
278    /**
279    * Returns <code>true</code> if the given value is a valid first character
280    * of an XML name.
281    * <p>
282    * See http://www.w3.org/TR/xml/#NT-NameStartChar.
283    * </p>
284    *
285    * @param ch the character to check
286    * @param colonEnabled if this flag is <code>true</code> then this method
287    * accepts the ':' symbol.
288    * @return <code>true</code> if the given value is a valid first character
289    * for an XML name
290    */
 
291  1065 toggle public static boolean isValidXmlNameStartChar(char ch, boolean colonEnabled)
292    {
293  1065 if (ch == ':') {
294  0 return colonEnabled;
295    }
296  1065 return (ch >= 'A' && ch <= 'Z')
297    || ch == '_'
298    || (ch >= 'a' && ch <= 'z')
299    || (ch >= 0xC0 && ch <= 0xD6)
300    || (ch >= 0xD8 && ch <= 0xF6)
301    || (ch >= 0xF8 && ch <= 0x2FF)
302    || (ch >= 0x370 && ch <= 0x37D)
303    || (ch >= 0x37F && ch <= 0x1FFF)
304    || (ch >= 0x200C && ch <= 0x200D)
305    || (ch >= 0x2070 && ch <= 0x218F)
306    || (ch >= 0x2C00 && ch <= 0x2FEF)
307    || (ch >= 0x3001 && ch <= 0xD7FF)
308    || (ch >= 0xF900 && ch <= 0xFDCF)
309    || (ch >= 0xFDF0 && ch <= 0xFFFD)
310    || (ch >= 0x10000 && ch <= 0xEFFFF);
311    }
312    }