View | Details | Raw Unified | Return to bug 49561
Collapse All | Expand All

(-)impl/src/main/java/org/apache/taglibs/standard/tag/common/core/Util.java (-76 lines)
Lines 192-274 Link Here
192
        }
192
        }
193
        return input.substring(begin, end).trim();
193
        return input.substring(begin, end).trim();
194
    }
194
    }
195
    
196
    /**
197
     * URL encodes a string, based on the supplied character encoding.
198
     * This performs the same function as java.next.URLEncode.encode
199
     * in J2SDK1.4, and should be removed if the only platform supported
200
     * is 1.4 or higher.
201
     * @param s The String to be URL encoded.
202
     * @param enc The character encoding 
203
     * @return The URL encoded String
204
     * [taken from jakarta-tomcat-jasper/jasper2
205
     *  org.apache.jasper.runtime.JspRuntimeLibrary.java]
206
     */
207
    public static String URLEncode(String s, String enc) {
208
195
209
	if (s == null) {
210
	    return "null";
211
	}
212
213
	if (enc == null) {
214
	    enc = "UTF-8";	// Is this right?
215
	}
216
217
	StringBuffer out = new StringBuffer(s.length());
218
	ByteArrayOutputStream buf = new ByteArrayOutputStream();
219
	OutputStreamWriter writer = null;
220
	try {
221
	    writer = new OutputStreamWriter(buf, enc);
222
	} catch (java.io.UnsupportedEncodingException ex) {
223
	    // Use the default encoding?
224
	    writer = new OutputStreamWriter(buf);
225
	}
226
	
227
	for (int i = 0; i < s.length(); i++) {
228
	    int c = s.charAt(i);
229
	    if (c == ' ') {
230
		out.append('+');
231
	    } else if (isSafeChar(c)) {
232
		out.append((char)c);
233
	    } else {
234
		// convert to external encoding before hex conversion
235
		try {
236
		    writer.write(c);
237
		    writer.flush();
238
		} catch(IOException e) {
239
		    buf.reset();
240
		    continue;
241
		}
242
		byte[] ba = buf.toByteArray();
243
		for (int j = 0; j < ba.length; j++) {
244
		    out.append('%');
245
		    // Converting each byte in the buffer
246
		    out.append(Character.forDigit((ba[j]>>4) & 0xf, 16));
247
		    out.append(Character.forDigit(ba[j] & 0xf, 16));
248
		}
249
		buf.reset();
250
	    }
251
	}
252
	return out.toString();
253
    }
254
255
    private static boolean isSafeChar(int c) {
256
	if (c >= 'a' && c <= 'z') {
257
	    return true;
258
	}
259
	if (c >= 'A' && c <= 'Z') {
260
	    return true;
261
	}
262
	if (c >= '0' && c <= '9') {
263
	    return true;
264
	}
265
	if (c == '-' || c == '_' || c == '.' || c == '!' ||
266
	    c == '~' || c == '*' || c == '\'' || c == '(' || c == ')') {
267
	    return true;
268
	}
269
	return false;
270
    }    
271
    
272
    /**
196
    /**
273
     * HttpServletRequest.getLocales() returns the server's default locale 
197
     * HttpServletRequest.getLocales() returns the server's default locale 
274
     * if the request did not specify a preferred language.
198
     * if the request did not specify a preferred language.
(-)impl/src/main/java/org/apache/taglibs/standard/tag/common/core/ParamSupport.java (-4 / +7 lines)
Lines 17-22 Link Here
17
17
18
package org.apache.taglibs.standard.tag.common.core;
18
package org.apache.taglibs.standard.tag.common.core;
19
19
20
import java.io.UnsupportedEncodingException;
21
import java.net.URLEncoder;
20
import java.util.LinkedList;
22
import java.util.LinkedList;
21
import java.util.List;
23
import java.util.List;
22
24
Lines 86-96 Link Here
86
		value = bodyContent.getString().trim();
88
		value = bodyContent.getString().trim();
87
	}
89
	}
88
        if (encode) {
90
        if (encode) {
89
            // FIXME: revert to java.net.URLEncoder.encode(s, enc) once
90
            // we have a dependency on J2SE 1.4+.
91
            String enc = pageContext.getResponse().getCharacterEncoding();
91
            String enc = pageContext.getResponse().getCharacterEncoding();
92
            parent.addParameter(
92
            try {
93
            Util.URLEncode(name, enc), Util.URLEncode(value, enc));
93
                parent.addParameter(URLEncoder.encode(name, enc), URLEncoder.encode(value, enc));
94
            } catch (UnsupportedEncodingException e) {
95
                throw new JspTagException(e);
96
            }
94
        } else {
97
        } else {
95
            parent.addParameter(name, value);
98
            parent.addParameter(name, value);
96
        }
99
        }

Return to bug 49561