1 /*******************************************************************************
2 * Copyright © Igor Barma, Alexandre Desoubeaux, Christian Martel, Eric Brun, Mathieu Amblard, Gwenael Gevet, Pierre Guillot, 2012
4 * This file is part of the work and learning management system Pentila Nero.
6 * Pentila Nero is free software. You can redistribute it and/or modify since
7 * you respect the terms of either (at least one of the both license) :
8 * - under the terms of the GNU Affero General Public License as
9 * published by the Free Software Foundation, either version 3 of the
10 * License, or (at your option) any later version.
11 * - the CeCILL-C as published by CeCILL-C; either version 1 of the
12 * License, or any later version
13 * - the GNU Lesser General Public License as published by the
14 * Free Software Foundation, either version 3 of the license,
15 * or (at your option) any later version.
17 * There are special exceptions to the terms and conditions of the
18 * licenses as they are applied to this software. View the full text of
19 * the exception in file LICENSE-PROJECT.txt in the directory of this software
22 * Pentila Nero is distributed in the hope that it will be useful,
23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 * Licenses for more details.
27 * You should have received a copy of the GNU Affero General Public License
28 * and the CeCILL-C and the GNU Lesser General Public License along with
29 * Pentila Nero. If not, see :
30 * <http://www.gnu.org/licenses/> and
31 * <http://www.cecill.info/licences.fr.html>.
32 ******************************************************************************/
33 package com.pentila.entSavoie.utils;
35 import java.io.UnsupportedEncodingException;
37 public class URIFormat {
39 public static final String ALLOWED_CHARS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.!~*'()";
41 public static String encodeURIComponent(String input) {
46 int l = input.length();
47 StringBuilder o = new StringBuilder(l * 3);
49 for (int i = 0; i < l; i++) {
50 String e = input.substring(i, i + 1);
51 if (ALLOWED_CHARS.indexOf(e) == -1) {
52 byte[] b = e.getBytes("utf-8");
59 } catch(UnsupportedEncodingException e) {
65 private static String getHex(byte buf[]) {
66 StringBuilder o = new StringBuilder(buf.length * 3);
67 for (int i = 0; i < buf.length; i++) {
68 int n = (int) buf[i] & 0xff;
73 o.append(Long.toString(n, 16).toUpperCase());
78 public static String decodeURIComponent(String encodedURI) {
81 StringBuffer buffer = new StringBuffer();
83 int bytePattern, sumb = 0;
85 for (int i = 0, more = -1; i < encodedURI.length(); i++) {
86 actualChar = encodedURI.charAt(i);
90 actualChar = encodedURI.charAt(++i);
91 int hb = (Character.isDigit(actualChar) ? actualChar - '0'
92 : 10 + Character.toLowerCase(actualChar) - 'a') & 0xF;
93 actualChar = encodedURI.charAt(++i);
94 int lb = (Character.isDigit(actualChar) ? actualChar - '0'
95 : 10 + Character.toLowerCase(actualChar) - 'a') & 0xF;
96 bytePattern = (hb << 4) | lb;
104 bytePattern = actualChar;
108 if ((bytePattern & 0xc0) == 0x80) { // 10xxxxxx
109 sumb = (sumb << 6) | (bytePattern & 0x3f);
111 buffer.append((char) sumb);
112 } else if ((bytePattern & 0x80) == 0x00) { // 0xxxxxxx
113 buffer.append((char) bytePattern);
114 } else if ((bytePattern & 0xe0) == 0xc0) { // 110xxxxx
115 sumb = bytePattern & 0x1f;
117 } else if ((bytePattern & 0xf0) == 0xe0) { // 1110xxxx
118 sumb = bytePattern & 0x0f;
120 } else if ((bytePattern & 0xf8) == 0xf0) { // 11110xxx
121 sumb = bytePattern & 0x07;
123 } else if ((bytePattern & 0xfc) == 0xf8) { // 111110xx
124 sumb = bytePattern & 0x03;
127 sumb = bytePattern & 0x01;
131 return buffer.toString();