★ Implement the directory tree with Java

xiaoxiao2021-03-06  53

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.? * Provide support from other objects to generate directory trees. • 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 are objects other than File or Document To generate a directory tree, implement the {@Link Treenode} or {@Link Container} interface. ? * For tables in SQL, suggestions are required for ID (ID), ParentID, other fields, and then generate an object that implements the TREENODE interface for each of the queries. ? * If the TREENODE interface is implemented:? * ?? Class Group imports treenode {}? * ?? group [] groups = ...? * ?? Tree T = tree.gettree (groups, 0); // where 0 For root node ID, and groups do 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 the directory tree. ? *

Copyright: CopyRight (C) 2004

? *

? * @Author flyxxxxx

? * @version 1.0

? * /

Final Public Class Tree

??? EXTENDS NODE

{

? / **

?? * Node's biggest identification

?? * /

? private static int maxid = 0;

PRIVATE TREE ()? {??? super (getmaxid (), null) ;?}

? private trees? {??? 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. ?? * The type of all nodes in the directory tree is preferably the same. ?? * @PARAM PARENT NODE Father 's Node ?? * @Param Value Object Node's value ?? * @Return Node Added Node (Node Parent, Object Value)? {?? ? Node = new node (getmaxid (), parent); ??? rs.setValue (value); ??? Return RS;?}

? / ** ?? * Create a directory tree.? * If the object implements the interface {@link container}, you can join the directory tree through this method. ?? * The directory tree created by this method, all the IDs of the Node are generated by the system. Obj will be directly used as root nodes, and the root node call method is * {@link node # getValue ()} will get OBJ. ?? * @Param obj object directory tree root node ?? * @Param Container Container gets the interface of the child object to the object ?? * @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

? / ** ?? * Create a directory tree.? * If the object implements the interface {@link container}, you can join the directory tree through this method. ?? * Create a directory tree created by this method, all node IDs are generated by the system, call the root node call method {@link node # getValue ()} ?? * will get NULL. ?? * The OBJ array will be directly subjected to the direct sub-node of the root node. * @Param Object direct sub-node of the root node of the directory tree. ?? * @Param Container Container Get the interface 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 objects implements interface {@link treenode}, and you can join the directory tree in the ID of each object. ?? * The directory tree obtained by this method, its root node ID value is rootid, 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 is certainly greater than the child node. ?? * @Param Treenode Treenode [] constitutes the node of the 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 (not found NULL) ?? * /? public node getnode (int ID)? {??? if (id == getId ()) ??? {????? Return this; ???} ??? Return GetNode (), ID );?}? private statino getnode (iterator it, int id)? {// lookup node ??? while (it.hasnext ()) ??? {????? node n = (node) IT .next (); ????? if (n.GetId () == id) ????? {??????} ????? f ( n.GetchildsNumber ()> 0) ????? {??????? n = getNode (n.GetChilds (), ID); ??????? ing (n! = null) ?? ????? {????????? Return N; ???????} ?????} ???}? ??? Return null ;?}

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

? Private void sort (arraylist childs, comparator com)? {// pair sub-node sorting ??? collections.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 = new arraylist (); ??? getnodelist (childs, filter, rs); ??? Return Rs.iterator () ;?}

? private void getnodelist (arraylist childs, nodefilter filter, ????????????????????????????????????????????????????????????????????????????????????????????? ??? for (int i = 0; i

}

Class Compare ??? IMPLEMENTS COURATOR / / Sort by ID Nogon {? 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;

/ **? * The directory tree has a node.? * Its main attribute has a node identifier, the Father's node, it is in the level of the directory tree (0), the node value, 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 = pent; ????? 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. The level of the middle * /? Final public int getlevel ()? {??? Return Level ;?}

? / ** ?? * Get the value of the node. ?? * is also the object referenced by the Treenode interface ?? * @Return Object Node value? * /? Final public object getValue ()? {??? Return Value ;?}

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

? / ** ?? * Get the list of sub-knots. ?? * Iterator stores the Node object ?? * @return Iterator sub-node list ?? * /? Final public iplicor getChilds ()? {??? Return Childs.ITerator () ;?}

? / ** ?? * Get the number of child nodes. ?? * @RETURN INT sub-node number ?? * /? Final public inte getchildsnumber ()? {??? Return childs.size () ;?}

? / ** ?? * Is there a sub-point. ?? * @Return Boolean has a child node to return True ?? * /? Final public boolean haschilds ()? {??? Return childs.size ()> 0; ?

? / ** ?? * Get the form of the father. ?? * If the node is root node, return null ?? * @return node father node ?? * /? Final public node getParent ()? {??? RETURN PARENT;?}? / ** ?? * Get the parent node of the level. ?? * @Param Level INT Father's hierarchy (Level is more equal to 0, less than this node) ?? * @return Node Sevel Level Father Node? * /? Final Public Node getParent (int level)? {??? if (level <0 || level> = this.level) ??? {????? throw new ArrayIndexOutofboundsexception ("Level IS Error."); ???} ??? node n = pent; ??? for (int i = 1; i

? / ** ?? * Get the relative position of the nodes in the same level. ?? * @RETURN INT Node relative position in the same level node ?? * /? Final public int getPosition ()? ? if (parent == null) ??? {????? Return 0; ???} ??? Return Parent.Childs.indexof (this) ;?}

? / ** ?? * Nogon is the last one of the same level. ?? * @Return Boolean is Return true ?? * /? Final public boolean islast ()? {??? == null ) ??? {????? Return True; ???} ??? Return getPosition () == parent.childs.size () - 1 ;?}

? / ** ?? * Nogout is the first one of the same level. ?? * @Return Boolean is Return true ?? * /? Final public bolean 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 ()? {??? if ()> 0) ??? {????? Return (Node) childs.get (0); ???} ??? Node n = pent ; ??? while (n! = null) ??? {????? node node = n.Getnextsibling (); ????? if (node! = null) ????? {??? ???? Return node; ?????} ????? n = N.GETParent (); ???} ??? Return null ;?}

? / ** ?? * Get the next class node. ?? * No next level node returns null ?? * @return node Next level node ?? * /? Final public node getNextSibling () ? {??? if (parent == null) ??? {????? Return null; ???} ??? int K = getPosition (); ??? IF (k == parent.getchildsnumber) ) - 1) ??? {????? Return null; ???} ??? Return (node) parent.childs.get (k 1) ;?}? / ** ?? * Get the previous one The same level node. ?? * No previous syndrome node return null ?? * @RETURN NODE ahead of the same level 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 front node of the root point is null ?? * @return node front node ?? * /? Final public node getprevious ()? {? ?? node n = getprevioussibling (); ??? if (n! = Null) ??? {????? Return n; ???} ??? Return Parent ;?}

}

/ **? * Node filter interface. * This interface is used to find the node that meets a certain condition from the directory tree. ? *

Copyright: CopyRight (C) 2004

? * @Author flyxxxxx

? * @version 1.0

? * /

Public Interface Nodefilter

{

? / **

?? * Judging whether the node satisfies certain conditions.

?? * @Param n node to judge the node

?? * @Return Boolean meets the condition Return true

?? * /

PUBLIC BOOLEAN Accept (Node N);

}

/ **? * A node of the directory tree.? * To convert a set of objects to the 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 identification 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 siding? * @Return String Node ID ?? * /? Public int GetId ();

? / ** ?? * Get the identity of the father's node ?? * @Return String the identity of the Father 's node ?? * /? 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 sub-nodes, which get 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 the method? * {@Link node # getValue ()} will be obtained. * Such as: Tree T = Tree.gettree (New File ("D: /"), New FileContainer ());? * Each node in t, to it, 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 child objects of the object.

?? * @Param Obj object parent object

?? * @Return Object [] sub-object list

?? * /

PUBLIC Object [] getChilds (Object Obj);

}

Import java.util.arraylist;

Import org.w3c.dom.element; import org.w3c.dom.nodelist; import org.w3c.dom.node;

/ **? * XML document container.? * Translate an XML file into a directory tree, this directory tree will correspond to a ELEMENT in the XML file, which is the root of the directory tree Node, push it accordingly. • 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.?? 使用: tree t = new Tree Document Document, New DocumentContainer ());? * Where Document is an XML document element.? *

Copyright: CopyRight (C) 2004

? *

? * @Author flyxxxxx

? * @version 1.0

? * /

Final Public Class DocumentContainer

? ??? IMPLEMENTS Container

{

? / **

?? * XML document container construction method

?? * /

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 = new arraylist (); ????? nodelist list = ((element) OBJ ); ????? for (int i = 0; i

/ **? * File container.? * Translate 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.?? 使用: 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 ()? {?}

• ** ?? * Constructor with file filters. ?? * Through this method, will there be a directory or file in the directory tree ?? * @Param Filter FileFilter file filter ?? * / PUBLIC FILTER FILTER? {??? this.filter = filter ;?}

? / ** ?? * Get all the child objects of the object. ?? * @Param obj object parent object (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]; ?????} ????? f (filter == null) ????? {?????? • Return f.listfiles (); ?????} ????? Else ????? {??????? Return F.ListFiles (filter); ?????}??? } ??? throw new IllegalargumentException ("Required Param Type is java.io.file.");?}}

?

JSP part of the reference

<% @ 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 / minslast.gif"); ??? images.put (" Image_midblk, "images / midblk.gif"); ??? images.put ("image_blank", "images / blank.gif"); ??? images.put ("image_lastblk", "images / lastblk.gif" ); ??? Images.put ("image_line", "images / line.gif"); ??? Images.put ("image_folder", "images / folder.gif"); ??? images.put (" Images_folder_open "," images / folderopen.gif "); ??? StringBuffer sc = new stringbuffer ("