📄 pathmap.java
字号:
*/ public void remove() { remove(true); } /** * Remove this element. Delegates the call to the parent item. * @param shift if index of same name siblings will be shifted. */ public void remove(boolean shift) { if (parent != null) { parent.remove(getPathElement(), shift, true); } else { // Removing the root node is not possible: if it has become // invalid, remove all its children and the associated object children = null; childrenCount = 0; obj = null; } } /** * Remove all children of this element. Removes this element itself * if this element does not contain associated information. */ public void removeAll() { children = null; childrenCount = 0; if (obj == null && parent != null) { parent.remove(getPathElement(), false, true); } } /** * Move a child of this element to a different location inside the * same parent. * * @param oldNameIndex old name/index * @param newNameIndex new name/index * @return <code>true</code> if the element was successfully moved; * otherwise <code>false</code> */ public boolean move(Path.PathElement oldNameIndex, Path.PathElement newNameIndex) { Element child = remove(oldNameIndex, false, false); if (child != null) { put(newNameIndex, child); return true; } return false; } /** * Return the object associated with this element * @return object associated with this element */ public Object get() { return obj; } /** * Set the object associated with this element * @param obj object associated with this element */ public void set(Object obj) { this.obj = obj; if (obj == null && childrenCount == 0 && parent != null) { parent.remove(getPathElement(), false, true); } } /** * Return the name of this element * @return name */ public QName getName() { return name; } /** * Return the non-normalized 1-based index of this element. Note that * this method can return a value of 0 which should be treated as 1. * @return index * @see #getNormalizedIndex() */ public int getIndex() { return index; } /** * Return the 1-based index of this element. * Same as {@link #getIndex()} except that an {@link Path#INDEX_UNDEFINED * undefined index} value is automatically converted to the * {@link Path#INDEX_DEFAULT default index} value. * @return 1-based index */ public int getNormalizedIndex() { if (index == Path.INDEX_UNDEFINED) { return Path.INDEX_DEFAULT; } else { return index; } } /** * Return a path element pointing to this element * @return path element */ public Path.PathElement getPathElement() { return Path.create(name, index).getNameElement(); } /** * Return the path of this element. * @return path * @throws MalformedPathException if building the path fails */ public Path getPath() throws MalformedPathException { if (parent == null) { return Path.ROOT; } Path.PathBuilder builder = new Path.PathBuilder(); getPath(builder); return builder.getPath(); } /** * Internal implementation of {@link #getPath()} that populates entries * in a builder. On exit, <code>builder</code> contains the path * of this element */ private void getPath(Path.PathBuilder builder) { if (parent == null) { builder.addRoot(); return; } parent.getPath(builder); if (index == Path.INDEX_UNDEFINED || index == Path.INDEX_DEFAULT) { builder.addLast(name); } else { builder.addLast(name, index); } } /** * Checks whether this element has the specified path. Introduced to * avoid catching a <code>MalformedPathException</code> for simple * path comparisons. * @param path path to compare to * @return <code>true</code> if this child has the path * <code>path</code>, <code>false</code> otherwise */ public boolean hasPath(Path path) { return hasPath(path.getElements(), path.getLength()); } /** * Checks whether this element has the specified path, given by * path elements. * @param elements path elements to compare to * @param len number of elements to compare to * @return <code>true</code> if this element has the path given; * otherwise <code>false</code> */ private boolean hasPath(Path.PathElement[] elements, int len) { if (getPathElement().equals(elements[len - 1])) { if (parent != null) { return parent.hasPath(elements, len - 1); } return true; } return false; } /** * Return 0-based index of a path element. */ private static int getZeroBasedIndex(Path.PathElement nameIndex) { return nameIndex.getNormalizedIndex() - 1; } /** * Recursively invoked traversal method. This method visits the element * first, then its children. * @param visitor visitor to invoke * @param includeEmpty if <code>true</code> invoke call back on every * element regardless, whether the associated object is empty * or not; otherwise call back on non-empty children only */ public void traverse(ElementVisitor visitor, boolean includeEmpty) { if (includeEmpty || obj != null) { visitor.elementVisited(this); } if (children != null) { Iterator iter = children.values().iterator(); while (iter.hasNext()) { ArrayList list = (ArrayList) iter.next(); for (int i = 0; i < list.size(); i++) { Element element = (Element) list.get(i); if (element != null) { element.traverse(visitor, includeEmpty); } } } } } /** * Return the depth of this element. Defined to be <code>0</code> for the * root element and <code>n + 1</code> for some element if the depth of * its parent is <code>n</code>. */ public int getDepth() { if (parent != null) { return parent.getDepth() + 1; } // Root return Path.ROOT_DEPTH; } /** * Return a flag indicating whether the specified node is a * child of this node. * @param other node to check */ public boolean isAncestorOf(Element other) { Element parent = other.parent; while (parent != null) { if (parent == this) { return true; } parent = parent.parent; } return false; } /** * Return the parent of this element * @return parent or <code>null</code> if this is the root element */ public Element getParent() { return parent; } /** * Return the children count of this element * @return children count */ public int getChildrenCount() { return childrenCount; } /** * Return an iterator over all of this element's children. Every * element returned by this iterator is of type {@link #Element}. */ public Iterator getChildren() { ArrayList result = new ArrayList(); if (children != null) { Iterator iter = children.values().iterator(); while (iter.hasNext()) { ArrayList list = (ArrayList) iter.next(); for (int i = 0; i < list.size(); i++) { Element element = (Element) list.get(i); if (element != null) { result.add(element); } } } } return result.iterator(); } /** * Map a relPath starting at <code>this</code> Element. If * <code>exact</code> is <code>false</code>, returns the last available * item along the relPath that is stored in the map. * * @param relPath relPath to map * @param exact flag indicating whether an exact match is required * @return descendant, maybe <code>null</code> if <code>exact</code> is * <code>true</code> */ public Element getDescendant(Path relPath, boolean exact) { Path.PathElement[] elements = relPath.getElements(); Element current = this; for (int i = 0; i < elements.length; i++) { Element next = current.getChild(elements[i]); if (next == null) { if (exact) { return null; } break; } current = next; } return current; } } /** * Element visitor used in {@link PathMap#traverse} */ public interface ElementVisitor { /** * Invoked for every element visited on a tree traversal * @param element element visited */ void elementVisited(Element element); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -