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.regex.Matcher; |
24 | |
import java.util.regex.Pattern; |
25 | |
|
26 | |
import org.apache.maven.doxia.util.ByLineSource; |
27 | |
import org.apache.maven.doxia.parser.ParseException; |
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | |
|
34 | |
|
35 | 95 | public class SectionBlockParser |
36 | |
implements BlockParser |
37 | |
{ |
38 | |
|
39 | |
|
40 | |
|
41 | 1 | private static final Pattern HEADER_DA = Pattern.compile( "^---([+]+)\\s*(.+)\\s*$" ); |
42 | |
|
43 | |
|
44 | |
|
45 | |
|
46 | |
private ParagraphBlockParser paraParser; |
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
private HRuleBlockParser hrulerParser; |
52 | |
|
53 | |
|
54 | |
private VerbatimBlockParser verbatimBlockParser; |
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
public final boolean accept( final String line ) |
60 | |
{ |
61 | 53 | return HEADER_DA.matcher( line ).lookingAt(); |
62 | |
} |
63 | |
|
64 | |
|
65 | |
|
66 | |
|
67 | |
public final Block visit( final String line, final ByLineSource source ) |
68 | |
throws ParseException |
69 | |
{ |
70 | 6 | final Matcher m = HEADER_DA.matcher( line ); |
71 | |
|
72 | 6 | if ( !m.lookingAt() ) |
73 | |
{ |
74 | 0 | throw new IllegalArgumentException( "don't know how to handle: " + line ); |
75 | |
} |
76 | |
|
77 | |
String newLine; |
78 | 6 | final ArrayList<Block> blocks = new ArrayList<Block>(); |
79 | |
|
80 | 14 | while ( ( newLine = source.getNextLine() ) != null && !accept( newLine ) ) |
81 | |
{ |
82 | 8 | if ( hrulerParser.accept( newLine ) ) |
83 | |
{ |
84 | 1 | blocks.add( hrulerParser.visit( newLine, source ) ); |
85 | |
} |
86 | |
else |
87 | |
{ |
88 | 7 | if ( verbatimBlockParser.accept( newLine ) ) |
89 | |
{ |
90 | 1 | blocks.add( verbatimBlockParser.visit( newLine, source ) ); |
91 | |
} |
92 | |
else |
93 | |
{ |
94 | 6 | blocks.add( paraParser.visit( newLine, source ) ); |
95 | |
} |
96 | |
} |
97 | |
} |
98 | |
|
99 | 6 | if ( newLine != null ) |
100 | |
{ |
101 | 1 | source.ungetLine(); |
102 | |
} |
103 | |
|
104 | 6 | return new SectionBlock( m.group( 2 ), getLevel( m.group( 1 ) ), blocks.toArray( new Block[] {} ) ); |
105 | |
} |
106 | |
|
107 | |
|
108 | |
|
109 | |
|
110 | |
|
111 | |
|
112 | |
static int getLevel( final String s ) |
113 | |
throws IllegalArgumentException |
114 | |
{ |
115 | 25 | for ( int i = 0, n = s.length(); i < n; i++ ) |
116 | |
{ |
117 | 18 | if ( s.charAt( i ) != '+' ) |
118 | |
{ |
119 | 1 | throw new IllegalArgumentException( "the argument must have only" + " '+' characters" ); |
120 | |
} |
121 | |
} |
122 | 7 | return s.length(); |
123 | |
} |
124 | |
|
125 | |
|
126 | |
|
127 | |
|
128 | |
|
129 | |
|
130 | |
public final void setParaParser( final ParagraphBlockParser paraParser ) |
131 | |
{ |
132 | 95 | if ( paraParser == null ) |
133 | |
{ |
134 | 0 | throw new IllegalArgumentException( "argument can't be null" ); |
135 | |
} |
136 | 95 | this.paraParser = paraParser; |
137 | 95 | } |
138 | |
|
139 | |
|
140 | |
|
141 | |
|
142 | |
|
143 | |
|
144 | |
public final void setHrulerParser( final HRuleBlockParser hrulerParser ) |
145 | |
{ |
146 | 95 | if ( hrulerParser == null ) |
147 | |
{ |
148 | 0 | throw new IllegalArgumentException( "argument can't be null" ); |
149 | |
} |
150 | 95 | this.hrulerParser = hrulerParser; |
151 | 95 | } |
152 | |
|
153 | |
|
154 | |
|
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
public final void setVerbatimBlockParser( VerbatimBlockParser verbatimBlockParser ) |
160 | |
{ |
161 | 96 | if ( verbatimBlockParser == null ) |
162 | |
{ |
163 | 0 | throw new IllegalArgumentException( "argument can't be null" ); |
164 | |
} |
165 | 96 | this.verbatimBlockParser = verbatimBlockParser; |
166 | 96 | } |
167 | |
} |