]> gerrit.simantics Code Review - simantics/platform.git/commitdiff
Escape/unescape names of the externals when converting to/from URIs 58/1558/2
authorJussi Koskela <jussi.koskela@semantum.fi>
Mon, 12 Mar 2018 13:25:46 +0000 (15:25 +0200)
committerTuukka Lehtonen <tuukka.lehtonen@semantum.fi>
Mon, 12 Mar 2018 16:52:14 +0000 (18:52 +0200)
refs #7812

Change-Id: I491a0d57a076c73785e120c3632d48c6258ea1ab

bundles/org.simantics.graph.db/src/org/simantics/graph/db/TransferableGraphImportProcess.java
bundles/org.simantics.graph/src/org/simantics/graph/query/UriUtils.java
bundles/org.simantics.graph/src/org/simantics/graph/refactoring/GraphRefactoringUtils.java
bundles/org.simantics.graph/src/org/simantics/graph/representation/PrettyPrintTG.java
bundles/org.simantics.graph/src/org/simantics/graph/representation/TransferableGraphQueries.java
bundles/org.simantics.graph/src/org/simantics/graph/representation/TransferableGraphUtils.java

index 586562fa34df83ef4bccdffaebe9e5a373bd90f6..4ec7138660fc2dd8c585fefe07bceb96bc4b5c00 100644 (file)
@@ -13,6 +13,7 @@ import org.simantics.databoard.Bindings;
 import org.simantics.databoard.accessor.error.AccessorException;
 import org.simantics.databoard.adapter.AdaptException;
 import org.simantics.databoard.binding.mutable.Variant;
+import org.simantics.databoard.util.URIStringUtils;
 import org.simantics.db.ReadGraph;
 import org.simantics.db.Resource;
 import org.simantics.db.WriteOnlyGraph;
@@ -200,14 +201,14 @@ public class TransferableGraphImportProcess implements TransferableGraphImporter
                                                        if(child == null) {
                                                                String uri = graph.getPossibleURI(parent);
                                                                if(uri == null) {
-                                                                       addMissing(NameUtils.getSafeName(graph, parent) + " /" + def.name);
+                                                                       addMissing(URIStringUtils.escape(NameUtils.getSafeName(graph, parent)) + " /" + URIStringUtils.escape(def.name));
                                                                } else {
-                                                                       addMissing(graph.getURI(parent) + "/" + def.name);
+                                                                       addMissing(graph.getURI(parent) + "/" + URIStringUtils.escape(def.name));
                                                                }
                                                        }
                                                        resources[identity.resource] = child;
                                                } else {
-                                                   addMissing(TransferableGraphUtils.getURI(tg, def.parent) + "/" + def.name);
+                                                   addMissing(TransferableGraphUtils.getURI(tg, def.parent) + "/" + URIStringUtils.escape(def.name));
                                                }
                                        }
                                }
index ef22553244d78de555f5ee2a2ed1d3cc809c1eec..6911f782d75e24a902476187fd428f2a7bd2f404 100644 (file)
@@ -1,5 +1,6 @@
 package org.simantics.graph.query;
 
+import org.simantics.databoard.util.URIStringUtils;
 
 public class UriUtils {
 
@@ -27,4 +28,28 @@ public class UriUtils {
                return cur;
        }
        
+       public static Path uriToPathUnescaped(String uri) {
+               String[] segments;
+               Path cur;
+               if(uri.startsWith("http:/")) {
+                       if(uri.length() == 6)
+                               return Paths.Root;
+                       segments = uri.substring(7).split("/");
+                       cur = Paths.Root;
+               }
+               else {
+                       int p = uri.indexOf('/');
+                       if(p == -1)
+                               return new PathRoot(URIStringUtils.unescape(uri));
+                       else {
+                               segments = uri.substring(p+1).split("/");
+                               cur = new PathRoot(URIStringUtils.unescape(uri.substring(0, p)));
+                       }
+               }
+
+               for(String segment : segments)
+                       cur = new PathChild(URIStringUtils.unescape(segment), cur);
+               return cur;
+       }
+       
 }
index 90b134e33762b5f991baa5288a2974906f2bad7b..2f00156c3b36dfdd93db0010f38ec3d54af2b0d3 100644 (file)
@@ -96,7 +96,7 @@ public class GraphRefactoringUtils {
        String[] parts = URIStringUtils.splitURI(path);
        Identity parentId = recursePath(tg, parts[0]);
        tg.identities = Arrays.copyOf(tg.identities, tg.identities.length+1);
-       Identity childIdentity = new Identity(tg.resourceCount++, new External(parentId.resource, parts[1]));
+       Identity childIdentity = new Identity(tg.resourceCount++, new External(parentId.resource, URIStringUtils.unescape(parts[1])));
                tg.identities[tg.identities.length-1] = childIdentity;
                return childIdentity;
        
@@ -113,10 +113,10 @@ public class GraphRefactoringUtils {
                        
                        String[] parts = URIStringUtils.splitURI(ext.name);
                        Identity path = recursePath(tg, parts[0]);
-                       id.definition = new Internal(path.resource, parts[1]);
+                       id.definition = new Internal(path.resource, URIStringUtils.unescape(parts[1]));
                        
                        GraphStore store = TransferableGraphConversion.convert(tg);
-                       int rootId = store.identities.createPathToId(UriUtils.uriToPath(ext.name));
+                       int rootId = store.identities.createPathToId(UriUtils.uriToPathUnescaped(ext.name));
                        propagateNewMarks(store.identities, rootId);
 
                        TransferableGraph1 tgNew = TransferableGraphConversion.convert(store);
@@ -125,16 +125,16 @@ public class GraphRefactoringUtils {
                        tg.identities = tgNew.identities;
                        tg.values = tgNew.values;
                        tg.statements = tgNew.statements;
-                               
+
                        return;
                        
                 } else if (ext.type.startsWith("http://")) {
-                    String first = "http://Projects/Development Project";
+                    String first = "http://Projects/Development%20Project";
                     Identity path = recursePath(tg, first);
                     id.definition = new Internal(path.resource, ext.name);
                     
                     GraphStore store = TransferableGraphConversion.convert(tg);
-                    int rootId = store.identities.createPathToId(UriUtils.uriToPath(first + "/" + ext.name));
+                    int rootId = store.identities.createPathToId(UriUtils.uriToPathUnescaped(first + "/" + ext.name));
                     propagateNewMarks(store.identities, rootId);
 
                     TransferableGraph1 tgNew = TransferableGraphConversion.convert(store);
@@ -143,7 +143,7 @@ public class GraphRefactoringUtils {
                     tg.identities = tgNew.identities;
                     tg.values = tgNew.values;
                     tg.statements = tgNew.statements;
-
+                    
                 }
             }
         }
index cb06d6addd1604bb13070f5249428816d120bf73..095d9e586ccea634c0ea688ecdaedb53389b7cd4 100644 (file)
@@ -30,6 +30,7 @@ import org.simantics.databoard.container.DataContainers;
 import org.simantics.databoard.parser.DataValuePrinter;
 import org.simantics.databoard.parser.PrintFormat;
 import org.simantics.databoard.parser.repository.DataValueRepository;
+import org.simantics.databoard.util.URIStringUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -260,10 +261,10 @@ public class PrettyPrintTG {
         // else {
         Identity id = query.getIdentity(parentId);
         if (id.definition instanceof External) {
-            return getExternalURI((External) id.definition) + "/" + name;
+            return getExternalURI((External) id.definition) + "/" + URIStringUtils.escape(name);
         } else if (id.definition instanceof Root) {
             Root root = (Root) id.definition;
-            return "http:/" + root.name + "/" + name;
+            return "http:/" + URIStringUtils.escape(root.name) + "/" + URIStringUtils.escape(name);
         } else {
             return null;
         }
index 5d4c1850b38040db18ddf527271a496a99388720..9a0c2da1fdaeea051e10d25e6c88bfb39a008bd1 100644 (file)
@@ -5,6 +5,8 @@ import java.util.Set;
 import java.util.TreeMap;
 import java.util.TreeSet;
 
+import org.simantics.databoard.util.URIStringUtils;
+
 import gnu.trove.impl.Constants;
 import gnu.trove.list.array.TIntArrayList;
 import gnu.trove.map.hash.TIntObjectHashMap;
@@ -58,14 +60,14 @@ public class TransferableGraphQueries {
         if(definition instanceof External) {
             External def = (External)definition;
             if(def.parent == -1) return "http:/";
-            else return getURI(def.parent) + "/" + def.name;
+            else return getURI(def.parent) + "/" + URIStringUtils.escape(def.name);
         } else if(definition instanceof Root) {
             Root def = (Root)definition;
             if(def.name.isEmpty()) return "http:/";
             return def.name;
         } else if (definition instanceof Internal) {
             Internal def = (Internal)definition;
-            return getURI(def.parent) + "/" + def.name;
+            return getURI(def.parent) + "/" + URIStringUtils.escape(def.name);
         } else {
             return "";
         }
index 10637ad65caf0d861d52e3d1adfffdee66d5f2ca..b0690532b85d584969526609181ba6fc712b5ba5 100644 (file)
@@ -70,7 +70,7 @@ public class TransferableGraphUtils {
         if("http:/".equals(uri)) return identity;
         String[] tokens = uri.substring("http://".length()).split("/");
         for(String token : tokens) {
-            identity = findExternalWithNameAndParent(tg, identity.resource, token);
+            identity = findExternalWithNameAndParent(tg, identity.resource, URIStringUtils.unescape(token));
             if (identity == null) {
                 return null;
             }
@@ -301,14 +301,14 @@ public class TransferableGraphUtils {
                 if(definition instanceof External) {
                     External def = (External)definition;
                     if(def.parent == -1) return "http:/";
-                    else return getURI(identities, def.parent) + "/" + def.name;
+                    else return getURI(identities, def.parent) + "/" + URIStringUtils.escape(def.name);
                 } else if(definition instanceof Root) {
                     Root def = (Root)definition;
                     if(def.name.isEmpty()) return "http:/";
                     return def.name;
                 } else if (definition instanceof Internal) {
                     Internal def = (Internal)definition;
-                    return getURI(identities, def.parent) + "/" + def.name;
+                    return getURI(identities, def.parent) + "/" + URIStringUtils.escape(def.name);
                 } else {
                     return "";
                 }