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 | 162 | 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 | 418 | if ( isList( line ) ) |
46 | |
{ |
47 | 62 | return true; |
48 | |
} |
49 | |
|
50 | 356 | return false; |
51 | |
} |
52 | |
|
53 | |
|
54 | |
public Block visit( String line, ByLineSource source ) |
55 | |
throws ParseException |
56 | |
{ |
57 | 60 | TreeListBuilder treeListBuilder = new TreeListBuilder(); |
58 | |
|
59 | 60 | StringBuffer text = new StringBuffer(); |
60 | |
|
61 | |
do |
62 | |
{ |
63 | 180 | if ( line.trim().length() == 0 ) |
64 | |
{ |
65 | 46 | break; |
66 | |
} |
67 | |
|
68 | 134 | if ( text.length() > 0 && isList( line ) ) |
69 | |
{ |
70 | |
|
71 | 58 | addItem( treeListBuilder, text ); |
72 | |
} |
73 | |
|
74 | 134 | if ( text.length() == 0 ) |
75 | |
{ |
76 | 118 | text.append( line.trim() ); |
77 | |
} |
78 | |
else |
79 | |
{ |
80 | 16 | text.append( " " + line.trim() ); |
81 | |
} |
82 | |
|
83 | |
} |
84 | 134 | while ( ( line = source.getNextLine() ) != null ); |
85 | |
|
86 | 60 | if ( text.length() > 0 ) |
87 | |
{ |
88 | 60 | addItem( treeListBuilder, text ); |
89 | |
} |
90 | |
|
91 | 60 | return treeListBuilder.getBlock(); |
92 | |
} |
93 | |
|
94 | |
private void addItem( TreeListBuilder treeListBuilder, StringBuffer text ) |
95 | |
{ |
96 | 118 | String item = text.toString(); |
97 | 118 | int level = getLevel( item ); |
98 | |
|
99 | 118 | if ( isBulletedList( item, level - 1 ) ) |
100 | |
{ |
101 | 96 | treeListBuilder.feedEntry( BULLETED_LIST, level, item.substring( level ) ); |
102 | |
} |
103 | |
else |
104 | |
{ |
105 | 22 | treeListBuilder.feedEntry( NUMBERED_LIST, level, item.substring( level ) ); |
106 | |
} |
107 | 118 | text.setLength( 0 ); |
108 | 118 | } |
109 | |
|
110 | |
private int getLevel( String line ) |
111 | |
{ |
112 | 118 | int level = 0; |
113 | |
|
114 | 256 | while ( line.charAt( level ) == '*' || line.charAt( level ) == '-' || line.charAt( level ) == '#' ) |
115 | |
{ |
116 | 138 | level++; |
117 | |
} |
118 | |
|
119 | 118 | return level; |
120 | |
} |
121 | |
|
122 | |
private boolean isBulletedList( String line, int deph ) |
123 | |
{ |
124 | 118 | return ( line.charAt( deph ) == '*' || line.charAt( deph ) == '-' ); |
125 | |
} |
126 | |
|
127 | |
private boolean isList( String line ) |
128 | |
{ |
129 | 492 | line = line.trim(); |
130 | |
|
131 | 492 | if ( line.startsWith( "*" ) || line.startsWith( "-" ) || line.startsWith( "#" ) ) |
132 | |
{ |
133 | 126 | String temp = line.substring( 1 ); |
134 | 150 | while ( temp.length() > 0 && ( temp.charAt( 0 ) == '*' || temp.charAt( 0 ) == '-' || temp.charAt( 0 ) == '#' ) ) |
135 | |
{ |
136 | 24 | temp = temp.substring( 1 ); |
137 | |
} |
138 | |
|
139 | 126 | if ( temp.length() > 0 && temp.charAt( 0 ) == ' ' ) |
140 | |
{ |
141 | 120 | return true; |
142 | |
} |
143 | |
} |
144 | |
|
145 | 372 | return false; |
146 | |
} |
147 | |
} |