husted 2004/01/01 11:27:20 Modified: src/share/org/apache/struts/taglib TagUtils.java src/share/org/apache/struts/taglib/bean IncludeTag.java WriteTag.java src/share/org/apache/struts/taglib/html ImgTag.java LinkTag.java RewriteTag.java src/share/org/apache/struts/taglib/logic RedirectTag.java src/share/org/apache/struts/util RequestUtils.java Log: In link-related tags, allow LocalCharacterEncoding to be specified conditionally, per # 24631 (Yasuhiko Sakakibara). Revision Changes Path 1.27 +94 -21 jakarta-struts/src/share/org/apache/struts/taglib/TagUtils.java Index: TagUtils.java =================================================================== RCS file: /home/cvs/jakarta-struts/src/share/org/apache/struts/taglib/TagUtils.java,v retrieving revision 1.26 retrieving revision 1.27 diff -u -r1.26 -r1.27 --- TagUtils.java 9 Sep 2003 03:44:42 -0000 1.26 +++ TagUtils.java 1 Jan 2004 19:27:19 -0000 1.27 @@ -310,6 +310,28 @@ } + public String computeURL( + PageContext pageContext, + String forward, + String href, + String page, + String action, + Map params, + String anchor, + boolean redirect) + throws MalformedURLException { + return this.computeURLWithCharEncoding( + pageContext, + forward, + href, + page, + action, + params, + anchor, + redirect, + false); + } + /** * Compute a hyperlink URL based on the <code>forward</code>, * <code>href</code>, <code>action</code> or <code>page</code> parameter @@ -335,7 +357,7 @@ * @exception java.net.MalformedURLException if a URL cannot be created * for the specified parameters */ - public String computeURL( + public String computeURLWithCharEncoding( PageContext pageContext, String forward, String href, @@ -343,10 +365,11 @@ String action, Map params, String anchor, - boolean redirect) + boolean redirect, + boolean useLocalEncoding) throws MalformedURLException { - return computeURL( + return computeURLWithCharEncoding( pageContext, forward, href, @@ -355,9 +378,35 @@ params, anchor, redirect, - true); + true, + useLocalEncoding); } + public String computeURL( + PageContext pageContext, + String forward, + String href, + String page, + String action, + Map params, + String anchor, + boolean redirect, + boolean encodeSeparator) + throws MalformedURLException { + return computeURLWithCharEncoding( + pageContext, + forward, + href, + page, + action, + params, + anchor, + redirect, + encodeSeparator, + false + ); + } + /** * Compute a hyperlink URL based on the <code>forward</code>, * <code>href</code>, <code>action</code> or <code>page</code> parameter @@ -382,11 +431,14 @@ * @param encodeSeparator This is only checked if redirect is set to false (never * encoded for a redirect). If true, query string parameter separators are encoded * as >amp;, else & is used. + * @param useLocalEncoding If set to true, urlencoding is done on the bytes of + * character encoding from ServletResponse#getCharacterEncoding. Use UTF-8 + * otherwise. * @return URL with session identifier * @exception java.net.MalformedURLException if a URL cannot be created * for the specified parameters */ - public String computeURL( + public String computeURLWithCharEncoding( PageContext pageContext, String forward, String href, @@ -395,8 +447,13 @@ Map params, String anchor, boolean redirect, - boolean encodeSeparator) + boolean encodeSeparator, + boolean useLocalEncoding) throws MalformedURLException { + String charEncoding = "UTF-8"; + if(useLocalEncoding){ + charEncoding = pageContext.getResponse().getCharacterEncoding(); + } // TODO All the computeURL() methods need refactoring! @@ -456,7 +513,7 @@ url.setLength(hash); } url.append('#'); - url.append(this.encodeURL(anchor)); + url.append(this.encodeURL(anchor, charEncoding)); } // Add dynamic parameters if requested @@ -496,7 +553,7 @@ } else { url.append(separator); } - url.append(this.encodeURL(key)); + url.append(this.encodeURL(key, charEncoding)); url.append('='); // Interpret null as "no value" } else if (value instanceof String) { if (!question) { @@ -505,9 +562,9 @@ } else { url.append(separator); } - url.append(this.encodeURL(key)); + url.append(this.encodeURL(key, charEncoding)); url.append('='); - url.append(this.encodeURL((String) value)); + url.append(this.encodeURL((String) value, charEncoding)); } else if (value instanceof String[]) { String values[] = (String[]) value; for (int i = 0; i < values.length; i++) { @@ -517,9 +574,9 @@ } else { url.append(separator); } - url.append(this.encodeURL(key)); + url.append(this.encodeURL(key, charEncoding)); url.append('='); - url.append(this.encodeURL(values[i])); + url.append(this.encodeURL(values[i], charEncoding)); } } else /* Convert other objects to a string */ { if (!question) { @@ -528,16 +585,16 @@ } else { url.append(separator); } - url.append(this.encodeURL(key)); + url.append(this.encodeURL(key, charEncoding)); url.append('='); - url.append(this.encodeURL(value.toString())); + url.append(this.encodeURL(value.toString(), charEncoding)); } } // Re-add the saved anchor (if any) if (anchor != null) { url.append('#'); - url.append(this.encodeURL(anchor)); + url.append(this.encodeURL(anchor, charEncoding)); } } @@ -556,19 +613,35 @@ } + + /** + * URLencodes a string assuming the character encoding is UTF-8. + * + * @param url + * @return String The encoded url in UTF-8 + */ + public String encodeURL(String url) { + return encodeURL(url, "UTF-8"); + } + /** * Use the new URLEncoder.encode() method from Java 1.4 if available, else * use the old deprecated version. This method uses reflection to find the * appropriate method; if the reflection operations throw exceptions, this * will return the url encoded with the old URLEncoder.encode() method. + * @param enc The character encoding the urlencode is performed on. * @return String The encoded url. */ - public String encodeURL(String url) { + public String encodeURL(String url, String enc) { try { + + if(enc==null || enc.length()==0){ + enc = "UTF-8"; + } // encode url with new 1.4 method and UTF-8 encoding if (encode != null) { - return (String) encode.invoke(null, new Object[]{url, "UTF-8"}); + return (String) encode.invoke(null, new Object[]{url, enc}); } } catch (IllegalAccessException e) { 1.26 +14 -4 jakarta-struts/src/share/org/apache/struts/taglib/bean/IncludeTag.java Index: IncludeTag.java =================================================================== RCS file: /home/cvs/jakarta-struts/src/share/org/apache/struts/taglib/bean/IncludeTag.java,v retrieving revision 1.25 retrieving revision 1.26 diff -u -r1.25 -r1.26 --- IncludeTag.java 27 Nov 2003 05:38:54 -0000 1.25 +++ IncludeTag.java 1 Jan 2004 19:27:19 -0000 1.26 @@ -192,6 +192,16 @@ public void setTransaction(boolean transaction) { this.transaction = transaction; } + + protected boolean useLocalEncoding = false; + + public boolean isUseLocalEncoding() { + return useLocalEncoding; + } + + public void setUseLocalEncoding(boolean b) { + useLocalEncoding = b; + } // --------------------------------------------------------- Public Methods @@ -220,7 +230,7 @@ URL url = null; try { urlString = - TagUtils.getInstance().computeURL(pageContext, forward, href, page, null,params, anchor, false); + TagUtils.getInstance().computeURLWithCharEncoding(pageContext, forward, href, page, null,params, anchor, false, useLocalEncoding); if (urlString.indexOf(':') < 0) { HttpServletRequest request = (HttpServletRequest) pageContext.getRequest(); url = new URL(RequestUtils.requestURL(request), urlString); @@ -306,6 +316,6 @@ id = null; page = null; transaction = false; - } + } } 1.30 +4 -5 jakarta-struts/src/share/org/apache/struts/taglib/bean/WriteTag.java Index: WriteTag.java =================================================================== RCS file: /home/cvs/jakarta-struts/src/share/org/apache/struts/taglib/bean/WriteTag.java,v retrieving revision 1.29 retrieving revision 1.30 diff -u -r1.29 -r1.30 --- WriteTag.java 31 Jul 2003 00:34:15 -0000 1.29 +++ WriteTag.java 1 Jan 2004 19:27:19 -0000 1.30 @@ -415,7 +415,6 @@ format = new SimpleDateFormat(formatString); } } - } } 1.33 +23 -8 jakarta-struts/src/share/org/apache/struts/taglib/html/ImgTag.java Index: ImgTag.java =================================================================== RCS file: /home/cvs/jakarta-struts/src/share/org/apache/struts/taglib/html/ImgTag.java,v retrieving revision 1.32 retrieving revision 1.33 diff -u -r1.32 -r1.33 --- ImgTag.java 2 Aug 2003 20:35:28 -0000 1.32 +++ ImgTag.java 1 Jan 2004 19:27:19 -0000 1.33 @@ -381,7 +381,17 @@ this.width = width; } - // --------------------------------------------------------- Public Methods + protected boolean useLocalEncoding = false; + + public boolean isUseLocalEncoding() { + return useLocalEncoding; + } + + public void setUseLocalEncoding(boolean b) { + useLocalEncoding = b; + } + + // --------------------------------------------------------- Public Methods /** * Render the beginning of the IMG tag. @@ -600,6 +610,11 @@ return (url); } + String charEncoding = "UTF-8"; + if(useLocalEncoding){ + charEncoding = pageContext.getResponse().getCharacterEncoding(); + } + // Start with an unadorned URL as specified StringBuffer src = new StringBuffer(url); @@ -614,7 +629,7 @@ src.append('='); Object value = TagUtils.getInstance().lookup(pageContext, paramName, paramProperty, paramScope); if (value != null) - src.append(TagUtils.getInstance().encodeURL(value.toString())); + src.append(TagUtils.getInstance().encodeURL(value.toString(), charEncoding)); } // Just return the URL if there is no bean to look up @@ -665,7 +680,7 @@ } src.append(key); src.append('='); - src.append(TagUtils.getInstance().encodeURL(values[i])); + src.append(TagUtils.getInstance().encodeURL(values[i], charEncoding)); } } else { @@ -677,7 +692,7 @@ } src.append(key); src.append('='); - src.append(TagUtils.getInstance().encodeURL(value.toString())); + src.append(TagUtils.getInstance().encodeURL(value.toString(), charEncoding)); } } 1.32 +16 -6 jakarta-struts/src/share/org/apache/struts/taglib/html/LinkTag.java Index: LinkTag.java =================================================================== RCS file: /home/cvs/jakarta-struts/src/share/org/apache/struts/taglib/html/LinkTag.java,v retrieving revision 1.31 retrieving revision 1.32 diff -u -r1.31 -r1.32 --- LinkTag.java 31 Jul 2003 00:19:04 -0000 1.31 +++ LinkTag.java 1 Jan 2004 19:27:19 -0000 1.32 @@ -324,6 +324,16 @@ this.indexId = indexId; } + protected boolean useLocalEncoding = false; + + public boolean isUseLocalEncoding() { + return useLocalEncoding; + } + + public void setUseLocalEncoding(boolean b) { + useLocalEncoding = b; + } + // --------------------------------------------------------- Public Methods @@ -488,8 +498,8 @@ String url = null; try { - url = TagUtils.getInstance().computeURL(pageContext, forward, href, - page, action, params, anchor, false); + url = TagUtils.getInstance().computeURLWithCharEncoding(pageContext, forward, href, + page, action, params, anchor, false, useLocalEncoding); } catch (MalformedURLException e) { TagUtils.getInstance().saveException(pageContext, e); throw new JspException 1.16 +7 -6 jakarta-struts/src/share/org/apache/struts/taglib/html/RewriteTag.java Index: RewriteTag.java =================================================================== RCS file: /home/cvs/jakarta-struts/src/share/org/apache/struts/taglib/html/RewriteTag.java,v retrieving revision 1.15 retrieving revision 1.16 diff -u -r1.15 -r1.16 --- RewriteTag.java 10 Aug 2003 10:05:51 -0000 1.15 +++ RewriteTag.java 1 Jan 2004 19:27:19 -0000 1.16 @@ -97,7 +97,7 @@ // Note that we're encoding the & character to & in XHTML mode only, // otherwise the & is written as is to work in javascripts. url = - TagUtils.getInstance().computeURL( + TagUtils.getInstance().computeURLWithCharEncoding( pageContext, forward, href, @@ -106,7 +106,8 @@ params, anchor, false, - this.isXhtml()); + this.isXhtml(), + useLocalEncoding); } catch (MalformedURLException e) { TagUtils.getInstance().saveException(pageContext, e); 1.20 +21 -6 jakarta-struts/src/share/org/apache/struts/taglib/logic/RedirectTag.java Index: RedirectTag.java =================================================================== RCS file: /home/cvs/jakarta-struts/src/share/org/apache/struts/taglib/logic/RedirectTag.java,v retrieving revision 1.19 retrieving revision 1.20 diff -u -r1.19 -r1.20 --- RedirectTag.java 28 Aug 2003 05:57:58 -0000 1.19 +++ RedirectTag.java 1 Jan 2004 19:27:19 -0000 1.20 @@ -246,6 +246,20 @@ this.transaction = transaction; } + /** + * Use character encoding from ServletResponse#getCharacterEncoding + * to get bytes of the url string for urlencoding? + */ + protected boolean useLocalEncoding = false; + + public boolean isUseLocalEncoding() { + return useLocalEncoding; + } + + public void setUseLocalEncoding(boolean b) { + useLocalEncoding = b; + } + // --------------------------------------------------------- Public Methods /** @@ -293,7 +307,7 @@ String url = null; try { url = - TagUtils.getInstance().computeURL( + TagUtils.getInstance().computeURLWithCharEncoding( pageContext, forward, href, @@ -301,7 +315,8 @@ null, params, anchor, - true); + true, + useLocalEncoding); } catch (MalformedURLException e) { TagUtils.getInstance().saveException(pageContext, e); 1.140 +6 -7 jakarta-struts/src/share/org/apache/struts/util/RequestUtils.java Index: RequestUtils.java =================================================================== RCS file: /home/cvs/jakarta-struts/src/share/org/apache/struts/util/RequestUtils.java,v retrieving revision 1.139 retrieving revision 1.140 diff -u -r1.139 -r1.140 --- RequestUtils.java 20 Dec 2003 22:42:52 -0000 1.139 +++ RequestUtils.java 1 Jan 2004 19:27:20 -0000 1.140 @@ -1162,9 +1162,8 @@ throws MalformedURLException { // :TODO: Remove after Struts 1.2 - return computeURL(pageContext, forward, href, page, null, params, - anchor, redirect); - + return (TagUtils.getInstance().computeURLWithCharEncoding( + pageContext, forward, href, page, null, params, anchor, redirect, false)); }
--------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]