Revision: 19600
          http://sourceforge.net/p/gate/code/19600
Author:   markagreenwood
Date:     2016-09-26 12:40:57 +0000 (Mon, 26 Sep 2016)
Log Message:
-----------
first cut of an API for extracting resources from a plugin for people to edit 
and re-use (mostly so that people stop editing the main ANNIE gaz files and 
then wondering why ANNIE stops working later on)

Modified Paths:
--------------
    gate/branches/sawdust2/gate-core/src/main/java/gate/Plugin.java

Modified: gate/branches/sawdust2/gate-core/src/main/java/gate/Plugin.java
===================================================================
--- gate/branches/sawdust2/gate-core/src/main/java/gate/Plugin.java     
2016-09-26 01:22:42 UTC (rev 19599)
+++ gate/branches/sawdust2/gate-core/src/main/java/gate/Plugin.java     
2016-09-26 12:40:57 UTC (rev 19600)
@@ -23,7 +23,18 @@
 import java.net.URISyntaxException;
 import java.net.URL;
 import java.net.URLClassLoader;
+import java.nio.file.CopyOption;
+import java.nio.file.FileSystem;
+import java.nio.file.FileSystems;
+import java.nio.file.FileVisitResult;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.SimpleFileVisitor;
+import java.nio.file.StandardCopyOption;
+import java.nio.file.attribute.BasicFileAttributes;
 import java.util.ArrayList;
+import java.util.HashMap;
 import java.util.Iterator;
 import java.util.LinkedHashMap;
 import java.util.LinkedHashSet;
@@ -153,6 +164,14 @@
     return valid;
   }
   
+  public void copyResources(File dir) throws IOException {
+    throw new UnsupportedOperationException("This plugin does not contain any 
resources that can be copied");
+  }
+  
+  public boolean hasResources() {
+    return false;
+  }
+  
   protected void parseCreole() {
     
     valid = true;
@@ -466,9 +485,55 @@
       Settings effectiveSettings =
               settingsBuildingResult.getEffectiveSettings();
       return effectiveSettings;
-    }     
+    }   
 
     @Override
+    public void copyResources(File dir) throws IOException {
+      
+      if (!hasResources())
+        throw new UnsupportedOperationException(
+            "this plugin doesn't have any resources you can copy as you would 
now had you called hasResources first :P");
+      
+      try (FileSystem zipFs =
+          FileSystems.newFileSystem(baseURL.toURI(), new HashMap<>());) {
+
+        Path target = Paths.get(dir.toURI());
+        Path pathInZip = zipFs.getPath("/resources");
+
+        Files.walkFileTree(pathInZip, new SimpleFileVisitor<Path>() {
+          @Override
+          public FileVisitResult visitFile(Path filePath,
+              BasicFileAttributes attrs) throws IOException {
+            // Make sure that we conserve the hierachy of files and folders
+            // inside the zip
+            Path relativePathInZip = pathInZip.relativize(filePath);
+            Path targetPath = target.resolve(relativePathInZip.toString());
+            Files.createDirectories(targetPath.getParent());
+
+            // And extract the file
+            Files.copy(filePath, targetPath, 
StandardCopyOption.REPLACE_EXISTING);
+
+            return FileVisitResult.CONTINUE;
+          }
+        });
+      } catch(URISyntaxException e) {
+        // this shouldn't be possible because of where the URI is coming from
+        throw new IOException(e);
+      }
+    }
+    
+    @Override
+    public boolean hasResources() {   
+      try (FileSystem zipFs = FileSystems.newFileSystem(baseURL.toURI(), new 
HashMap<>());) {        
+        Path pathInZip = zipFs.getPath("/resources");
+        return Files.isDirectory(pathInZip);
+      }
+      catch (IOException | URISyntaxException e) {
+        return false;
+      }
+    }
+
+    @Override
     public Document getCreoleXML() throws Exception {
       Artifact artifactObj =
               new DefaultArtifact(group, artifact, "jar", version);
@@ -523,6 +588,7 @@
       CollectRequest collectRequest = new CollectRequest();
       collectRequest.setRoot(dependency);
       collectRequest.addRepository(central);
+      //TODO don't we need repos from settings.xml in here as well?
       DependencyNode node =
               getRepositorySystem().collectDependencies(getRepositorySession(),
                       collectRequest).getRoot();
@@ -717,12 +783,15 @@
     Gate.init();
     
         
-    /*Plugin annieDir = new Plugin.Directory(new 
File("/home/mark/gate-top/externals/gate-ix/plugins/Tools").toURI().toURL());
-    System.out.println(annieDir.getResourceInfoList());
-    System.out.println(annieDir.getRequiredPlugins());
     
-    Plugin annieMaven = new Plugin.Maven("uk.ac.gate", "ANNIE", "9");
-    System.out.println(annieMaven.getResourceInfoList());*/
+    
+    Plugin annieMaven = new Plugin.Maven("uk.ac.gate.plugins", "annie", 
"9.0-SNAPSHOT");
+    
+    Gate.getCreoleRegister().registerPlugin(annieMaven);
+    
+    File r = new File("testResources");
+    System.out.println(annieMaven.hasResources());
+    annieMaven.copyResources(r);
 
   }
 }

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


------------------------------------------------------------------------------
_______________________________________________
GATE-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/gate-cvs

Reply via email to