Java-based automatic numbering system (similar to automatic numbering in word)

Source: Internet
Author: User

Java-based automatic numbering system (similar to automatic numbering in word)

Functional requirements:

1. Obtain the next number based on the number of chapters and the current title to meet the requirements of different levels of numbers, just like the automatic numbering function in word.

2. You can call the generation method directly in freemarker to obtain the correct number.

Objective: To obtain the number of the current chapter by calling a method 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 serial number is a tree structure. Therefore, you must use the tree to manage the entire list.

2. There is a way to obtain the next one based on the maximum value (for example, the maximum value 3, the next 4, the maximum value 4, and the next five). This incremental method can be expanded and redefined.

3. The delimiter between chapters can be specified. It should be specified during the definition process.

Not to mention, the above Code:

 

public abstract class Number {public abstract String produceNext(String crrnt);public String rootNumber() {return "0";}public String firstNumber() {return "1";}}
Number abstract class, which defines the number of element classes (such as 1, 2, 3, or 1, 2, and 3)

 

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 ("non-numeric string! ") ;}} Return next ;}}

Node class:

 

 

public class Node implements Comparable
 
   {private String id;private String number;private String text;private String parentId;private int level;public Node(String id, String number, String parentId, int level) {this.id = id;this.number = 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==null){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";}}
 

Procedure:

 

 

Public class NumberTree {private String id; private List
 
  
NodeList; private String separator; private int idSeq; private 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
  
   
() ;}} 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
   
    
GetNodeList () {return nodeList;} public void setNodeList (List
    
     
NodeList) {this. nodeList = nodeList ;}/****
     

Function Description: obtains all Child Nodes Based on the parent node.

* @ Param pNode * @ return * @ since JDK1.6. *

Creation date: 8:20:47.

*

Updated Date: [date YYYY-MM-DD] [Change description].

*/Public List GetChildNodes (Node pNode) {String pId = pNode. getId (); return getChildNodes (pId );}/****

Function Description: obtains all Child Nodes Based on the parent node.

* @ Param pId * @ return * @ since JDK1.6. *

Creation date: 8:21:02.

*

Updated Date: [date YYYY-MM-DD] [Change description].

*/Public List GetChildNodes (String pId) {List ChildNodes = new ArrayList (); For (Node n: nodeList) {if (pId. equals (n. getParentId () {childNodes. add (n) ;}} return childNodes ;}/****

Function Description: next to the maximum node of the current level.

* @ Param level * @ return * @ since JDK1.6. *

Creation date: 7:44:15.

*

Updated Date: [date YYYY-MM-DD] [Change description].

*/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 () {// child nodes exist at the current level, and non-root nodes nextNumber = number. produceNext (maxNode. getNumber (); level = maxNode. getLevel ();} nextNode = new Node (String. valueOf (++ idSeq), nextNumber, node. getId (), level); generateNodeText (nextNode, nextNumber); return nextNode ;}/****

Function Description: gets the node with the maximum value of this level.

* @ Param level * @ return * @ since JDK1.6. *

Creation date: 7:43:26.

*

Updated Date: [date YYYY-MM-DD] [Change description].

*/Public Node getMaxNodeForThisLevel (Node pNode) {List 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 = node ;}} return maxNode ;}/****

Function Description: obtains a node based on the content.

* @ Param text * @ return * @ since JDK1.6. *

Creation date: 8:34:44.

*

Updated Date: [date YYYY-MM-DD] [Change description].

*/Public Node getNodeByText (String text) {Node node = null; for (Node n: nodeList) {if (text. equals (n. getText () {node = n ;}} return node ;}/****

Function Description: generate the next subnode.

* @ Param node: when the parent node or sibling node generates the root node, it is set to null * @ return * @ since JDK1.6. *

Creation date: 10:22:58.

*/Public Node generateNextChildNode (Node node) {Node newNode = generateNextNodeForThisLevel (node); return newNode ;}/****

Function Description: gets the parent node.

* @ Param node * @ return * @ since JDK1.6. *

Creation date: 8:44:50.

*/Public Node getParentNode (Node node) {for (Node n: nodeList) {if (node. getParentId () = n. getId () {return n ;}} return node ;}/****

Function Description: generate a Node path.

* @ Param node * @ return * @ since JDK1.6. *

Creation date: 7:42:45.

*

Updated Date: [date YYYY-MM-DD] [Change description].

*/Public void generateNodeText (Node node, String text) {if (node = null | "0 ". equals (node. getId () {return;} Node pNode = getParentNode (node); if (! "0". equals (pNode. getId () {text = pNode. getText () + separator + text;} node. setText (text );}/****

Function Description: traverses all Tree nodes.

* @ Param node * @ since JDK1.6. *

Creation date: 9:39:13.

*

Updated Date: [date YYYY-MM-DD] [Change description].

*/Public void traverseNodeList (Node node) {if (node = null) {node = getRoot (nodeList);} List 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 );}/****

Function Description: gets the root node.

* @ Param nodeList * @ return * author zhangxl * @ since JDK1.6. *

Creation date: 6:09:23.

*/Public Node getRoot (List NodeList) {Node root = null; if (nodeList. size () <= 0 | (root = getNodeById (nodeList, "0") = null) {root = createRoot (); nodeList. add (root);} return root;} private Node getNodeById (List 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 ;}/****

Function Description: Test adding a node.

* @ Return * @ since JDK1.6. *

Creation date: 10:53:22.

*

Updated Date: [date YYYY-MM-DD] [Change description].

*/Private static Node addSomeNodes (NumberTree tree) {Node root = tree. getRoot (tree. nodeList); Node node1 = getNextNode (tree, root); // 1 Node node2 = getNextNode (tree, root); // 2 Node node3 = getNextNode (tree, root ); // 3 Node node11 = getNextNode (tree, node1); // 1.1 Node node12 = getNextNode (tree, node1); // 1.2 Node node21 = getNextNode (tree, node2 ); // 2.1 Node node211 = getNextNode (tree, node21); // 2.1.1Node nod E212 = getNextNode (tree, node21); // 2.1.2Node node22 = getNextNode (tree, node2); // 2.2 Node 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, Node, node311); return root;} public static Node getNextNode (NumberTree, Node pNode) {Node node = tree. generateNextChi LdNode (pNode); if (node! = Null) {tree. nodeList. add (node) ;}return node ;}}

getNextNode(NumberTree tree, Node pNode)
This method is designed as a static method to facilitate calling in freemarker. Registering NumberTree to freemarker can be called at will.

 

 

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 operation object <# assign rootNode = root. rootNode/> -- the actual node starting from entering freemarker <# assign num1 = numberTree. getNextNode (numberTreeObj, rootNode)/> -- the generated title object (Node)

Disadvantages:

 

1. Currently, only child node numbers can be generated. Brother node numbers are not supported.

2. Generating a numbered object requires a lot of dependencies, which is a little complicated.

 


 

Related Article

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.