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.HashMap; |
24 | |
import java.util.List; |
25 | |
import java.util.Map; |
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
|
32 | |
|
33 | 95 | public class FormatedTextParser |
34 | |
{ |
35 | |
|
36 | |
|
37 | |
|
38 | |
private TextParser textParser; |
39 | |
|
40 | |
|
41 | |
|
42 | |
|
43 | 1 | private static final Map<String, FormatBlockFactory> FACTORY_MAP = new HashMap<String, FormatBlockFactory>(); |
44 | |
|
45 | |
|
46 | |
|
47 | |
|
48 | 1 | private static final FormatBlockFactory BOLD_FACTORY = new FormatBlockFactory() |
49 | |
{ |
50 | |
|
51 | 1 | public Block createBlock( final Block[] childrens ) |
52 | |
{ |
53 | 11 | return new BoldBlock( childrens ); |
54 | |
} |
55 | |
}; |
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | 1 | private static final FormatBlockFactory ITALIC_FACTORY = new FormatBlockFactory() |
61 | |
{ |
62 | |
|
63 | 1 | public Block createBlock( final Block[] childrens ) |
64 | |
{ |
65 | 6 | return new ItalicBlock( childrens ); |
66 | |
} |
67 | |
}; |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | 1 | private static final FormatBlockFactory MONOSPACED_FACTORY = new FormatBlockFactory() |
73 | |
{ |
74 | |
|
75 | 1 | public Block createBlock( final Block[] childrens ) |
76 | |
{ |
77 | 5 | return new MonospaceBlock( childrens ); |
78 | |
} |
79 | |
}; |
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | 1 | private static final FormatBlockFactory BOLDITALIC_FACTORY = new FormatBlockFactory() |
85 | |
{ |
86 | |
|
87 | 1 | public Block createBlock( final Block[] childrens ) |
88 | |
{ |
89 | 2 | return new BoldBlock( new Block[] { new ItalicBlock( childrens ) } ); |
90 | |
} |
91 | |
}; |
92 | |
|
93 | |
|
94 | |
|
95 | |
|
96 | 1 | private static final FormatBlockFactory BOLDMONO_FACTORY = new FormatBlockFactory() |
97 | |
{ |
98 | |
|
99 | 1 | public Block createBlock( final Block[] childrens ) |
100 | |
{ |
101 | 2 | return new BoldBlock( new Block[] { new MonospaceBlock( childrens ) } ); |
102 | |
} |
103 | |
}; |
104 | |
|
105 | |
|
106 | |
|
107 | |
|
108 | 1 | private static final String[] SPECIAL_CHAR = new String[] { "__", "==", "*", "_", "=" }; |
109 | |
|
110 | |
static |
111 | |
{ |
112 | 1 | FACTORY_MAP.put( "*", BOLD_FACTORY ); |
113 | 1 | FACTORY_MAP.put( "_", ITALIC_FACTORY ); |
114 | 1 | FACTORY_MAP.put( "=", MONOSPACED_FACTORY ); |
115 | 1 | FACTORY_MAP.put( "__", BOLDITALIC_FACTORY ); |
116 | 1 | FACTORY_MAP.put( "==", BOLDMONO_FACTORY ); |
117 | 1 | } |
118 | |
|
119 | |
|
120 | |
|
121 | |
|
122 | |
|
123 | |
final Block[] parse( final String line ) |
124 | |
{ |
125 | 71 | return parseFormat( line ).toArray( new Block[] {} ); |
126 | |
} |
127 | |
|
128 | |
|
129 | |
|
130 | |
|
131 | |
|
132 | |
static boolean isSpace( final char c ) |
133 | |
{ |
134 | 106 | return c == ' ' || c == '\t'; |
135 | |
} |
136 | |
|
137 | |
|
138 | |
|
139 | |
|
140 | |
|
141 | |
static boolean isSpecial( final char c ) |
142 | |
{ |
143 | 90 | boolean ret = false; |
144 | |
|
145 | 495 | for ( int i = 0; !ret && i < SPECIAL_CHAR.length; i++ ) |
146 | |
{ |
147 | 405 | if ( SPECIAL_CHAR[i].charAt( 0 ) == c ) |
148 | |
{ |
149 | 14 | ret = true; |
150 | |
} |
151 | |
} |
152 | |
|
153 | 90 | return ret; |
154 | |
} |
155 | |
|
156 | |
|
157 | |
|
158 | |
|
159 | |
|
160 | |
|
161 | |
|
162 | |
|
163 | |
|
164 | |
private List<Block> parseFormat( final String line ) |
165 | |
{ |
166 | 149 | final List<Block> ret = new ArrayList<Block>(); |
167 | 149 | final int[] lhOffsets = new int[SPECIAL_CHAR.length]; |
168 | 149 | final int[] rhOffsets = new int[SPECIAL_CHAR.length]; |
169 | |
|
170 | |
|
171 | 894 | for ( int i = 0; i < SPECIAL_CHAR.length; i++ ) |
172 | |
{ |
173 | 745 | final int specialLen = SPECIAL_CHAR[i].length(); |
174 | 745 | int t = 0; |
175 | |
|
176 | 786 | while ( t != -1 && ( t = line.indexOf( SPECIAL_CHAR[i], t ) ) != -1 ) |
177 | |
{ |
178 | |
|
179 | 83 | if ( t == 0 || isSpace( line.charAt( t - 1 ) ) || isParenthesis( line.charAt( t - 1 ) ) ) |
180 | |
{ |
181 | |
|
182 | 57 | if ( t + specialLen < line.length() ) |
183 | |
{ |
184 | |
|
185 | |
|
186 | 55 | if ( isSpecial( line.charAt( t + specialLen ) ) ) |
187 | |
{ |
188 | 13 | t += specialLen; |
189 | |
} |
190 | |
else |
191 | |
{ |
192 | |
|
193 | |
break; |
194 | |
} |
195 | |
} |
196 | |
else |
197 | |
{ |
198 | 2 | t = -1; |
199 | |
} |
200 | |
} |
201 | |
else |
202 | |
{ |
203 | 26 | t += specialLen; |
204 | |
} |
205 | |
} |
206 | 745 | lhOffsets[i] = t; |
207 | |
} |
208 | |
|
209 | |
|
210 | 894 | for ( int i = 0; i < lhOffsets.length; i++ ) |
211 | |
{ |
212 | 745 | final int specialLen = SPECIAL_CHAR[i].length(); |
213 | |
|
214 | 745 | if ( lhOffsets[i] != -1 ) |
215 | |
{ |
216 | 42 | int t = lhOffsets[i] + specialLen; |
217 | |
|
218 | 44 | while ( ( t = line.indexOf( SPECIAL_CHAR[i], t ) ) != -1 ) |
219 | |
{ |
220 | |
|
221 | 36 | final char c = line.charAt( t - 1 ); |
222 | 36 | if ( t > 0 && !isSpace( c ) && !isSpecial( c ) ) |
223 | |
{ |
224 | 34 | break; |
225 | |
} |
226 | |
else |
227 | |
{ |
228 | 2 | t += specialLen; |
229 | |
} |
230 | 2 | } |
231 | 42 | rhOffsets[i] = t; |
232 | |
} |
233 | |
} |
234 | |
|
235 | |
|
236 | 149 | int minIndex = -1; |
237 | 149 | int charType = 0; |
238 | 894 | for ( int i = 0; i < lhOffsets.length; i++ ) |
239 | |
{ |
240 | 745 | if ( lhOffsets[i] != -1 && rhOffsets[i] != 1 ) |
241 | |
{ |
242 | 42 | if ( minIndex == -1 || lhOffsets[i] < minIndex ) |
243 | |
{ |
244 | 38 | if ( rhOffsets[i] > lhOffsets[i] ) |
245 | |
{ |
246 | |
|
247 | 30 | minIndex = lhOffsets[i]; |
248 | 30 | charType = i; |
249 | |
} |
250 | |
} |
251 | |
} |
252 | |
} |
253 | |
|
254 | 149 | if ( minIndex == -1 ) |
255 | |
{ |
256 | 123 | ret.addAll( textParser.parse( line ) ); |
257 | |
} |
258 | |
else |
259 | |
{ |
260 | 26 | int len = SPECIAL_CHAR[charType].length(); |
261 | 26 | ret.addAll( parseFormat( line.substring( 0, minIndex ) ) ); |
262 | 26 | ret.add( FACTORY_MAP.get( SPECIAL_CHAR[charType] ) |
263 | |
.createBlock( parseFormat( line.substring( minIndex + len, rhOffsets[charType] ) ) |
264 | |
.toArray( new Block[] {} ) ) ); |
265 | 26 | ret.addAll( parseFormat( line.substring( rhOffsets[charType] + len ) ) ); |
266 | |
} |
267 | |
|
268 | |
|
269 | 149 | return ret; |
270 | |
} |
271 | |
|
272 | |
|
273 | |
|
274 | |
|
275 | |
|
276 | |
private boolean isParenthesis( final char c ) |
277 | |
{ |
278 | 27 | return c == '(' || c == ')'; |
279 | |
} |
280 | |
|
281 | |
|
282 | |
|
283 | |
|
284 | |
|
285 | |
|
286 | |
|
287 | |
public final void setTextParser( final TextParser textParser ) |
288 | |
{ |
289 | 95 | if ( textParser == null ) |
290 | |
{ |
291 | 0 | throw new IllegalArgumentException( "argument can't be null" ); |
292 | |
} |
293 | |
|
294 | 95 | this.textParser = textParser; |
295 | 95 | } |
296 | |
} |
297 | |
|
298 | |
|
299 | |
|
300 | |
|
301 | |
|
302 | |
interface FormatBlockFactory |
303 | |
{ |
304 | |
|
305 | |
|
306 | |
|
307 | |
|
308 | |
|
309 | |
|
310 | |
Block createBlock( final Block[] childrens ); |
311 | |
} |