kinman      2003/04/01 18:04:00

  Modified:    jasper2/src/share/org/apache/jasper/compiler Generator.java
               jasper2/src/share/org/apache/jasper/runtime
                        JspRuntimeLibrary.java
  Log:
  - Implement an URL encoder with character encoding (Actaully heavily based
    on catalina/util/URLEncoder.java.
  - URL encode the parameters for jsp:include and jsp:forward with the request
    char encoding.
  
  Revision  Changes    Path
  1.180     +10 -9     
jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/Generator.java
  
  Index: Generator.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/compiler/Generator.java,v
  retrieving revision 1.179
  retrieving revision 1.180
  diff -u -r1.179 -r1.180
  --- Generator.java    31 Mar 2003 21:14:04 -0000      1.179
  +++ Generator.java    2 Apr 2003 02:04:00 -0000       1.180
  @@ -62,7 +62,6 @@
   
   import java.util.*;
   import java.beans.*;
  -import java.net.URLEncoder;
   import java.io.ByteArrayOutputStream;
   import java.io.PrintStream;
   import java.lang.reflect.Method;
  @@ -720,14 +719,14 @@
                    }
                }
                if (encode) {
  -                 return "java.net.URLEncoder.encode(\"\" + " + v + ")";
  +                 return "org.apache.jasper.runtime.JspRuntimeLibrary.URLEncode(" + 
v + ", request.getCharacterEncoding())";
                }
                return v;
               } else if( attr.isNamedAttribute() ) {
                   return attr.getNamedAttributeNode().getTemporaryVariableName();
            } else {
                if (encode) {
  -                 v = URLEncoder.encode(v);
  +                 return "org.apache.jasper.runtime.JspRuntimeLibrary.URLEncode(" + 
quote(v) + ", request.getCharacterEncoding())";
                }
                return quote(v);
            }
  @@ -753,9 +752,11 @@
   
                    out.print(" + ");
                    out.print(separator);
  -                 out.print(" + \"");
  -                 out.print(URLEncoder.encode(n.getTextAttribute("name")));
  -                 out.print("=\" + ");
  +                 out.print(" + ");
  +                 out.print("org.apache.jasper.runtime.JspRuntimeLibrary." +
  +                           "URLEncode(" + quote(n.getTextAttribute("name")) +
  +                                      ", request.getCharacterEncoding())");
  +                 out.print("+ \"=\" + ");
                    out.print(attributeValue(n.getValue(), true, String.class));
   
                    // The separator is '&' after the second use
  
  
  
  1.20      +77 -3     
jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/runtime/JspRuntimeLibrary.java
  
  Index: JspRuntimeLibrary.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-tomcat-jasper/jasper2/src/share/org/apache/jasper/runtime/JspRuntimeLibrary.java,v
  retrieving revision 1.19
  retrieving revision 1.20
  diff -u -r1.19 -r1.20
  --- JspRuntimeLibrary.java    31 Mar 2003 17:54:30 -0000      1.19
  +++ JspRuntimeLibrary.java    2 Apr 2003 02:04:00 -0000       1.20
  @@ -992,4 +992,78 @@
   
       }
   
  +    /**
  +     * 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
  +     */
  +    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: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to