1 package org.simantics.db.layer0.util;
\r
3 import java.util.ArrayList;
\r
5 import org.simantics.databoard.util.URIStringUtils;
\r
6 import org.simantics.db.ReadGraph;
\r
7 import org.simantics.db.Resource;
\r
8 import org.simantics.db.common.uri.UnescapedChildMapOfResource;
\r
9 import org.simantics.db.exception.DatabaseException;
\r
10 import org.simantics.layer0.Layer0;
\r
12 public class RelativeReference {
\r
13 public final Resource base;
\r
14 public final String path;
\r
16 public RelativeReference(Resource base, String path) {
\r
21 public Resource resolve(ReadGraph graph) throws DatabaseException {
\r
22 return resolve(graph, base, path);
\r
25 public static Resource resolve(ReadGraph graph,
\r
27 String path) throws DatabaseException {
\r
28 String[] splitPath = path.split("/");
\r
29 for(String name : splitPath) {
\r
30 base = graph.syncRequest(new UnescapedChildMapOfResource(base)).get(
\r
31 URIStringUtils.unescape(name)
\r
39 public static RelativeReference createReference(ReadGraph graph, Resource baseType, Resource resource) throws DatabaseException {
\r
40 ArrayList<String> path = new ArrayList<String>();
\r
41 Layer0 L0 = Layer0.getInstance(graph);
\r
43 while(!graph.isInstanceOf(resource, baseType)) {
\r
44 path.add((String)graph.getRelatedValue(resource, L0.HasName));
\r
45 resource = graph.getSingleObject(resource, L0.PartOf);
\r
47 } catch(DatabaseException e) {
\r
50 StringBuilder b = new StringBuilder();
\r
51 for(int i=path.size()-1;i>=0;--i) {
\r
52 b.append(URIStringUtils.escape(path.get(i)));
\r
56 return new RelativeReference(resource, b.toString());
\r