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