1 | |
package org.apache.maven.doxia.module.twiki; |
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
|
19 | |
|
20 | |
|
21 | |
|
22 | |
import org.apache.maven.doxia.module.twiki.parser.Block; |
23 | |
import org.apache.maven.doxia.module.twiki.parser.BlockParser; |
24 | |
import org.apache.maven.doxia.module.twiki.parser.FormatedTextParser; |
25 | |
import org.apache.maven.doxia.module.twiki.parser.GenericListBlockParser; |
26 | |
import org.apache.maven.doxia.module.twiki.parser.HRuleBlockParser; |
27 | |
import org.apache.maven.doxia.module.twiki.parser.ParagraphBlockParser; |
28 | |
import org.apache.maven.doxia.module.twiki.parser.SectionBlock; |
29 | |
import org.apache.maven.doxia.module.twiki.parser.SectionBlockParser; |
30 | |
import org.apache.maven.doxia.module.twiki.parser.TableBlockParser; |
31 | |
import org.apache.maven.doxia.module.twiki.parser.TextParser; |
32 | |
import org.apache.maven.doxia.module.twiki.parser.VerbatimBlockParser; |
33 | |
import org.apache.maven.doxia.module.twiki.parser.XHTMLWikiWordLinkResolver; |
34 | |
import org.apache.maven.doxia.parser.AbstractTextParser; |
35 | |
import org.apache.maven.doxia.parser.ParseException; |
36 | |
import org.apache.maven.doxia.sink.Sink; |
37 | |
import org.apache.maven.doxia.util.ByLineReaderSource; |
38 | |
import org.apache.maven.doxia.util.ByLineSource; |
39 | |
|
40 | |
import java.io.Reader; |
41 | |
import java.util.ArrayList; |
42 | |
import java.util.List; |
43 | |
|
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | |
|
49 | |
|
50 | |
|
51 | |
|
52 | |
|
53 | |
public class TWikiParser |
54 | |
extends AbstractTextParser |
55 | |
{ |
56 | |
private static final int EXTENSION_LENGTH = 6; |
57 | |
|
58 | |
|
59 | 104 | private final ParagraphBlockParser paraParser = new ParagraphBlockParser(); |
60 | |
|
61 | |
|
62 | 104 | private final SectionBlockParser sectionParser = new SectionBlockParser(); |
63 | |
|
64 | |
|
65 | 104 | private final GenericListBlockParser listParser = new GenericListBlockParser(); |
66 | |
|
67 | |
|
68 | 104 | private final FormatedTextParser formatTextParser = new FormatedTextParser(); |
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | |
|
74 | |
|
75 | 104 | private final TextParser textParser = new TextParser( new XHTMLWikiWordLinkResolver() ); |
76 | |
|
77 | |
|
78 | 104 | private final HRuleBlockParser hrulerParser = new HRuleBlockParser(); |
79 | |
|
80 | |
|
81 | 104 | private final TableBlockParser tableParser = new TableBlockParser(); |
82 | |
|
83 | |
|
84 | 104 | private final VerbatimBlockParser verbatimParser = new VerbatimBlockParser(); |
85 | |
|
86 | |
|
87 | |
private BlockParser[] parsers; |
88 | |
|
89 | |
|
90 | |
|
91 | |
|
92 | |
public TWikiParser() |
93 | 104 | { |
94 | 104 | init(); |
95 | 104 | } |
96 | |
|
97 | |
|
98 | |
|
99 | |
|
100 | |
|
101 | |
|
102 | |
|
103 | |
|
104 | |
public final List<Block> parse( final ByLineSource source ) |
105 | |
throws ParseException |
106 | |
{ |
107 | 20 | final List<Block> ret = new ArrayList<Block>(); |
108 | |
|
109 | |
String line; |
110 | 90 | while ( ( line = source.getNextLine() ) != null ) |
111 | |
{ |
112 | 70 | boolean accepted = false; |
113 | |
|
114 | 112 | for ( BlockParser parser : parsers ) |
115 | |
{ |
116 | 112 | if ( parser.accept( line ) ) |
117 | |
{ |
118 | 70 | accepted = true; |
119 | 70 | ret.add( parser.visit( line, source ) ); |
120 | 70 | break; |
121 | |
} |
122 | |
} |
123 | 70 | if ( !accepted ) |
124 | |
{ |
125 | 0 | throw new ParseException( "Line number not handle : " + source.getLineNumber() + ": " + line ); |
126 | |
} |
127 | 70 | } |
128 | |
|
129 | 20 | return ret; |
130 | |
} |
131 | |
|
132 | |
|
133 | |
public final synchronized void parse( final Reader source, final Sink sink ) |
134 | |
throws ParseException |
135 | |
{ |
136 | 6 | init(); |
137 | |
|
138 | |
List<Block> blocks; |
139 | 6 | final ByLineSource src = new ByLineReaderSource( source ); |
140 | |
|
141 | |
try |
142 | |
{ |
143 | 6 | blocks = parse( src ); |
144 | |
} |
145 | 0 | catch ( final Exception e ) |
146 | |
{ |
147 | |
|
148 | 0 | throw new ParseException( e, src.getName(), src.getLineNumber(), -1 ); |
149 | 6 | } |
150 | |
|
151 | 6 | sink.head(); |
152 | |
|
153 | 6 | final String title = getTitle( blocks, src ); |
154 | 6 | if ( title != null ) |
155 | |
{ |
156 | 4 | sink.title(); |
157 | 4 | sink.text( title ); |
158 | 4 | sink.title_(); |
159 | |
} |
160 | |
|
161 | 6 | sink.head_(); |
162 | 6 | sink.body(); |
163 | 6 | for ( Block block : blocks ) |
164 | |
{ |
165 | 50 | block.traverse( sink ); |
166 | |
} |
167 | 6 | sink.body_(); |
168 | 6 | sink.flush(); |
169 | 6 | sink.close(); |
170 | |
|
171 | 6 | setSecondParsing( false ); |
172 | 6 | init(); |
173 | 6 | } |
174 | |
|
175 | |
|
176 | |
|
177 | |
|
178 | |
|
179 | |
|
180 | |
|
181 | |
|
182 | |
|
183 | |
|
184 | |
|
185 | |
public final String getTitle( final List<Block> blocks, final ByLineSource source ) |
186 | |
{ |
187 | 12 | String title = null; |
188 | |
|
189 | 12 | for ( Block block : blocks ) |
190 | |
{ |
191 | 12 | if ( block instanceof SectionBlock ) |
192 | |
{ |
193 | 6 | final SectionBlock sectionBlock = (SectionBlock) block; |
194 | 6 | title = sectionBlock.getTitle(); |
195 | 6 | break; |
196 | |
} |
197 | |
} |
198 | |
|
199 | 12 | if ( title == null ) |
200 | |
{ |
201 | 6 | String name = source.getName(); |
202 | 6 | if ( name != null ) |
203 | |
{ |
204 | 6 | name = name.trim(); |
205 | |
|
206 | 6 | if ( name.length() != 0 ) |
207 | |
{ |
208 | 4 | if ( name.endsWith( ".twiki" ) ) |
209 | |
{ |
210 | 2 | title = name.substring( 0, name.length() - EXTENSION_LENGTH ); |
211 | |
} |
212 | |
else |
213 | |
{ |
214 | 2 | title = name; |
215 | |
} |
216 | |
} |
217 | |
} |
218 | |
} |
219 | |
|
220 | 12 | return title; |
221 | |
} |
222 | |
|
223 | |
|
224 | |
protected void init() |
225 | |
{ |
226 | 116 | super.init(); |
227 | |
|
228 | 116 | paraParser.setSectionParser( sectionParser ); |
229 | 116 | paraParser.setListParser( listParser ); |
230 | 116 | paraParser.setTextParser( formatTextParser ); |
231 | 116 | paraParser.setHrulerParser( hrulerParser ); |
232 | 116 | paraParser.setTableBlockParser( tableParser ); |
233 | 116 | paraParser.setVerbatimParser( verbatimParser ); |
234 | 116 | sectionParser.setParaParser( paraParser ); |
235 | 116 | sectionParser.setHrulerParser( hrulerParser ); |
236 | 116 | sectionParser.setVerbatimBlockParser( verbatimParser ); |
237 | 116 | listParser.setTextParser( formatTextParser ); |
238 | 116 | formatTextParser.setTextParser( textParser ); |
239 | 116 | tableParser.setTextParser( formatTextParser ); |
240 | |
|
241 | 116 | this.parsers = new BlockParser[] { sectionParser, hrulerParser, verbatimParser, paraParser }; |
242 | 116 | } |
243 | |
} |