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 org.apache.maven.doxia.util.ByLineSource;
23 import org.apache.maven.doxia.module.confluence.parser.Block;
24 import org.apache.maven.doxia.module.confluence.parser.BlockParser;
25 import org.apache.maven.doxia.parser.ParseException;
26
27
28
29
30
31
32 public class ListBlockParser
33 implements BlockParser
34 {
35
36 public static final int BULLETED_LIST = 0;
37
38
39 public static final int NUMBERED_LIST = 1;
40
41
42 public boolean accept( String line, ByLineSource source )
43 {
44 if ( isList( line ) )
45 {
46 return true;
47 }
48
49 return false;
50 }
51
52
53 public Block visit( String line, ByLineSource source )
54 throws ParseException
55 {
56 TreeListBuilder treeListBuilder = new TreeListBuilder();
57
58 StringBuilder text = new StringBuilder();
59
60 do
61 {
62 if ( line.trim().length() == 0 )
63 {
64 break;
65 }
66
67 if ( text.length() > 0 && isList( line ) )
68 {
69
70 addItem( treeListBuilder, text );
71 }
72
73 if ( text.length() == 0 )
74 {
75 text.append( line.trim() );
76 }
77 else
78 {
79 text.append( " " ).append( line.trim() );
80 }
81
82 }
83 while ( ( line = source.getNextLine() ) != null );
84
85 if ( text.length() > 0 )
86 {
87 addItem( treeListBuilder, text );
88 }
89
90 return treeListBuilder.getBlock();
91 }
92
93 private void addItem( TreeListBuilder treeListBuilder, StringBuilder text )
94 {
95 String item = text.toString();
96 int level = getLevel( item );
97
98 if ( isBulletedList( item, level - 1 ) )
99 {
100 treeListBuilder.feedEntry( BULLETED_LIST, level, item.substring( level ) );
101 }
102 else
103 {
104 treeListBuilder.feedEntry( NUMBERED_LIST, level, item.substring( level ) );
105 }
106 text.setLength( 0 );
107 }
108
109 private int getLevel( String line )
110 {
111 int level = 0;
112
113 while ( line.charAt( level ) == '*' || line.charAt( level ) == '-' || line.charAt( level ) == '#' )
114 {
115 level++;
116 }
117
118 return level;
119 }
120
121 private boolean isBulletedList( String line, int deph )
122 {
123 return ( line.charAt( deph ) == '*' || line.charAt( deph ) == '-' );
124 }
125
126 private boolean isList( String line )
127 {
128 line = line.trim();
129
130 if ( line.startsWith( "*" ) || line.startsWith( "-" ) || line.startsWith( "#" ) )
131 {
132 String temp = line.substring( 1 );
133 while ( temp.length() > 0
134 && ( temp.charAt( 0 ) == '*' || temp.charAt( 0 ) == '-' || temp.charAt( 0 ) == '#' ) )
135 {
136 temp = temp.substring( 1 );
137 }
138
139 if ( temp.length() > 0 && temp.charAt( 0 ) == ' ' )
140 {
141 return true;
142 }
143 }
144
145 return false;
146 }
147 }