Revision: 19997
          http://sourceforge.net/p/gate/code/19997
Author:   markagreenwood
Date:     2017-01-26 18:04:47 +0000 (Thu, 26 Jan 2017)
Log Message:
-----------
creole:// URIs now work in such a way that they don't have to only refer to 
maven based plugins :) -- there is a weird issue with neeing to add a . the 
path that I need to think about further to check I've not missed anything but 
this looks promising

Modified Paths:
--------------
    
gate/branches/sawdust2/gate-core/src/main/java/gate/creole/ResourceReference.java
    
gate/branches/sawdust2/gate-core/src/test/java/gate/creole/TestResourceReference.java

Modified: 
gate/branches/sawdust2/gate-core/src/main/java/gate/creole/ResourceReference.java
===================================================================
--- 
gate/branches/sawdust2/gate-core/src/main/java/gate/creole/ResourceReference.java
   2017-01-26 17:20:42 UTC (rev 19996)
+++ 
gate/branches/sawdust2/gate-core/src/main/java/gate/creole/ResourceReference.java
   2017-01-26 18:04:47 UTC (rev 19997)
@@ -1,15 +1,15 @@
 /*
- *  ResourceReference.java
+ * ResourceReference.java
  *
- *  Copyright (c) 2017, The University of Sheffield. See the file
- *  COPYRIGHT.txt in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
+ * Copyright (c) 2017, The University of Sheffield. See the file COPYRIGHT.txt
+ * in the software or at http://gate.ac.uk/gate/COPYRIGHT.txt
  *
- *  This file is part of GATE (see http://gate.ac.uk/), and is free
- *  software, licenced under the GNU Library General Public License,
- *  Version 3, June 2007 (in the distribution as file licence.html,
- *  and also available at http://gate.ac.uk/gate/licence.html).
+ * This file is part of GATE (see http://gate.ac.uk/), and is free software,
+ * licenced under the GNU Library General Public License, Version 3, June 2007
+ * (in the distribution as file licence.html, and also available at
+ * http://gate.ac.uk/gate/licence.html).
  *
- *  Mark A. Greenwood, 25th January 2017
+ * Mark A. Greenwood, 25th January 2017
  */
 
 package gate.creole;
@@ -24,67 +24,71 @@
 
 import gate.Gate;
 
-
 public class ResourceReference {
-       
-       private URI uri;
-       
-       public ResourceReference(URL url) throws URISyntaxException {
-               uri = url.toURI();
-       }
-       
-       public ResourceReference(URI uri) {
-               this.uri = uri;
-       }
-       
-       public ResourceReference(Plugin plugin, String path) throws 
URISyntaxException {
-               this(plugin.getBaseURI(),path);
-       }
-       
-       public ResourceReference(URL context, String path) throws 
URISyntaxException, MalformedURLException {
-               uri = (new URL(context,path)).toURI();
-       }
-       
-       public ResourceReference(URI context, String path) {
-               uri = context.resolve(path);
-       }
-       
-       public ResourceReference(ResourceReference context, String path) throws 
IOException, URISyntaxException {
-               this(context.uri,path);
-       }
 
-       public InputStream openStream() throws IOException {
-               return toURL().openStream();
-       }
+  private URI uri;
 
-       public URLConnection openConnection() throws IOException {
-               return toURL().openConnection();
-       }
-       
-       public URL toURL() throws IOException {
-               
-               if (!uri.isAbsolute())
-                       throw new IOException("Unable to access relative 
resource reference: "+uri);
-               
-               if (!uri.getScheme().equals("creole")) 
-                       return uri.toURL();
+  public ResourceReference(URL url) throws URISyntaxException {
+    uri = url.toURI();
+  }
 
-               String authority = uri.getAuthority();
-               String[] data = authority.split(";");
-               
-               Plugin plugin = new Plugin.Maven(data[0], data[1], data[2]);
-               
-               for (Plugin p : Gate.getCreoleRegister().getPlugins()) {
-                       System.out.println(p.getClass());
-                       if (p.equals(plugin)) {
-                               return new URL(p.getBaseURL(),uri.getPath());
-                       }
-               }
-               
-               throw new IOException("Unable to locate URI: "+uri);
-       }
-       
-       public URI toURI() throws URISyntaxException {
-               return uri;
-       }
+  public ResourceReference(URI uri) {
+    this.uri = uri;
+  }
+
+  public ResourceReference(Plugin plugin, String path)
+      throws URISyntaxException {
+    this(plugin.getBaseURI(), path);
+  }
+
+  public ResourceReference(URL context, String path)
+      throws URISyntaxException, MalformedURLException {
+    uri = (new URL(context, path)).toURI();
+  }
+
+  public ResourceReference(URI context, String path) {
+    uri = context.resolve(path);
+  }
+
+  public ResourceReference(ResourceReference context, String path)
+      throws IOException, URISyntaxException {
+    this(context.uri, path);
+  }
+
+  public InputStream openStream() throws IOException {
+    return toURL().openStream();
+  }
+
+  public URLConnection openConnection() throws IOException {
+    return toURL().openConnection();
+  }
+
+  public URL toURL() throws IOException {
+
+    if(!uri.isAbsolute())
+      throw new IOException(
+          "Unable to access relative resource reference: " + uri);
+
+    if(!uri.getScheme().equals("creole")) return uri.toURL();
+
+    try {
+      URI base = new URI("creole", uri.getAuthority(), "/", null, null);
+      for(Plugin plugin : Gate.getCreoleRegister().getPlugins()) {
+        if(plugin.getBaseURI().equals(base)) {
+          // requiring the additional . seems like a hack but I can't figure 
out
+          // any other way of doing it
+          return new URL(plugin.getBaseURL(), "." + uri.getPath());
+        }
+      }
+    } catch(URISyntaxException e) {
+      // this is impossible so ignore it
+      e.printStackTrace();
+    }
+
+    throw new IOException("Unable to locate URI: " + uri);
+  }
+
+  public URI toURI() throws URISyntaxException {
+    return uri;
+  }
 }

Modified: 
gate/branches/sawdust2/gate-core/src/test/java/gate/creole/TestResourceReference.java
===================================================================
--- 
gate/branches/sawdust2/gate-core/src/test/java/gate/creole/TestResourceReference.java
       2017-01-26 17:20:42 UTC (rev 19996)
+++ 
gate/branches/sawdust2/gate-core/src/test/java/gate/creole/TestResourceReference.java
       2017-01-26 18:04:47 UTC (rev 19997)
@@ -68,7 +68,8 @@
                assertEquals("References do not match", url, 
((ResourceReference)resource.getParameterValue("param")).toURL());
        }
        
-       public void testRelativeReferences() throws Exception {
+       @SuppressWarnings("serial")
+  public void testRelativeReferences() throws Exception {
                URL testURL = new URL(TestDocument.getTestServerName() + 
"tests/");
                URL creoleURL = new URL(TestDocument.getTestServerName() + 
"tests/creole.xml");
                
@@ -99,8 +100,7 @@
                        @Override
                    public Document getCreoleXML() throws Exception, 
JDOMException {
                                Document doc = new Document();
-                               Element element;
-                               doc.addContent(element = new 
Element("CREOLE-DIRECTORY"));
+                               doc.addContent(new Element("CREOLE-DIRECTORY"));
                                return doc;
                        }
                };
@@ -109,6 +109,6 @@
                context = new ResourceReference(plugin,"folder/");
                rr = new ResourceReference(context,"../creole.xml");
                assertEquals("References do not match (4)", creoleURI, 
rr.toURI());
-               //System.out.println(rr.toURL());
+               assertEquals("URLs do not match (4)", creoleURL, rr.toURL());
        }
 }

This was sent by the SourceForge.net collaborative development platform, the 
world's largest Open Source development site.


------------------------------------------------------------------------------
Check out the vibrant tech community on one of the world's most
engaging tech sites, SlashDot.org! http://sdm.link/slashdot
_______________________________________________
GATE-cvs mailing list
GATE-cvs@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/gate-cvs

Reply via email to