1 |
|
|
2 |
|
|
3 |
|
|
4 |
|
|
5 |
|
|
6 |
|
|
7 |
|
|
8 |
|
|
9 |
|
|
10 |
|
|
11 |
|
|
12 |
|
|
13 |
|
|
14 |
|
|
15 |
|
|
16 |
|
|
17 |
|
|
18 |
|
package org.qedeq.kernel.bo.service.latex; |
19 |
|
|
20 |
|
import org.qedeq.base.io.TextInput; |
21 |
|
import org.qedeq.base.trace.Trace; |
22 |
|
import org.qedeq.kernel.bo.parser.MementoTextInput; |
23 |
|
|
24 |
|
|
25 |
|
|
26 |
|
|
27 |
|
@version |
28 |
|
@author |
29 |
|
|
|
|
| 0% |
Uncovered Elements: 236 (236) |
Complexity: 64 |
Complexity Density: 0,42 |
|
30 |
|
public final class LatexTextParser { |
31 |
|
|
32 |
|
|
33 |
|
private static final Class CLASS = LatexTextParser.class; |
34 |
|
|
35 |
|
|
36 |
|
private static final String SPECIALCHARACTERS = "(),{}\\~%$&"; |
37 |
|
|
38 |
|
|
39 |
|
private MementoTextInput input; |
40 |
|
|
41 |
|
|
42 |
|
private StringBuffer output; |
43 |
|
|
44 |
|
|
45 |
|
|
46 |
|
|
47 |
|
@param |
48 |
|
@return |
49 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0,5 |
|
50 |
0
|
public static final String transform(final String input) {... |
51 |
0
|
final LatexTextParser parser = new LatexTextParser(input); |
52 |
0
|
return parser.parse(); |
53 |
|
} |
54 |
|
|
55 |
|
|
56 |
|
|
57 |
|
|
58 |
|
@param |
59 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0,5 |
|
60 |
0
|
private LatexTextParser(final String input) {... |
61 |
0
|
this.input = new MementoTextInput(new TextInput(input)); |
62 |
0
|
this.output = new StringBuffer(); |
63 |
|
} |
64 |
|
|
65 |
|
|
66 |
|
|
67 |
|
|
68 |
|
@return |
69 |
|
|
|
|
| 0% |
Uncovered Elements: 41 (41) |
Complexity: 9 |
Complexity Density: 0,36 |
|
70 |
0
|
private String parse() {... |
71 |
0
|
while (!eof()) { |
72 |
0
|
final String token = readToken(); |
73 |
0
|
if ("\\begin".equals(token)) { |
74 |
0
|
final String curly = readCurlyBraceContents(); |
75 |
0
|
if ("eqnarray".equals(curly)) { |
76 |
0
|
printMathTillEnd(curly); |
77 |
0
|
} else if ("eqnarray*".equals(curly)) { |
78 |
0
|
printMathTillEnd(curly); |
79 |
0
|
} else if ("equation".equals(curly)) { |
80 |
0
|
printMathTillEnd(curly); |
81 |
0
|
} else if ("equation*".equals(curly)) { |
82 |
0
|
printMathTillEnd(curly); |
83 |
|
} else { |
84 |
0
|
print(token + "{" + curly + "}"); |
85 |
|
} |
86 |
0
|
} else if ("$$".equals(token)) { |
87 |
0
|
println(); |
88 |
0
|
println("<MATH>"); |
89 |
0
|
printMathTillToken(token); |
90 |
0
|
println("\\,</MATH>"); |
91 |
0
|
println(); |
92 |
0
|
} else if ("$".equals(token)) { |
93 |
0
|
print("<MATH>"); |
94 |
0
|
printMathTillToken(token); |
95 |
0
|
print("\\,</MATH>"); |
96 |
|
} else { |
97 |
0
|
print(token); |
98 |
|
} |
99 |
|
} |
100 |
0
|
return output.toString(); |
101 |
|
} |
102 |
|
|
|
|
| 0% |
Uncovered Elements: 14 (14) |
Complexity: 4 |
Complexity Density: 0,4 |
|
103 |
0
|
private void printMathTillEnd(final String curly) {... |
104 |
0
|
final StringBuffer buffer = new StringBuffer(); |
105 |
0
|
do { |
106 |
0
|
final String item = readToken(); |
107 |
0
|
if ("\\end".equals(item)) { |
108 |
0
|
final String curly2 = readCurlyBraceContents(); |
109 |
0
|
if (curly.equals(curly2)) { |
110 |
0
|
break; |
111 |
|
} |
112 |
0
|
buffer.append(item + "{" + curly2 + "}"); |
113 |
|
} else { |
114 |
0
|
buffer.append(item); |
115 |
|
} |
116 |
|
} while (true); |
117 |
|
|
118 |
|
|
119 |
|
|
120 |
|
|
121 |
|
|
122 |
|
|
123 |
|
|
124 |
0
|
printMath(buffer); |
125 |
|
} |
126 |
|
|
127 |
|
|
128 |
|
|
129 |
|
|
130 |
|
@param |
131 |
|
|
|
|
| 0% |
Uncovered Elements: 9 (9) |
Complexity: 3 |
Complexity Density: 0,43 |
|
132 |
0
|
private void printMathTillToken(final String token) {... |
133 |
0
|
final StringBuffer buffer = new StringBuffer(); |
134 |
0
|
do { |
135 |
0
|
final String item = readToken(); |
136 |
0
|
if (token.equals(item)) { |
137 |
0
|
break; |
138 |
|
} else { |
139 |
0
|
buffer.append(item); |
140 |
|
} |
141 |
|
} while (true); |
142 |
0
|
printMath(buffer); |
143 |
|
} |
144 |
|
|
145 |
|
|
146 |
|
|
147 |
|
|
148 |
|
@param |
149 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
150 |
0
|
private void printMath(final StringBuffer buffer) {... |
151 |
0
|
print(buffer.toString()); |
152 |
|
} |
153 |
|
|
154 |
|
|
155 |
|
|
156 |
|
|
157 |
|
@return |
158 |
|
|
|
|
| 0% |
Uncovered Elements: 77 (77) |
Complexity: 21 |
Complexity Density: 0,4 |
|
159 |
0
|
protected final String readToken() {... |
160 |
0
|
final String method = "readToken()"; |
161 |
0
|
Trace.begin(CLASS, this, method); |
162 |
0
|
StringBuffer token = new StringBuffer(); |
163 |
0
|
try { |
164 |
0
|
do { |
165 |
0
|
if (eof()) { |
166 |
0
|
if (token.length() <= 0) { |
167 |
0
|
token = null; |
168 |
|
} |
169 |
0
|
break; |
170 |
|
} |
171 |
0
|
final int c = getChar(); |
172 |
0
|
if (Character.isDigit((char) c)) { |
173 |
0
|
token.append((char) readChar()); |
174 |
0
|
if (Character.isDigit((char) getChar())) { |
175 |
0
|
continue; |
176 |
|
} |
177 |
0
|
break; |
178 |
|
} |
179 |
0
|
if (Character.isLetter((char) c)) { |
180 |
0
|
token.append((char) readChar()); |
181 |
0
|
if (Character.isLetter((char) getChar())) { |
182 |
0
|
continue; |
183 |
|
} |
184 |
0
|
break; |
185 |
|
} |
186 |
0
|
if (SPECIALCHARACTERS.indexOf(c) >= 0) { |
187 |
0
|
switch (c) { |
188 |
0
|
case '&': |
189 |
0
|
case '%': |
190 |
0
|
case '{': |
191 |
0
|
case '}': |
192 |
0
|
case '~': |
193 |
0
|
token.append((char) readChar()); |
194 |
0
|
break; |
195 |
0
|
case '$': |
196 |
0
|
token.append((char) readChar()); |
197 |
0
|
if ('$' == getChar()) { |
198 |
0
|
continue; |
199 |
|
} |
200 |
0
|
break; |
201 |
0
|
case '\\': |
202 |
0
|
final String t = readBackslashToken(); |
203 |
0
|
token.append(t); |
204 |
0
|
if ('_' == getChar() || '^' == getChar()) { |
205 |
0
|
token.append((char) readChar()); |
206 |
0
|
continue; |
207 |
|
} |
208 |
0
|
break; |
209 |
0
|
default: |
210 |
0
|
readChar(); |
211 |
0
|
token.append((char) c); |
212 |
|
} |
213 |
0
|
break; |
214 |
|
} |
215 |
0
|
token.append((char) readChar()); |
216 |
0
|
if ('_' == getChar() || '^' == getChar()) { |
217 |
0
|
token.append((char) readChar()); |
218 |
0
|
continue; |
219 |
|
} |
220 |
0
|
break; |
221 |
0
|
} while (!eof()); |
222 |
0
|
Trace.param(CLASS, this, method, "Read token", token); |
223 |
0
|
return (token != null ? token.toString() : null); |
224 |
|
} finally { |
225 |
0
|
Trace.end(CLASS, this, method); |
226 |
|
} |
227 |
|
} |
228 |
|
|
229 |
|
|
230 |
|
|
231 |
|
|
232 |
|
@return |
233 |
|
|
|
|
| 0% |
Uncovered Elements: 27 (27) |
Complexity: 6 |
Complexity Density: 0,32 |
|
234 |
0
|
private String readBackslashToken() {... |
235 |
0
|
final String method = "readBackslashToken()"; |
236 |
0
|
Trace.begin(CLASS, this, method); |
237 |
0
|
if (getChar() != '\\') { |
238 |
0
|
throw new IllegalArgumentException("\\ expected"); |
239 |
|
} |
240 |
0
|
readChar(); |
241 |
0
|
if (eof()) { |
242 |
0
|
Trace.param(CLASS, this, method, "return", null); |
243 |
0
|
Trace.end(CLASS, this, method); |
244 |
0
|
return null; |
245 |
|
} |
246 |
0
|
if (!Character.isLetter((char) getChar())) { |
247 |
0
|
Trace.param(CLASS, this, method, "return", (char) getChar()); |
248 |
0
|
Trace.end(CLASS, this, method); |
249 |
0
|
return "\\" + ((char) readChar()); |
250 |
|
} |
251 |
0
|
final StringBuffer buffer = new StringBuffer("\\"); |
252 |
0
|
do { |
253 |
0
|
buffer.append((char) readChar()); |
254 |
0
|
} while (!eof() && Character.isLetter((char) getChar())); |
255 |
0
|
Trace.param(CLASS, this, method, "return", buffer.toString()); |
256 |
0
|
Trace.end(CLASS, this, method); |
257 |
0
|
return buffer.toString(); |
258 |
|
} |
259 |
|
|
260 |
|
|
261 |
|
|
262 |
|
|
263 |
|
@return |
264 |
|
|
|
|
| 0% |
Uncovered Elements: 26 (26) |
Complexity: 6 |
Complexity Density: 0,38 |
|
265 |
0
|
private String readCurlyBraceContents() {... |
266 |
0
|
final String first = readToken(); |
267 |
0
|
if (!"{".equals(first)) { |
268 |
0
|
throw new IllegalArgumentException("\"{\" expected, but was: \"" + first + "\""); |
269 |
|
} |
270 |
0
|
final StringBuffer buffer = new StringBuffer(); |
271 |
0
|
String next; |
272 |
0
|
int level = 1; |
273 |
0
|
while (level > 0) { |
274 |
0
|
next = readToken(); |
275 |
0
|
if ("{".equals(next)) { |
276 |
0
|
level++; |
277 |
0
|
} else if ("}".equals(next)) { |
278 |
0
|
level--; |
279 |
|
} |
280 |
0
|
if (level <= 0) { |
281 |
0
|
break; |
282 |
|
} |
283 |
0
|
buffer.append(next); |
284 |
|
} |
285 |
0
|
return buffer.toString(); |
286 |
|
} |
287 |
|
|
288 |
|
|
289 |
|
|
290 |
|
|
291 |
|
@param |
292 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
293 |
0
|
private final void print(final String line) {... |
294 |
0
|
output.append(line); |
295 |
|
} |
296 |
|
|
297 |
|
|
298 |
|
|
299 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
300 |
0
|
private final void println() {... |
301 |
0
|
println(""); |
302 |
|
} |
303 |
|
|
304 |
|
|
305 |
|
|
306 |
|
|
307 |
|
@param |
308 |
|
|
|
|
| 0% |
Uncovered Elements: 2 (2) |
Complexity: 1 |
Complexity Density: 0,5 |
|
309 |
0
|
private final void println(final String line) {... |
310 |
0
|
print(line); |
311 |
0
|
print("\n"); |
312 |
|
} |
313 |
|
|
314 |
|
|
315 |
|
|
316 |
|
|
317 |
|
@return |
318 |
|
|
|
|
| 0% |
Uncovered Elements: 4 (4) |
Complexity: 1 |
Complexity Density: 0,25 |
|
319 |
0
|
public final String getToken() {... |
320 |
0
|
markPosition(); |
321 |
0
|
final String result = readToken(); |
322 |
0
|
rewindPosition(); |
323 |
0
|
return result; |
324 |
|
} |
325 |
|
|
326 |
|
|
327 |
|
|
328 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
329 |
0
|
protected final void markPosition() {... |
330 |
0
|
input.markPosition(); |
331 |
|
} |
332 |
|
|
333 |
|
|
334 |
|
|
335 |
|
|
336 |
|
@return |
337 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
338 |
0
|
protected final long rewindPosition() {... |
339 |
0
|
return input.rewindPosition(); |
340 |
|
} |
341 |
|
|
342 |
|
|
343 |
|
|
344 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
345 |
0
|
protected final void clearMark() {... |
346 |
0
|
input.clearMark(); |
347 |
|
} |
348 |
|
|
349 |
|
|
350 |
|
|
351 |
|
|
352 |
|
@return |
353 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
354 |
0
|
protected long getPosition() {... |
355 |
0
|
return input.getPosition(); |
356 |
|
} |
357 |
|
|
358 |
|
|
359 |
|
|
360 |
|
|
361 |
|
|
362 |
|
@return |
363 |
|
|
364 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
365 |
0
|
protected final int getChar() {... |
366 |
0
|
return input.getChar(); |
367 |
|
} |
368 |
|
|
369 |
|
|
370 |
|
|
371 |
|
|
372 |
|
|
373 |
|
@return |
374 |
|
|
375 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
376 |
0
|
protected final int readChar() {... |
377 |
0
|
return input.readChar(); |
378 |
|
} |
379 |
|
|
380 |
|
|
381 |
|
|
382 |
|
|
383 |
|
@return |
384 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
385 |
0
|
public final boolean eof() {... |
386 |
0
|
return input.eof(); |
387 |
|
} |
388 |
|
|
389 |
|
|
390 |
|
|
391 |
|
|
392 |
|
@return |
393 |
|
|
|
|
| 0% |
Uncovered Elements: 1 (1) |
Complexity: 1 |
Complexity Density: 1 |
|
394 |
0
|
public final int getRewindStackSize() {... |
395 |
0
|
return input.getRewindStackSize(); |
396 |
|
} |
397 |
|
|
398 |
|
} |