hierarchymanagerimpl.java

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

JAVA
513
字号
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements.  See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License.  You may obtain a copy of the License at * *      http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.apache.jackrabbit.core;import org.apache.jackrabbit.core.state.ItemState;import org.apache.jackrabbit.core.state.ItemStateException;import org.apache.jackrabbit.core.state.ItemStateManager;import org.apache.jackrabbit.core.state.NoSuchItemStateException;import org.apache.jackrabbit.core.state.NodeState;import org.apache.jackrabbit.core.state.PropertyState;import org.apache.jackrabbit.name.MalformedPathException;import org.apache.jackrabbit.name.Path;import org.apache.jackrabbit.name.PathResolver;import org.apache.jackrabbit.name.QName;import org.slf4j.Logger;import org.slf4j.LoggerFactory;import javax.jcr.ItemNotFoundException;import javax.jcr.NamespaceException;import javax.jcr.RepositoryException;/** * <code>HierarchyManagerImpl</code> ... */public class HierarchyManagerImpl implements HierarchyManager {    private static Logger log = LoggerFactory.getLogger(HierarchyManagerImpl.class);    /**     * The parent name returned for orphaned or root nodes.     * TODO: Is it proper to use an invalid QName for this.     */    private static final QName EMPTY_NAME = new QName("", "");    protected final NodeId rootNodeId;    protected final ItemStateManager provider;    /**     * Path resolver for outputting user-friendly paths in error messages.     */    protected final PathResolver resolver;    public HierarchyManagerImpl(NodeId rootNodeId,                                ItemStateManager provider,                                PathResolver resolver) {        this.rootNodeId = rootNodeId;        this.provider = provider;        this.resolver = resolver;    }    public NodeId getRootNodeId() {        return rootNodeId;    }    //-------------------------------------------------< misc. helper methods >    /**     * Failsafe conversion of internal <code>Path</code> to JCR path for use in     * error messages etc.     *     * @param path path to convert     * @return JCR path     */    public String safeGetJCRPath(Path path) {        try {            return resolver.getJCRPath(path);        } catch (NamespaceException e) {            log.error("failed to convert {} to a JCR path", path);            // return string representation of internal path as a fallback            return path.toString();        }    }    /**     * Failsafe translation of internal <code>ItemId</code> to JCR path for use     * in error messages etc.     *     * @param id id to translate     * @return JCR path     */    public String safeGetJCRPath(ItemId id) {        try {            return safeGetJCRPath(getPath(id));        } catch (RepositoryException re) {            log.error(id + ": failed to determine path to");            // return string representation if id as a fallback            return id.toString();        }    }    //---------------------------------------------------------< overridables >    /**     * Return an item state, given its item id.     * <p/>     * Low-level hook provided for specialized derived classes.     *     * @param id item id     * @return item state     * @throws NoSuchItemStateException if the item does not exist     * @throws ItemStateException       if an error occurs     * @see ZombieHierarchyManager#getItemState(ItemId)     */    protected ItemState getItemState(ItemId id)            throws NoSuchItemStateException, ItemStateException {        return provider.getItemState(id);    }    /**     * Determines whether an item state for a given item id exists.     * <p/>     * Low-level hook provided for specialized derived classes.     *     * @param id item id     * @return <code>true</code> if an item state exists, otherwise     *         <code>false</code>     * @see ZombieHierarchyManager#hasItemState(ItemId)     */    protected boolean hasItemState(ItemId id) {        return provider.hasItemState(id);    }    /**     * Returns the <code>parentUUID</code> of the given item.     * <p/>     * Low-level hook provided for specialized derived classes.     *     * @param state item state     * @return <code>parentUUID</code> of the given item     * @see ZombieHierarchyManager#getParentId(ItemState)     */    protected NodeId getParentId(ItemState state) {        return state.getParentId();    }    /**     * Returns the <code>ChildNodeEntry</code> of <code>parent</code> with the     * specified <code>uuid</code> or <code>null</code> if there's no such entry.     * <p/>     * Low-level hook provided for specialized derived classes.     *     * @param parent node state     * @param id   id of child node entry     * @return the <code>ChildNodeEntry</code> of <code>parent</code> with     *         the specified <code>uuid</code> or <code>null</code> if there's     *         no such entry.     * @see ZombieHierarchyManager#getChildNodeEntry(NodeState, NodeId)     */    protected NodeState.ChildNodeEntry getChildNodeEntry(NodeState parent,                                                         NodeId id) {        return parent.getChildNodeEntry(id);    }    /**     * Returns the <code>ChildNodeEntry</code> of <code>parent</code> with the     * specified <code>name</code> and <code>index</code> or <code>null</code>     * if there's no such entry.     * <p/>     * Low-level hook provided for specialized derived classes.     *     * @param parent node state     * @param name   name of child node entry     * @param index  index of child node entry     * @return the <code>ChildNodeEntry</code> of <code>parent</code> with     *         the specified <code>name</code> and <code>index</code> or     *         <code>null</code> if there's no such entry.     * @see ZombieHierarchyManager#getChildNodeEntry(NodeState, QName, int)     */    protected NodeState.ChildNodeEntry getChildNodeEntry(NodeState parent,                                                         QName name,                                                         int index) {        return parent.getChildNodeEntry(name, index);    }    /**     * Resolve a path into an item id. Recursively invoked method that may be     * overridden by some subclass to either return cached responses or add     * response to cache.     *     * @param path full path of item to resolve     * @param id   intermediate item id     * @param next next path element index to resolve     * @return the id of the item denoted by <code>path</code>     */    protected ItemId resolvePath(Path path, ItemId id, int next)            throws RepositoryException {        try {            return resolvePath(path, getItemState(id), next);        } catch (NoSuchItemStateException e) {            String msg = "failed to retrieve state of intermediary node";            log.debug(msg);            throw new RepositoryException(msg, e);        } catch (ItemStateException e) {            String msg = "failed to retrieve state of intermediary node";            log.debug(msg);            throw new RepositoryException(msg, e);        }    }    /**     * Resolve a path into an item id. Recursively invoked method that may be     * overridden by some subclass to either return cached responses or add     * response to cache.     *     * @param path  full path of item to resolve     * @param state intermediate state     * @param next  next path element index to resolve     * @return the id of the item denoted by <code>path</code> or     *         <code>null</code> if no item exists at <code>path</code>.     */    protected ItemId resolvePath(Path path, ItemState state, int next)            throws ItemStateException {        Path.PathElement[] elements = path.getElements();        if (elements.length == next) {            return state.getId();        }        Path.PathElement elem = elements[next];        QName name = elem.getName();        int index = elem.getIndex();        if (index == 0) {            index = 1;        }        NodeState parentState = (NodeState) state;        ItemId childId;        if (parentState.hasChildNodeEntry(name, index)) {            // child node            NodeState.ChildNodeEntry nodeEntry =                    getChildNodeEntry(parentState, name, index);            childId = nodeEntry.getId();        } else if (parentState.hasPropertyName(name)) {            // property            if (index > 1) {                // properties can't have same name siblings                return null;            } else if (next < elements.length - 1) {                // property is not the last element in the path                return null;

⌨️ 快捷键说明

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