1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.rendering.internal.parser.plain

File PlainTextStreamParser.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart9.png
38% 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 6
0.907692390.8%
 

Contributing tests

This file is covered by 105 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: a9edc058a5e53c3b56778278e0f906f72f5359bc $
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 toggle @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  2761711 toggle private int readChar(Reader source) throws ParseException
68    {
69  2761714 int c;
70   
71  2761714 try {
72  2761712 c = source.read();
73    } catch (IOException e) {
74  0 throw new ParseException("Failed to read input source", e);
75    }
76   
77  2761721 return c;
78    }
79   
 
80  32173 toggle @Override
81    public void parse(Reader source, Listener listener) throws ParseException
82    {
83  32172 StringBuffer word = new StringBuffer();
84  32166 BufferedReader bufferedSource = new BufferedReader(source);
85  32169 int charAsInt;
86   
87  32169 listener.beginDocument(MetaData.EMPTY);
88   
89  32173 boolean paragraphSent = false;
90   
91  ? while ((charAsInt = readChar(bufferedSource)) != -1) {
92  2729549 if (!paragraphSent) {
93  32155 listener.beginParagraph(Listener.EMPTY_PARAMETERS);
94  32153 paragraphSent = true;
95    }
96   
97  2729554 parseChar(charAsInt, word, listener);
98    }
99   
100  32172 if (word.length() > 0) {
101  27129 listener.onWord(word.toString());
102    }
103   
104  32169 if (paragraphSent) {
105  32150 listener.endParagraph(Listener.EMPTY_PARAMETERS);
106    }
107   
108  32169 listener.endDocument(MetaData.EMPTY);
109    }
110   
 
111  2729553 toggle private void parseChar(int charAsInt, StringBuffer word, Listener listener) throws ParseException
112    {
113  2729553 char c = (char) charAsInt;
114  2729552 if (c == '\n') {
115  40766 if (word.length() > 0) {
116  34735 listener.onWord(word.toString());
117    }
118  40766 listener.onNewLine();
119   
120  40766 word.setLength(0);
121  2688789 } else if (c == '\r') {
122    // Do nothing, skip it
123  2688781 } else if (c == ' ') {
124  225691 if (word.length() > 0) {
125  147190 listener.onWord(word.toString());
126    }
127  225695 listener.onSpace();
128   
129  225693 word.setLength(0);
130  2463068 } else if (SPECIALSYMBOL_PATTERN.matcher(String.valueOf(c)).matches()) {
131  249931 if (word.length() > 0) {
132  176099 listener.onWord(word.toString());
133    }
134  249922 listener.onSpecialSymbol(c);
135   
136  249925 word.setLength(0);
137    } else {
138  2213146 word.append(c);
139    }
140    }
141    }