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.List; |
24 | |
import java.util.regex.Matcher; |
25 | |
import java.util.regex.Pattern; |
26 | |
|
27 | |
import org.apache.maven.doxia.parser.ParseException; |
28 | |
import org.apache.maven.doxia.util.ByLineSource; |
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | 142 | public class VerbatimBlockParser |
38 | |
implements BlockParser |
39 | |
{ |
40 | |
|
41 | |
|
42 | |
|
43 | 1 | private static final Pattern VERBATIM_START_PATTERN = Pattern.compile( "\\s*<verbatim>" ); |
44 | |
|
45 | 1 | private static final Pattern VERBATIM_END_PATTERN = Pattern.compile( "</verbatim>" ); |
46 | |
|
47 | |
|
48 | |
public final boolean accept( final String line ) |
49 | |
{ |
50 | 45 | return VERBATIM_START_PATTERN.matcher( line ).lookingAt(); |
51 | |
} |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
public final Block visit( final String line, final ByLineSource source ) |
57 | |
throws ParseException |
58 | |
{ |
59 | 3 | if ( !accept( line ) ) |
60 | |
{ |
61 | 0 | throw new IllegalAccessError( "call accept before this ;)" ); |
62 | |
} |
63 | |
|
64 | 3 | final List<Block> lines = new ArrayList<Block>(); |
65 | 3 | Matcher matcher = VERBATIM_START_PATTERN.matcher( line ); |
66 | 3 | matcher.find(); |
67 | 3 | String l = line.substring( matcher.end() ); |
68 | |
|
69 | 10 | while ( l != null ) |
70 | |
{ |
71 | 10 | matcher = VERBATIM_END_PATTERN.matcher( l ); |
72 | 10 | if ( matcher.find() ) |
73 | |
{ |
74 | 3 | lines.add( new TextBlock( l.substring( 0, matcher.start() ) + "\n" ) ); |
75 | 3 | break; |
76 | |
} |
77 | 7 | lines.add( new TextBlock( l + "\n" ) ); |
78 | 7 | l = source.getNextLine(); |
79 | |
} |
80 | |
|
81 | 3 | return new VerbatimBlock( lines.toArray( new Block[] {} ) ); |
82 | |
} |
83 | |
} |