X-Git-Url: https://gerrit.simantics.org/r/gitweb?a=blobdiff_plain;f=bundles%2Forg.simantics.db.layer0%2Fsrc%2Forg%2Fsimantics%2Fdb%2Flayer0%2Futil%2FRelativeReference.java;h=3cb855da4c2c27fcf7863e8434178802e7208545;hb=b844ca924d129072e11f7e4c18918dd388a37e8c;hp=3629644b94c4fbc50264afd8ff897db1417b201f;hpb=969bd23cab98a79ca9101af33334000879fb60c5;p=simantics%2Fplatform.git diff --git a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/RelativeReference.java b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/RelativeReference.java index 3629644b9..3cb855da4 100644 --- a/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/RelativeReference.java +++ b/bundles/org.simantics.db.layer0/src/org/simantics/db/layer0/util/RelativeReference.java @@ -1,58 +1,58 @@ -package org.simantics.db.layer0.util; - -import java.util.ArrayList; - -import org.simantics.databoard.util.URIStringUtils; -import org.simantics.db.ReadGraph; -import org.simantics.db.Resource; -import org.simantics.db.common.uri.UnescapedChildMapOfResource; -import org.simantics.db.exception.DatabaseException; -import org.simantics.layer0.Layer0; - -public class RelativeReference { - public final Resource base; - public final String path; - - public RelativeReference(Resource base, String path) { - this.base = base; - this.path = path; - } - - public Resource resolve(ReadGraph graph) throws DatabaseException { - return resolve(graph, base, path); - } - - public static Resource resolve(ReadGraph graph, - Resource base, - String path) throws DatabaseException { - String[] splitPath = path.split("/"); - for(String name : splitPath) { - base = graph.syncRequest(new UnescapedChildMapOfResource(base)).get( - URIStringUtils.unescape(name) - ); - if(base == null) - return null; - } - return base; - } - - public static RelativeReference createReference(ReadGraph graph, Resource baseType, Resource resource) throws DatabaseException { - ArrayList path = new ArrayList(); - Layer0 L0 = Layer0.getInstance(graph); - try { - while(!graph.isInstanceOf(resource, baseType)) { - path.add((String)graph.getRelatedValue(resource, L0.HasName)); - resource = graph.getSingleObject(resource, L0.PartOf); - } - } catch(DatabaseException e) { - return null; - } - StringBuilder b = new StringBuilder(); - for(int i=path.size()-1;i>=0;--i) { - b.append(URIStringUtils.escape(path.get(i))); - if(i > 0) - b.append('/'); - } - return new RelativeReference(resource, b.toString()); - } -} +package org.simantics.db.layer0.util; + +import java.util.ArrayList; + +import org.simantics.databoard.util.URIStringUtils; +import org.simantics.db.ReadGraph; +import org.simantics.db.Resource; +import org.simantics.db.common.uri.UnescapedChildMapOfResource; +import org.simantics.db.exception.DatabaseException; +import org.simantics.layer0.Layer0; + +public class RelativeReference { + public final Resource base; + public final String path; + + public RelativeReference(Resource base, String path) { + this.base = base; + this.path = path; + } + + public Resource resolve(ReadGraph graph) throws DatabaseException { + return resolve(graph, base, path); + } + + public static Resource resolve(ReadGraph graph, + Resource base, + String path) throws DatabaseException { + String[] splitPath = path.split("/"); + for(String name : splitPath) { + base = graph.syncRequest(new UnescapedChildMapOfResource(base)).get( + URIStringUtils.unescape(name) + ); + if(base == null) + return null; + } + return base; + } + + public static RelativeReference createReference(ReadGraph graph, Resource baseType, Resource resource) throws DatabaseException { + ArrayList path = new ArrayList(); + Layer0 L0 = Layer0.getInstance(graph); + try { + while(!graph.isInstanceOf(resource, baseType)) { + path.add((String)graph.getRelatedValue(resource, L0.HasName)); + resource = graph.getSingleObject(resource, L0.PartOf); + } + } catch(DatabaseException e) { + return null; + } + StringBuilder b = new StringBuilder(); + for(int i=path.size()-1;i>=0;--i) { + b.append(URIStringUtils.escape(path.get(i))); + if(i > 0) + b.append('/'); + } + return new RelativeReference(resource, b.toString()); + } +}