BrightSide Workbench Full Report + Source Code
SafeURLEncoder.java
Go to the documentation of this file.
1 /*
2  * TurrĂ³ i Cutiller Foundation. License notice.
3  * Copyright (C) 2011 Lluis TurrĂ³ Cutiller <http://www.turro.org/>
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU Affero General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program 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
13  * GNU Affero General Public License for more details.
14  *
15  * You should have received a copy of the GNU Affero General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  */
18 package org.turro.elephant.impl.util;
19 
24 public class SafeURLEncoder {
25  private static final String[][] map = {
26  {"%", "%25"},
27  {"$", "%24"},
28  {"&", "%26"},
29  {"+", "%2B"},
30  {",", "%2C"},
31  {"/", "%2F"},
32  {":", "%3A"},
33  {";", "%3B"},
34  {"=", "%3D"},
35  {"?", "%3F"},
36  {"@", "%40"},
37  {" ", "%20"},
38  {"\"", "%22"},
39  {"<", "%3C"},
40  {">", "%3E"},
41  {"#", "%23"},
42  {"{", "%7B"},
43  {"}", "%7D"},
44  {"|", "%7C"},
45  {"\\", "%5C"},
46  {"^", "%5E"},
47  {"~", "%7E"},
48  {"[", "%5B"},
49  {"]", "%5D"},
50  {"`", "%60"}
51  };
52 
56  private SafeURLEncoder() {
57  }
58 
59  public static String encode(String value) {
60  for(int i = 0; i < map.length; i++) {
61  value = value.replace(map[i][0], map[i][1]);
62  }
63  return value;
64  }
65 
66  public static String decode(String value) {
67  for(int i = map.length - 1; i >= 0; i--) {
68  value = value.replace(map[i][1], map[i][0]);
69  }
70  return value;
71  }
72 
73 }