Couldn't do it without letting a context's currentUrl be the full url of
the script - sorry :)

I didn't remove the rootUrl stuff: it's still inferred from currentUrl
(although context-relative url resolution uses new URL(URL, String) now)

I used jdk 1.3.1

---

TODO:

- org.apache.commons.jelly.task.JellyTask needs fixing
- org.apache.commons.jelly.test.BaseJellyTest needs fixing
 (BTW: it's in src/java instead of into src/test)
- org.apache.commons.jelly.core.BaseMemoryLeakTest could need fixing

---

I took a look at xml-commons-resolver: didn't know xml had a commons
project...
It looks like an implementation of OASIS xml catalog - AFAIK it is a
standard for mapping URNs to URLs (eg: for locating the DTD
corresponding to a particular public doctype).
I guess it could be helpful for the tag-library stuff: jelly could
set-up a catalog based on all the catalog files it finds inside the
various META-INF in the classpath and use it for locating taglib
descriptors (if they are to be introduced into jelly - or classes
otherwise).
I imagine we could even let taglibs be defined via doctype declarations
instead of xmlns attributes... it could sound a little bit useless but i
think it would be really cool :)

About "exotic" URL schemes, what I was trying to say is that
URL-to-resource resolution is entirely dependand on the URL scheme (ie:
protocol): in the patch I added two tests wich use a custom URL scheme
named "dummyprotocol" that resolves

        dummyprotocol:a.jelly

to the file

        src/test/org/apache/commons/jelly/core/a.jelly

In the same way I could define a scheme to resolve

        home:///giorgio/a.jelly

to the file

        /home/giorgio/a.jelly

All theese custom protocols pose no problems to jelly as long as they
have a path-like structure, with path elements separated by '/' and in
"descending" order.

Now imagine i want to define a scheme that maps URLs to email messages:
your message (ie: the one I am replying to) is assigned and id (I guess
from your smtp - but I don't know really who assigns theese ids) of
[EMAIL PROTECTED]

I could want to resolve

        emailmessage:[EMAIL PROTECTED]

to that message and write an URLStreamHandler that does so.

Well, there are really no relative urls inside an email message (besides
stuff inside the message itself) so never mind about if this could pose
any threat to jelly... :)

Let's go back to the "home:" stuff.

Instead of the former "usual-way-structured" scheme, I could (for
whatever reason) want to have URLs like

        home:[EMAIL PROTECTED]

resolved to files inside my home directory - and i could write an
URLStreamHandler to do so.

What about referring to a file that is beneath my home dir, buryed into
tons of subfolders?

It could be

        home:java-stuff/asf/jelly/src/org/[...]/core/[EMAIL PROTECTED]

and this would definitely break jelly...



Hope to have my point explained a little bit more clearly than yesterday
- I was _really_ sleepy when I wrote that message :)

PS:
Sorry for the crappy examples - I just couldn't think of any widely-used
protocol which doesn't use a path-like structure and is commonly used
with relative urls...



Paul Libbrecht wrote:
> Giorgio,
> 
> I am not sure I have grasped the whole picture...
> 
> Would you be able to make a first patch that only tackles the usage of
> new URL(url) and then pinpoint the further issues ?
> 
> I am wondering if the usage of xml-commons-resolver would not help here
> ? It's a quite small package, it's well licensed (!), and it's very
> close to our needs.
> 
> paul
> 
> 
> Le 11 avr. 05, à 20:30, Giorgio Gallo a écrit :
> 
>>
>> I am trying to write a patch to solve issue JELLY-208
>> (http://issues.apache.org/jira/browse/JELLY-208)
>>
>> Let me know if you committers prefer to handle this one directly,
>> because of the fact that URLs contain "/" beeing assumed just about
>> everywhere in jelly... :)
>>
>> Also:
>>
>> Thomas Burns (the issue reporter) mentions a "custom url protocol".
>>
>> It is not needed for the specific issue to manifest (think of
>> "file:script.jelly") to have a custom protocol, but it could be the case
>>  that a protocol does not use the "usual" URL pattern
>> (protocol://user:[EMAIL PROTECTED]:port/path...) and that therefore we (ie:
>> jelly) cannot resolve relative URLs simply by parsing the string
>> representation of the base URL and assuming it has more meaning that
>> protocol:opaquePart...
>>
>> Besides modifying jelly to ALWAYS use
>>     new URL(URL, String)
>> to resolve relative URLs, another fact should/could be addressed: it
>> could be impossible in some protocols to infer the "parent" URL of a
>> script from the script's own URL (ie: it's what
>> JellyContext#getJellyContextURL does)
>>
>>
>> I see a few options to address this:
>>
>> 1. The "protocol:<opaquePart>" thing above comes from some confused
>> corner of my head and I have to read RFCs again :)
>>
>> 2. Declare those "exotic" protocols unsupported :)
>>
>> 3. Refactor jelly to use a script's very url (eg:
>> file:///dir/dir/script.jelly) to resolve relative URLs and remove
>> auto-detection of "context URLs" altoghether (ie: context-relative URLs
>> will be resolved against the script URL unless a different context URL
>> is explicitly specifyed)
>>
>> 4. use null as context url when there are no "/"s and assume the usual
>> URL pattern if a "/" is present
>>
>>
>> I would opt for the third possibility (really - can't see any drawback
>> in it, not at 4:00AM at least).
>>
>> Please let me know if such a patch would be welcome or, again, if it's
>> like you committers prefer to handle this on your own.
>>
>> (please pardon me if my english is not very clear)
>>
>> rgds,
>> Giorgio
>>
>>
>>
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: [EMAIL PROTECTED]
>> For additional commands, e-mail: [EMAIL PROTECTED]
>>
> 
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail: [EMAIL PROTECTED]
> 
Index: src/test/org/apache/commons/jelly/core/TestIncludeTag.java
===================================================================
--- src/test/org/apache/commons/jelly/core/TestIncludeTag.java  (revision 
161082)
+++ src/test/org/apache/commons/jelly/core/TestIncludeTag.java  (working copy)
@@ -64,12 +64,11 @@
         jelly = new Jelly();
 
         jelly.setUrl(url);
-
-        String exturl = url.toExternalForm();
-        int lastSlash = exturl.lastIndexOf("/");
-        String extBase = exturl.substring(0,lastSlash+1);
-        URL baseurl = new URL(extBase);
-        context.setCurrentURL(baseurl);
+        // GIORGIO: modifyed
+        //          currentUrl can safely be the script's very URL, since it
+        //          is only used to resolve "truly" relative urls (ie: those
+        //          which are relative to the script url itself)
+        context.setCurrentURL(url);
     }
 
     public void testInnermost() throws Exception {
@@ -104,7 +103,37 @@
         assertTrue("should have set 'a' variable to 'true'",
                    context.getVariable("a").equals("true"));
     }
+    
+    // GIORGIO: added this test
+    public void testDummyProtocolIncludeWithSlashes() throws Exception {
+        // testing outermost
+        org.apache.commons.jelly.dummyprotocol.Handler.registerProtocol();
+        setUpFromURL(new URL("dummyprotocol:../core/a.jelly"));
+        Script script = jelly.compileScript();
+        script.run(context,xmlOutput);
+        assertTrue("should have set 'c' variable to 'true'",
+                   context.getVariable("c").equals("true"));
+        assertTrue("should have set 'b' variable to 'true'",
+                   context.getVariable("b").equals("true"));
+        assertTrue("should have set 'a' variable to 'true'",
+                   context.getVariable("a").equals("true"));
+    }
 
+    // GIORGIO: added this test
+    public void testDummyProtocolIncludeNoSlashes() throws Exception {
+        // testing outermost
+        org.apache.commons.jelly.dummyprotocol.Handler.registerProtocol();
+        setUpFromURL(new URL("dummyprotocol:a.jelly"));
+        Script script = jelly.compileScript();
+        script.run(context,xmlOutput);
+        assertTrue("should have set 'c' variable to 'true'",
+                   context.getVariable("c").equals("true"));
+        assertTrue("should have set 'b' variable to 'true'",
+                   context.getVariable("b").equals("true"));
+        assertTrue("should have set 'a' variable to 'true'",
+                   context.getVariable("a").equals("true"));
+    }
+
     /**
      * Insure that includes happen correctly when Jelly scripts
      * are referenced as a file (rather than as a classpath
Index: src/test/org/apache/commons/jelly/dummyprotocol/Handler.java
===================================================================
--- src/test/org/apache/commons/jelly/dummyprotocol/Handler.java        
(revision 0)
+++ src/test/org/apache/commons/jelly/dummyprotocol/Handler.java        
(revision 0)
@@ -0,0 +1,39 @@
+
+package org.apache.commons.jelly.dummyprotocol;
+
+import java.io.File;
+import java.io.IOException;
+import java.net.URL;
+import java.net.URLConnection;
+import java.net.URLStreamHandler;
+
+/**
+ * Custom URL protocol handler for protocol "test-protocol", which will resolve
+ * <tt>"dummyprotocol://whatever/folder/file.name"</tt> to a file with path
+ * <tt>BASE_PATH + "folder/file.name"</tt>.
+ * <br>
+ * <b>WARNING:</b> this protocol handler is not complete - it's been 
implemented
+ * for testing only - do <b>not</b> use this class for deriving a
+ * production-ready protocol handler!
+ * <br>
+ * For an explaination of how it works, see 
+ * <a href="http://java.sun.com/developer/onlineTraining/protocolhandlers/";
+ * >// http://java.sun.com/developer/onlineTraining/protocolhandlers/</a>
+ * 
+ * @author <a href="[EMAIL PROTECTED]">Giorgio Gallo</a>
+ */
+public class Handler extends URLStreamHandler{
+       
+       private static final String BASE_PATH = 
"src/test/org/apache/commons/jelly/core";
+       
+       public static void registerProtocol(){          
+               // define a new protocol named after this package's name (ie: 
"dummyprotocol")
+               System.setProperty("java.protocol.handler.pkgs", 
"org.apache.commons.jelly");
+       }
+
+       protected URLConnection openConnection(URL u) throws IOException{
+               URL realURL = new File(BASE_PATH, u.getPath()).toURL();
+               return realURL.openConnection();
+       }
+
+}
Index: src/java/org/apache/commons/jelly/Jelly.java
===================================================================
--- src/java/org/apache/commons/jelly/Jelly.java        (revision 161082)
+++ src/java/org/apache/commons/jelly/Jelly.java        (working copy)
@@ -208,16 +208,20 @@
      */
     public JellyContext getJellyContext() throws MalformedURLException {
         if (context == null) {
-            // take off the name off the URL
-            String text = getUrl().toString();
-            int idx = text.lastIndexOf('/');
-            text = text.substring(0, idx + 1);
-            context = new JellyContext(getRootContext(), new URL(text));
+            // GIORGIO: modifyed
+            //          currentUrl can safely be the script's very URL, since 
it
+            //          is only used to resolve "truly" relative urls (ie: 
those
+            //          which are relative to the script url itself)
+            //// take off the name off the URL
+            //String text = getUrl().toString();
+            //int idx = text.lastIndexOf('/');
+            //text = text.substring(0, idx + 1);
+            //context = new JellyContext(getRootContext(), new URL(text));
+            context = new JellyContext(getRootContext(), getUrl());
         }
         return context;
     }
 
-
     /**
      * Set the jelly namespace to use for unprefixed elements.
      * Will be overridden by an explicit namespace in the
Index: src/java/org/apache/commons/jelly/impl/TagScript.java
===================================================================
--- src/java/org/apache/commons/jelly/impl/TagScript.java       (revision 
161082)
+++ src/java/org/apache/commons/jelly/impl/TagScript.java       (working copy)
@@ -172,10 +172,23 @@
      * FIXME: Copied from JellyContext
      */
     private URL getJellyContextURL(URL url) throws MalformedURLException {
-        String text = url.toString();
-        int idx = text.lastIndexOf('/');
-        text = text.substring(0, idx + 1);
-        return new URL(text);
+        // GIORGIO try to infer parent url
+        //         will assume assume all URLs have an intellegible format
+        //         (still copied from JellyContext)
+        //String text = url.toString();
+        //int idx = text.lastIndexOf('/');
+        //text = text.substring(0, idx + 1);
+        //return new URL(text);
+        String authorityPart = url.getProtocol()
+                             + ":"
+                             + (url.getAuthority() != null ? "/" + 
url.getAuthority() : "");
+        URL authorityURL = new URL(authorityPart);
+        String path = url.getPath();
+        int lastSlash = path.lastIndexOf('/');
+        if(lastSlash != -1){
+            path = path.substring(0, lastSlash);
+        }
+        return new URL(authorityURL, path);
     }
 
     // Script interface
Index: src/java/org/apache/commons/jelly/JellyContext.java
===================================================================
--- src/java/org/apache/commons/jelly/JellyContext.java (revision 161082)
+++ src/java/org/apache/commons/jelly/JellyContext.java (working copy)
@@ -110,7 +110,9 @@
      * Create a new context with the currentURL set to the rootURL
      */
     public JellyContext() {
-        this.currentURL = rootURL;
+        // GIORGIO: removed
+        //          no way can rootURL be != null here
+        // this.currentURL = rootURL;
         init();
     }
 
@@ -997,21 +999,39 @@
             File file = new File(System.getProperty("user.dir"));
             url = file.toURL();
         }
-        String urlText = url.toString() + relativeURI;
-        if ( log.isDebugEnabled() ) {
-            log.debug("Attempting to open url: " + urlText);
+        // GIORGIO changed to use new URL(URL, String)
+        //String urlText = url.toString() + relativeURI;
+        //if ( log.isDebugEnabled() ) {
+        //    log.debug("Attempting to open url: " + urlText);
+        //}
+        //return new URL(urlText);
+        URL rv = new URL(url, relativeURI);
+        if(log.isDebugEnabled()){
+            log.debug("Attempting to open url: " + url.toExternalForm());
         }
-        return new URL(urlText);
+        return rv;
     }
 
     /**
      * Strips off the name of a script to create a new context URL
      */
     protected URL getJellyContextURL(URL url) throws MalformedURLException {
-        String text = url.toString();
-        int idx = text.lastIndexOf('/');
-        text = text.substring(0, idx + 1);
-        return new URL(text);
+        // GIORGIO try to infer parent url
+        //         will assume assume all URLs have an intellegible format
+        //String text = url.toString();
+        //int idx = text.lastIndexOf('/');
+        //text = text.substring(0, idx + 1);
+        //return new URL(text);
+        String authorityPart = url.getProtocol()
+                             + ":"
+                             + (url.getAuthority() != null ? "//" + 
url.getAuthority() : "");
+        URL authorityURL = new URL(authorityPart);
+        String path = url.getPath();
+        int lastSlash = path.lastIndexOf('/');
+        if(lastSlash != -1){
+            path = path.substring(0, lastSlash +1);
+        }
+        return new URL(authorityURL, path);
     }
 
     /**
@@ -1020,9 +1040,11 @@
     protected URL getJellyContextURL(InputSource source) throws 
MalformedURLException {
         String text = source.getSystemId();
         if (text != null) {
-            int idx = text.lastIndexOf('/');
-            text = text.substring(0, idx + 1);
-            return new URL(text);
+            // GIORGIO refactored to use getJellyContextURL(URL)
+            //int idx = text.lastIndexOf('/');
+            //text = text.substring(0, idx + 1);
+            //return new URL(text);
+            return getJellyContextURL(new URL(text));
         } else {
             return null;
         }
Index: .classpath
===================================================================
--- .classpath  (revision 161082)
+++ .classpath  (working copy)
@@ -1,23 +1,23 @@
 <?xml version="1.0" encoding="UTF-8"?>
-
 <classpath>
-  <classpathentry excluding="" kind="src" path="src\java"/>
-  <classpathentry output="target\test-classes" kind="src" path="src\test"/>
-  <classpathentry kind="var" path="MAVEN_REPO/junit/jars/junit-3.8.1.jar"/>
-  <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
-  <classpathentry kind="var" 
path="MAVEN_REPO/commons-jexl/jars/commons-jexl-1.0.jar"/>
-  <classpathentry kind="var" 
path="MAVEN_REPO/xml-apis/jars/xml-apis-1.0.b2.jar"/>
-  <classpathentry kind="var" 
path="MAVEN_REPO/commons-beanutils/jars/commons-beanutils-1.7.0.jar"/>
-  <classpathentry kind="var" 
path="MAVEN_REPO/commons-collections/jars/commons-collections-2.1.jar"/>
-  <classpathentry kind="var" 
path="MAVEN_REPO/commons-logging/jars/commons-logging-1.0.3.jar"/>
-  <classpathentry kind="var" path="MAVEN_REPO/dom4j/jars/dom4j-1.5.jar"/>
-  <classpathentry kind="var" 
path="MAVEN_REPO/jaxen/jars/jaxen-1.1-beta-2.jar"/>
-  <classpathentry kind="var" path="MAVEN_REPO/xerces/jars/xerces-2.2.1.jar"/>
-  <classpathentry kind="var" 
path="MAVEN_REPO/servletapi/jars/servletapi-2.3.jar"/>
-  <classpathentry kind="var" 
path="MAVEN_REPO/commons-cli/jars/commons-cli-1.0.jar"/>
-  <classpathentry kind="var" 
path="MAVEN_REPO/commons-lang/jars/commons-lang-2.0.jar"/>
-  <classpathentry kind="var" 
path="MAVEN_REPO/commons-discovery/jars/commons-discovery-20030211.213356.jar"/>
-  <classpathentry kind="var" 
path="MAVEN_REPO/forehead/jars/forehead-1.0-beta-5.jar"/>
-  <classpathentry kind="var" path="MAVEN_REPO/jstl/jars/jstl-1.0.6.jar"/>
-  <classpathentry kind="output" path="target\classes"/>
-</classpath>
\ No newline at end of file
+       <classpathentry kind="src" path="src/java"/>
+       <classpathentry output="target/test-classes" kind="src" 
path="src/test"/>
+       <classpathentry kind="var" 
path="MAVEN_REPO/junit/jars/junit-3.8.1.jar"/>
+       <classpathentry kind="con" 
path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/jre1.3.1"/>
+       <classpathentry kind="var" 
path="MAVEN_REPO/servletapi/jars/servletapi-2.3.jar"/>
+       <classpathentry kind="var" 
path="MAVEN_REPO/commons-cli/jars/commons-cli-1.0.jar"/>
+       <classpathentry kind="var" 
path="MAVEN_REPO/commons-lang/jars/commons-lang-2.0.jar"/>
+       <classpathentry kind="var" 
path="MAVEN_REPO/commons-discovery/jars/commons-discovery-20030211.213356.jar"/>
+       <classpathentry kind="var" 
path="MAVEN_REPO/forehead/jars/forehead-1.0-beta-5.jar"/>
+       <classpathentry kind="var" path="MAVEN_REPO/jstl/jars/jstl-1.0.6.jar"/>
+       <classpathentry kind="var" 
path="MAVEN_REPO/commons-jexl/jars/commons-jexl-1.0.jar"/>
+       <classpathentry kind="var" 
path="MAVEN_REPO/xml-apis/jars/xml-apis-1.0.b2.jar"/>
+       <classpathentry kind="var" 
path="MAVEN_REPO/commons-beanutils/jars/commons-beanutils-1.6.jar"/>
+       <classpathentry kind="var" 
path="MAVEN_REPO/commons-collections/jars/commons-collections-2.1.jar"/>
+       <classpathentry kind="var" 
path="MAVEN_REPO/commons-logging/jars/commons-logging-1.0.3.jar"/>
+       <classpathentry kind="var" 
path="MAVEN_REPO/dom4j/jars/dom4j-1.5.2.jar"/>
+       <classpathentry kind="var" 
path="MAVEN_REPO/jaxen/jars/jaxen-1.1-beta-4.jar"/>
+       <classpathentry kind="var" 
path="MAVEN_REPO/xerces/jars/xerces-2.2.1.jar"/>
+       <classpathentry kind="src" path="src/sratches"/>
+       <classpathentry kind="output" path="target/classes"/>
+</classpath>

---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to