Look at many people to ask the tree, my code can meet most requirements: flyxxxxx (神)

xiaoxiao2021-03-06  78

Disk files, tables in XML documents, and SQL (have certain requirements) can generate a directory tree. This code can use modifications, such as modifications, suggestions, or discovery errors, please send to flyxxxxx@163.com, thank you.

Import java.util.ArrayList; import java.util.collections; import java.util.comparator; import java.util.ITerator;

/ ** * directory tree. * Provides support from other objects to generate directory trees. Such as disk directory: Tree t = tree.gettree (new java.io.file ("d: /"), new filecontainer ()); * XML file: Document doc = ... * Tree T = Tree.gettree (DOC, New DocumentContainer ()); * If there is a target other than file or document to generate a directory tree, implement {@link treenode} or {@link Container }interface. * For tables in SQL, the suggestion field requires ID (ID), ParentId, other fields, and then generate an object that implements the TREENODE interface for each * strip of the query. * If the TREENODE interface is implemented: * Class Group Implements Treenode {= ... * Tree T = Tree.gettree (Groups, 0); // Where 0 is the ID of the root node, and Groups does not require the same object, as long as they all implement * interface Treenode, and the id is different, this can be processed according to the object type during the process of generating a directory tree. *

Copyright: Copyright (c) 2004 *

* @Author flyxxxxx * @version 1.0 * / final public class tree extends node {/ ** * NonP point maximum ID * / Private static int maxid = 0;

Private trees () {super (getmaxid (), null;}

Private Tree (int id) {super (id, null); maxid = ID ;}

Private static int getmaxid () {return maxid ;}

/ ** * Create an empty directory tree. * @Return Tree empty directory tree * / public static tree gettree () {return new tree ();}

/ ** * Add a node to the directory tree. * All nodes in the directory tree are preferably the same. * @Param Parent Node Father Node * @Param Value Object Node's value * @Return Node Add Node * / Public Node AddNode (Node Parent, Object Value) {Node RS = New Node (getmaxid (), PARENT) Rs.setValue (value); Return RS;} / ** * Create a directory tree. * If the object implements interface {@link container}, you can join the directory tree through this method. * The directory tree created by this method, all of the nodes are generated by the system. Obj will be directly used as root nodes, and the root node call method * {@link node # getValue ()} will get OBJ. * @Param Obj Object directory tree root node * @Param container Container gets the subject's Sub-object interface * @return Tree directory tree * / public static tree gettree (Object obj, container container {Tree RS = New tree (); rs.setValue (obj); object [] o = container.getChilds (OBJ); for (int i = 0; i

Private static void addnode (node ​​n) {node node = new node (getmaxid (), n); node.setValue (obj); object [] o = container.getChilds (obj); for (int i = 0; i

/ ** * Create a directory tree. * If the object implements interface {@link container}, you can join the directory tree through this method. * The directory tree created by this method, all nodes of the ID are generated by the system, and the root node call method {@link node # getValue ()} * will get NULL. * OBJ array will directly be the direct sub-node of root nodes. * @Param Object direct sub-node of root nodes of the directory tree. * @Param Container Container Get the object of the child object of the object * @return Tree directory tree * / public static tree gettree (Object obj [], container container {Tree Rs = New Tree (); for (int i = 0; i

/ ** * Create a directory tree. * As long as a set of object implements interface {@link treenode}, and you can join the directory tree. * The directory tree obtained by this method, its root node ID value is rootid, and the value of other nodes is the ID of the object of the interface TREENODE. * If the root node is included in Treenode, the value of the root node can be obtained by the method {@link node # getValue ()}, the return is NULL. * Treenode may have no order, but the ID of the parent node must be larger than the sub-node. * @Param Treenode Treenode [] Makes a directory tree * @Param rootid int root node ID * @Return Tree creation directory tree * / public static tree gettree (Treenode [] Treenode, int rootid) {Tree RS = New Tree (rootid); arraylist list = new arraylist (); for (int i = 0; i

/ ** * Find the node that identifies the ID from the directory tree. * @Param ID String Node ID * @Return Node ID (Node NULL) * / public node getnode (int id) { IF (id == getId ()) {return this;} return getnode (getChilds (), id);} private statino getnode (iterator it, int id) {// lookup Node While (it.hasnext ()) {Node n = (node) it.next (); if (n.GetId () == id) {return n;} if (n.getchildsnumber ()> 0) {n = getnode (N.GetChilds (), ID); if (n! = null) {return n;}}}} Return NULL;}

/ ** * Sort by the directory tree * @Param COM Comparator sort interface * / public void sort (Comparator COM) {sort (child (childs, com);}

Private Void Sort (ArrayList Childs, Comparator COM) {// Pair of Son Node Sort Collectes.Sort (CHILDS, COM); for (int i = 0; i 1) {sort (n.childs, com);}}}

/ ** * Get a list of nodes to meet the conditions. * @Param Filter Nodefilter Node Filter * @Return Iterator Node list (Storage Node Object) * / Public Iterator GetNodelist (Nodefilter Filter) {ArrayList RS = New ArrayList () GetNodelist (Childs, Filter, RS); Return Rs.Itemrator ();

Private Void GetNodelist (ArrayList Childs, Nodefilter Filter, ArrayList RS) {// Retrieves Node for Meet Condition (INT I = 0; I

}

Class Compare Implements Comparator // Press ID Sort by ID {Public Compare () {}

Public int compare (object obj1, object obj2) {int ID1 = ((TREENODE) ​​OBJ1) .GETID (); int ID2 = ((Treenode) Obj2) .GETID (); Return ID1 - ID2;}}} import java.util .Arraylist; import java.util.iterator; / ** * A node of the directory tree. * Its main attribute has a node identifier, the Father's node, its level in the directory tree (root node point 0), the value of the node, the sub-node. *

Copyright: Copyright (c) 2004 * @author flyxxxxx * @version 1.0 * / public class Node {private int id; private Node parent; private int level; private Object value; protected ArrayList childs = new Arraylist ();

/ ** * Constructor * @Param ID INT Node ID * @Param Parent Node Father Node * / Node (INT ID, Node Parent) {this.id = ID; if (Parent! = Null) {this.parent = Parent; parent.childs.add (this); this.Level = Parent.getlevel () 1;} else {level = 0;}}

/ ** * Get the node ID. * @Return Int Node ID * / public int getId () {return ID;}

/ ** * Get the hierarchy of the node in the directory tree. * Among them, the root node is 0, the son node of the root node is 1, and push the @return Int node in the directory tree * / final public Int getlevel () {return level;}

/ ** * Get the value of the node. * Is also the object of the TREENODE interface referenced * @return Object GetValue () {Return Value;}

/ ** * Set the value of the node. * / Final void setValue (Object value) {this.value = value;}

/ ** * Get a list of sub-nodes. * Iterator stores Node object * @return Iterator sub-node list * / final public itrator getChilds () {return childs.ITerator ();}

/ ** * Get the number of sub-nodes. * @Return INT sub-point number * / final public int getChildsNumber () {return childs.size ();}

/ ** * Is there a sub-node. * @Return Boolean has a child node return True * / final public boolean Haschilds () {returnch childs.size ()> 0;}

/ ** * Get the formation point. * If the node is rooted point, return null * @return node father node * / final public node getparent () {return parent;} / ** * Get a level of the first level Point. * @Param Level int Father's hierarch (Level is equal to 0, less than this node) * @return node 百家 级 级 * / final public node getparent (int level) {ix (Level < 0 || Level> = this.level) {Throw new arrayindexoutofboundsexception ("Level IS ERROR);} node n = parent; for (int i = 1; i

/ ** * Get the relative position of the node in the same level. * @Return INT Node relative position in the same level * / final public int getPosition () {if (parent == null) {return 0; } Return parent.childs.indexof (this);

/ ** * Node is the last one of the same level node. * @Return Boolean is Return true * / final public bolean islast () {if (parent == null) {Return True;} return getPosition () == Parent.childs.size () - 1;}

/ ** * Node is the first one of the same level. * @Return Boolean is Return true * / final public boolean isfirst () {return getPosition () == 0;}

/ ** * Get the next node in the directory tree. * If this node is the last node of the directory tree, return null * @return node Next Node * / Final Public Node getNext () {ix (childs.size ()> 0) {return (node) child.get (0);} node n = parent; while (n! = Null) {node node = n.Getnextsibling (); if (node! = Null) {Return Node } N = N.GetParent ();} return null;}

/ ** * Get the next class of syndrome. * No next level node returns null * @return node Next episode * / final public node getnextsibling () {i (parent == null) {Return NULL;} int K = getPosition (); if (k == parent.getchildsnumber () - 1) {return null;} return (node) parent.childs.get (k 1);} / ** * get before A same level node. * No previous syndrome Node Back Null * @return Node front one syndrome Node * / final public node getprevioussibling () {int K = getPosition (); if (k == 0) { Return null;} return (node) Parent.Childs.get (k - 1);

/ ** * Get the previous node. * The former node of the root node is null * @return node Previous Node * / final public node getprevious () {node n = getprevioussibling (); if (n! = NULL) {RETURN N;} Return Parent;

}

/ ** * Node filter interface. * This interface is used to find nodes that meet certain conditions from the directory tree. *

Copyright: CopyRight (c) 2004 * @Author flyxxxxx * @version 1.0 * / public interface nodefilter {/ ** * Judgment Node meets a certain condition. * @Param n node The node to judge * @Return Boolean meets the condition to return true * / public boilean accept (node ​​n);}

/ ** * A node of the directory tree. * To convert a set of objects into a directory tree, it must implement this interface or {@Link Container interface. * By implementing the directory tree of this interface, the identity of each node of the directory tree is equal to the identity of the corresponding object of this interface. * A node is the most important attribute is its identity and its identity of its superior directory. *

Copyright: CopyRight (C) 2004 * @AuThor flyxxxxx * @version 1.0 * /

Public interface treenode {

/ ** * Get the identity of this sink * @Return String Node ID * / public int GetID ();

/ ** * Get the identity of the father's node * @Return String the logo of the Father * / public int getParentId ();

} / ** * Container interface. * To convert a group or an object into a directory tree, it must implement this interface or {@Link Treenode} interface. * The container can include a sub-node, which gets a sub-node of an object by method {@Link #getchilds (java.lang.object)}. * The value of each node in the directory tree will reference the object points to this container, that is, by calling method * {@link node # getValue ()} will be obtained. * such as: Tree T = Tree.gettree (New File ("D: /"), New FileContainer ()); * Each node in t, call method to it {@ Link node # getValue ()} will get a File object. *

Copyright: CopyRight (c) 2004 *

* @Author flyxxxxx * @version 1.0 * / public interface container {/ ** * get all the objects Sub-object. * @Param Obj Object parent object * @Return Object [] child object list * / public object [] getChilds (object obj);} import java.util.ArrayList; import org.w3c.dom.ement; import Org .w3c.dom.nodelist; import org.w3c.dom.node;

/ ** * XML document container. * Translate an XML file into a directory tree, each node in this directory tree will correspond to a Element in the XML file, which is the Document * node corresponding to the directory tree The root node is pushed. * The value of each node of the directory tree ({@Link Node # getValue ()} is Element, the ID of the node is generated by the system, the root node is 0. * How to use: Tree T = New Tree (Document Document, New DocumentContainer ()); wherein document is an XML document element. *

Copyright: CopyRight (c) 2004 *

* @Author flyxxxx * @version 1.0 * / final public class documentcontainer imports Container {/ ** * XML document container constructor * / public documentcontainer () {}

/ ** * Get all the child objects of the object. * @Param obj object parent (type element) * @return object [] sub-object list (type element []) * / public object [] getChilds (Object obj) { IF (Obj InstanceOf Element) {ArrayList (); Nodelist List = ((Element) Obj) .getChildNodes (); for (INT i = 0; i

/ ** * File container. Transforms a disk directory into a directory tree, and each node in this directory tree will correspond to a directory or file in the disk. * The value of each node of the directory tree ({@link node # getValue ()} is file, the ID of the node is generated by the system, the root node is 0. * How to use: Tree T = New Tree (File F, New FileContainer ()); *

Copyright: CopyRight (C) 2004 *

* @Author flyxxxxx * @version 1.0

* / Final Public Class FileContainer Implements Container {Private FileFilter Filter;

/ ** * Default container constructor * / public filecontainer () {}

/ ** * Structure of file filters. * In this way, there will be no directory or files that do not meet the conditions or files * @Param Filter Filefilter file filter * / public filecontainer {THIS. Filter = filter;

/ ** * Get all the sub objects of the object. * @Param obj object parent (type file) * @Return Object [] sub-object list (type file []) * / public object [] getChilds (Object obj) { IF (Obj InstanceOf File) {File F = (File) OBJ; IF (F.IsFile ()) {Return New Object [0];} if (filter == null) {Return F.ListFiles ();} else { Return F.ListFiles (Filter);}} throw new IllegalargumentException ("Required Param Type is java.io.file.");}}

<% @ Page ContentType = "Text / HTML; Charset = GBK"%> <% @ Page Import = "..."%> // Univergent package <%! static hashtable images = new hashtable (); static hashtable actions = new hashtable (); static string script; static {images.put ("image_plus", "images / plus.gif"); Images.put ("Image_Plus_Last", "ImageS / Pluslast.gif"); images.put "Image_minus", "images / minus.gif"); images.put ("image_minus_last", "images / minlast.gif"); images.put ("image_midblk", "images / midblk.gif"); images.put ("Image_blank.gif"); Images.put ("Image_Lastblk", "Images / Lastblk.gif"); Images.put ("Image_Line", "Images / line.gif");. PUT ("image_folder", "images / folder.gif"); images.put ("images_folder_open", "images / folderopen.gif"); stringbuffer sc = new stringbuffer ("