Revision: 19995
          http://sourceforge.net/p/gate/code/19995
Author:   markagreenwood
Date:     2017-01-26 12:47:44 +0000 (Thu, 26 Jan 2017)
Log Message:
-----------
wip

Modified Paths:
--------------
    gate/branches/sawdust2/gate-core/src/main/java/gate/creole/Plugin.java
    
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/Plugin.java
===================================================================
--- gate/branches/sawdust2/gate-core/src/main/java/gate/creole/Plugin.java      
2017-01-26 05:09:13 UTC (rev 19994)
+++ gate/branches/sawdust2/gate-core/src/main/java/gate/creole/Plugin.java      
2017-01-26 12:47:44 UTC (rev 19995)
@@ -101,7 +101,7 @@
    * This is the URL against which all relative URLs in the CREOLE
    * metadata are resolved
    */
-  protected transient URI baseURL;
+  protected transient URL baseURL;
   
   protected transient String name;
 
@@ -158,15 +158,13 @@
   }
   
   public URL getBaseURL() {
-    try {
-      return baseURL.toURL();
-    } catch(MalformedURLException e) {
-      // this should be impossible because of the way we have got hold of the
-      // URI
-      throw new RuntimeException(e);
-    }
+    return baseURL;
   }
   
+  public URI getBaseURI() throws URISyntaxException {
+       return baseURL.toURI();
+  }
+  
   public boolean isValid() {
     return valid;
   }
@@ -376,13 +374,8 @@
 
   public static class Directory extends Plugin {
 
-    public Directory(URL directoryURL) {
-      try {
-        baseURL = Gate.normaliseCreoleUrl(directoryURL).toURI();
-      } catch(URISyntaxException e) {
-        //this should never happen but....
-        throw new RuntimeException(e);
-      }
+    public Directory(URL directoryURL) {      
+        baseURL = Gate.normaliseCreoleUrl(directoryURL);      
     }    
 
     @Override
@@ -433,7 +426,12 @@
       this.version = version;
       
       name = group+":"+artifact+":"+version;
-    }    
+    }
+    
+    @Override
+    public URI getBaseURI() throws URISyntaxException {
+       return new URI("creole://"+group+";"+artifact+";"+version+"/");
+    }
 
     @Override
     public int hashCode() {
@@ -542,7 +540,7 @@
                       + artifactResult.getArtifact().getFile().toURI().toURL()
                       + "!/");
       
-      baseURL = artifactURL.toURI();
+      baseURL = artifactURL;
 
       // check it has a creole.xml at the root
       URL directoryXmlFileUrl = new URL(artifactURL, "creole.xml");
@@ -704,12 +702,7 @@
     
     public Component(Class<? extends Resource> resourceClass) throws 
MalformedURLException {
       this.resourceClass = resourceClass;
-      try {
-        baseURL = (new 
URL(resourceClass.getResource("/gate/creole/CreoleRegisterImpl.class"), 
".")).toURI();
-      } catch(URISyntaxException e) {
-        //this should never happen
-        throw new RuntimeException(e);
-      }
+      baseURL = (new 
URL(resourceClass.getResource("/gate/creole/CreoleRegisterImpl.class"), "."));  
    
     }
     
     @Override

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 05:09:13 UTC (rev 19994)
+++ 
gate/branches/sawdust2/gate-core/src/main/java/gate/creole/ResourceReference.java
   2017-01-26 12:47:44 UTC (rev 19995)
@@ -16,31 +16,75 @@
 
 import java.io.IOException;
 import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URISyntaxException;
 import java.net.URL;
 import java.net.URLConnection;
 
+import gate.Gate;
 
+
 public class ResourceReference {
        
-       private URL actualURL;
+       private URI uri;
        
-       public ResourceReference(URL url) throws IOException {
-               actualURL = url;
+       public ResourceReference(URL url) throws URISyntaxException {
+               uri = url.toURI();
        }
        
-       public ResourceReference(Plugin plugin, String path) throws IOException 
{
-               actualURL = new URL(plugin.getBaseURL(), path);
+       public ResourceReference(URI uri) {
+               this.uri = uri;
        }
        
-       public ResourceReference(ResourceReference base, String path) throws 
IOException {
-               //possibly use URI to do the resolving but keep inside the 
plugin
+       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 actualURL.openStream();
+               return toURL().openStream();
        }
 
        public URLConnection openConnection() throws IOException {
-               return actualURL.openConnection();
+               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();
+
+               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;
+       }
 }

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 05:09:13 UTC (rev 19994)
+++ 
gate/branches/sawdust2/gate-core/src/test/java/gate/creole/TestResourceReference.java
       2017-01-26 12:47:44 UTC (rev 19995)
@@ -1,9 +1,13 @@
 package gate.creole;
 
 import java.io.InputStream;
+import java.net.URI;
 import java.net.URL;
 
 import org.apache.commons.io.IOUtils;
+import org.jdom.Document;
+import org.jdom.Element;
+import org.jdom.JDOMException;
 
 import gate.Gate;
 import gate.Resource;
@@ -58,13 +62,53 @@
                        }
                };
                
-               URL url = 
getClass().getClassLoader().getResource("gate/resources/gate.ac.uk/creole/creole.xml");
+               URL url = new URL("http://gate.ac.uk";);
                resource.setParameterValue("param", url);
                
-               try (InputStream in = 
((ResourceReference)resource.getParameterValue("param")).openStream()) {
-                       String contents = IOUtils.toString(in);
-
-                       assertEquals("Length of data read not as expected", 98, 
contents.length());
-               }
+               assertEquals("References do not match", url, 
((ResourceReference)resource.getParameterValue("param")).toURL());
        }
+       
+       public void testRelativeReferences() throws Exception {
+               URL testURL = new URL(TestDocument.getTestServerName() + 
"tests/");
+               URL creoleURL = new URL(TestDocument.getTestServerName() + 
"tests/creole.xml");
+               
+               ResourceReference rr = new 
ResourceReference(testURL,"./creole.xml");
+               assertEquals("References do not match (1)", creoleURL, 
rr.toURL());
+               
+               ResourceReference context = new ResourceReference(testURL);
+               rr = new ResourceReference(context,"./creole.xml");
+               assertEquals("References do not match (2)", creoleURL, 
rr.toURL());
+               
+               Plugin plugin = new Plugin.Directory(testURL);
+               context = new ResourceReference(plugin,"abc");
+               rr = new ResourceReference(context,"./creole.xml");
+               assertEquals("References do not match (3)", creoleURL, 
rr.toURL());
+               
+               context = new ResourceReference(plugin,"html/");
+               rr = new ResourceReference(context,"../creole.xml");
+               assertEquals("References do not match (3)", creoleURL, 
rr.toURL());
+               
+               URI creoleURI = new 
URI("creole://ac.uk.gate.plugins;annie;9.0-SNAPSHOT/creole.xml");
+               plugin = new Plugin.Maven("ac.uk.gate.plugins", "annie", 
"9.0-SNAPSHOT") {
+                       
+                       @Override
+                       public URL getBaseURL() {
+                               return testURL;
+                       }
+                       
+                       @Override
+                   public Document getCreoleXML() throws Exception, 
JDOMException {
+                               Document doc = new Document();
+                               Element element;
+                               doc.addContent(element = new 
Element("CREOLE-DIRECTORY"));
+                               return doc;
+                       }
+               };
+               
+               Gate.getCreoleRegister().registerPlugin(plugin);
+               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());
+       }
 }

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