nodestateex.java
来自「jsr170接口的java实现。是个apache的开源项目。」· Java 代码 · 共 611 行 · 第 1/2 页
JAVA
611 行
} /** * removes the (first) child node with the given name. * * @param name * @return <code>true</code> if the child was removed * @throws RepositoryException */ public boolean removeNode(QName name) throws RepositoryException { return removeNode(name, 1); } /** * removes the child node with the given name and 1-based index * * @param name * @param index * @return <code>true</code> if the child was removed. * @throws RepositoryException */ public boolean removeNode(QName name, int index) throws RepositoryException { try { NodeState.ChildNodeEntry entry = nodeState.getChildNodeEntry(name, index); if (entry == null) { return false; } else { removeNode(entry.getId()); nodeState.removeChildNodeEntry(name, index); nodeState.setStatus(ItemState.STATUS_EXISTING_MODIFIED); return true; } } catch (ItemStateException e) { throw new RepositoryException(e); } } /** * removes recursively the node with the given id * * @param id * @throws ItemStateException */ private void removeNode(NodeId id) throws ItemStateException { NodeState state = (NodeState) stateMgr.getItemState(id); // remove properties Iterator iter = state.getPropertyNames().iterator(); while (iter.hasNext()) { QName name = (QName) iter.next(); PropertyId propId = new PropertyId(id, name); PropertyState propState = (PropertyState) stateMgr.getItemState(propId); stateMgr.destroy(propState); } state.removeAllPropertyNames(); // remove child nodes iter = state.getChildNodeEntries().iterator(); while (iter.hasNext()) { NodeState.ChildNodeEntry entry = (NodeState.ChildNodeEntry) iter.next(); removeNode(entry.getId()); } state.removeAllChildNodeEntries(); // destroy the state itself stateMgr.destroy(state); } /** * removes the property with the given name * * @param name * @return <code>true</code> if the property was removed. * @throws RepositoryException */ public boolean removeProperty(QName name) throws RepositoryException { try { if (!nodeState.hasPropertyName(name)) { return false; } else { PropertyId propId = new PropertyId(nodeState.getNodeId(), name); ItemState state = stateMgr.getItemState(propId); stateMgr.destroy(state); nodeState.removePropertyName(name); nodeState.setStatus(ItemState.STATUS_EXISTING_MODIFIED); return true; } } catch (ItemStateException e) { throw new RepositoryException(e); } } /** * retrieves the child node with the given name and 1-base index or * <code>null</code> if the node does not exist. * * @param name * @param index * @return the node state. * @throws RepositoryException */ public NodeStateEx getNode(QName name, int index) throws RepositoryException { NodeState.ChildNodeEntry entry = nodeState.getChildNodeEntry(name, index); if (entry == null) { return null; } try { NodeState state = (NodeState) stateMgr.getItemState(entry.getId()); return new NodeStateEx(stateMgr, ntReg, state, name); } catch (ItemStateException e) { throw new RepositoryException("Unable to getNode: " + e.toString()); } } /** * Adds a new child node with the given name * * @param nodeName * @param nodeTypeName * @return the node state * @throws NoSuchNodeTypeException * @throws ConstraintViolationException * @throws RepositoryException */ public NodeStateEx addNode(QName nodeName, QName nodeTypeName, NodeId id, boolean referenceable) throws NoSuchNodeTypeException, ConstraintViolationException, RepositoryException { NodeStateEx node = createChildNode(nodeName, nodeTypeName, id); if (referenceable) { node.setPropertyValue(QName.JCR_UUID, InternalValue.create(node.getNodeId().getUUID().toString())); } return node; } /** * creates a new child node * * @param name * @param id * @return the newly created node. */ private NodeStateEx createChildNode(QName name, QName nodeTypeName, NodeId id) throws RepositoryException { NodeId parentId = nodeState.getNodeId(); // create a new node state if (id == null) { id = new NodeId(UUID.randomUUID()); } NodeState state = stateMgr.createNew(id, nodeTypeName, parentId); NodeDef cnd = getEffectiveNodeType().getApplicableChildNodeDef(name, nodeTypeName, ntReg); state.setDefinitionId(cnd.getId()); // create Node instance wrapping new node state NodeStateEx node = new NodeStateEx(stateMgr, ntReg, state, name); node.setPropertyValue(QName.JCR_PRIMARYTYPE, InternalValue.create(nodeTypeName)); // add new child node entryn nodeState.addChildNodeEntry(name, id); if (nodeState.getStatus() == ItemState.STATUS_EXISTING) { nodeState.setStatus(ItemState.STATUS_EXISTING_MODIFIED); } return node; } /** * returns all child nodes * * @return the child nodes. * @throws RepositoryException */ public NodeStateEx[] getChildNodes() throws RepositoryException { try { List entries = nodeState.getChildNodeEntries(); NodeStateEx[] children = new NodeStateEx[entries.size()]; for (int i = 0; i < entries.size(); i++) { NodeState.ChildNodeEntry entry = (NodeState.ChildNodeEntry) entries.get(i); NodeState state = (NodeState) stateMgr.getItemState(entry.getId()); children[i] = new NodeStateEx(stateMgr, ntReg, state, entry.getName()); } return children; } catch (ItemStateException e) { throw new RepositoryException(e); } } /** * stores the persistent state recursively * * @throws RepositoryException */ public void store() throws RepositoryException { try { store(nodeState); } catch (ItemStateException e) { throw new RepositoryException(e); } } /** * stores the given persistent state recursively * * @param state * @throws ItemStateException */ private void store(NodeState state) throws ItemStateException { if (state.getStatus() != ItemState.STATUS_EXISTING) { // first store all transient properties Set props = state.getPropertyNames(); for (Iterator iter = props.iterator(); iter.hasNext();) { QName propName = (QName) iter.next(); PropertyState pstate = (PropertyState) stateMgr.getItemState( new PropertyId(state.getNodeId(), propName)); if (pstate.getStatus() != ItemState.STATUS_EXISTING) { stateMgr.store(pstate); } } // now store all child node entries List nodes = state.getChildNodeEntries(); for (int i = 0; i < nodes.size(); i++) { NodeState.ChildNodeEntry entry = (NodeState.ChildNodeEntry) nodes.get(i); NodeState nstate = (NodeState) stateMgr.getItemState(entry.getId()); store(nstate); } // and store itself stateMgr.store(state); } } /** * reloads the persistent state recursively * * @throws RepositoryException */ public void reload() throws RepositoryException { try { reload(nodeState); // refetch nodestate if discarded nodeState = (NodeState) stateMgr.getItemState(nodeState.getNodeId()); } catch (ItemStateException e) { throw new RepositoryException(e); } } /** * reloads the given persistent state recursively * * @param state * @throws ItemStateException */ private void reload(NodeState state) throws ItemStateException { if (state.getStatus() != ItemState.STATUS_EXISTING) { // first discard all all transient properties Set props = state.getPropertyNames(); for (Iterator iter = props.iterator(); iter.hasNext();) { QName propName = (QName) iter.next(); PropertyState pstate = (PropertyState) stateMgr.getItemState( new PropertyId(state.getNodeId(), propName)); if (pstate.getStatus() != ItemState.STATUS_EXISTING) { pstate.discard(); } } // now reload all child node entries List nodes = state.getChildNodeEntries(); for (int i = 0; i < nodes.size(); i++) { NodeState.ChildNodeEntry entry = (NodeState.ChildNodeEntry) nodes.get(i); NodeState nstate = (NodeState) stateMgr.getItemState(entry.getId()); reload(nstate); } // and reload itself state.discard(); } } /** * copies a property * * @param prop * @throws RepositoryException */ public void copyFrom(PropertyImpl prop) throws RepositoryException { if (prop.getDefinition().isMultiple()) { InternalValue[] values = prop.internalGetValues(); int type; if (values.length > 0) { type = values[0].getType(); } else { type = prop.getDefinition().getRequiredType(); } InternalValue[] copiedValues = new InternalValue[values.length]; for (int i = 0; i < values.length; i++) { copiedValues[i] = values[i].createCopy(); } setPropertyValues(prop.getQName(), type, copiedValues); } else { setPropertyValue(prop.getQName(), prop.internalGetValue().createCopy()); } }}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?