hierarchymanagerimpl.java

来自「jsr170接口的java实现。是个apache的开源项目。」· Java 代码 · 共 513 行 · 第 1/2 页

JAVA
513
字号
            }            childId = new PropertyId(parentState.getNodeId(), name);        } else {            // no such item            return null;        }        return resolvePath(path, getItemState(childId), next + 1);    }    /**     * Adds the path element of an item id to the path currently being built.     * Recursively invoked method that may be overridden by some subclass to     * either return cached responses or add response to cache. On exit,     * <code>builder</code> contains the path of <code>state</code>.     *     * @param builder builder currently being used     * @param state   item to find path of     */    protected void buildPath(Path.PathBuilder builder, ItemState state)            throws ItemStateException, RepositoryException {        // shortcut        if (state.getId().equals(rootNodeId)) {            builder.addRoot();            return;        }        NodeId parentId = getParentId(state);        if (parentId == null) {            String msg = "failed to build path of " + state.getId()                    + ": orphaned item";            log.debug(msg);            throw new ItemNotFoundException(msg);        }        NodeState parent = (NodeState) getItemState(parentId);        // recursively build path of parent        buildPath(builder, parent);        if (state.isNode()) {            NodeState nodeState = (NodeState) state;            NodeId id = nodeState.getNodeId();            NodeState.ChildNodeEntry entry = getChildNodeEntry(parent, id);            if (entry == null) {                String msg = "failed to build path of " + state.getId() + ": "                        + parent.getNodeId() + " has no child entry for "                        + id;                log.debug(msg);                throw new ItemNotFoundException(msg);            }            // add to path            if (entry.getIndex() == 1) {                builder.addLast(entry.getName());            } else {                builder.addLast(entry.getName(), entry.getIndex());            }        } else {            PropertyState propState = (PropertyState) state;            QName name = propState.getName();            // add to path            builder.addLast(name);        }    }    //-----------------------------------------------------< HierarchyManager >    /**     * {@inheritDoc}     */    public ItemId resolvePath(Path path) throws RepositoryException {        // shortcut        if (path.denotesRoot()) {            return rootNodeId;        }        if (!path.isCanonical()) {            String msg = "path is not canonical";            log.debug(msg);            throw new RepositoryException(msg);        }        return resolvePath(path, rootNodeId, 1);    }    /**     * {@inheritDoc}     */    public Path getPath(ItemId id)            throws ItemNotFoundException, RepositoryException {        // shortcut        if (id.equals(rootNodeId)) {            return Path.ROOT;        }        Path.PathBuilder builder = new Path.PathBuilder();        try {            buildPath(builder, getItemState(id));            return builder.getPath();        } catch (NoSuchItemStateException nsise) {            String msg = "failed to build path of " + id;            log.debug(msg);            throw new ItemNotFoundException(msg, nsise);        } catch (ItemStateException ise) {            String msg = "failed to build path of " + id;            log.debug(msg);            throw new RepositoryException(msg, ise);        } catch (MalformedPathException mpe) {            String msg = "failed to build path of " + id;            log.debug(msg);            throw new RepositoryException(msg, mpe);        }    }    /**     * {@inheritDoc}     */    public QName getName(ItemId itemId)            throws ItemNotFoundException, RepositoryException {        if (itemId.denotesNode()) {            NodeId nodeId = (NodeId) itemId;            NodeState parentState;            try {                NodeState nodeState = (NodeState) getItemState(nodeId);                NodeId parentId = getParentId(nodeState);                if (parentId == null) {                    // this is the root or an orphaned node                    // FIXME                    return EMPTY_NAME;                }                parentState = (NodeState) getItemState(parentId);            } catch (NoSuchItemStateException nsis) {                String msg = "failed to resolve name of " + nodeId;                log.debug(msg);                throw new ItemNotFoundException(nodeId.toString());            } catch (ItemStateException ise) {                String msg = "failed to resolve name of " + nodeId;                log.debug(msg);                throw new RepositoryException(msg, ise);            }            NodeState.ChildNodeEntry entry =                    getChildNodeEntry(parentState, nodeId);            if (entry == null) {                String msg = "failed to resolve name of " + nodeId;                log.debug(msg);                throw new RepositoryException(msg);            }            return entry.getName();        } else {            return ((PropertyId) itemId).getName();        }    }    /**     * {@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);        }    }    /**     * {@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);        }    }    /**     * {@inheritDoc}     */    public boolean isAncestor(NodeId nodeId, ItemId itemId)            throws ItemNotFoundException, RepositoryException {        if (nodeId.equals(itemId)) {            // can't be ancestor of self            return false;        }        try {            ItemState state = getItemState(itemId);            NodeId parentId = getParentId(state);            while (parentId != null) {                if (parentId.equals(nodeId)) {                    return true;                }                state = getItemState(parentId);                parentId = getParentId(state);            }            // not an ancestor            return false;        } catch (NoSuchItemStateException nsise) {            String msg = "failed to determine degree of relationship of "                    + nodeId + " and " + itemId;            log.debug(msg);            throw new ItemNotFoundException(msg, nsise);        } catch (ItemStateException ise) {            String msg = "failed to determine degree of relationship of "                    + nodeId + " and " + itemId;            log.debug(msg);            throw new RepositoryException(msg, ise);        }    }}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?