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