You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ant.apache.org by mb...@apache.org on 2010/01/15 00:26:48 UTC

svn commit: r899468 - in /ant/antlibs/props/trunk: docs/index.html src/main/org/apache/ant/props/EncodeURLEvaluator.java src/main/org/apache/ant/props/antlib.xml src/tests/antunit/encodeURL-test.xml

Author: mbenson
Date: Thu Jan 14 23:26:47 2010
New Revision: 899468

URL: http://svn.apache.org/viewvc?rev=899468&view=rev
Log:
add EncodeURLEvaluator

Added:
    ant/antlibs/props/trunk/src/main/org/apache/ant/props/EncodeURLEvaluator.java   (with props)
    ant/antlibs/props/trunk/src/tests/antunit/encodeURL-test.xml   (with props)
Modified:
    ant/antlibs/props/trunk/docs/index.html
    ant/antlibs/props/trunk/src/main/org/apache/ant/props/antlib.xml

Modified: ant/antlibs/props/trunk/docs/index.html
URL: http://svn.apache.org/viewvc/ant/antlibs/props/trunk/docs/index.html?rev=899468&r1=899467&r2=899468&view=diff
==============================================================================
--- ant/antlibs/props/trunk/docs/index.html (original)
+++ ant/antlibs/props/trunk/docs/index.html Thu Jan 14 23:26:47 2010
@@ -129,6 +129,14 @@
             <code><em>type</em></code> constructor <code>(Project, <em>arg</em>)</code>,
             then <code>(<em>arg</em>)</code>.</td>
       </tr>
+      <tr>
+        <a name="encodeURL" />
+        <td align="center">encodeURL</td>
+        <td align="center">PropertyEvaluator</td>
+        <td>Given <code>encodeURL:<em>arg</em></code>, attempts to encode <em>arg</em>
+            as a URL per the suggested approach in the javadoc API of <code>java.net.URL</code>.
+        </td>
+      </tr>
     </table>
     <hr/>
   </body>

Added: ant/antlibs/props/trunk/src/main/org/apache/ant/props/EncodeURLEvaluator.java
URL: http://svn.apache.org/viewvc/ant/antlibs/props/trunk/src/main/org/apache/ant/props/EncodeURLEvaluator.java?rev=899468&view=auto
==============================================================================
--- ant/antlibs/props/trunk/src/main/org/apache/ant/props/EncodeURLEvaluator.java (added)
+++ ant/antlibs/props/trunk/src/main/org/apache/ant/props/EncodeURLEvaluator.java Thu Jan 14 23:26:47 2010
@@ -0,0 +1,60 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ *
+ */
+package org.apache.ant.props;
+
+import java.net.URI;
+import java.net.URL;
+
+import org.apache.tools.ant.Project;
+import org.apache.tools.ant.PropertyHelper;
+
+/**
+ * PropertyEvaluator that resolves a reference against the current project.
+ */
+public class EncodeURLEvaluator extends StaticPrefixedEvaluator {
+    /** Default prefix */
+    public static final String DEFAULT_PREFIX = "encodeURL";
+
+    /**
+     * Create a new EncodeURLEvaluator.
+     */
+    public EncodeURLEvaluator() {
+        super(DEFAULT_PREFIX);
+    }
+
+    /**
+     * {@inheritDoc}
+     * 
+     * @see org.apache.ant.props.PrefixedEvaluator#evaluatePrefixed(java.lang.String,
+     *      java.lang.String, org.apache.tools.ant.PropertyHelper)
+     */
+    protected Object evaluate(String property, String prefix, PropertyHelper propertyHelper) {
+        try {
+            URL url = new URL(property);
+            URI uri = new URI(url.getProtocol(), url.getUserInfo(),
+                     url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());
+            return uri.toASCIIString();
+        } catch (Exception e) {
+            propertyHelper.getProject().log("Encountered exception encoding URL text \""
+                    + property + "\"; aborting", e, Project.MSG_ERR);
+            return null;
+        }
+    }
+}

Propchange: ant/antlibs/props/trunk/src/main/org/apache/ant/props/EncodeURLEvaluator.java
------------------------------------------------------------------------------
    svn:eol-style = native

Modified: ant/antlibs/props/trunk/src/main/org/apache/ant/props/antlib.xml
URL: http://svn.apache.org/viewvc/ant/antlibs/props/trunk/src/main/org/apache/ant/props/antlib.xml?rev=899468&r1=899467&r2=899468&view=diff
==============================================================================
--- ant/antlibs/props/trunk/src/main/org/apache/ant/props/antlib.xml (original)
+++ ant/antlibs/props/trunk/src/main/org/apache/ant/props/antlib.xml Thu Jan 14 23:26:47 2010
@@ -22,4 +22,5 @@
   <typedef name="stringops" classname="org.apache.ant.props.stringops.StringOperationsEvaluator" />
   <typedef name="types" classname="org.apache.ant.props.ComponentTypeEvaluator" />
   <typedef name="refs" classname="org.apache.ant.props.ReferenceResolvingEvaluator" />
+  <typedef name="encodeURL" classname="org.apache.ant.props.EncodeURLEvaluator" />
 </antlib>

Added: ant/antlibs/props/trunk/src/tests/antunit/encodeURL-test.xml
URL: http://svn.apache.org/viewvc/ant/antlibs/props/trunk/src/tests/antunit/encodeURL-test.xml?rev=899468&view=auto
==============================================================================
--- ant/antlibs/props/trunk/src/tests/antunit/encodeURL-test.xml (added)
+++ ant/antlibs/props/trunk/src/tests/antunit/encodeURL-test.xml Thu Jan 14 23:26:47 2010
@@ -0,0 +1,52 @@
+<?xml version="1.0"?>
+<!--
+   Licensed to the Apache Software Foundation (ASF) under one or more
+   contributor license agreements.  See the NOTICE file distributed with
+   this work for additional information regarding copyright ownership.
+   The ASF licenses this file to You under the Apache License, Version 2.0
+   (the "License"); you may not use this file except in compliance with
+   the License.  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+   Unless required by applicable law or agreed to in writing, software
+   distributed under the License is distributed on an "AS IS" BASIS,
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+   See the License for the specific language governing permissions and
+   limitations under the License.
+-->
+<project default="antunit" xmlns:au="antlib:org.apache.ant.antunit"
+         xmlns:props="antlib:org.apache.ant.props">
+  <target name="setUp">
+    <propertyhelper>
+      <props:encodeURL />
+      <props:encodeURL delimiter="," />
+      <props:nested />
+    </propertyhelper>
+    <path id="cp" path="${java.class.path}" />
+  </target>
+
+  <target name="testBasic" depends="setUp">
+    <au:assertTrue>
+      <equals arg1="${encodeURL:http://ant.apache.org}" arg2="http://ant.apache.org" />
+    </au:assertTrue>
+    <au:assertTrue>
+      <equals arg1="${encodeURL:http://ant.apache.org/foo bar}"
+              arg2="http://ant.apache.org/foo%20bar" />
+    </au:assertTrue>
+  </target>
+
+  <target name="testDelimiter" depends="setUp">
+    <au:assertTrue>
+      <equals arg1="${encodeURL,http://ant.apache.org/foo bar?baz}"
+              arg2="http://ant.apache.org/foo%20bar?baz" />
+    </au:assertTrue>
+  </target>
+
+  <target name="antunit">
+    <au:antunit>
+      <fileset file="${ant.file}" />
+    </au:antunit>
+  </target>
+
+</project>

Propchange: ant/antlibs/props/trunk/src/tests/antunit/encodeURL-test.xml
------------------------------------------------------------------------------
    svn:eol-style = native



Re: svn commit: r899468 - in /ant/antlibs/props/trunk: docs/index.html src/main/org/apache/ant/props/EncodeURLEvaluator.java src/main/org/apache/ant/props/antlib.xml src/tests/antunit/encodeURL-test.xml

Posted by Matt Benson <gu...@gmail.com>.
On Jan 15, 2010, at 6:48 PM, Jesse Glick wrote:

> Matt Benson wrote:
>> Java 5 blows up when call toURI() against a URL with a space  
>> included.
>
> Which is correct; such a URL is invalid:
>
> $ jrunscript
> js> new java.net.URL("file:/tmp/foo bar").toURI()
> script error: sun.org.mozilla.javascript.internal.WrappedException:  
> Wrapped java.net.URISyntaxException: Illegal character in path at  
> index 13: file:/tmp/foo bar (<STDIN>#1) in <STDIN> at line number 1
> js> new java.net.URL("file:/tmp/foo%20bar").toURI()
> file:/tmp/foo%20bar
>
> Maybe I'm missing the purpose of EncodeURLEvaluator, but where  
> would you get a bogus URL like that from to begin with? Surely not  
> from <makeurl> on a file, which ought to escape spaces in  
> filenames. I assumed that the purpose was just to encode non-ASCII  
> characters, for which toURI is fine:
>

The main idea here is to allow folk to use URLs containing e.g.  
spaces that their browser will accept and silently convert for them.

> js> new java.net.URL("file:/tmp/foočbar").toURI().toASCIIString()
> file:/tmp/foo%C4%8Dbar
>
> Or are you trying to encode path sequences (rather than complete  
> URLs)? But then there is an easier way, without using URL at all:
>
> js> new java.net.URI(null, "foo bar", null).rawPath
> foo%20bar
>
> Anyway does this belong in Ant 1.8.0 so late in the release cycle?
>

Ah, but it's not in Ant--it's in the props antlib!  -Matt

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


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


Re: svn commit: r899468 - in /ant/antlibs/props/trunk: docs/index.html src/main/org/apache/ant/props/EncodeURLEvaluator.java src/main/org/apache/ant/props/antlib.xml src/tests/antunit/encodeURL-test.xml

Posted by Jesse Glick <je...@sun.com>.
Matt Benson wrote:
> Java 5 blows up when call toURI() against a URL with a space included.

Which is correct; such a URL is invalid:

$ jrunscript
js> new java.net.URL("file:/tmp/foo bar").toURI()
script error: sun.org.mozilla.javascript.internal.WrappedException: Wrapped java.net.URISyntaxException: Illegal character in path at index 13: file:/tmp/foo bar 
(<STDIN>#1) in <STDIN> at line number 1
js> new java.net.URL("file:/tmp/foo%20bar").toURI()
file:/tmp/foo%20bar

Maybe I'm missing the purpose of EncodeURLEvaluator, but where would you get a bogus URL like that from to begin with? Surely not from <makeurl> on a file, which ought to 
escape spaces in filenames. I assumed that the purpose was just to encode non-ASCII characters, for which toURI is fine:

js> new java.net.URL("file:/tmp/foočbar").toURI().toASCIIString()
file:/tmp/foo%C4%8Dbar

Or are you trying to encode path sequences (rather than complete URLs)? But then there is an easier way, without using URL at all:

js> new java.net.URI(null, "foo bar", null).rawPath
foo%20bar

Anyway does this belong in Ant 1.8.0 so late in the release cycle?


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


AW: svn commit: r899468 - in /ant/antlibs/props/trunk: docs/index.html src/main/org/apache/ant/props/EncodeURLEvaluator.java src/main/org/apache/ant/props/antlib.xml src/tests/antunit/encodeURL-test.xml

Posted by Ja...@rzf.fin-nrw.de.
Is this a candidate for FileUtils with a proper javadoc?

Jan 

>-----Ursprüngliche Nachricht-----
>Von: Matt Benson [mailto:gudnabrsam@gmail.com] 
>Gesendet: Freitag, 15. Januar 2010 00:52
>An: Ant Developers List
>Betreff: Re: svn commit: r899468 - in 
>/ant/antlibs/props/trunk: docs/index.html 
>src/main/org/apache/ant/props/EncodeURLEvaluator.java 
>src/main/org/apache/ant/props/antlib.xml 
>src/tests/antunit/encodeURL-test.xml
>
>To confirm that re-testing on OSX Tiger w/ Java 5 blows up when call  
>toURI() against a URL with a space included.
>
>On Jan 14, 2010, at 5:45 PM, Matt Benson wrote:
>
>> Yes--my testing seems to indicate that the single-arg constructor  
>> of URI is used in this case, which blows up for improperly escaped  
>> characters per its javadoc.  I'll double-check though.
>>
>> Thanks,
>> Matt
>>
>> On Jan 14, 2010, at 5:36 PM, Jesse Glick wrote:
>>
>>> mbenson@apache.org wrote:
>>>> URL: http://svn.apache.org/viewvc/ant/antlibs/props/trunk/src/ 
>>>> main/org/apache/ant/props/EncodeURLEvaluator.java? 
>>>> rev=899468&view=auto
>>>> 
>==================================================================== 
>>>> ==========
>>>> +            URI uri = new URI(url.getProtocol(), 
>url.getUserInfo(),
>>>> +                     url.getHost(), url.getPort(), url.getPath 
>>>> (), url.getQuery(), url.getRef());
>>>
>>> Something wrong with
>>>
>>>   URI uri = url.toURI();
>>>
>>> ?
>>>
>>> 
>---------------------------------------------------------------------
>>> To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
>>> For additional commands, e-mail: dev-help@ant.apache.org
>>>
>>
>
>
>---------------------------------------------------------------------
>To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
>For additional commands, e-mail: dev-help@ant.apache.org
>
>

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


Re: svn commit: r899468 - in /ant/antlibs/props/trunk: docs/index.html src/main/org/apache/ant/props/EncodeURLEvaluator.java src/main/org/apache/ant/props/antlib.xml src/tests/antunit/encodeURL-test.xml

Posted by Matt Benson <gu...@gmail.com>.
To confirm that re-testing on OSX Tiger w/ Java 5 blows up when call  
toURI() against a URL with a space included.

On Jan 14, 2010, at 5:45 PM, Matt Benson wrote:

> Yes--my testing seems to indicate that the single-arg constructor  
> of URI is used in this case, which blows up for improperly escaped  
> characters per its javadoc.  I'll double-check though.
>
> Thanks,
> Matt
>
> On Jan 14, 2010, at 5:36 PM, Jesse Glick wrote:
>
>> mbenson@apache.org wrote:
>>> URL: http://svn.apache.org/viewvc/ant/antlibs/props/trunk/src/ 
>>> main/org/apache/ant/props/EncodeURLEvaluator.java? 
>>> rev=899468&view=auto
>>> ==================================================================== 
>>> ==========
>>> +            URI uri = new URI(url.getProtocol(), url.getUserInfo(),
>>> +                     url.getHost(), url.getPort(), url.getPath 
>>> (), url.getQuery(), url.getRef());
>>
>> Something wrong with
>>
>>   URI uri = url.toURI();
>>
>> ?
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
>> For additional commands, e-mail: dev-help@ant.apache.org
>>
>


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


Re: svn commit: r899468 - in /ant/antlibs/props/trunk: docs/index.html src/main/org/apache/ant/props/EncodeURLEvaluator.java src/main/org/apache/ant/props/antlib.xml src/tests/antunit/encodeURL-test.xml

Posted by Matt Benson <gu...@gmail.com>.
Yes--my testing seems to indicate that the single-arg constructor of  
URI is used in this case, which blows up for improperly escaped  
characters per its javadoc.  I'll double-check though.

Thanks,
Matt

On Jan 14, 2010, at 5:36 PM, Jesse Glick wrote:

> mbenson@apache.org wrote:
>> URL: http://svn.apache.org/viewvc/ant/antlibs/props/trunk/src/main/ 
>> org/apache/ant/props/EncodeURLEvaluator.java?rev=899468&view=auto
>> ===================================================================== 
>> =========
>> +            URI uri = new URI(url.getProtocol(), url.getUserInfo(),
>> +                     url.getHost(), url.getPort(), url.getPath(),  
>> url.getQuery(), url.getRef());
>
> Something wrong with
>
>   URI uri = url.toURI();
>
> ?
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: dev-unsubscribe@ant.apache.org
> For additional commands, e-mail: dev-help@ant.apache.org
>


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


Re: svn commit: r899468 - in /ant/antlibs/props/trunk: docs/index.html src/main/org/apache/ant/props/EncodeURLEvaluator.java src/main/org/apache/ant/props/antlib.xml src/tests/antunit/encodeURL-test.xml

Posted by Jesse Glick <Je...@Sun.COM>.
mbenson@apache.org wrote:
> URL: http://svn.apache.org/viewvc/ant/antlibs/props/trunk/src/main/org/apache/ant/props/EncodeURLEvaluator.java?rev=899468&view=auto
> ==============================================================================
> +            URI uri = new URI(url.getProtocol(), url.getUserInfo(),
> +                     url.getHost(), url.getPort(), url.getPath(), url.getQuery(), url.getRef());

Something wrong with

   URI uri = url.toURI();

?

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