1 | |
package org.apache.maven.doxia.module.twiki.parser; |
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.Arrays; |
24 | |
import java.util.List; |
25 | |
import java.util.regex.Matcher; |
26 | |
import java.util.regex.Pattern; |
27 | |
|
28 | |
import org.apache.maven.doxia.util.ByLineSource; |
29 | |
import org.apache.maven.doxia.parser.ParseException; |
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | 95 | public class ParagraphBlockParser |
38 | |
implements BlockParser |
39 | |
{ |
40 | |
|
41 | |
|
42 | |
|
43 | 95 | private final Pattern paragraphSeparator = Pattern.compile( "^(\\s*)$" ); |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
private SectionBlockParser sectionParser; |
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
private GenericListBlockParser listParser; |
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
private FormatedTextParser textParser; |
59 | |
|
60 | |
|
61 | |
|
62 | |
|
63 | |
private HRuleBlockParser hrulerParser; |
64 | |
|
65 | |
|
66 | |
|
67 | |
|
68 | |
private TableBlockParser tableBlockParser; |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
private VerbatimBlockParser verbatimParser; |
74 | |
|
75 | |
|
76 | |
|
77 | |
|
78 | 1 | private static final NopBlock NOP = new NopBlock(); |
79 | |
|
80 | |
|
81 | |
public final boolean accept( final String line ) |
82 | |
{ |
83 | 28 | return !sectionParser.accept( line ) && !hrulerParser.accept( line ) && !verbatimParser.accept( line ); |
84 | |
} |
85 | |
|
86 | |
|
87 | |
|
88 | |
|
89 | |
public final Block visit( final String line, final ByLineSource source ) |
90 | |
throws ParseException |
91 | |
{ |
92 | 16 | StringBuffer sb = new StringBuffer(); |
93 | 16 | List<Block> childs = new ArrayList<Block>(); |
94 | |
|
95 | 16 | boolean sawText = false; |
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | 16 | boolean pre = false; |
102 | 16 | String l = line; |
103 | |
do |
104 | |
{ |
105 | 35 | Matcher m = paragraphSeparator.matcher( l ); |
106 | |
|
107 | 35 | if ( m.lookingAt() ) |
108 | |
{ |
109 | 11 | if ( sawText ) |
110 | |
{ |
111 | 5 | break; |
112 | |
} |
113 | |
} |
114 | |
else |
115 | |
{ |
116 | 24 | sawText = true; |
117 | |
|
118 | |
|
119 | 24 | if ( listParser.accept( l ) ) |
120 | |
{ |
121 | 2 | if ( sb.length() != 0 ) |
122 | |
{ |
123 | 1 | childs.addAll( Arrays.asList( textParser.parse( sb.toString().trim() ) ) ); |
124 | 1 | sb = new StringBuffer(); |
125 | |
} |
126 | 2 | childs.add( listParser.visit( l, source ) ); |
127 | |
} |
128 | 22 | else if ( tableBlockParser.accept( l ) ) |
129 | |
{ |
130 | 0 | childs.add( tableBlockParser.visit( l, source ) ); |
131 | |
} |
132 | |
else |
133 | |
{ |
134 | 22 | sb.append( l ); |
135 | |
|
136 | 22 | if ( l.indexOf( "<pre>" ) != -1 ) |
137 | |
{ |
138 | 0 | pre = true; |
139 | |
} |
140 | 22 | if ( l.indexOf( "</pre>" ) != -1 ) |
141 | |
{ |
142 | 0 | pre = false; |
143 | |
} |
144 | |
|
145 | 22 | if ( !pre ) |
146 | |
{ |
147 | 22 | sb.append( " " ); |
148 | |
} |
149 | |
else |
150 | |
{ |
151 | |
|
152 | 0 | sb.append( "\n" ); |
153 | |
} |
154 | |
} |
155 | |
} |
156 | 30 | l = source.getNextLine(); |
157 | |
} |
158 | 30 | while ( l != null && accept( l ) ); |
159 | |
|
160 | 16 | if ( line != null ) |
161 | |
{ |
162 | 16 | source.ungetLine(); |
163 | |
} |
164 | |
|
165 | 16 | if ( sb.length() != 0 ) |
166 | |
{ |
167 | 16 | childs.addAll( Arrays.asList( textParser.parse( sb.toString().trim() ) ) ); |
168 | 16 | sb = new StringBuffer(); |
169 | |
} |
170 | |
|
171 | 16 | if ( childs.size() == 0 ) |
172 | |
{ |
173 | 0 | return NOP; |
174 | |
} |
175 | |
|
176 | 16 | return new ParagraphBlock( childs.toArray( new Block[] {} ) ); |
177 | |
} |
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
public final void setSectionParser( final SectionBlockParser aSectionParser ) |
185 | |
{ |
186 | 95 | if ( aSectionParser == null ) |
187 | |
{ |
188 | 0 | throw new IllegalArgumentException( "arg can't be null" ); |
189 | |
} |
190 | 95 | this.sectionParser = aSectionParser; |
191 | 95 | } |
192 | |
|
193 | |
|
194 | |
|
195 | |
|
196 | |
|
197 | |
|
198 | |
public final void setListParser( final GenericListBlockParser aListParser ) |
199 | |
{ |
200 | 95 | if ( aListParser == null ) |
201 | |
{ |
202 | 0 | throw new IllegalArgumentException( "arg can't be null" ); |
203 | |
} |
204 | |
|
205 | 95 | this.listParser = aListParser; |
206 | 95 | } |
207 | |
|
208 | |
|
209 | |
|
210 | |
|
211 | |
|
212 | |
|
213 | |
|
214 | |
public final void setTextParser( final FormatedTextParser aTextParser ) |
215 | |
{ |
216 | 95 | if ( aTextParser == null ) |
217 | |
{ |
218 | 0 | throw new IllegalArgumentException( "arg can't be null" ); |
219 | |
} |
220 | 95 | this.textParser = aTextParser; |
221 | 95 | } |
222 | |
|
223 | |
|
224 | |
|
225 | |
|
226 | |
|
227 | |
|
228 | |
public final void setHrulerParser( final HRuleBlockParser aHrulerParser ) |
229 | |
{ |
230 | 95 | if ( aHrulerParser == null ) |
231 | |
{ |
232 | 0 | throw new IllegalArgumentException( "arg can't be null" ); |
233 | |
} |
234 | |
|
235 | 95 | this.hrulerParser = aHrulerParser; |
236 | 95 | } |
237 | |
|
238 | |
|
239 | |
|
240 | |
|
241 | |
|
242 | |
|
243 | |
public final void setTableBlockParser( final TableBlockParser aTableBlockParser ) |
244 | |
{ |
245 | 95 | if ( aTableBlockParser == null ) |
246 | |
{ |
247 | 0 | throw new IllegalArgumentException( "arg can't be null" ); |
248 | |
} |
249 | |
|
250 | 95 | this.tableBlockParser = aTableBlockParser; |
251 | 95 | } |
252 | |
|
253 | |
|
254 | |
|
255 | |
|
256 | |
|
257 | |
|
258 | |
|
259 | |
public final void setVerbatimParser( final VerbatimBlockParser aVerbatimParser ) |
260 | |
{ |
261 | 95 | if ( aVerbatimParser == null ) |
262 | |
{ |
263 | 0 | throw new IllegalArgumentException( "arg can't be null" ); |
264 | |
} |
265 | 95 | this.verbatimParser = aVerbatimParser; |
266 | 95 | } |
267 | |
} |