use of org.apache.jackrabbit.core.id.NodeId in project jackrabbit by apache.
the class HierarchyManagerImpl method resolvePath.
//-------------------------------------------------------< implementation >
/**
* Internal implementation that iteratively resolves a path into an item.
*
* @param elements path elements
* @param next index of next item in <code>elements</code> to inspect
* @param id id of item at path <code>elements[0]</code>..<code>elements[next - 1]</code>
* @param typesAllowed one of <code>RETURN_ANY</code>, <code>RETURN_NODE</code>
* or <code>RETURN_PROPERTY</code>
* @return id or <code>null</code>
* @throws ItemStateException if an intermediate item state is not found
* @throws MalformedPathException if building an intermediate path fails
*/
protected ItemId resolvePath(Path.Element[] elements, int next, ItemId id, int typesAllowed) throws ItemStateException, MalformedPathException {
PathBuilder builder = new PathBuilder();
for (int i = 0; i < next; i++) {
builder.addLast(elements[i]);
}
for (int i = next; i < elements.length; i++) {
Path.Element elem = elements[i];
NodeId parentId = (NodeId) id;
id = null;
Name name = elem.getName();
int index = elem.getIndex();
if (index == 0) {
index = 1;
}
int typeExpected = typesAllowed;
if (i < elements.length - 1) {
// intermediate items must always be nodes
typeExpected = RETURN_NODE;
}
NodeState parentState = (NodeState) getItemState(parentId);
if ((typeExpected & RETURN_NODE) != 0) {
ChildNodeEntry nodeEntry = getChildNodeEntry(parentState, name, index);
if (nodeEntry != null) {
id = nodeEntry.getId();
}
}
if (id == null && (typeExpected & RETURN_PROPERTY) != 0) {
if (parentState.hasPropertyName(name) && (index <= 1)) {
// property
id = new PropertyId(parentState.getNodeId(), name);
}
}
if (id == null) {
break;
}
builder.addLast(elements[i]);
pathResolved(id, builder);
}
return id;
}
use of org.apache.jackrabbit.core.id.NodeId in project jackrabbit by apache.
the class HierarchyManagerImpl method isShareAncestor.
/**
* {@inheritDoc}
*/
public boolean isShareAncestor(NodeId ancestor, NodeId descendant) throws ItemNotFoundException, RepositoryException {
if (ancestor.equals(descendant)) {
// can't be ancestor of self
return false;
}
try {
ItemState state = getItemState(descendant);
Set<NodeId> parentIds = getParentIds(state, false);
while (parentIds.size() > 0) {
if (parentIds.contains(ancestor)) {
return true;
}
Set<NodeId> grandparentIds = new LinkedHashSet<NodeId>();
for (NodeId parentId : parentIds) {
grandparentIds.addAll(getParentIds(getItemState(parentId), false));
}
parentIds = grandparentIds;
}
// not an ancestor
return false;
} catch (NoSuchItemStateException nsise) {
String msg = "failed to determine degree of relationship of " + ancestor + " and " + descendant;
log.debug(msg);
throw new ItemNotFoundException(msg, nsise);
} catch (ItemStateException ise) {
String msg = "failed to determine degree of relationship of " + ancestor + " and " + descendant;
log.debug(msg);
throw new RepositoryException(msg, ise);
}
}
use of org.apache.jackrabbit.core.id.NodeId in project jackrabbit by apache.
the class HierarchyManagerImpl method getParentIds.
/**
* Return all parents of a node. A shareable node has possibly more than
* one parent.
*
* @param state item state
* @param useOverlayed whether to use overlayed state for shareable nodes
* @return set of parent <code>NodeId</code>s. If state has no parent,
* array has length <code>0</code>.
*/
protected Set<NodeId> getParentIds(ItemState state, boolean useOverlayed) {
if (state.isNode()) {
// if this is a node, quickly check whether it is shareable and
// whether it contains more than one parent
NodeState ns = (NodeState) state;
if (ns.isShareable() && useOverlayed && ns.hasOverlayedState()) {
ns = (NodeState) ns.getOverlayedState();
}
Set<NodeId> s = ns.getSharedSet();
if (s.size() > 1) {
return s;
}
}
NodeId parentId = getParentId(state);
if (parentId != null) {
LinkedHashSet<NodeId> s = new LinkedHashSet<NodeId>();
s.add(parentId);
return s;
}
return Collections.emptySet();
}
use of org.apache.jackrabbit.core.id.NodeId in project jackrabbit by apache.
the class HierarchyManagerImpl method getDepth.
/**
* {@inheritDoc}
*/
public int getDepth(ItemId id) throws ItemNotFoundException, RepositoryException {
// shortcut
if (id.equals(rootNodeId)) {
return 0;
}
try {
ItemState state = getItemState(id);
NodeId parentId = getParentId(state);
int depth = 0;
while (parentId != null) {
depth++;
state = getItemState(parentId);
parentId = getParentId(state);
}
return depth;
} catch (NoSuchItemStateException nsise) {
String msg = "failed to determine depth of " + id;
log.debug(msg);
throw new ItemNotFoundException(msg, nsise);
} catch (ItemStateException ise) {
String msg = "failed to determine depth of " + id;
log.debug(msg);
throw new RepositoryException(msg, ise);
}
}
use of org.apache.jackrabbit.core.id.NodeId in project jackrabbit by apache.
the class HierarchyManagerImpl method getRelativeDepth.
/**
* {@inheritDoc}
*/
public int getRelativeDepth(NodeId ancestorId, ItemId descendantId) throws ItemNotFoundException, RepositoryException {
if (ancestorId.equals(descendantId)) {
return 0;
}
int depth = 1;
try {
ItemState state = getItemState(descendantId);
NodeId parentId = getParentId(state);
while (parentId != null) {
if (parentId.equals(ancestorId)) {
return depth;
}
depth++;
state = getItemState(parentId);
parentId = getParentId(state);
}
// not an ancestor
return -1;
} catch (NoSuchItemStateException nsise) {
String msg = "failed to determine depth of " + descendantId + " relative to " + ancestorId;
log.debug(msg);
throw new ItemNotFoundException(msg, nsise);
} catch (ItemStateException ise) {
String msg = "failed to determine depth of " + descendantId + " relative to " + ancestorId;
log.debug(msg);
throw new RepositoryException(msg, ise);
}
}
Aggregations