|
||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
PlainTextStreamParser | Line # 46 | 34 | 0% | 13 | 8 | 85.5% |
0.8545455
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||
(17) | |||
Result | |||
0.76363635
|
org.xwiki.rendering.test.integration.RenderingTest.execute org.xwiki.rendering.test.integration.RenderingTest.execute | 1 PASS | |
0.7090909
|
org.xwiki.rendering.test.integration.RenderingTest.execute org.xwiki.rendering.test.integration.RenderingTest.execute | 1 PASS | |
0.7090909
|
org.xwiki.rendering.test.integration.RenderingTest.execute org.xwiki.rendering.test.integration.RenderingTest.execute | 1 PASS | |
0.6363636
|
org.xwiki.rendering.test.integration.RenderingTest.execute org.xwiki.rendering.test.integration.RenderingTest.execute | 1 PASS | |
0.6363636
|
org.xwiki.rendering.test.integration.RenderingTest.execute org.xwiki.rendering.test.integration.RenderingTest.execute | 1 PASS | |
0.6181818
|
org.xwiki.rendering.test.integration.RenderingTest.execute org.xwiki.rendering.test.integration.RenderingTest.execute | 1 PASS | |
0.6181818
|
org.xwiki.rendering.test.integration.RenderingTest.execute org.xwiki.rendering.test.integration.RenderingTest.execute | 1 PASS | |
0.6181818
|
org.xwiki.rendering.test.integration.RenderingTest.execute org.xwiki.rendering.test.integration.RenderingTest.execute | 1 PASS | |
0.6
|
org.xwiki.rendering.test.integration.RenderingTest.execute org.xwiki.rendering.test.integration.RenderingTest.execute | 1 PASS | |
0.6
|
org.xwiki.rendering.test.integration.RenderingTest.execute org.xwiki.rendering.test.integration.RenderingTest.execute | 1 PASS | |
0.6
|
org.xwiki.rendering.test.integration.RenderingTest.execute org.xwiki.rendering.test.integration.RenderingTest.execute | 1 PASS | |
0.6
|
org.xwiki.rendering.test.integration.RenderingTest.execute org.xwiki.rendering.test.integration.RenderingTest.execute | 1 PASS | |
0.6
|
org.xwiki.rendering.test.integration.RenderingTest.execute org.xwiki.rendering.test.integration.RenderingTest.execute | 1 PASS | |
0.6
|
org.xwiki.rendering.test.integration.RenderingTest.execute org.xwiki.rendering.test.integration.RenderingTest.execute | 1 PASS | |
0.58181816
|
org.xwiki.rendering.test.integration.RenderingTest.execute org.xwiki.rendering.test.integration.RenderingTest.execute | 1 PASS | |
0.4909091
|
org.xwiki.rendering.test.integration.RenderingTest.execute org.xwiki.rendering.test.integration.RenderingTest.execute | 1 PASS | |
0.4909091
|
org.xwiki.rendering.test.integration.RenderingTest.execute org.xwiki.rendering.test.integration.RenderingTest.execute | 1 PASS | |
1 | /* | |
2 | * See the NOTICE file distributed with this work for additional | |
3 | * information regarding copyright ownership. | |
4 | * | |
5 | * This is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU Lesser General Public License as | |
7 | * published by the Free Software Foundation; either version 2.1 of | |
8 | * the License, or (at your option) any later version. | |
9 | * | |
10 | * This software is distributed in the hope that it will be useful, | |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
13 | * Lesser General Public License for more details. | |
14 | * | |
15 | * You should have received a copy of the GNU Lesser General Public | |
16 | * License along with this software; if not, write to the Free | |
17 | * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA | |
18 | * 02110-1301 USA, or see the FSF site: http://www.fsf.org. | |
19 | */ | |
20 | package org.xwiki.rendering.internal.parser; | |
21 | ||
22 | import java.io.BufferedReader; | |
23 | import java.io.IOException; | |
24 | import java.io.Reader; | |
25 | import java.util.regex.Pattern; | |
26 | ||
27 | import javax.inject.Named; | |
28 | import javax.inject.Singleton; | |
29 | ||
30 | import org.xwiki.component.annotation.Component; | |
31 | import org.xwiki.rendering.listener.Listener; | |
32 | import org.xwiki.rendering.listener.MetaData; | |
33 | import org.xwiki.rendering.parser.ParseException; | |
34 | import org.xwiki.rendering.parser.StreamParser; | |
35 | import org.xwiki.rendering.syntax.Syntax; | |
36 | ||
37 | /** | |
38 | * Plain Text Parser to convert a text source into a events. | |
39 | * | |
40 | * @version $Id$ | |
41 | * @since 2.1M1 | |
42 | */ | |
43 | @Component | |
44 | @Named("plain/1.0") | |
45 | @Singleton | |
46 | public class PlainTextStreamParser implements StreamParser | |
47 | { | |
48 | /** | |
49 | * The characters which are considered as "special" symbols for {@link org.xwiki.rendering.block.SpecialSymbolBlock} | |
50 | * . | |
51 | */ | |
52 | public static final Pattern SPECIALSYMBOL_PATTERN = Pattern.compile("[!\"#$%&'()*+,-./:;<=>?@\\[\\]^_`{|}~]"); | |
53 | ||
54 | 0 | @Override |
55 | public Syntax getSyntax() | |
56 | { | |
57 | 0 | return Syntax.PLAIN_1_0; |
58 | } | |
59 | ||
60 | /** | |
61 | * Read a single char from an Reader source. | |
62 | * | |
63 | * @param source the input to read from | |
64 | * @return the char read | |
65 | * @throws ParseException in case of reading error | |
66 | */ | |
67 | 596 | private int readChar(Reader source) throws ParseException |
68 | { | |
69 | 596 | int c; |
70 | ||
71 | 596 | try { |
72 | 596 | c = source.read(); |
73 | } catch (IOException e) { | |
74 | 0 | throw new ParseException("Failed to read input source", e); |
75 | } | |
76 | ||
77 | 596 | return c; |
78 | } | |
79 | ||
80 | 100 | @Override |
81 | public void parse(Reader source, Listener listener) throws ParseException | |
82 | { | |
83 | 100 | StringBuffer word = new StringBuffer(); |
84 | 100 | BufferedReader bufferedSource = new BufferedReader(source); |
85 | 100 | int charAsInt; |
86 | ||
87 | 100 | listener.beginDocument(MetaData.EMPTY); |
88 | 100 | listener.beginParagraph(Listener.EMPTY_PARAMETERS); |
89 | ||
90 | ? | while ((charAsInt = readChar(bufferedSource)) != -1) { |
91 | 496 | char c = (char) charAsInt; |
92 | 496 | if (c == '\n') { |
93 | 3 | if (word.length() > 0) { |
94 | 0 | listener.onWord(word.toString()); |
95 | } | |
96 | 3 | listener.onNewLine(); |
97 | ||
98 | 3 | word.setLength(0); |
99 | 493 | } else if (c == '\r') { |
100 | // Do nothing, skip it | |
101 | 493 | } else if (c == ' ') { |
102 | 56 | if (word.length() > 0) { |
103 | 45 | listener.onWord(word.toString()); |
104 | } | |
105 | 56 | listener.onSpace(); |
106 | ||
107 | 56 | word.setLength(0); |
108 | 437 | } else if (SPECIALSYMBOL_PATTERN.matcher(String.valueOf(c)).matches()) { |
109 | 126 | if (word.length() > 0) { |
110 | 22 | listener.onWord(word.toString()); |
111 | } | |
112 | 126 | listener.onSpecialSymbol(c); |
113 | ||
114 | 126 | word.setLength(0); |
115 | } else { | |
116 | 311 | word.append(c); |
117 | } | |
118 | } | |
119 | ||
120 | 100 | if (word.length() > 0) { |
121 | 32 | listener.onWord(word.toString()); |
122 | } | |
123 | ||
124 | 100 | listener.endParagraph(Listener.EMPTY_PARAMETERS); |
125 | 100 | listener.endDocument(MetaData.EMPTY); |
126 | } | |
127 | } |
|