📄 hierarchypropertyparser.java
字号:
} m_Current = (TreeNode)m_Current.children.elementAt(pos); } return true; } /** * Go to a certain node of the tree down from the current node * according to the specified relative path. The path does not * contain the value of current node * * @param path the given relative path * @return whether the path exists, if false the current position does * not move */ public synchronized boolean goDown(String path){ if(!isHierachic(path)) return goToChild(path); TreeNode old = m_Current; m_Current = new TreeNode(); String[] nodes = tokenize(path); int pos = search(old.children, nodes[0]); if(pos == -1){ m_Current = old; return false; } m_Current = (TreeNode)old.children.elementAt(pos); for(int i=1; i < nodes.length; i++){ pos = search(m_Current.children, nodes[i]); if(pos == -1){ m_Current = old; return false; } m_Current = (TreeNode)m_Current.children.elementAt(pos); } return true; } /** * Go to the root of the tree */ public synchronized void goToRoot(){ m_Current=m_Root; } /** * Go to the parent from the current position in the tree * If the current position is the root, it stays there and does * not move */ public synchronized void goToParent(){ if(m_Current.parent != null) // Not root m_Current = m_Current.parent; } /** * Go to one child node from the current position in the tree * according to the given value <br> * If the child node with the given value cannot be found it * returns false, true otherwise. If false, the current position * does not change * * @param value the value of the given child * @return whether the child can be found */ public synchronized boolean goToChild(String value){ if(m_Current.children == null) // Leaf return false; int pos = search(m_Current.children, value); if(pos == -1) return false; m_Current = (TreeNode)m_Current.children.elementAt(pos); return true; } /** * Go to one child node from the current position in the tree * according to the given position <br> * * @param pos the position of the given child * @exception if the position is out of range or leaf is reached */ public synchronized void goToChild(int pos) throws Exception { if((m_Current.children == null) || (pos < 0) || (pos >= m_Current.children.size())) throw new Exception("Position out of range or leaf reached"); m_Current = (TreeNode)m_Current.children.elementAt(pos); } /** * The number of the children nodes. If current node is leaf, it * returns 0. * * @return the number of the children nodes of the current position */ public synchronized int numChildren(){ if(m_Current.children == null) // Leaf return 0; return m_Current.children.size(); } /** * The value in the children nodes. If current node is leaf, it * returns null. * * @return the value in the children nodes */ public synchronized String[] childrenValues(){ if(m_Current.children == null) // Leaf return null; else{ Vector kids = m_Current.children; String[] values = new String[kids.size()]; for(int i=0; i < kids.size(); i++) values[i] = ((TreeNode)kids.elementAt(i)).value; return values; } } /** * The value in the parent node. If current node is root, it * returns null. * * @return the value in the parent node */ public synchronized String parentValue(){ if(m_Current.parent != null) // Not root return m_Current.parent.value; else return null; } /** * Whether the current position is a leaf * * @return whether the current position is a leaf */ public synchronized boolean isLeafReached(){ return (m_Current.children == null); } /** * Whether the current position is the root * * @return whether the current position is the root */ public synchronized boolean isRootReached(){ return (m_Current.parent == null); } /** * Get the value of current node * * @return value level */ public synchronized String getValue(){ return m_Current.value; } /** * Get the level of current node. Note the level starts from 0 * * @return the level */ public synchronized int getLevel(){ return m_Current.level; } /** * Get the depth of the tree, i.e. (the largest level)+1 * * @return the depth of the tree */ public int depth(){ return m_Depth; } /** * The context of the current node, i.e. the path from the * root to the parent node of the current node, seperated by * the seperator. If root, it returns null * * @return the context path */ public synchronized String context(){ return m_Current.context; } /** * The full value of the current node, i.e. its context + seperator * + its value. For root, only its value. * * @return the context path */ public synchronized String fullValue(){ if(m_Current == m_Root) return m_Root.value; else return (m_Current.context + m_Seperator + m_Current.value); } /** * Show the whole tree in text format * * @return the whole tree in text format */ public String showTree(){ return showNode(m_Root, null); } /** * Show one node of the tree in text format * * @param node the node in question * @return the node in text format */ private String showNode(TreeNode node, boolean[] hasBar){ StringBuffer text = new StringBuffer(); for(int i=0; i < (node.level - 1); i++) if(hasBar[i]) text.append(" | "); else text.append(" "); if(node.level != 0) text.append(" |------ "); text.append(node.value+"("+node.level+")"+"["+node.context+"]\n"); if(node.children != null) for(int i=0; i < node.children.size(); i++){ boolean[] newBar = new boolean[node.level+1]; int lvl = node.level; if(hasBar != null) for(int j=0; j < lvl; j++) newBar[j] = hasBar[j]; if((i == (node.children.size()-1))) newBar[lvl] = false; else newBar[lvl] = true; text.append(showNode((TreeNode)node.children.elementAt(i), newBar)); } return text.toString(); } /** * Tests out the parser. * * @param args should contain nothing */ public static void main(String args[]){ StringBuffer sb = new StringBuffer(); sb.append("node1.node1_1.node1_1_1.node1_1_1_1, "); sb.append("node1.node1_1.node1_1_1.node1_1_1_2, "); sb.append("node1.node1_1.node1_1_1.node1_1_1_3, "); sb.append("node1.node1_1.node1_1_2.node1_1_2_1, "); sb.append("node1.node1_1.node1_1_3.node1_1_3_1, "); sb.append("node1.node1_2.node1_2_1.node1_2_1_1, "); sb.append("node1.node1_2.node1_2_3.node1_2_3_1, "); sb.append("node1.node1_3.node1_3_3.node1_3_3_1, "); sb.append("node1.node1_3.node1_3_3.node1_3_3_2, "); String p = sb.toString(); try{ HierarchyPropertyParser hpp = new HierarchyPropertyParser(p, ", "); System.out.println("seperator: "+hpp.getSeperator()); System.out.println("depth: "+hpp.depth()); System.out.println("The tree:\n\n"+hpp.showTree()); hpp.goToRoot(); System.out.println("goto: "+hpp.goTo("node1.node1_2.node1_2_1") +": "+hpp.getValue()+" | "+hpp.fullValue()+ " leaf? "+ hpp.isLeafReached()); System.out.println("go down(wrong): "+hpp.goDown("node1")); System.out.println("Stay still? "+hpp.getValue()); System.out.println("go to child: "+hpp.goToChild("node1_2_1_1") +": "+hpp.getValue()+" | "+hpp.fullValue()+ " leaf? "+ hpp.isLeafReached() +" root? "+ hpp.isRootReached()); System.out.println("parent: "+hpp.parentValue()); System.out.println("level: "+hpp.getLevel()); System.out.println("context: "+hpp.context()); hpp.goToRoot(); System.out.println("After gotoRoot. leaf? "+ hpp.isLeafReached() +" root? "+ hpp.isRootReached()); System.out.println("Go down(correct): "+ hpp.goDown("node1_1.node1_1_1")+ " value: "+hpp.getValue()+" | "+hpp.fullValue() +" level: "+hpp.getLevel() +" leaf? "+ hpp.isLeafReached() +" root? "+ hpp.isRootReached()); hpp.goToParent(); System.out.println("value: "+hpp.getValue()+" | "+hpp.fullValue()); System.out.println("level: "+hpp.getLevel()); String[] chd = hpp.childrenValues(); for(int i=0; i < chd.length; i++){ System.out.print("children "+i+": "+chd[i]); hpp.goDown(chd[i]); System.out.println("real value: "+hpp.getValue()+" | "+ hpp.fullValue()+ "(level: "+hpp.getLevel()+")"); hpp.goToParent(); } System.out.println("Another way to go to root:"+hpp.goTo("node1") +": "+hpp.getValue()+" | "+hpp.fullValue()); }catch(Exception e){ System.out.println(e.getMessage()); e.printStackTrace(); } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -