1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.rendering.internal.parser.plain

File PlainTextStreamParser.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart10.png
0% of files have more coverage

Code metrics

22
39
4
1
141
84
16
0.41
9.75
4
4

Classes

Class Line # Actions
PlainTextStreamParser 46 39 0% 16 4
0.9384615493.8%
 

Contributing tests

This file is covered by 100 tests. .

Source view

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.plain;
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: 15114d0ab76c6c02c38cc777181e886c7e5d52e0 $
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  5 toggle @Override
55    public Syntax getSyntax()
56    {
57  5 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  21670249 toggle private int readChar(Reader source) throws ParseException
68    {
69  21670276 int c;
70   
71  21670279 try {
72  21670283 c = source.read();
73    } catch (IOException e) {
74  0 throw new ParseException("Failed to read input source", e);
75    }
76   
77  21670414 return c;
78    }
79   
 
80  285292 toggle @Override
81    public void parse(Reader source, Listener listener) throws ParseException
82    {
83  285322 StringBuilder word = new StringBuilder();
84  285316 BufferedReader bufferedSource = new BufferedReader(source);
85  285065 int charAsInt;
86   
87  285069 listener.beginDocument(MetaData.EMPTY);
88   
89  285197 boolean paragraphSent = false;
90   
91  ? while ((charAsInt = readChar(bufferedSource)) != -1) {
92  21384636 if (!paragraphSent) {
93  285028 listener.beginParagraph(Listener.EMPTY_PARAMETERS);
94  285051 paragraphSent = true;
95    }
96   
97  21384892 parseChar(charAsInt, word, listener);
98    }
99   
100  285358 if (word.length() > 0) {
101  240962 listener.onWord(word.toString());
102    }
103   
104  285311 if (paragraphSent) {
105  285072 listener.endParagraph(Listener.EMPTY_PARAMETERS);
106    }
107   
108  285292 listener.endDocument(MetaData.EMPTY);
109    }
110   
 
111  21384703 toggle private void parseChar(int charAsInt, StringBuilder word, Listener listener) throws ParseException
112    {
113  21384629 char c = (char) charAsInt;
114  21384680 if (c == '\n') {
115  236688 if (word.length() > 0) {
116  151195 listener.onWord(word.toString());
117    }
118  236688 listener.onNewLine();
119   
120  236688 word.setLength(0);
121  21147995 } else if (c == '\r') {
122    // Do nothing, skip it
123  21147810 } else if (c == ' ') {
124  1539078 if (word.length() > 0) {
125  1178254 listener.onWord(word.toString());
126    }
127  1539075 listener.onSpace();
128   
129  1539074 word.setLength(0);
130  19609805 } else if (SPECIALSYMBOL_PATTERN.matcher(String.valueOf(c)).matches()) {
131  1934778 if (word.length() > 0) {
132  1259860 listener.onWord(word.toString());
133    }
134  1934776 listener.onSpecialSymbol(c);
135   
136  1934773 word.setLength(0);
137    } else {
138  17674961 word.append(c);
139    }
140    }
141    }