jakarta-taglibs-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From pier...@apache.org
Subject cvs commit: jakarta-taglibs/standard/src/org/apache/taglibs/standard/tag/common/core ParamSupport.java Util.java
Date Wed, 30 Apr 2003 22:53:09 GMT
pierred     2003/04/30 15:53:08

  Modified:    standard/src/org/apache/taglibs/standard/tag/common/core
                        ParamSupport.java Util.java
  Log:
  Fix for bug 16912.
  
  Revision  Changes    Path
  1.10      +12 -49    jakarta-taglibs/standard/src/org/apache/taglibs/standard/tag/common/core/ParamSupport.java
  
  Index: ParamSupport.java
  ===================================================================
  RCS file: /home/cvs/jakarta-taglibs/standard/src/org/apache/taglibs/standard/tag/common/core/ParamSupport.java,v
  retrieving revision 1.9
  retrieving revision 1.10
  diff -u -r1.9 -r1.10
  --- ParamSupport.java	14 Mar 2003 22:04:35 -0000	1.9
  +++ ParamSupport.java	30 Apr 2003 22:53:08 -0000	1.10
  @@ -57,10 +57,13 @@
   
   import java.lang.reflect.*;
   import java.util.*;
  +import java.io.*;
  +
   import javax.servlet.jsp.*;
   import javax.servlet.jsp.tagext.*;
  -import java.net.URLEncoder;
  +
   import org.apache.taglibs.standard.resources.Resources;
  +import org.apache.taglibs.standard.tag.common.core.Util;
   
   /**
    * <p>Support for tag handlers for &lt;param&gt;, the URL parameter
  @@ -73,12 +76,6 @@
   public abstract class ParamSupport extends BodyTagSupport {
   
       //*********************************************************************
  -    // Private constants
  -
  -    private static final Class[] URL_ENCODER_PARAM_TYPES =
  -	new Class[] { String.class, String.class };
  -
  -    //*********************************************************************
       // Protected state
   
       protected String name;                       // 'name' attribute
  @@ -92,26 +89,8 @@
       protected boolean encode = true;
   
       //*********************************************************************
  -    // Private state
  -
  -    private static Method encodeMethod1_4 = null;
  -
  -    //*********************************************************************
       // Constructor and initialization
   
  -    // URLEncoder.encode(String) has been deprecated in J2SE 1.4. 
  -    // Take advantage of the new method URLEncoder.encode(String, enc)
  -    // if J2SE 1.4 is used.
  -    static {
  -	try {
  -	    Class urlEncoderClass = Class.forName("java.net.URLEncoder");
  -	    encodeMethod1_4 = 
  -                urlEncoderClass.getMethod(
  -                    "encode",
  -		    new Class[] {String.class, String.class});
  -        } catch (Exception ex) {} // encodeMethod1_4 will be null if exception
  -    }
  -
       public ParamSupport() {
   	super();
   	init();
  @@ -121,7 +100,6 @@
   	name = value = null;
       }
   
  -
       //*********************************************************************
       // Tag logic
   
  @@ -145,29 +123,14 @@
   	    else
   		value = bodyContent.getString().trim();
   	}
  -	if (encode) {
  -            if (encodeMethod1_4 != null) {
  -	        Object[] methodArgsName = new Object[2];
  -	        methodArgsName[0] = name;
  -	        methodArgsName[1] = pageContext.getResponse().getCharacterEncoding();
  -	        Object[] methodArgsValue = new Object[2];
  -	        methodArgsValue[0] = value;
  -	        methodArgsValue[1] = pageContext.getResponse().getCharacterEncoding();
  -	        
  -                try {
  -                    parent.addParameter(
  -                    (String)encodeMethod1_4.invoke(null, methodArgsName),
  -                    (String)encodeMethod1_4.invoke(null, methodArgsValue));  
  -                } catch (Exception ex) {
  -                    throw new JspException("System error invoking URLEncoder.encode() by
reflection.", ex);
  -                }
  -            } else {
  -                // must use J2SE 1.3 version
  -	        parent.addParameter(
  -		    URLEncoder.encode(name), URLEncoder.encode(value));
  -            }
  -	} else {
  -	    parent.addParameter(name, value);
  +        if (encode) {
  +            // FIXME: revert to java.net.URLEncoder.encode(s, enc) once
  +            // we have a dependency on J2SE 1.4+.
  +            String enc = pageContext.getResponse().getCharacterEncoding();
  +            parent.addParameter(
  +            Util.URLEncode(name, enc), Util.URLEncode(value, enc));
  +        } else {
  +            parent.addParameter(name, value);
           }
   	return EVAL_PAGE;
       }
  
  
  
  1.11      +79 -0     jakarta-taglibs/standard/src/org/apache/taglibs/standard/tag/common/core/Util.java
  
  Index: Util.java
  ===================================================================
  RCS file: /home/cvs/jakarta-taglibs/standard/src/org/apache/taglibs/standard/tag/common/core/Util.java,v
  retrieving revision 1.10
  retrieving revision 1.11
  diff -u -r1.10 -r1.11
  --- Util.java	31 Mar 2003 15:53:51 -0000	1.10
  +++ Util.java	30 Apr 2003 22:53:08 -0000	1.11
  @@ -55,9 +55,12 @@
   
   package org.apache.taglibs.standard.tag.common.core;
   
  +import java.io.*;
   import java.text.DateFormat;
  +
   import javax.servlet.jsp.JspException;
   import javax.servlet.jsp.PageContext;
  +
   import org.apache.taglibs.standard.resources.Resources;
   
   /**
  @@ -194,4 +197,80 @@
           }
           return input.substring(begin, end).trim();
       }
  +    
  +    /**
  +     * URL encodes a string, based on the supplied character encoding.
  +     * This performs the same function as java.next.URLEncode.encode
  +     * in J2SDK1.4, and should be removed if the only platform supported
  +     * is 1.4 or higher.
  +     * @param s The String to be URL encoded.
  +     * @param enc The character encoding 
  +     * @return The URL encoded String
  +     * [taken from jakarta-tomcat-jasper/jasper2
  +     *  org.apache.jasper.runtime.JspRuntimeLibrary.java]
  +     */
  +    public static String URLEncode(String s, String enc) {
  +
  +	if (s == null) {
  +	    return "null";
  +	}
  +
  +	if (enc == null) {
  +	    enc = "UTF-8";	// Is this right?
  +	}
  +
  +	StringBuffer out = new StringBuffer(s.length());
  +	ByteArrayOutputStream buf = new ByteArrayOutputStream();
  +	OutputStreamWriter writer = null;
  +	try {
  +	    writer = new OutputStreamWriter(buf, enc);
  +	} catch (java.io.UnsupportedEncodingException ex) {
  +	    // Use the default encoding?
  +	    writer = new OutputStreamWriter(buf);
  +	}
  +	
  +	for (int i = 0; i < s.length(); i++) {
  +	    int c = s.charAt(i);
  +	    if (c == ' ') {
  +		out.append('+');
  +	    } else if (isSafeChar(c)) {
  +		out.append((char)c);
  +	    } else {
  +		// convert to external encoding before hex conversion
  +		try {
  +		    writer.write(c);
  +		    writer.flush();
  +		} catch(IOException e) {
  +		    buf.reset();
  +		    continue;
  +		}
  +		byte[] ba = buf.toByteArray();
  +		for (int j = 0; j < ba.length; j++) {
  +		    out.append('%');
  +		    // Converting each byte in the buffer
  +		    out.append(Character.forDigit((ba[j]>>4) & 0xf, 16));
  +		    out.append(Character.forDigit(ba[j] & 0xf, 16));
  +		}
  +		buf.reset();
  +	    }
  +	}
  +	return out.toString();
  +    }
  +
  +    private static boolean isSafeChar(int c) {
  +	if (c >= 'a' && c <= 'z') {
  +	    return true;
  +	}
  +	if (c >= 'A' && c <= 'Z') {
  +	    return true;
  +	}
  +	if (c >= '0' && c <= '9') {
  +	    return true;
  +	}
  +	if (c == '-' || c == '_' || c == '.' || c == '!' ||
  +	    c == '~' || c == '*' || c == '\'' || c == '(' || c == ')') {
  +	    return true;
  +	}
  +	return false;
  +    }    
   }
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: taglibs-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: taglibs-dev-help@jakarta.apache.org


Mime
View raw message