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 public class VerbatimBlockParser
38 implements BlockParser
39 {
40
41
42
43 private static final Pattern VERBATIM_START_PATTERN = Pattern.compile( "\\s*<verbatim>" );
44
45 private static final Pattern VERBATIM_END_PATTERN = Pattern.compile( "</verbatim>" );
46
47
48 public final boolean accept( final String line )
49 {
50 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 if ( !accept( line ) )
60 {
61 throw new IllegalAccessError( "call accept before this ;)" );
62 }
63
64 final List<Block> lines = new ArrayList<Block>();
65 Matcher matcher = VERBATIM_START_PATTERN.matcher( line );
66 matcher.find();
67 String l = line.substring( matcher.end() );
68
69 while ( l != null )
70 {
71 matcher = VERBATIM_END_PATTERN.matcher( l );
72 if ( matcher.find() )
73 {
74 lines.add( new TextBlock( l.substring( 0, matcher.start() ) + "\n" ) );
75 break;
76 }
77 lines.add( new TextBlock( l + "\n" ) );
78 l = source.getNextLine();
79 }
80
81 return new VerbatimBlock( lines.toArray( new Block[] {} ) );
82 }
83 }