From da0a902de6b2b4c7b4b635d11b397f663db8ecd9 Mon Sep 17 00:00:00 2001 From: Jussi Koskela Date: Mon, 12 Mar 2018 15:25:46 +0200 Subject: [PATCH] Escape/unescape names of the externals when converting to/from URIs refs #7812 Change-Id: I491a0d57a076c73785e120c3632d48c6258ea1ab --- .../db/TransferableGraphImportProcess.java | 7 +++--- .../org/simantics/graph/query/UriUtils.java | 25 +++++++++++++++++++ .../refactoring/GraphRefactoringUtils.java | 14 +++++------ .../graph/representation/PrettyPrintTG.java | 5 ++-- .../TransferableGraphQueries.java | 6 +++-- .../TransferableGraphUtils.java | 6 ++--- 6 files changed, 46 insertions(+), 17 deletions(-) diff --git a/bundles/org.simantics.graph.db/src/org/simantics/graph/db/TransferableGraphImportProcess.java b/bundles/org.simantics.graph.db/src/org/simantics/graph/db/TransferableGraphImportProcess.java index 586562fa3..4ec713866 100644 --- a/bundles/org.simantics.graph.db/src/org/simantics/graph/db/TransferableGraphImportProcess.java +++ b/bundles/org.simantics.graph.db/src/org/simantics/graph/db/TransferableGraphImportProcess.java @@ -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)); } } } diff --git a/bundles/org.simantics.graph/src/org/simantics/graph/query/UriUtils.java b/bundles/org.simantics.graph/src/org/simantics/graph/query/UriUtils.java index ef2255324..6911f782d 100644 --- a/bundles/org.simantics.graph/src/org/simantics/graph/query/UriUtils.java +++ b/bundles/org.simantics.graph/src/org/simantics/graph/query/UriUtils.java @@ -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; + } + } diff --git a/bundles/org.simantics.graph/src/org/simantics/graph/refactoring/GraphRefactoringUtils.java b/bundles/org.simantics.graph/src/org/simantics/graph/refactoring/GraphRefactoringUtils.java index 90b134e33..2f00156c3 100644 --- a/bundles/org.simantics.graph/src/org/simantics/graph/refactoring/GraphRefactoringUtils.java +++ b/bundles/org.simantics.graph/src/org/simantics/graph/refactoring/GraphRefactoringUtils.java @@ -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; - + } } } diff --git a/bundles/org.simantics.graph/src/org/simantics/graph/representation/PrettyPrintTG.java b/bundles/org.simantics.graph/src/org/simantics/graph/representation/PrettyPrintTG.java index cb06d6add..095d9e586 100644 --- a/bundles/org.simantics.graph/src/org/simantics/graph/representation/PrettyPrintTG.java +++ b/bundles/org.simantics.graph/src/org/simantics/graph/representation/PrettyPrintTG.java @@ -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; } diff --git a/bundles/org.simantics.graph/src/org/simantics/graph/representation/TransferableGraphQueries.java b/bundles/org.simantics.graph/src/org/simantics/graph/representation/TransferableGraphQueries.java index 5d4c1850b..9a0c2da1f 100644 --- a/bundles/org.simantics.graph/src/org/simantics/graph/representation/TransferableGraphQueries.java +++ b/bundles/org.simantics.graph/src/org/simantics/graph/representation/TransferableGraphQueries.java @@ -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 ""; } diff --git a/bundles/org.simantics.graph/src/org/simantics/graph/representation/TransferableGraphUtils.java b/bundles/org.simantics.graph/src/org/simantics/graph/representation/TransferableGraphUtils.java index 10637ad65..b0690532b 100644 --- a/bundles/org.simantics.graph/src/org/simantics/graph/representation/TransferableGraphUtils.java +++ b/bundles/org.simantics.graph/src/org/simantics/graph/representation/TransferableGraphUtils.java @@ -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 ""; } -- 2.47.1