Java implementation Auto-numbering system (similar to automatic numbering in Word)

Source: Internet
Author: User
Tags gettext

Functional Requirements:

1, according to the number of chapters and the current title, to obtain the next number, to meet the different level number requirements. As with the AutoNumber feature in Word

2, can be implemented in Freemarker directly call the Generation method, get the correct number

Target: The number of the current chapter is obtained by means of a method call in the Freemarker code

11.11.222.12.1.12.1.22.22.2.133.13.1.13.1.1.13.2


Ideas:

1, the number is a tree structure on the whole, so you need to use the tree to manage the entire list

2, there is a method can be based on the maximum value, get the next (such as: the maximum value of 3, the next 4, the maximum value of four, the next five), this increment can be extended and redefined.

3, the delimiter between chapters can be specified and should be specified during the definition process.

Not much to say, on the code:

Public abstract class Number {public abstract string Producenext (String crrnt);p ublic string Rootnumber () {return "0";} Public String Firstnumber () {return "1";}}
Numbering abstract class, defining the number of element classes (e.g., two, three)

Implementation class:

public class SerialNumber extends number {@Overridepublic string Producenext (String crrnt) {string next = "0"; if (crrnt! = NULL) {try {int crrntnum = Integer.parseint (crrnt); next = string.valueof (Crrntnum + 1);} catch (Exception e) {System.err. println ("String of non-numeric type!");}} return next;}}

Node class:

public class Node implements comparable<node> {private string Id;private string Number;private string Text;private S Tring parentid;private int level;public Node (string ID, string number, string parentid, int level) {this.id = Id;this.numb ER = Number;this.parentid = Parentid;this.level = level;} Public Node () {}public String getId () {return ID;} public void SetId (String id) {this.id = ID;} Public String GetNumber () {return number;} public void Setnumber (String number) {this.number = number;} Public String GetText () {return text;} public void SetText (String text) {this.text = text;} Public String Getparentid () {return parentid;} public void Setparentid (String parentid) {this.parentid = ParentID;} public int Getlevel () {return level;} public void SetLevel (Int. level) {this.level = level;} @Overridepublic int compareTo (Node o) {if (this.level! = o.level) {return o.level-this.level;} else {if (This.number==nul L) {return-1;} Return This.number.compareTo (O.number);}} @Overridepublic String toString ({return "id=" + this.id + "pid=" + This.parentid + "number=" + this.number + "text=" + This.text + "\ r \ n";} 

Specific Operating Procedures:

public class Numbertree {private String id;private list<node> nodelist;private String separator;private int idseq;p Rivate number Number;public Numbertree () {init ();} Public Numbertree (String separator, number number) {init (); this.separator = Separator;this.number = number;} public void init () {idseq = 0;this.separator = "."; if (this.nodelist = = null) {nodeList = new arraylist<node> ();}} Public String GetId () {return ID;} public void Setserialid (String id) {this.id = ID;} Public String Getseparator () {return separator;} public void Setseparator (String separator) {this.separator = separator;} Public list<node> getnodelist () {return nodeList;} public void Setnodelist (list<node> nodeList) {this.nodelist = nodeList;} /** * * <p> function Description: Gets all child nodes based on parent node.  </p> * @param pnode * @return * @since JDK1.6. * <p> Date Created: 2015-4-23 8:20:47. </p> * <p> Update Date: [Date yyyy-mm-dd][change description]. </p> */public list<node> getchildnodes (Node pnode) {String pId = pnode.gEtid (); return getchildnodes (pId);} /** * * <p> function Description: Gets all child nodes based on parent node.  </p> * @param pId * @return * @since JDK1.6. * <p> Date Created: 2015-4-23 8:21:02. </p> * <p> Update Date: [Date yyyy-mm-dd][change description]. </p> */public list<node> getchildnodes (String pId) {list<node> childNodes = new arraylist<node> (); for (Node n:nodelist) {if (Pid.equals (N.getparentid ())) {Childnodes.add (n);}} return childNodes;} /** * * <p> function Description: The next largest node of this level.  </p> * @param level * @return * @since JDK1.6. * <p> Date Created: 2015-4-23 7:44:15. </p> * <p> Update Date: [Date yyyy-mm-dd][change description]. </p> */public node Generatenextnodeforthislevel (node node) {node nextnode = null; Node Maxnode = getmaxnodeforthislevel (node); String Nextnumber = Number.firstnumber (); int level = Node.getlevel (); if (Maxnode! = null &&! ") 0 ". Equals (Maxnode.getid ())) {//This level has child nodes, and non-root node Nextnumber = Number.producenext (Maxnode.getnumber ()); level = Maxnode.getlevel ();} NextNode = new Node (string.valueof(++IDSEQ), Nextnumber, Node.getid (), level) Generatenodetext (NextNode, Nextnumber); return nextnode;} /** * * <p> function Description: Gets the node with the largest value of this level.  </p> * @param level * @return * @since JDK1.6. * <p> Date Created: 2015-4-23 7:43:26. </p> * <p> Update Date: [Date yyyy-mm-dd][change description]. </p> */public node Getmaxnodeforthislevel (node Pnode) {list<node> childlist = Getchildnodes (Pnode); Node root = Getroot (nodeList), if (childlist.size () <= 0) {return null;} int level = Pnode.getlevel (); Node Maxnode = root;for (node node:childlist) {if (Maxnode.getnumber (). CompareTo (Node.getnumber ()) < 0) {Maxnode = no De;}} return maxnode;} /** * * <p> function Description: Gets nodes based on content.  </p> * @param text * @return * @since JDK1.6. * <p> Date Created: 2015-4-23 8:34:44. </p> * <p> Update Date: [Date yyyy-mm-dd][change description]. </p> */public node Getnodebytext (String text) {node node = null;for (node n:nodelist) {if (Text.equals (N.gettext ( )) {node = n;}} return node;} /** * * <p> function Description: Generates the next child node. </p&Gt  * Set to NULL * @return * @since JDK1.6 when generating the root node @param node parent or sibling node. * <p> Date Created: 2015-4-23 10:22:58. </p> */public node Generatenextchildnode (node node) {node NewNode = generatenextnodeforthislevel (node); return NewNode;} /** * * <p> function Description: Gets the parent node.  </p> * @param node * @return * @since JDK1.6. * <p> Date Created: 2015-4-23 8:44:50. </p> */public node Getparentnode (node node) {for (node N:nodelist) {if (Node.getparentid () = = N.getid ()) {return n ;}} return node;} /** * * <p> function Description: Generate node path.  </p> * @param node * @return * @since JDK1.6. * <p> Date Created: 2015-4-23 7:42:45. </p> * <p> Update Date: [Date yyyy-mm-dd][change description]. </p> */public void Generatenodetext (node node, String text) {if (node = = NULL | | "0". Equals (Node.getid ())) {return;} Node Pnode = getparentnode (node); 0 ". Equals (Pnode.getid ())) {text = Pnode.gettext () + separator + text;} Node.settext (text);} /** * * <p> function Description: Traverse all tree nodes.  </p> * @param node * @since JDK1.6. * &LT;p > Date Created: 2015-4-24 9:39:13. </p> * <p> Update Date: [Date yyyy-mm-dd][change description]. </p> */public void Traversenodelist (node node) {if (node==null) {node = Getroot (nodeList);} list<node> childNodes = Getchildnodes (Node); System.out.println (Node.gettext ()); if (childnodes.size () > 0) {for (node N:childnodes) {traversenodelist (n);}}} public static void Main (string[] args) {Number number = new SerialNumber (); Numbertree TreeNode = new Numbertree (".", number); Addsomenodes (TreeNode); treenode.traversenodelist (null);} /** * * <p> function Description: Gets the root node.  </p> * @param nodeList * @return * Author ZHANGXL * @since JDK1.6. * <p> Date Created: 2015-5-17 6:09:23. </p> */public node Getroot (list<node> nodeList) {Node root = null;if (nodelist.size () <= 0 | | (Root = Getnodebyid (NodeList, "0")) = = null) {root = Createroot (); Nodelist.add (root);} return root;} Private node Getnodebyid (list<node> nodeList, String ID) {Node node = null;if (Id!=null) {for (Node n:nodelist) {if (ID. Equals (N.getid ())) {node = N;break;}}} return node;} Private Node Createroot () {node root = new Node ("0", Number.rootnumber (), "1", 0); Root.settext ("0"); return root;} /** * * <p> Function Description: Test add node.  </p> * @return * @since JDK1.6. * <p> Date Created: 2015-4-24 10:53:22. </p> * <p> Update Date: [Date yyyy-mm-dd][change description]. </p> */private static node addsomenodes (Numbertree tree) {Node root = Tree.getroot (tree.nodelist); Node Node1 = Getnextnode (tree, root),//1node Node2 = Getnextnode (tree, root);//2node Node3 = Getnextnode (tree, root);//3no De node11 = Getnextnode (tree, Node1);//1.1node node12 = Getnextnode (tree, Node1);//1.2node node21 = Getnextnode (Tree, node 2);//2.1node node211 = Getnextnode (tree, NODE21);//2.1.1node node212 = Getnextnode (tree, NODE21);//2.1.2node Node22 = Get NextNode (tree, Node2);//2.2node node221 = Getnextnode (tree, NODE22);//2.2.1node node31 = Getnextnode (tree, NODE3); Node node32 = Getnextnode (tree, NODE3); Node node311 = Getnextnode (tree, node31); Node node3111 = Getnextnode (tree, node311); return root;} public static node Getnextnode (Numbertree Tree, node Pnode) {node node = Tree.generatenextchildnode (Pnode); if (Node! = nul L) {tree.nodeList.add (node);} return node;}}

Getnextnode (numbertree tree, Node pnode)
The method is designed to be static, so it is convenient to invoke in Freemarker. Register Numbertree to Freemarker, you can implement random call.

Datamap.put ("Numbertree", Ftlutil.gethelperclass ("Com.report.bctcms.number.NumberTree"));

public class Ftlutil {public static Templatehashmodel Usestaticpackage (String packagename) {Templatehashmodel Filestatics = null;try {Beanswrapper wrapper = Beanswrapper.getdefaultinstance (); Templatehashmodel staticmodels = Wrapper.getstaticmodels (); filestatics = (Templatehashmodel) staticModels.get ( PackageName);} catch (Exception e) {e.printstacktrace ();} return filestatics;} public static Templatehashmodel Gethelperclass (String clazz) {Templatehashmodel usestaticpackage = Ftlutil.usestaticpackage (clazz); return usestaticpackage;}}

Freemarker Code:

< #assign Numbertree = root.numbertree/>--Registered class < #assign numbertreeobj = root.numbertreeobj/>--Current Action Object <# Assign RootNode = Root.rootnode/><span style= "font-family:arial, Helvetica, Sans-serif;" >--from entering Freemarker node </span>< #assign num1 = Numbertree.getnextnode (numbertreeobj,rootnode)/>-- Generated Header object (Node)

Deficiencies:

1, temporarily only support the generation of child node number, does not support sibling node number

2, the generation of a numbered object requires more dependence, slightly cumbersome, but only in accordance with the current needs




Java implementation Auto-numbering system (similar to automatic numbering in Word)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.