1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24 package org.modeshape.graph.connector.federation;
25
26 import java.util.List;
27 import net.jcip.annotations.Immutable;
28 import org.modeshape.graph.ExecutionContext;
29 import org.modeshape.graph.Location;
30 import org.modeshape.graph.property.Path;
31 import org.modeshape.graph.property.PathFactory;
32
33
34
35
36
37
38 @Immutable
39 final class OffsetMirrorProjector extends ProjectorWithPlaceholders {
40
41
42
43
44
45
46
47
48
49 static OffsetMirrorProjector with( ExecutionContext context,
50 List<Projection> projections ) {
51 assert projections != null;
52 assert context != null;
53 if (projections.size() != 1) return null;
54 Projection projection = projections.get(0);
55 assert projection != null;
56 if (projection.getRules().size() != 1) return null;
57 PathFactory pathFactory = context.getValueFactories().getPathFactory();
58 List<Path> topLevelPaths = projection.getRules().get(0).getTopLevelPathsInRepository(pathFactory);
59 if (topLevelPaths.size() != 1) return null;
60 Path topLevelPath = topLevelPaths.get(0);
61 assert topLevelPath != null;
62 if (topLevelPath.isRoot()) return null;
63 return new OffsetMirrorProjector(context, projections, topLevelPath);
64 }
65
66 private final Projection projection;
67 private final Path offset;
68 private final int offsetSize;
69
70 private OffsetMirrorProjector( ExecutionContext context,
71 List<Projection> projections,
72 Path offset ) {
73 super(context, projections);
74 this.projection = projections.get(0);
75 this.offset = offset;
76 this.offsetSize = offset.size();
77 }
78
79
80
81
82
83
84 public ProjectedNode project( ExecutionContext context,
85 Location location,
86 boolean requiresUpdate ) {
87 if (requiresUpdate && projection.isReadOnly()) return null;
88 PlaceholderNode placeholder = isPlaceholder(location);
89 if (placeholder != null) return placeholder;
90
91 Path path = location.getPath();
92 Location locationInSource = location;
93 if (path != null) {
94 if (path.size() == offsetSize) {
95
96 if (path.equals(offset)) {
97 locationInSource = location.with(context.getValueFactories().getPathFactory().createRootPath());
98 } else {
99 return null;
100 }
101 } else {
102
103 if (path.isAtOrBelow(offset)) {
104 locationInSource = location.with(path.subpath(offsetSize));
105 } else {
106
107 return null;
108 }
109 }
110 }
111 return new ProxyNode(projection, locationInSource, location);
112 }
113
114 }