You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@ant.apache.org by mb...@apache.org on 2005/12/19 18:52:14 UTC

svn commit: r357737 - /ant/core/trunk/src/main/org/apache/tools/ant/launch/Locator.java

Author: mbenson
Date: Mon Dec 19 09:52:05 2005
New Revision: 357737

URL: http://svn.apache.org/viewcvs?rev=357737&view=rev
Log:
add'l fixes RE using URI >= Java 1.4:  ensure the URI is encoded prior to
passing to URI.create(); ensure no new object is created by encodeUri until
encoding is determined to be necessary.

Modified:
    ant/core/trunk/src/main/org/apache/tools/ant/launch/Locator.java

Modified: ant/core/trunk/src/main/org/apache/tools/ant/launch/Locator.java
URL: http://svn.apache.org/viewcvs/ant/core/trunk/src/main/org/apache/tools/ant/launch/Locator.java?rev=357737&r1=357736&r2=357737&view=diff
==============================================================================
--- ant/core/trunk/src/main/org/apache/tools/ant/launch/Locator.java (original)
+++ ant/core/trunk/src/main/org/apache/tools/ant/launch/Locator.java Mon Dec 19 09:52:05 2005
@@ -156,6 +156,11 @@
         // things when the path is not absolute, and fall back to the old parsing behavior.
         if (uriClazz != null && uri.startsWith("file:/")) {
             try {
+                uri = encodeUri(uri);
+            } catch (UnsupportedEncodingException e) {
+                //leave as-is?
+            }
+            try {
                 java.lang.reflect.Method createMethod = uriClazz.getMethod("create", new Class[] {String.class});
                 Object uriObj = createMethod.invoke(null, new Object[] {uri});
                 java.lang.reflect.Constructor fileConst = File.class.getConstructor(new Class[] {uriClazz});
@@ -257,25 +262,31 @@
         int i = 0;
         int len = path.length();
         int ch = 0;
-        StringBuffer sb = new StringBuffer(len);
+        StringBuffer sb = null;
         for (; i < len; i++) {
             ch = path.charAt(i);
             // if it's not an ASCII character, break here, and use UTF-8 encoding
             if (ch >= 128)
                 break;
             if (gNeedEscaping[ch]) {
+                if (sb == null) {
+                    sb = new StringBuffer(path.substring(0, i));
+                }
                 sb.append('%');
                 sb.append(gAfterEscaping1[ch]);
                 sb.append(gAfterEscaping2[ch]);
                 // record the fact that it's escaped
             }
-            else {
-                sb.append((char)ch);
+            else if (sb != null) {
+                sb.append((char) ch);
             }
         }
 
         // we saw some non-ascii character
         if (i < len) {
+            if (sb == null) {
+                sb = new StringBuffer(path.substring(0, i));
+            }
             // get UTF-8 bytes for the remaining sub-string
             byte[] bytes = null;
             byte b;
@@ -291,18 +302,17 @@
                     sb.append('%');
                     sb.append(gHexChs[ch >> 4]);
                     sb.append(gHexChs[ch & 0xf]);
-                }
-                else if (gNeedEscaping[b]) {
+                } else if (gNeedEscaping[b]) {
                     sb.append('%');
                     sb.append(gAfterEscaping1[b]);
                     sb.append(gAfterEscaping2[b]);
                 }
                 else {
-                    sb.append((char)b);
+                    sb.append((char) b);
                 }
             }
         }
-        return sb.toString();
+        return sb == null ? path : sb.toString();
     }
 
     /**



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


Re: svn commit: r357737 - /ant/core/trunk/src/main/org/apache/tools/ant/launch/Locator.java

Posted by Antoine Levy-Lambert <an...@gmx.de>.
Hello Matt,

1) were your optimizations regarding StringBuffer in Locator#encodeURI()
really necessary ?  With the changes introduced  recently, this method
is rarely called
if you run ant with JDK >= 1.4. On top of that, we could change it to
shortcut it to URLEncoder.encode("UTF-8") under JDK 1.5.

2) the part of your commit where you encode the URI sounds wrong. The
method that you have changed is called fromURI.
What would happen if the input does contain some % characters ?

Cheers,

Antoine

mbenson@apache.org wrote:

>Author: mbenson
>Date: Mon Dec 19 09:52:05 2005
>New Revision: 357737
>
>URL: http://svn.apache.org/viewcvs?rev=357737&view=rev
>Log:
>add'l fixes RE using URI >= Java 1.4:  ensure the URI is encoded prior to
>passing to URI.create(); ensure no new object is created by encodeUri until
>encoding is determined to be necessary.
>
>Modified:
>    ant/core/trunk/src/main/org/apache/tools/ant/launch/Locator.java
>
>Modified: ant/core/trunk/src/main/org/apache/tools/ant/launch/Locator.java
>URL: http://svn.apache.org/viewcvs/ant/core/trunk/src/main/org/apache/tools/ant/launch/Locator.java?rev=357737&r1=357736&r2=357737&view=diff
>==============================================================================
>--- ant/core/trunk/src/main/org/apache/tools/ant/launch/Locator.java (original)
>+++ ant/core/trunk/src/main/org/apache/tools/ant/launch/Locator.java Mon Dec 19 09:52:05 2005
>@@ -156,6 +156,11 @@
>         // things when the path is not absolute, and fall back to the old parsing behavior.
>         if (uriClazz != null && uri.startsWith("file:/")) {
>             try {
>+                uri = encodeUri(uri);
>+            } catch (UnsupportedEncodingException e) {
>+                //leave as-is?
>+            }
>+            try {
>  
>


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