nodestateex.java
来自「jsr170接口的java实现。是个apache的开源项目。」· Java 代码 · 共 611 行 · 第 1/2 页
JAVA
611 行
/* * 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.version;import org.apache.jackrabbit.core.NodeId;import org.apache.jackrabbit.core.PropertyId;import org.apache.jackrabbit.core.PropertyImpl;import org.apache.jackrabbit.core.nodetype.EffectiveNodeType;import org.apache.jackrabbit.core.nodetype.NodeDef;import org.apache.jackrabbit.core.nodetype.NodeTypeConflictException;import org.apache.jackrabbit.core.nodetype.NodeTypeRegistry;import org.apache.jackrabbit.core.nodetype.PropDef;import org.apache.jackrabbit.core.state.ItemState;import org.apache.jackrabbit.core.state.ItemStateException;import org.apache.jackrabbit.core.state.NodeState;import org.apache.jackrabbit.core.state.PropertyState;import org.apache.jackrabbit.core.state.UpdatableItemStateManager;import org.apache.jackrabbit.core.value.InternalValue;import org.apache.jackrabbit.name.QName;import org.apache.jackrabbit.uuid.UUID;import java.util.HashSet;import java.util.Iterator;import java.util.List;import java.util.Set;import javax.jcr.RepositoryException;import javax.jcr.nodetype.ConstraintViolationException;import javax.jcr.nodetype.NoSuchNodeTypeException;/** * This Class provides some basic node operations directly on the node state. */public class NodeStateEx { /** * the underlying persistent state */ private NodeState nodeState; /** * the state manager */ private final UpdatableItemStateManager stateMgr; /** * the node type registry for resolving item defs */ private final NodeTypeRegistry ntReg; /** * the cached name */ private QName name; /** * Creates a new persistent node * * @param stateMgr * @param nodeState */ public NodeStateEx(UpdatableItemStateManager stateMgr, NodeTypeRegistry ntReg, NodeState nodeState, QName name) { this.nodeState = nodeState; this.ntReg = ntReg; this.stateMgr = stateMgr; this.name = name; } /** * returns the name of this node * * @return the name of this node */ public QName getName() { if (name == null) { try { NodeId parentId = nodeState.getParentId(); NodeState parent = (NodeState) stateMgr.getItemState(parentId); name = parent.getChildNodeEntry(nodeState.getNodeId()).getName(); } catch (ItemStateException e) { // should never occurr throw new IllegalStateException(e.toString()); } } return name; } /** * Returns the id of this node. * * @return the id of this node. */ public NodeId getNodeId() { return nodeState.getNodeId(); } /** * Returns the parent id of this node * * @return the parent id of this node */ public NodeId getParentId() { return nodeState.getParentId(); } /** * Returns the underlaying node state. * @return the underlaying node state. */ public NodeState getState() { return nodeState; } /** * Returns the properties of this node * * @return the properties of this node */ public PropertyState[] getProperties() throws ItemStateException { Set set = nodeState.getPropertyNames(); PropertyState[] props = new PropertyState[set.size()]; int i = 0; for (Iterator iter = set.iterator(); iter.hasNext();) { QName propName = (QName) iter.next(); PropertyId propId = new PropertyId(nodeState.getNodeId(), propName); props[i++] = (PropertyState) stateMgr.getItemState(propId); } return props; } /** * Checks if the given property exists * * @param name * @return <code>true</code> if the given property exists. */ public boolean hasProperty(QName name) { PropertyId propId = new PropertyId(nodeState.getNodeId(), name); return stateMgr.hasItemState(propId); } /** * Returns the values of the given property of <code>null</code> * * @param name * @return the values of the given property. */ public InternalValue[] getPropertyValues(QName name) { PropertyId propId = new PropertyId(nodeState.getNodeId(), name); try { PropertyState ps = (PropertyState) stateMgr.getItemState(propId); return ps.getValues(); } catch (ItemStateException e) { return null; } } /** * Returns the value of the given property or <code>null</code> * * @param name * @return the value of the given property. */ public InternalValue getPropertyValue(QName name) { PropertyId propId = new PropertyId(nodeState.getNodeId(), name); try { PropertyState ps = (PropertyState) stateMgr.getItemState(propId); return ps.getValues()[0]; } catch (ItemStateException e) { return null; } } /** * Sets the property value * * @param name * @param value * @throws RepositoryException */ public void setPropertyValue(QName name, InternalValue value) throws RepositoryException { setPropertyValues(name, value.getType(), new InternalValue[]{value}, false); } /** * Sets the property values * * @param name * @param type * @param values * @throws RepositoryException */ public void setPropertyValues(QName name, int type, InternalValue[] values) throws RepositoryException { setPropertyValues(name, type, values, true); } /** * Sets the property values * * @param name * @param type * @param values * @throws RepositoryException */ public void setPropertyValues(QName name, int type, InternalValue[] values, boolean multiple) throws RepositoryException { PropertyState prop = getOrCreatePropertyState(name, type, multiple); prop.setValues(values); } /** * Retrieves or creates a new property state as child property of this node * * @param name * @param type * @param multiValued * @return the property state * @throws RepositoryException */ private PropertyState getOrCreatePropertyState(QName name, int type, boolean multiValued) throws RepositoryException { PropertyId propId = new PropertyId(nodeState.getNodeId(), name); if (stateMgr.hasItemState(propId)) { try { PropertyState propState = (PropertyState) stateMgr.getItemState(propId); // someone calling this method will always alter the property state, so set status to modified if (propState.getStatus() == ItemState.STATUS_EXISTING) { propState.setStatus(ItemState.STATUS_EXISTING_MODIFIED); } // although this is not quite correct, we mark node as modified aswell if (nodeState.getStatus() == ItemState.STATUS_EXISTING) { nodeState.setStatus(ItemState.STATUS_EXISTING_MODIFIED); } return propState; } catch (ItemStateException e) { throw new RepositoryException("Unable to create property: " + e.toString()); } } else { PropertyState propState = stateMgr.createNew(name, nodeState.getNodeId()); propState.setType(type); propState.setMultiValued(multiValued); PropDef pd = getEffectiveNodeType().getApplicablePropertyDef(name, type, multiValued); propState.setDefinitionId(pd.getId()); // need to store nodestate nodeState.addPropertyName(name); if (nodeState.getStatus() == ItemState.STATUS_EXISTING) { nodeState.setStatus(ItemState.STATUS_EXISTING_MODIFIED); } return propState; } } /** * Returns the effective (i.e. merged and resolved) node type representation * of this node's primary and mixin node types. * * @return the effective node type * @throws RepositoryException */ public EffectiveNodeType getEffectiveNodeType() throws RepositoryException { // build effective node type of mixins & primary type // existing mixin's HashSet set = new HashSet((nodeState).getMixinTypeNames()); // primary type set.add(nodeState.getNodeTypeName()); try { return ntReg.getEffectiveNodeType((QName[]) set.toArray(new QName[set.size()])); } catch (NodeTypeConflictException ntce) { String msg = "internal error: failed to build effective node type for node " + nodeState.getNodeId(); throw new RepositoryException(msg, ntce); } } /** * checks if the given child node exists. * * @param name * @return <code>true</code> if the given child exists. */ public boolean hasNode(QName name) { return nodeState.hasChildNodeEntry(name);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?