001: /*
002: * Copyright 1999,2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: package org.apache.catalina.util;
017:
018: import java.io.ByteArrayOutputStream;
019: import java.io.IOException;
020: import java.io.OutputStreamWriter;
021: import java.util.BitSet;
022:
023: /**
024: *
025: * This class is very similar to the java.net.URLEncoder class.
026: *
027: * Unfortunately, with java.net.URLEncoder there is no way to specify to the
028: * java.net.URLEncoder which characters should NOT be encoded.
029: *
030: * This code was moved from DefaultServlet.java
031: *
032: * @author Craig R. McClanahan
033: * @author Remy Maucherat
034: */
035: public class URLEncoder {
036: protected static final char[] hexadecimal = { '0', '1', '2', '3',
037: '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' };
038:
039: //Array containing the safe characters set.
040: protected BitSet safeCharacters = new BitSet(256);
041:
042: public URLEncoder() {
043: for (char i = 'a'; i <= 'z'; i++) {
044: addSafeCharacter(i);
045: }
046: for (char i = 'A'; i <= 'Z'; i++) {
047: addSafeCharacter(i);
048: }
049: for (char i = '0'; i <= '9'; i++) {
050: addSafeCharacter(i);
051: }
052: }
053:
054: public void addSafeCharacter(char c) {
055: safeCharacters.set(c);
056: }
057:
058: public String encode(String path) {
059: int maxBytesPerChar = 10;
060: int caseDiff = ('a' - 'A');
061: StringBuffer rewrittenPath = new StringBuffer(path.length());
062: ByteArrayOutputStream buf = new ByteArrayOutputStream(
063: maxBytesPerChar);
064: OutputStreamWriter writer = null;
065: try {
066: writer = new OutputStreamWriter(buf, "UTF8");
067: } catch (Exception e) {
068: e.printStackTrace();
069: writer = new OutputStreamWriter(buf);
070: }
071:
072: for (int i = 0; i < path.length(); i++) {
073: int c = (int) path.charAt(i);
074: if (safeCharacters.get(c)) {
075: rewrittenPath.append((char) c);
076: } else {
077: // convert to external encoding before hex conversion
078: try {
079: writer.write((char) c);
080: writer.flush();
081: } catch (IOException e) {
082: buf.reset();
083: continue;
084: }
085: byte[] ba = buf.toByteArray();
086: for (int j = 0; j < ba.length; j++) {
087: // Converting each byte in the buffer
088: byte toEncode = ba[j];
089: rewrittenPath.append('%');
090: int low = (int) (toEncode & 0x0f);
091: int high = (int) ((toEncode & 0xf0) >> 4);
092: rewrittenPath.append(hexadecimal[high]);
093: rewrittenPath.append(hexadecimal[low]);
094: }
095: buf.reset();
096: }
097: }
098: return rewrittenPath.toString();
099: }
100: }
|