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