Changed license to LGPLv3 or later.
[svjatoslav_commons.git] / src / main / java / eu / svjatoslav / commons / network / UrlParamEncoder.java
1 /*
2  * Svjatoslav Commons - shared library of common functionality.
3  * Copyright ©2012-2014, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
4  * 
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of version 3 of the GNU Lesser General Public License
7  * or later as published by the Free Software Foundation.
8  */
9
10 package eu.svjatoslav.commons.network;
11
12 public class UrlParamEncoder {
13
14         public static String decode(final String source) {
15
16                 final String result = source.replaceAll("%20", " ");
17
18                 return result;
19         }
20
21         public static String encode(final String source) {
22
23                 final StringBuffer buffer = new StringBuffer();
24                 for (int i = 0; i < source.length(); i++) {
25                         boolean replaced = false;
26                         final char character = source.charAt(i);
27
28                         if (character == ' ') {
29                                 buffer.append("%20");
30                                 replaced = true;
31                         }
32
33                         if (character == '?') {
34                                 buffer.append("%3F");
35                                 replaced = true;
36                         }
37
38                         if (character == ',') {
39                                 buffer.append("%2C");
40                                 replaced = true;
41                         }
42
43                         if (character == ':') {
44                                 buffer.append("%3A");
45                                 replaced = true;
46                         }
47
48                         if (!replaced)
49                                 buffer.append(character);
50                 }
51
52                 return buffer.toString();
53         }
54
55 }