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