1 package org.apache.maven.doxia.module.confluence.parser.list;
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22 import java.util.ArrayList;
23 import java.util.List;
24
25 import org.apache.maven.doxia.module.confluence.parser.Block;
26 import org.apache.maven.doxia.module.confluence.parser.ChildBlocksBuilder;
27
28
29
30
31 public class TreeListBuilder
32 {
33 private TreeComponent root;
34
35 private TreeComponent current;
36
37 TreeListBuilder()
38 {
39 root = new TreeComponent( null, "root", 0 );
40
41 current = root;
42 }
43
44 void feedEntry( int type, int level, String text )
45 {
46 int currentDepth = current.getDepth();
47
48 int incomingLevel = level - 1;
49
50 if ( incomingLevel == currentDepth )
51 {
52
53 }
54 else if ( incomingLevel > currentDepth )
55 {
56
57 List<TreeComponent> components = current.getChildren();
58
59 if ( components.size() == 0 )
60 {
61
62
63
64
65 for ( int i = 0, n = incomingLevel - currentDepth; i < n; i++ )
66 {
67 current = current.addChildren( "", type );
68 }
69 }
70 else
71 {
72 current = components.get( components.size() - 1 );
73 }
74 }
75 else
76 {
77 for ( int i = 0, n = currentDepth - incomingLevel; i < n; i++ )
78 {
79 current = current.getFather();
80
81 if ( current == null )
82 {
83 throw new IllegalStateException();
84 }
85 }
86 }
87 current.addChildren( text.trim(), type );
88 }
89
90 ListBlock getBlock()
91 {
92 return getList( root );
93 }
94
95 private ListBlock getList( TreeComponent treeComponent )
96 {
97 List<Block> list = getListItems( treeComponent );
98
99 int type = treeComponent.getChildren().get( 0 ).getType();
100
101 if ( type == ListBlockParser.BULLETED_LIST )
102 {
103 return new BulletedListBlock( list );
104 }
105
106 return new NumberedListBlock( list );
107 }
108
109 private List<Block> getListItems( TreeComponent tc )
110 {
111 List<Block> blocks = new ArrayList<>();
112
113 for ( TreeComponent child : tc.getChildren() )
114 {
115 List<Block> childBlocks = new ArrayList<>();
116
117 if ( child.getFather() != null )
118 {
119 childBlocks.addAll( new ChildBlocksBuilder( child.getText() ).getBlocks() );
120 }
121
122 if ( child.getChildren().size() != 0 )
123 {
124 blocks.add( new ListItemBlock( childBlocks, getList( child ) ) );
125 }
126 else
127 {
128 blocks.add( new ListItemBlock( childBlocks ) );
129 }
130 }
131
132 return blocks;
133 }
134 }