|
1 /* |
|
2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. |
|
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
|
4 * |
|
5 * This code is free software; you can redistribute it and/or modify it |
|
6 * under the terms of the GNU General Public License version 2 only, as |
|
7 * published by the Free Software Foundation. Oracle designates this |
|
8 * particular file as subject to the "Classpath" exception as provided |
|
9 * by Oracle in the LICENSE file that accompanied this code. |
|
10 * |
|
11 * This code is distributed in the hope that it will be useful, but WITHOUT |
|
12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
14 * version 2 for more details (a copy is included in the LICENSE file that |
|
15 * accompanied this code). |
|
16 * |
|
17 * You should have received a copy of the GNU General Public License version |
|
18 * 2 along with this work; if not, write to the Free Software Foundation, |
|
19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
|
20 * |
|
21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
|
22 * or visit www.oracle.com if you need additional information or have any |
|
23 * questions. |
|
24 */ |
|
25 |
|
26 package com.sun.xml.internal.messaging.saaj.util; |
|
27 |
|
28 |
|
29 // Cut&paste from sun.net.www.ParseUtil: decode, unescape |
|
30 |
|
31 public class ParseUtil { |
|
32 /** |
|
33 * Un-escape and return the character at position i in string s. |
|
34 */ |
|
35 private static char unescape(String s, int i) { |
|
36 return (char) Integer.parseInt(s.substring(i+1,i+3),16); |
|
37 } |
|
38 |
|
39 /** |
|
40 * Returns a new String constructed from the specified String by replacing |
|
41 * the URL escape sequences and UTF8 encoding with the characters they |
|
42 * represent. |
|
43 */ |
|
44 public static String decode(String s) { |
|
45 StringBuffer sb = new StringBuffer(); |
|
46 |
|
47 int i=0; |
|
48 while (i<s.length()) { |
|
49 char c = s.charAt(i); |
|
50 char c2, c3; |
|
51 |
|
52 if (c != '%') { |
|
53 i++; |
|
54 } else { |
|
55 try { |
|
56 c = unescape(s, i); |
|
57 i += 3; |
|
58 |
|
59 if ((c & 0x80) != 0) { |
|
60 switch (c >> 4) { |
|
61 case 0xC: case 0xD: |
|
62 c2 = unescape(s, i); |
|
63 i += 3; |
|
64 c = (char)(((c & 0x1f) << 6) | (c2 & 0x3f)); |
|
65 break; |
|
66 |
|
67 case 0xE: |
|
68 c2 = unescape(s, i); |
|
69 i += 3; |
|
70 c3 = unescape(s, i); |
|
71 i += 3; |
|
72 c = (char)(((c & 0x0f) << 12) | |
|
73 ((c2 & 0x3f) << 6) | |
|
74 (c3 & 0x3f)); |
|
75 break; |
|
76 |
|
77 default: |
|
78 throw new IllegalArgumentException(); |
|
79 } |
|
80 } |
|
81 } catch (NumberFormatException e) { |
|
82 throw new IllegalArgumentException(); |
|
83 } |
|
84 } |
|
85 |
|
86 sb.append(c); |
|
87 } |
|
88 |
|
89 return sb.toString(); |
|
90 } |
|
91 |
|
92 |
|
93 } |