1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package com.xpn.xwiki.util

File Util.java

 

Coverage histogram

../../../../img/srcFileCovDistChart8.png
56% of files have more coverage

Code metrics

46
466
37
1
902
687
77
0.17
12.59
37
2.08

Classes

Class Line # Actions
Util 73 466 0% 77 109
0.8014571780.1%
 

Contributing tests

This file is covered by 200 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 com.xpn.xwiki.util;
21   
22    import java.io.BufferedReader;
23    import java.io.File;
24    import java.io.FileInputStream;
25    import java.io.IOException;
26    import java.io.InputStream;
27    import java.io.StreamTokenizer;
28    import java.io.StringReader;
29    import java.net.URLDecoder;
30    import java.net.URLEncoder;
31    import java.util.ArrayList;
32    import java.util.Date;
33    import java.util.HashMap;
34    import java.util.HashSet;
35    import java.util.Hashtable;
36    import java.util.List;
37    import java.util.Locale;
38    import java.util.Map;
39    import java.util.MissingResourceException;
40    import java.util.Set;
41    import java.util.Vector;
42   
43    import javax.servlet.http.Cookie;
44    import javax.servlet.http.HttpServletRequest;
45    import javax.xml.parsers.DocumentBuilderFactory;
46    import javax.xml.parsers.ParserConfigurationException;
47   
48    import org.apache.commons.codec.digest.DigestUtils;
49    import org.apache.commons.lang3.ArrayUtils;
50    import org.apache.commons.lang3.StringUtils;
51    import org.apache.oro.text.PatternCache;
52    import org.apache.oro.text.PatternCacheLRU;
53    import org.apache.oro.text.perl.Perl5Util;
54    import org.apache.oro.text.regex.MalformedPatternException;
55    import org.apache.oro.text.regex.MatchResult;
56    import org.apache.oro.text.regex.Pattern;
57    import org.apache.oro.text.regex.PatternMatcherInput;
58    import org.apache.oro.text.regex.Perl5Matcher;
59    import org.slf4j.Logger;
60    import org.slf4j.LoggerFactory;
61    import org.xml.sax.InputSource;
62    import org.xml.sax.SAXException;
63    import org.xwiki.container.Container;
64    import org.xwiki.xml.XMLUtils;
65   
66    import com.xpn.xwiki.XWikiContext;
67    import com.xpn.xwiki.XWikiException;
68    import com.xpn.xwiki.monitor.api.MonitorPlugin;
69    import com.xpn.xwiki.render.WikiSubstitution;
70    import com.xpn.xwiki.web.Utils;
71    import com.xpn.xwiki.web.XWikiRequest;
72   
 
73    public class Util
74    {
75    /**
76    * Encoding used for URL encoding/decoding. UTF-8 is the default encoding in RFC 3986, and is recommended by the W3
77    * consortium.
78    */
79    private static final String URL_ENCODING = "UTF-8";
80   
81    private static final Logger LOGGER = LoggerFactory.getLogger(Util.class);
82   
83    private static PatternCache patterns = new PatternCacheLRU(200);
84   
85    private Perl5Matcher matcher = new Perl5Matcher();
86   
87    private Perl5Util p5util = new Perl5Util(getPatterns());
88   
 
89  226 toggle public String substitute(String pattern, String text)
90    {
91  226 return getP5util().substitute(pattern, text);
92    }
93   
 
94  90 toggle public boolean match(String pattern, String text)
95    {
96  90 return getP5util().match(pattern, text);
97    }
98   
 
99  0 toggle public boolean matched()
100    {
101  0 return (getP5util().getMatch() != null);
102    }
103   
 
104  0 toggle public String substitute(String pattern, String substitution, String text)
105    {
106  0 WikiSubstitution subst = new WikiSubstitution(this, pattern);
107  0 subst.setSubstitution(substitution);
108  0 return subst.substitute(text);
109    }
110   
 
111  0 toggle public Perl5Matcher getMatcher()
112    {
113  0 return this.matcher;
114    }
115   
 
116  316 toggle public Perl5Util getP5util()
117    {
118  316 return this.p5util;
119    }
120   
 
121  2569 toggle public List<String> getAllMatches(String content, String spattern, int group) throws MalformedPatternException
122    {
123  2569 List<String> list = new ArrayList<String>();
124  2569 PatternMatcherInput input = new PatternMatcherInput(content);
125  2569 Pattern pattern = patterns.addPattern(spattern);
126  2577 while (this.matcher.contains(input, pattern)) {
127  8 MatchResult result = this.matcher.getMatch();
128  8 String smatch = result.group(group);
129  8 list.add(smatch);
130    }
131   
132  2569 return list;
133    }
134   
 
135  2569 toggle public List<String> getUniqueMatches(String content, String spattern, int group) throws MalformedPatternException
136    {
137    // Remove duplicate entries
138  2569 Set<String> uniqueMatches = new HashSet<String>();
139  2569 uniqueMatches.addAll(getAllMatches(content, spattern, group));
140   
141  2569 List<String> matches = new ArrayList<String>();
142  2569 matches.addAll(uniqueMatches);
143   
144  2569 return matches;
145    }
146   
 
147  0 toggle public static String cleanValue(String value)
148    {
149  0 value = StringUtils.replace(value, "\r\r\n", "%_N_%");
150  0 value = StringUtils.replace(value, "\r\n", "%_N_%");
151  0 value = StringUtils.replace(value, "\n\r", "%_N_%");
152  0 value = StringUtils.replace(value, "\r", "\n");
153  0 value = StringUtils.replace(value, "\n", "%_N_%");
154  0 value = StringUtils.replace(value, "\"", "%_Q_%");
155   
156  0 return value;
157    }
158   
 
159  0 toggle public static String restoreValue(String value)
160    {
161  0 value = StringUtils.replace(value, "%_N_%", "\n");
162  0 value = StringUtils.replace(value, "%_Q_%", "\"");
163   
164  0 return value;
165    }
166   
167    /**
168    * Create a Map from a string holding a space separated list of key=value pairs. If keys or values must contain
169    * spaces, they can be placed inside quotes, like <code>"this key"="a larger value"</code>. To use a quote as part
170    * of a key/value, use <code>%_Q_%</code>.
171    *
172    * @param mapString The string that must be parsed.
173    * @return A Map containing the keys and values. If a key is defined more than once, the last value is used.
174    */
 
175  0 toggle public static Hashtable<String, String> keyValueToHashtable(String mapString) throws IOException
176    {
177  0 Hashtable<String, String> result = new Hashtable<String, String>();
178  0 StreamTokenizer st = new StreamTokenizer(new BufferedReader(new StringReader(mapString)));
179  0 st.resetSyntax();
180  0 st.quoteChar('"');
181  0 st.wordChars('a', 'z');
182  0 st.wordChars('A', 'Z');
183  0 st.whitespaceChars(' ', ' ');
184  0 st.whitespaceChars('=', '=');
185  0 while (st.nextToken() != StreamTokenizer.TT_EOF) {
186  0 String key = st.sval;
187  0 st.nextToken();
188  0 String value = (st.sval != null) ? st.sval : "";
189  0 result.put(key, restoreValue(value));
190    }
191  0 return result;
192    }
193   
 
194  520 toggle public static PatternCache getPatterns()
195    {
196  520 return patterns;
197    }
198   
 
199  149 toggle public static Map<String, String[]> getObject(XWikiRequest request, String prefix)
200    {
201  149 @SuppressWarnings("unchecked")
202    Map<String, String[]> parameters = request.getParameterMap();
203  149 return getSubMap(parameters, prefix);
204    }
205   
 
206  149 toggle public static <T> Map<String, T> getSubMap(Map<String, T> map, String prefix)
207    {
208  149 Map<String, T> result = new HashMap<String, T>();
209  149 for (String name : map.keySet()) {
210  1747 if (name.startsWith(prefix + "_")) {
211  344 String newname = name.substring(prefix.length() + 1);
212  344 result.put(newname, map.get(name));
213  1403 } else if (name.equals(prefix)) {
214  76 result.put("", map.get(name));
215    }
216    }
217   
218  149 return result;
219    }
220   
 
221  0 toggle public static String getWeb(String fullname)
222    {
223  0 int i = fullname.lastIndexOf(".");
224   
225  0 return fullname.substring(0, i);
226    }
227   
 
228  0 toggle public Vector<String> split(String pattern, String text)
229    {
230  0 Vector<String> results = new Vector<String>();
231  0 getP5util().split(results, pattern, text);
232   
233  0 return results;
234    }
235   
 
236  21 toggle public static boolean contains(String name, String list, String sep)
237    {
238  21 String[] sarray = StringUtils.split(list, sep);
239   
240  21 return ArrayUtils.contains(sarray, name);
241    }
242   
 
243  8 toggle public static String noaccents(String text)
244    {
245  8 String temp = text;
246  8 temp = temp.replaceAll("\u00c0", "A");
247  8 temp = temp.replaceAll("\u00c1", "A");
248  8 temp = temp.replaceAll("\u00c2", "A");
249  8 temp = temp.replaceAll("\u00c3", "A");
250  8 temp = temp.replaceAll("\u00c4", "A");
251  8 temp = temp.replaceAll("\u00c5", "A");
252  8 temp = temp.replaceAll("\u0100", "A");
253  8 temp = temp.replaceAll("\u0102", "A");
254  8 temp = temp.replaceAll("\u0104", "A");
255  8 temp = temp.replaceAll("\u01cd", "A");
256  8 temp = temp.replaceAll("\u01de", "A");
257  8 temp = temp.replaceAll("\u01e0", "A");
258  8 temp = temp.replaceAll("\u01fa", "A");
259  8 temp = temp.replaceAll("\u0200", "A");
260  8 temp = temp.replaceAll("\u0202", "A");
261  8 temp = temp.replaceAll("\u0226", "A");
262  8 temp = temp.replaceAll("\u00e0", "a");
263  8 temp = temp.replaceAll("\u00e1", "a");
264  8 temp = temp.replaceAll("\u00e2", "a");
265  8 temp = temp.replaceAll("\u00e3", "a");
266  8 temp = temp.replaceAll("\u00e4", "a");
267  8 temp = temp.replaceAll("\u00e5", "a");
268  8 temp = temp.replaceAll("\u0101", "a");
269  8 temp = temp.replaceAll("\u0103", "a");
270  8 temp = temp.replaceAll("\u0105", "a");
271  8 temp = temp.replaceAll("\u01ce", "a");
272  8 temp = temp.replaceAll("\u01df", "a");
273  8 temp = temp.replaceAll("\u01e1", "a");
274  8 temp = temp.replaceAll("\u01fb", "a");
275  8 temp = temp.replaceAll("\u0201", "a");
276  8 temp = temp.replaceAll("\u0203", "a");
277  8 temp = temp.replaceAll("\u0227", "a");
278  8 temp = temp.replaceAll("\u00c6", "AE");
279  8 temp = temp.replaceAll("\u01e2", "AE");
280  8 temp = temp.replaceAll("\u01fc", "AE");
281  8 temp = temp.replaceAll("\u00e6", "ae");
282  8 temp = temp.replaceAll("\u01e3", "ae");
283  8 temp = temp.replaceAll("\u01fd", "ae");
284  8 temp = temp.replaceAll("\u008c", "OE");
285  8 temp = temp.replaceAll("\u0152", "OE");
286  8 temp = temp.replaceAll("\u009c", "oe");
287  8 temp = temp.replaceAll("\u0153", "oe");
288  8 temp = temp.replaceAll("\u00c7", "C");
289  8 temp = temp.replaceAll("\u0106", "C");
290  8 temp = temp.replaceAll("\u0108", "C");
291  8 temp = temp.replaceAll("\u010a", "C");
292  8 temp = temp.replaceAll("\u010c", "C");
293  8 temp = temp.replaceAll("\u00e7", "c");
294  8 temp = temp.replaceAll("\u0107", "c");
295  8 temp = temp.replaceAll("\u0109", "c");
296  8 temp = temp.replaceAll("\u010b", "c");
297  8 temp = temp.replaceAll("\u010d", "c");
298  8 temp = temp.replaceAll("\u00d0", "D");
299  8 temp = temp.replaceAll("\u010e", "D");
300  8 temp = temp.replaceAll("\u0110", "D");
301  8 temp = temp.replaceAll("\u00f0", "d");
302  8 temp = temp.replaceAll("\u010f", "d");
303  8 temp = temp.replaceAll("\u0111", "d");
304  8 temp = temp.replaceAll("\u00c8", "E");
305  8 temp = temp.replaceAll("\u00c9", "E");
306  8 temp = temp.replaceAll("\u00ca", "E");
307  8 temp = temp.replaceAll("\u00cb", "E");
308  8 temp = temp.replaceAll("\u0112", "E");
309  8 temp = temp.replaceAll("\u0114", "E");
310  8 temp = temp.replaceAll("\u0116", "E");
311  8 temp = temp.replaceAll("\u0118", "E");
312  8 temp = temp.replaceAll("\u011a", "E");
313  8 temp = temp.replaceAll("\u0204", "E");
314  8 temp = temp.replaceAll("\u0206", "E");
315  8 temp = temp.replaceAll("\u0228", "E");
316  8 temp = temp.replaceAll("\u00e8", "e");
317  8 temp = temp.replaceAll("\u00e9", "e");
318  8 temp = temp.replaceAll("\u00ea", "e");
319  8 temp = temp.replaceAll("\u00eb", "e");
320  8 temp = temp.replaceAll("\u0113", "e");
321  8 temp = temp.replaceAll("\u0115", "e");
322  8 temp = temp.replaceAll("\u0117", "e");
323  8 temp = temp.replaceAll("\u0119", "e");
324  8 temp = temp.replaceAll("\u011b", "e");
325  8 temp = temp.replaceAll("\u01dd", "e");
326  8 temp = temp.replaceAll("\u0205", "e");
327  8 temp = temp.replaceAll("\u0207", "e");
328  8 temp = temp.replaceAll("\u0229", "e");
329  8 temp = temp.replaceAll("\u011c", "G");
330  8 temp = temp.replaceAll("\u011e", "G");
331  8 temp = temp.replaceAll("\u0120", "G");
332  8 temp = temp.replaceAll("\u0122", "G");
333  8 temp = temp.replaceAll("\u01e4", "G");
334  8 temp = temp.replaceAll("\u01e6", "G");
335  8 temp = temp.replaceAll("\u01f4", "G");
336  8 temp = temp.replaceAll("\u011d", "g");
337  8 temp = temp.replaceAll("\u011f", "g");
338  8 temp = temp.replaceAll("\u0121", "g");
339  8 temp = temp.replaceAll("\u0123", "g");
340  8 temp = temp.replaceAll("\u01e5", "g");
341  8 temp = temp.replaceAll("\u01e7", "g");
342  8 temp = temp.replaceAll("\u01f5", "g");
343  8 temp = temp.replaceAll("\u0124", "H");
344  8 temp = temp.replaceAll("\u0126", "H");
345  8 temp = temp.replaceAll("\u021e", "H");
346  8 temp = temp.replaceAll("\u0125", "h");
347  8 temp = temp.replaceAll("\u0127", "h");
348  8 temp = temp.replaceAll("\u021f", "h");
349  8 temp = temp.replaceAll("\u00cc", "I");
350  8 temp = temp.replaceAll("\u00cd", "I");
351  8 temp = temp.replaceAll("\u00ce", "I");
352  8 temp = temp.replaceAll("\u00cf", "I");
353  8 temp = temp.replaceAll("\u0128", "I");
354  8 temp = temp.replaceAll("\u012a", "I");
355  8 temp = temp.replaceAll("\u012c", "I");
356  8 temp = temp.replaceAll("\u012e", "I");
357  8 temp = temp.replaceAll("\u0130", "I");
358  8 temp = temp.replaceAll("\u01cf", "I");
359  8 temp = temp.replaceAll("\u0208", "I");
360  8 temp = temp.replaceAll("\u020a", "I");
361  8 temp = temp.replaceAll("\u00ec", "i");
362  8 temp = temp.replaceAll("\u00ed", "i");
363  8 temp = temp.replaceAll("\u00ee", "i");
364  8 temp = temp.replaceAll("\u00ef", "i");
365  8 temp = temp.replaceAll("\u0129", "i");
366  8 temp = temp.replaceAll("\u012b", "i");
367  8 temp = temp.replaceAll("\u012d", "i");
368  8 temp = temp.replaceAll("\u012f", "i");
369  8 temp = temp.replaceAll("\u0131", "i");
370  8 temp = temp.replaceAll("\u01d0", "i");
371  8 temp = temp.replaceAll("\u0209", "i");
372  8 temp = temp.replaceAll("\u020b", "i");
373  8 temp = temp.replaceAll("\u0132", "IJ");
374  8 temp = temp.replaceAll("\u0133", "ij");
375  8 temp = temp.replaceAll("\u0134", "J");
376  8 temp = temp.replaceAll("\u0135", "j");
377  8 temp = temp.replaceAll("\u0136", "K");
378  8 temp = temp.replaceAll("\u01e8", "K");
379  8 temp = temp.replaceAll("\u0137", "k");
380  8 temp = temp.replaceAll("\u0138", "k");
381  8 temp = temp.replaceAll("\u01e9", "k");
382  8 temp = temp.replaceAll("\u0139", "L");
383  8 temp = temp.replaceAll("\u013b", "L");
384  8 temp = temp.replaceAll("\u013d", "L");
385  8 temp = temp.replaceAll("\u013f", "L");
386  8 temp = temp.replaceAll("\u0141", "L");
387  8 temp = temp.replaceAll("\u013a", "l");
388  8 temp = temp.replaceAll("\u013c", "l");
389  8 temp = temp.replaceAll("\u013e", "l");
390  8 temp = temp.replaceAll("\u0140", "l");
391  8 temp = temp.replaceAll("\u0142", "l");
392  8 temp = temp.replaceAll("\u0234", "l");
393  8 temp = temp.replaceAll("\u00d1", "N");
394  8 temp = temp.replaceAll("\u0143", "N");
395  8 temp = temp.replaceAll("\u0145", "N");
396  8 temp = temp.replaceAll("\u0147", "N");
397  8 temp = temp.replaceAll("\u014a", "N");
398  8 temp = temp.replaceAll("\u01f8", "N");
399  8 temp = temp.replaceAll("\u00f1", "n");
400  8 temp = temp.replaceAll("\u0144", "n");
401  8 temp = temp.replaceAll("\u0146", "n");
402  8 temp = temp.replaceAll("\u0148", "n");
403  8 temp = temp.replaceAll("\u0149", "n");
404  8 temp = temp.replaceAll("\u014b", "n");
405  8 temp = temp.replaceAll("\u01f9", "n");
406  8 temp = temp.replaceAll("\u0235", "n");
407  8 temp = temp.replaceAll("\u00d2", "O");
408  8 temp = temp.replaceAll("\u00d3", "O");
409  8 temp = temp.replaceAll("\u00d4", "O");
410  8 temp = temp.replaceAll("\u00d5", "O");
411  8 temp = temp.replaceAll("\u00d6", "O");
412  8 temp = temp.replaceAll("\u00d8", "O");
413  8 temp = temp.replaceAll("\u014c", "O");
414  8 temp = temp.replaceAll("\u014e", "O");
415  8 temp = temp.replaceAll("\u0150", "O");
416  8 temp = temp.replaceAll("\u01d1", "O");
417  8 temp = temp.replaceAll("\u01ea", "O");
418  8 temp = temp.replaceAll("\u01ec", "O");
419  8 temp = temp.replaceAll("\u01fe", "O");
420  8 temp = temp.replaceAll("\u020c", "O");
421  8 temp = temp.replaceAll("\u020e", "O");
422  8 temp = temp.replaceAll("\u022a", "O");
423  8 temp = temp.replaceAll("\u022c", "O");
424  8 temp = temp.replaceAll("\u022e", "O");
425  8 temp = temp.replaceAll("\u0230", "O");
426  8 temp = temp.replaceAll("\u00f2", "o");
427  8 temp = temp.replaceAll("\u00f3", "o");
428  8 temp = temp.replaceAll("\u00f4", "o");
429  8 temp = temp.replaceAll("\u00f5", "o");
430  8 temp = temp.replaceAll("\u00f6", "o");
431  8 temp = temp.replaceAll("\u00f8", "o");
432  8 temp = temp.replaceAll("\u014d", "o");
433  8 temp = temp.replaceAll("\u014f", "o");
434  8 temp = temp.replaceAll("\u0151", "o");
435  8 temp = temp.replaceAll("\u01d2", "o");
436  8 temp = temp.replaceAll("\u01eb", "o");
437  8 temp = temp.replaceAll("\u01ed", "o");
438  8 temp = temp.replaceAll("\u01ff", "o");
439  8 temp = temp.replaceAll("\u020d", "o");
440  8 temp = temp.replaceAll("\u020f", "o");
441  8 temp = temp.replaceAll("\u022b", "o");
442  8 temp = temp.replaceAll("\u022d", "o");
443  8 temp = temp.replaceAll("\u022f", "o");
444  8 temp = temp.replaceAll("\u0231", "o");
445  8 temp = temp.replaceAll("\u0156", "R");
446  8 temp = temp.replaceAll("\u0158", "R");
447  8 temp = temp.replaceAll("\u0210", "R");
448  8 temp = temp.replaceAll("\u0212", "R");
449  8 temp = temp.replaceAll("\u0157", "r");
450  8 temp = temp.replaceAll("\u0159", "r");
451  8 temp = temp.replaceAll("\u0211", "r");
452  8 temp = temp.replaceAll("\u0213", "r");
453  8 temp = temp.replaceAll("\u015a", "S");
454  8 temp = temp.replaceAll("\u015c", "S");
455  8 temp = temp.replaceAll("\u015e", "S");
456  8 temp = temp.replaceAll("\u0160", "S");
457  8 temp = temp.replaceAll("\u0218", "S");
458  8 temp = temp.replaceAll("\u015b", "s");
459  8 temp = temp.replaceAll("\u015d", "s");
460  8 temp = temp.replaceAll("\u015f", "s");
461  8 temp = temp.replaceAll("\u0161", "s");
462  8 temp = temp.replaceAll("\u0219", "s");
463  8 temp = temp.replaceAll("\u00de", "T");
464  8 temp = temp.replaceAll("\u0162", "T");
465  8 temp = temp.replaceAll("\u0164", "T");
466  8 temp = temp.replaceAll("\u0166", "T");
467  8 temp = temp.replaceAll("\u021a", "T");
468  8 temp = temp.replaceAll("\u00fe", "t");
469  8 temp = temp.replaceAll("\u0163", "t");
470  8 temp = temp.replaceAll("\u0165", "t");
471  8 temp = temp.replaceAll("\u0167", "t");
472  8 temp = temp.replaceAll("\u021b", "t");
473  8 temp = temp.replaceAll("\u0236", "t");
474  8 temp = temp.replaceAll("\u00d9", "U");
475  8 temp = temp.replaceAll("\u00da", "U");
476  8 temp = temp.replaceAll("\u00db", "U");
477  8 temp = temp.replaceAll("\u00dc", "U");
478  8 temp = temp.replaceAll("\u0168", "U");
479  8 temp = temp.replaceAll("\u016a", "U");
480  8 temp = temp.replaceAll("\u016c", "U");
481  8 temp = temp.replaceAll("\u016e", "U");
482  8 temp = temp.replaceAll("\u0170", "U");
483  8 temp = temp.replaceAll("\u0172", "U");
484  8 temp = temp.replaceAll("\u01d3", "U");
485  8 temp = temp.replaceAll("\u01d5", "U");
486  8 temp = temp.replaceAll("\u01d7", "U");
487  8 temp = temp.replaceAll("\u01d9", "U");
488  8 temp = temp.replaceAll("\u01db", "U");
489  8 temp = temp.replaceAll("\u0214", "U");
490  8 temp = temp.replaceAll("\u0216", "U");
491  8 temp = temp.replaceAll("\u00f9", "u");
492  8 temp = temp.replaceAll("\u00fa", "u");
493  8 temp = temp.replaceAll("\u00fb", "u");
494  8 temp = temp.replaceAll("\u00fc", "u");
495  8 temp = temp.replaceAll("\u0169", "u");
496  8 temp = temp.replaceAll("\u016b", "u");
497  8 temp = temp.replaceAll("\u016d", "u");
498  8 temp = temp.replaceAll("\u016f", "u");
499  8 temp = temp.replaceAll("\u0171", "u");
500  8 temp = temp.replaceAll("\u0173", "u");
501  8 temp = temp.replaceAll("\u01d4", "u");
502  8 temp = temp.replaceAll("\u01d6", "u");
503  8 temp = temp.replaceAll("\u01d8", "u");
504  8 temp = temp.replaceAll("\u01da", "u");
505  8 temp = temp.replaceAll("\u01dc", "u");
506  8 temp = temp.replaceAll("\u0215", "u");
507  8 temp = temp.replaceAll("\u0217", "u");
508  8 temp = temp.replaceAll("\u0174", "W");
509  8 temp = temp.replaceAll("\u0175", "w");
510  8 temp = temp.replaceAll("\u00dd", "Y");
511  8 temp = temp.replaceAll("\u0176", "Y");
512  8 temp = temp.replaceAll("\u0178", "Y");
513  8 temp = temp.replaceAll("\u0232", "Y");
514  8 temp = temp.replaceAll("\u00fd", "y");
515  8 temp = temp.replaceAll("\u00ff", "y");
516  8 temp = temp.replaceAll("\u0177", "y");
517  8 temp = temp.replaceAll("\u0233", "y");
518  8 temp = temp.replaceAll("\u0179", "Z");
519  8 temp = temp.replaceAll("\u017b", "Z");
520  8 temp = temp.replaceAll("\u017d", "Z");
521  8 temp = temp.replaceAll("\u017a", "z");
522  8 temp = temp.replaceAll("\u017c", "z");
523  8 temp = temp.replaceAll("\u017e", "z");
524  8 temp = temp.replaceAll("\u00df", "ss");
525   
526  8 return temp;
527    }
528   
 
529  0 toggle public static boolean isAlphaNumeric(String text)
530    {
531  0 return StringUtils.isAlphanumeric(text.replace('-', 'a').replace('.', 'a'));
532    }
533   
 
534  0 toggle public static String getName(String name)
535    {
536  0 int i0 = name.indexOf(":");
537  0 if (i0 != -1) {
538  0 name = name.substring(i0 + 1);
539  0 return name;
540    }
541   
542  0 if (name.indexOf(".") != -1) {
543  0 return name;
544    } else {
545  0 return "XWiki." + name;
546    }
547    }
548   
 
549  68 toggle public static String getName(String name, XWikiContext context)
550    {
551  68 String database = null;
552  68 int i0 = name.indexOf(":");
553  68 if (i0 != -1) {
554  57 database = name.substring(0, i0);
555  57 name = name.substring(i0 + 1);
556  57 context.setWikiId(database);
557  57 return name;
558    }
559   
560    // This does not make sense
561    // context.setWikiId(context.getWiki().getDatabase());
562  11 if (name.indexOf(".") != -1) {
563  11 return name;
564    } else {
565  0 return "XWiki." + name;
566    }
567    }
568   
 
569  13021 toggle public static Cookie getCookie(String cookieName, XWikiContext context)
570    {
571  13018 return getCookie(cookieName, context.getRequest());
572    }
573   
 
574  25523 toggle public static Cookie getCookie(String cookieName, HttpServletRequest request)
575    {
576  25527 Cookie[] cookies = request.getCookies();
577  25533 if (cookies != null) {
578  16398 for (Cookie cookie : cookies) {
579  43060 if (cookieName.equals(cookie.getName())) {
580  0 return (cookie);
581    }
582    }
583    }
584   
585  25537 return null;
586    }
587   
 
588  30 toggle public static String getHTMLExceptionMessage(XWikiException xe, XWikiContext context)
589    {
590  30 String title = XMLUtils.escape(xe.getMessage());
591  30 String text = XMLUtils.escape(xe.getFullMessage());
592   
593  30 return "<div class=\"xwikirenderingerror\" title=\"Read technical information related to this error\" "
594    + "style=\"cursor: pointer;\">" + title + "</div>"
595    + "<div class=\"xwikirenderingerrordescription hidden\"><pre>" + text + "</pre></div>";
596    }
597   
 
598  236547 toggle public static MonitorPlugin getMonitorPlugin(XWikiContext context)
599    {
600  236548 try {
601  236549 if ((context == null) || (context.getWiki() == null)) {
602  0 return null;
603    }
604   
605  236548 return (MonitorPlugin) context.getWiki().getPlugin("monitor", context);
606    } catch (Exception e) {
607  19446 return null;
608    }
609    }
610   
611    /**
612    * API to obtain a DOM document for the specified string
613    *
614    * @param str The parsed text
615    * @return A DOM document element corresponding to the string, or null on error
616    */
 
617  0 toggle public org.w3c.dom.Document getDOMForString(String str)
618    {
619  0 try {
620  0 return DocumentBuilderFactory.newInstance().newDocumentBuilder()
621    .parse(new InputSource(new StringReader(str)));
622    } catch (SAXException ex) {
623  0 LOGGER.warn("Cannot parse string:" + str, ex);
624    } catch (IOException ex) {
625  0 LOGGER.warn("Cannot parse string:" + str, ex);
626    } catch (ParserConfigurationException ex) {
627  0 LOGGER.warn("Cannot parse string:" + str, ex);
628    }
629   
630  0 return null;
631    }
632   
633    /**
634    * API to get a new DOM document
635    *
636    * @return a new DOM document element, or null on error
637    */
 
638  0 toggle public org.w3c.dom.Document getDOMDocument()
639    {
640  0 try {
641  0 return DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument();
642    } catch (ParserConfigurationException ex) {
643  0 LOGGER.warn("Cannot create DOM tree", ex);
644    }
645   
646  0 return null;
647    }
648   
649    /**
650    * API to protect Text from Radeox transformation
651    *
652    * @param text
653    * @return escaped text
654    * @deprecated dedicated to Radeox which is deprecated since a long time
655    */
 
656  0 toggle @Deprecated
657    public static String escapeText(String text)
658    {
659  0 text = text.replaceAll("http://", "&#104;ttp://");
660  0 text = text.replaceAll("ftp://", "&#102;tp://");
661  0 text = text.replaceAll("\\-", "&#45;");
662  0 text = text.replaceAll("\\*", "&#42;");
663  0 text = text.replaceAll("\\~", "&#126;");
664  0 text = text.replaceAll("\\[", "&#91;");
665  0 text = text.replaceAll("\\]", "&#93;");
666  0 text = text.replaceAll("\\{", "&#123;");
667  0 text = text.replaceAll("\\}", "&#125;");
668  0 text = text.replaceAll("\\1", "&#49;");
669   
670  0 return text;
671    }
672   
673    /**
674    * API to protect URLs from Radeox transformation
675    *
676    * @param url
677    * @return encoded URL
678    * @deprecated dedicated to Radeox which is deprecated since a long time
679    */
 
680  0 toggle @Deprecated
681    public static String escapeURL(String url)
682    {
683  0 url = url.replaceAll("\\~", "%7E");
684  0 url = url.replaceAll("\\[", "%5B");
685  0 url = url.replaceAll("\\]", "%5D");
686  0 url = url.replaceAll("\\{", "%7B");
687  0 url = url.replaceAll("\\}", "%7D");
688    // We should not encode the following char for non local urls
689    // since this might not be handle correctly by FF
690  0 if (url.indexOf("//") == -1) {
691  0 url = url.replaceAll("-", "%2D");
692  0 url = url.replaceAll("\\*", "%2A");
693    }
694   
695  0 return url;
696    }
697   
698    /**
699    * Translates a string into <code>application/x-www-form-urlencoded</code> format, so that it can be safely used in
700    * URIs, as a parameter value in a query string or as a segment in the URI path. This uses the UTF-8 encoding, the
701    * default encoding for URIs, as stated in <a href="http://tools.ietf.org/html/rfc3986#section-2.5">RFC 3986</a>.
702    *
703    * @param text the non encoded text
704    * @param context the current context
705    * @return encoded text
706    * @see #decodeURI(String, XWikiContext)
707    */
 
708  342 toggle public static String encodeURI(String text, XWikiContext context)
709    {
710  344 try {
711  344 return URLEncoder.encode(text, URL_ENCODING);
712    } catch (Exception e) {
713    // Should not happen (UTF-8 is always available), but if so, fail securely
714  0 return null;
715    }
716    }
717   
718    /**
719    * Decodes a <code>application/x-www-form-urlencoded</code> string, the reverse of
720    * {@link #encodeURI(String, XWikiContext)}. This uses the UTF-8 encoding, the default encoding for URIs, as stated
721    * in <a href="http://tools.ietf.org/html/rfc3986#section-2.5">RFC 3986</a>.
722    *
723    * @param text the encoded text
724    * @param context the current context
725    * @return decoded text
726    * @see #encodeURI(String, XWikiContext)
727    */
 
728  4046 toggle public static String decodeURI(String text, XWikiContext context)
729    {
730  4050 try {
731  4054 return URLDecoder.decode(text, URL_ENCODING);
732    } catch (Exception e) {
733    // Should not happen (UTF-8 is always available)
734  0 return text;
735    }
736    }
737   
738    /**
739    * Removes all non alpha numerical characters from the passed text. First tries to convert accented chars to their
740    * alpha numeric representation.
741    *
742    * @param text the text to convert
743    * @return the alpha numeric equivalent
744    */
 
745  4 toggle public static String convertToAlphaNumeric(String text)
746    {
747    // Start by removing accents
748  4 String textNoAccents = Util.noaccents(text);
749   
750    // Now remove all non alphanumeric chars
751  4 StringBuffer result = new StringBuffer(textNoAccents.length());
752  4 char[] testChars = textNoAccents.toCharArray();
753  4 for (char testChar : testChars) {
754  24 if (Character.isLetterOrDigit(testChar) && testChar < 128) {
755  14 result.append(testChar);
756    }
757    }
758   
759  4 return result.toString();
760    }
761   
 
762  343 toggle public static Date getFileLastModificationDate(String path)
763    {
764  343 try {
765  343 File f = new File(path);
766   
767  343 return (new Date(f.lastModified()));
768    } catch (Exception ex) {
769  0 return new Date();
770    }
771    }
772   
773    /**
774    * Validate a XML element name. XML elements must follow these naming rules :
775    * <ul>
776    * <li>Names can contain letters, numbers, and the following characters [., -, _].</li>
777    * <li>Names must not start with a number or punctuation character.</li>
778    * <li>Names must not start (case-insensitive) with the letters xml.</li>
779    * <li>Names cannot contain spaces.</li>
780    * </ul>
781    *
782    * @param elementName the XML element name to validate
783    * @return true if the element name is valid, false if it is not
784    */
 
785  112 toggle public static boolean isValidXMLElementName(String elementName)
786    {
787  112 if (elementName == null || elementName.equals("") || elementName.matches("(?i)^(xml).*")
788    || !elementName.matches("(^[a-zA-Z\\_]+[\\w\\.\\-]*$)")) {
789  9 return false;
790    }
791   
792  103 return true;
793    }
794   
795    /**
796    * Load resources from: 1. FileSystem 2. ServletContext 3. ClassPath in this order.
797    *
798    * @param resource resource path to load
799    * @return InputStream of resource or null if not found
800    */
 
801  393 toggle public static InputStream getResourceAsStream(String resource)
802    {
803  393 File file = new File(resource);
804  393 try {
805  393 if (file.exists()) {
806  37 return new FileInputStream(file);
807    }
808    } catch (Exception e) {
809    // Probably running under -security, which prevents calling File.exists()
810  0 LOGGER.debug("Failed load resource [" + resource + "] using a file path");
811    }
812  356 try {
813  356 Container container = Utils.getComponent(Container.class);
814  208 InputStream res = container.getApplicationContext().getResourceAsStream(resource);
815  39 if (res != null) {
816  39 return res;
817    }
818    } catch (Exception e) {
819  317 LOGGER.debug("Failed to load resource [" + resource + "] using the application context");
820    }
821   
822  317 return Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
823    }
824   
825    /**
826    * Normalize the given language code. Converts the given language code to lower case and checks its validity (i.e.
827    * whether it is an ISO 639 language code or the string "default").
828    *
829    * <pre>
830    * Util.normalizeLanguage(null) = null
831    * Util.normalizeLanguage("") = ""
832    * Util.normalizeLanguage(" ") = ""
833    * Util.normalizeLanguage("default") = "default"
834    * Util.normalizeLanguage("DeFault") = "default"
835    * Util.normalizeLanguage("invalid") = "default"
836    * Util.normalizeLanguage("en") = "en"
837    * Util.normalizeLanguage("DE_at") = "de_AT"
838    * </pre>
839    *
840    * @param languageCode the language code to normalize
841    * @return normalized language code or the string "default" if the code is invalid
842    */
 
843  197679 toggle public static String normalizeLanguage(String languageCode)
844    {
845  197713 if (languageCode == null) {
846  19606 return null;
847    }
848  178091 if (StringUtils.isBlank(languageCode)) {
849  31508 return "";
850    }
851    // handle language_COUNTRY case
852  146592 final String separator = "_";
853   
854  146593 String[] parts = StringUtils.split(languageCode.toLowerCase(), "_-.");
855  146567 String result = parts[0];
856  146587 if (parts.length > 1) {
857  172 parts[1] = parts[1].toUpperCase();
858    // NOTE cannot use Locale#toString(), because it would change some language codes
859  172 result = parts[0] + separator + parts[1];
860    }
861    // handle the "default" case
862  146570 final String defaultLanguage = "default";
863  146542 if (defaultLanguage.equals(result)) {
864  2 return defaultLanguage;
865    }
866  146553 try {
867  146539 Locale l = new Locale(parts[0], parts.length > 1 ? parts[1] : "");
868    // Will throw an exception if the language code is not valid
869  146534 l.getISO3Language();
870  145613 return result;
871    } catch (MissingResourceException ex) {
872  953 LOGGER.warn("Invalid language: " + languageCode);
873    }
874  953 return defaultLanguage;
875    }
876   
877    /**
878    * Get a likely unique 64bit hash representing the provided uid string. Use the MD5 hashing algorithm.
879    *
880    * @param uid an uid string usually provided by
881    * {@link org.xwiki.model.internal.reference.LocalUidStringEntityReferenceSerializer} or
882    * {@link org.xwiki.model.internal.reference.UidStringEntityReferenceSerializer}
883    * @return 64bit hash
884    * @since 4.0M1
885    */
 
886  1836957 toggle public static long getHash(String uid)
887    {
888  1837183 long hash = 0;
889   
890  1837399 try {
891  1837516 byte[] digest = DigestUtils.md5(uid);
892  18371715 for (int l = digest.length, i = Math.max(0, digest.length - 9); i < l; i++) {
893  16535060 hash = hash << 8 | ((long) digest[i] & 0xFF);
894    }
895    } catch (Exception ex) {
896  0 LOGGER.error("Id computation failed during MD5 processing", ex);
897  0 throw new RuntimeException("MD5 hash is required for id hash");
898    }
899   
900  1837479 return hash;
901    }
902    }