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
32
33 public class TreeListBuilder
34 {
35 private TreeComponent root;
36
37 private TreeComponent current;
38
39 TreeListBuilder()
40 {
41 root = new TreeComponent( null, "root", 0 );
42
43 current = root;
44 }
45
46 void feedEntry( int type, int level, String text )
47 {
48 int currentDepth = current.getDepth();
49
50 int incomingLevel = level - 1;
51
52 if ( incomingLevel == currentDepth )
53 {
54
55 }
56 else if ( incomingLevel > currentDepth )
57 {
58
59 List<TreeComponent> components = current.getChildren();
60
61 if ( components.size() == 0 )
62 {
63
64
65
66
67 for ( int i = 0, n = incomingLevel - currentDepth; i < n; i++ )
68 {
69 current = current.addChildren( "", type );
70 }
71 }
72 else
73 {
74 current = components.get( components.size() - 1 );
75 }
76 }
77 else
78 {
79 for ( int i = 0, n = currentDepth - incomingLevel; i < n; i++ )
80 {
81 current = current.getFather();
82
83 if ( current == null )
84 {
85 throw new IllegalStateException();
86 }
87 }
88 }
89 current.addChildren( text.trim(), type );
90 }
91
92 ListBlock getBlock()
93 {
94 return getList( root );
95 }
96
97 private ListBlock getList( TreeComponent treeComponent )
98 {
99 List<Block> list = getListItems( treeComponent );
100
101 int type = treeComponent.getChildren().get( 0 ).getType();
102
103 if ( type == ListBlockParser.BULLETED_LIST )
104 {
105 return new BulletedListBlock( list );
106 }
107
108 return new NumberedListBlock( list );
109 }
110
111 private List<Block> getListItems( TreeComponent tc )
112 {
113 List<Block> blocks = new ArrayList<Block>();
114
115 for ( TreeComponent child : tc.getChildren() )
116 {
117 List<Block> childBlocks = new ArrayList<Block>();
118
119 if ( child.getFather() != null )
120 {
121 childBlocks.addAll( new ChildBlocksBuilder( child.getText() ).getBlocks() );
122 }
123
124 if ( child.getChildren().size() != 0 )
125 {
126 blocks.add( new ListItemBlock( childBlocks, getList( child ) ) );
127 }
128 else
129 {
130 blocks.add( new ListItemBlock( childBlocks ) );
131 }
132 }
133
134 return blocks;
135 }
136 }