Dear List,

I would like to contribute a solution for ticket #331. Because I made a mistake with the Mailinglist, so my last two Emails did not reach you unfortunately. (I subscribed with @googlemail.com and wrote emails with @gmail.com) The original messages are attached bellow. The first email is a quick introduction of mine, the second explains what I developed till now.

Best Regards,
Matthias

-------- Original-Nachricht --------
Betreff:        #331 relative path name support
Datum:  Fri, 06 Sep 2013 18:47:52 +0200
Von:    Matthias Hinz <matthias.m.h...@gmail.com>
An:     OpenJump develop and use <jump-pilot-devel@lists.sourceforge.net>



Dear List,

according to ticket #331 [1] and the discussion on the user list [2], i found a solution for the relative path name support. It is aimed to use relative paths inside the jmp files for data sources located bellow the project file. I am not yet sure if I am allowed to commit to the SVN, so I created a patch file for you to review. I will do the commit if i get the approval.

I changed /AbstractSaveProjectPlugin/ and /OpenProjectWizzard/ for the loading and saving of jmp files. Basically, when the project is opened each layer is checked for relative paths before oj tries to load them. They are replaced by absolute paths based on the project file location. From then, everything works like it did before (-> load layer, try to find broken files). Internally, OJ still handles absolute paths so that no errors occur.

When the project is saved, all absolute path names are replaced by relative paths, if they are located bellow the project file (or in the same directory). Because the task object gets manipulated and OJ continues to use it, this step is /undone/ after the java2xml serialization. This was a more handy solution than string-matching on xml file or cloning the object and I hope it is intuitive.

I tested my solution manually with the shapefile samples and some jml-layers, that I created with oj. I moved the project file and saved it in different locations with no errors. Projects from older versions of oj can still be opened. On the other hand, errors arise newer projects are opened by older versions of oj: Relative paths cannot be found, because they are interpreted relative to the java work directory and the file finder will throw an IndexOutOfBoundsException when comparing the selected absolute path with the given relative path. So new projects may not be downward-compatible. That may or may not be a con for my solution (?)

I'm looking forward to your feedback.

Best Regards,
Matthias

[1] http://sourceforge.net/p/jump-pilot/bugs/331/
[2] https://groups.google.com/forum/#!topic/openjump-users/2WPu_wJtwgI <https://groups.google.com/forum/#%21topic/openjump-users/2WPu_wJtwgI>





-------- Original-Nachricht --------
Betreff:        start developing / ticket #331
Datum:  Tue, 27 Aug 2013 21:01:18 +0200
Von:    Matthias Hinz <matthias.m.h...@gmail.com>
An:     OpenJump develop and use <jump-pilot-devel@lists.sourceforge.net>



Dear List,

I would like to start developing with OpenJump and I appreciate any help
for getting started. To introduce myself, I am a student for master of
geoinformatics from University of Münster, Germany.

So far, I tried to get familiar with the source code and got it running
in Eclipse. Maven ans SVN are working fine as well, though I had some
dependency errors at first. I would like to work on bug #331 that is
currently discussed in the user-mailinglist. I got some good ideas about
how to implement that, already.

So I got some general questions:
1. If I want to work on a task, how do I let others know? There are at
least three ways I can think of: By "owning" a ticket, by telling so on
the developers list and by leaving a comment in the ticket-related
discussion.

2. How should I contribute my solutions. I'm familiar with SVN already,
but maybe you prefer to review patches first?

Best Regards,
Matthias






Index: 
src/com/vividsolutions/jump/workbench/ui/plugin/AbstractSaveProjectPlugIn.java
===================================================================
--- 
src/com/vividsolutions/jump/workbench/ui/plugin/AbstractSaveProjectPlugIn.java  
    (revision 3701)
+++ 
src/com/vividsolutions/jump/workbench/ui/plugin/AbstractSaveProjectPlugIn.java  
    (working copy)
@@ -39,11 +39,18 @@
 import java.io.StringWriter;
 import java.util.ArrayList;
 import java.util.Collection;
+import java.util.HashMap;
 import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
 
 import javax.swing.JInternalFrame;
 
 import com.vividsolutions.jump.I18N;
+import com.vividsolutions.jump.io.datasource.DataSource;
+import com.vividsolutions.jump.io.datasource.DataSourceQuery;
 import com.vividsolutions.jump.util.FileUtil;
 import com.vividsolutions.jump.util.java2xml.Java2XML;
 import com.vividsolutions.jump.workbench.model.Layer;
@@ -77,14 +84,18 @@
         task.setSavedViewEnvelope(frame.getContext().getLayerViewPanel()
                .getViewport().getEnvelopeInModelCoordinates());
 
+        //for all data sources bellow project file set pathnames relative
+        Map<Layer, String> undoMap = setRelativePaths(task, file);
         StringWriter stringWriter = new StringWriter();
-
+       
         try {
             new Java2XML().write(task, "project", stringWriter);
         } finally {
             stringWriter.flush();
         }
-
+        //
+        undoSetRelativePaths(undoMap);
+        
         FileUtil.setContents(file.getAbsolutePath(), stringWriter.toString());
         task.setName(GUIUtil.nameWithoutExtension(file));
         task.setProjectFile(file);
@@ -112,7 +123,67 @@
         }
     }
 
-    protected Collection ignoredLayers(Task task) {
+    private Map<Layer, String> setRelativePaths(Task task, File projectFile) {
+        Map<Layer, String> undoMap = new HashMap<Layer, String>();
+        List layers = task.getLayerManager().getLayers();
+        for (Object object : layers) {
+          if(!(object instanceof Layer)) {
+              continue;
+          }
+          Layer layer = (Layer) object;
+          DataSourceQuery dataSourceQuery = layer.getDataSourceQuery();
+          DataSource dataSource = dataSourceQuery.getDataSource();
+          Map properties = dataSource.getProperties();
+          Object property = properties.get(DataSource.FILE_KEY);
+          if(property == null)
+                 continue;
+          String fname = property.toString();
+          File sourceFile = new File(fname);
+          
+          if(isLocatedBellow(projectFile, sourceFile)){
+              String projectParentString = 
projectFile.getParentFile().getAbsolutePath();
+              String sourceFileString = sourceFile.getAbsolutePath();
+              String relativePath = 
sourceFileString.substring(projectParentString.length()+1);
+              undoMap.put(layer, fname);
+              if(dataSourceQuery.getQuery()!=null && 
dataSourceQuery.getQuery().equalsIgnoreCase(fname)){
+                  dataSourceQuery.setQuery(relativePath);
+              }
+              properties.put(DataSource.FILE_KEY, relativePath);
+              dataSource.setProperties(properties);
+            }
+          }
+        return undoMap;        
+    }
+    
+    private void undoSetRelativePaths(Map<Layer, String> undoMap){
+        Set<Entry<Layer,String>> entrySet = undoMap.entrySet();
+        for (Entry<Layer, String> entry : entrySet) {
+            Layer layer = entry.getKey();
+            DataSourceQuery dataSourceQuery = layer.getDataSourceQuery();
+            DataSource dataSource = dataSourceQuery.getDataSource();
+            Map properties = dataSource.getProperties();
+            String absoluteFilePath = entry.getValue();
+            String relativeFilePath = 
properties.get(DataSource.FILE_KEY).toString();
+            if(dataSourceQuery.getQuery()!=null && 
dataSourceQuery.getQuery().equalsIgnoreCase(relativeFilePath)) {
+                dataSourceQuery.setQuery(absoluteFilePath);
+            }
+            properties.put(DataSource.FILE_KEY, absoluteFilePath);
+        }
+    };
+    
+    private boolean isLocatedBellow(File projectFile, File layerFile) {
+        File projectParent = projectFile.getParentFile();
+        
+        for (File layerParent = layerFile.getParentFile(); layerParent != 
null; layerParent = layerParent.getParentFile()) {
+          if(layerParent.equals(projectParent)) {
+            return true;
+          }
+        }
+        
+        return false;
+    }
+
+       protected Collection ignoredLayers(Task task) {
         ArrayList ignoredLayers = new ArrayList();
 
         for (Iterator i = task.getLayerManager().getLayers().iterator();
Index: src/org/openjump/core/ui/plugin/file/open/OpenProjectWizard.java
===================================================================
--- src/org/openjump/core/ui/plugin/file/open/OpenProjectWizard.java    
(revision 3701)
+++ src/org/openjump/core/ui/plugin/file/open/OpenProjectWizard.java    
(working copy)
@@ -21,6 +21,7 @@
 import com.vividsolutions.jump.workbench.ui.plugin.PersistentBlackboardPlugIn;
 import com.vividsolutions.jump.workbench.ui.plugin.WorkbenchContextReference;
 import com.vividsolutions.jump.workbench.ui.wizard.WizardDialog;
+
 import org.openjump.core.model.TaskEvent;
 import org.openjump.core.model.TaskListener;
 import org.openjump.core.ui.plugin.file.FindFile;
@@ -29,6 +30,7 @@
 import org.openjump.core.ui.swing.wizard.AbstractWizardGroup;
 
 import javax.swing.*;
+
 import java.awt.*;
 import java.io.File;
 import java.io.FileNotFoundException;
@@ -242,6 +244,9 @@
 
         if (layerable instanceof Layer) {
           Layer layer = (Layer)layerable;
+          //replace all relative path names with absolute paths based on the 
project file location
+          File parentDir = this.newTask.getProjectFile().getParentFile();
+          replaceRelativePath(layer, parentDir);
           try {
             load(layer, registry, monitor);
           } catch (FileNotFoundException ex) {
@@ -263,7 +268,7 @@
             DataSourceQuery dataSourceQuery = layer.getDataSourceQuery();
             DataSource dataSource = dataSourceQuery.getDataSource();
             Map properties = dataSource.getProperties();
-            String fname = properties.get("File").toString();
+            String fname = properties.get(DataSource.FILE_KEY).toString();
             String filename = findFile.getFileName(fname);
             if (filename.length() > 0) {
               // set the new source for this layer
@@ -301,7 +306,8 @@
        }
   }
 
-  public static void load(Layer layer, CoordinateSystemRegistry registry,
+
+public static void load(Layer layer, CoordinateSystemRegistry registry,
     TaskMonitor monitor) throws Exception {
     DataSourceQuery dataSourceQuery = layer.getDataSourceQuery();
     String query = dataSourceQuery.getQuery();
@@ -323,5 +329,32 @@
       connection.close();
     }
   }
+  
+  /**
+   * Replaces any relative File path with an absolute path based on the 
project file location
+   * 
+   * @param layer
+   */
+  private void replaceRelativePath(Layer layer, File parentDir) {
+      DataSourceQuery dataSourceQuery = layer.getDataSourceQuery();
+      DataSource dataSource = dataSourceQuery.getDataSource();
+      Map properties = dataSource.getProperties();
+      Object property = properties.get(DataSource.FILE_KEY);
+      if(property == null)
+         return;
+      String fname = property.toString();
+      File sourceFile = new File(fname);
+      if(sourceFile.isAbsolute())
+        return;   
+
+      File absoluteFile = new File(parentDir, fname);
+      String absolutePath = absoluteFile.getAbsolutePath();
+      if(dataSourceQuery.getQuery()!=null && 
dataSourceQuery.getQuery().equalsIgnoreCase(fname)) {
+          dataSourceQuery.setQuery(absolutePath);
+      }
+      properties.put(DataSource.FILE_KEY, absolutePath);
+      dataSource.setProperties(properties);
+}
+
 
 }
------------------------------------------------------------------------------
Learn the latest--Visual Studio 2012, SharePoint 2013, SQL 2012, more!
Discover the easy way to master current and previous Microsoft technologies
and advance your career. Get an incredible 1,500+ hours of step-by-step
tutorial videos with LearnDevNow. Subscribe today and save!
http://pubads.g.doubleclick.net/gampad/clk?id=58041391&iu=/4140/ostg.clktrk
_______________________________________________
Jump-pilot-devel mailing list
Jump-pilot-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/jump-pilot-devel

Reply via email to