1. Project Clover database Wed Oct 21 2015 17:09:14 CEST
  2. Package org.xwiki.rendering.wikimodel

File WikiPageUtil.java

 

Coverage histogram

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

Code metrics

34
66
11
1
306
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 227 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: 249a2a152e02e908797802b8ca460c0f3c59a166 $
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  7 toggle static {
64  7 Arrays.sort(HTTP_RESERVED_SYMBOLS);
65  7 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  793 toggle public static String escapeXmlAttribute(String str)
140    {
141  793 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  2307 toggle public static String escapeXmlString(String str)
151    {
152  2307 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  3100 toggle public static String escapeXmlString(String str, boolean escapeQuots)
164    {
165  3100 if (str == null) {
166  3 return "";
167    }
168  3097 StringBuffer buf = new StringBuffer();
169  3097 char[] array = str.toCharArray();
170  20554 for (int i = 0; i < array.length; i++) {
171  17457 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  17443 buf.append(array[i]);
179    }
180    }
181  3097 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    *
210    * @param ch the value to check
211    * @return <code>true</code> if the given value is a valid XML character.
212    * @see http://www.w3.org/TR/xml/#charsets
213    */
 
214  59 toggle public static boolean isValidXmlChar(int ch)
215    {
216  59 return (ch == 0x9)
217    || (ch == 0xA)
218    || (ch == 0xD)
219    || (ch >= 0x20 && ch <= 0xD7FF)
220    || (ch >= 0xE000 && ch <= 0xFFFD)
221    || (ch >= 0x10000 && ch <= 0x10FFFF);
222    }
223   
224    /**
225    * This method checks the given string and returns <code>true</code> if it
226    * is a valid XML name
227    *
228    * @param tagName the name to check
229    * @param colonEnabled if this flag is <code>true</code> then this method
230    * accepts the ':' symbol in the name
231    * @return <code>true</code> if the given string is a valid XML name
232    * @see http://www.w3.org/TR/xml/#NT-Name
233    */
 
234  160 toggle public static boolean isValidXmlName(String tagName, boolean colonEnabled)
235    {
236  160 boolean valid = false;
237  160 int len = tagName != null ? tagName.length() : 0;
238  1225 for (int i = 0; i < len; i++) {
239  1065 char ch = tagName.charAt(i);
240  1065 if (i == 0) {
241  160 valid = isValidXmlNameStartChar(ch, colonEnabled);
242    } else {
243  905 valid = isValidXmlNameChar(ch, colonEnabled);
244    }
245  1065 if (!valid) {
246  0 break;
247    }
248    }
249  160 return valid;
250    }
251   
252    /**
253    * Returns <code>true</code> if the given value is a valid XML name
254    * character.
255    *
256    * @param ch the character to check
257    * @param colonEnabled if this flag is <code>true</code> then this method
258    * accepts the ':' symbol.
259    * @return <code>true</code> if the given value is a valid XML name
260    * character
261    * @see http://www.w3.org/TR/xml/#NT-NameChar
262    */
 
263  905 toggle public static boolean isValidXmlNameChar(char ch, boolean colonEnabled)
264    {
265  905 return isValidXmlNameStartChar(ch, colonEnabled)
266    || (ch == '-')
267    || (ch == '.')
268    || (ch >= '0' && ch <= '9')
269    || (ch == 0xB7)
270    || (ch >= 0x0300 && ch <= 0x036F)
271    || (ch >= 0x203F && ch <= 0x2040);
272    }
273   
274    /**
275    * Returns <code>true</code> if the given value is a valid first character
276    * of an XML name.
277    *
278    * @param ch the character to check
279    * @param colonEnabled if this flag is <code>true</code> then this method
280    * accepts the ':' symbol.
281    * @return <code>true</code> if the given value is a valid first character
282    * for an XML name
283    * @see http://www.w3.org/TR/xml/#NT-NameStartChar
284    */
 
285  1065 toggle public static boolean isValidXmlNameStartChar(char ch, boolean colonEnabled)
286    {
287  1065 if (ch == ':') {
288  0 return colonEnabled;
289    }
290  1065 return (ch >= 'A' && ch <= 'Z')
291    || ch == '_'
292    || (ch >= 'a' && ch <= 'z')
293    || (ch >= 0xC0 && ch <= 0xD6)
294    || (ch >= 0xD8 && ch <= 0xF6)
295    || (ch >= 0xF8 && ch <= 0x2FF)
296    || (ch >= 0x370 && ch <= 0x37D)
297    || (ch >= 0x37F && ch <= 0x1FFF)
298    || (ch >= 0x200C && ch <= 0x200D)
299    || (ch >= 0x2070 && ch <= 0x218F)
300    || (ch >= 0x2C00 && ch <= 0x2FEF)
301    || (ch >= 0x3001 && ch <= 0xD7FF)
302    || (ch >= 0xF900 && ch <= 0xFDCF)
303    || (ch >= 0xFDF0 && ch <= 0xFFFD)
304    || (ch >= 0x10000 && ch <= 0xEFFFF);
305    }
306    }