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

File PlainTextBlockFilter.java

 

Coverage histogram

../../../../img/srcFileCovDistChart9.png
38% of files have more coverage

Code metrics

6
19
3
1
104
53
9
0.47
6.33
3
3

Classes

Class Line # Actions
PlainTextBlockFilter 40 19 0% 9 3
0.8928571389.3%
 

Contributing tests

This file is covered by 18 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.block;
21   
22    import java.io.StringReader;
23    import java.util.Collections;
24    import java.util.HashSet;
25    import java.util.List;
26    import java.util.Set;
27   
28    import org.xwiki.rendering.listener.reference.ResourceReference;
29    import org.xwiki.rendering.listener.reference.ResourceType;
30    import org.xwiki.rendering.parser.ParseException;
31    import org.xwiki.rendering.parser.Parser;
32    import org.xwiki.rendering.renderer.reference.link.LinkLabelGenerator;
33   
34    /**
35    * Used to filter plain text blocks.
36    *
37    * @version $Id: d74bbec4d0a03909bfacf8d23feeb34f39444a44 $
38    * @since 1.9M1
39    */
 
40    public class PlainTextBlockFilter implements BlockFilter
41    {
42    /**
43    * The set of valid Block classes as plain text content.
44    */
45    private static final Set<Class<? extends Block>> VALID_PLAINTEXT_BLOCKS = new HashSet<Class<? extends Block>>()
46    {
 
47  5 toggle {
48  5 add(WordBlock.class);
49  5 add(SpaceBlock.class);
50  5 add(SpecialSymbolBlock.class);
51  5 add(NewLineBlock.class);
52    }
53    };
54   
55    /**
56    * A parser that knows how to parse plain text; this is used to transform link labels into plain text.
57    */
58    private Parser plainTextParser;
59   
60    /**
61    * Generate link label.
62    */
63    private LinkLabelGenerator linkLabelGenerator;
64   
65    /**
66    * @param plainTextParser a plain text parser used to transform link labels into plain text
67    * @param linkLabelGenerator generate link label.
68    * @since 2.0M3
69    */
 
70  19 toggle public PlainTextBlockFilter(Parser plainTextParser, LinkLabelGenerator linkLabelGenerator)
71    {
72  19 this.plainTextParser = plainTextParser;
73  19 this.linkLabelGenerator = linkLabelGenerator;
74    }
75   
 
76  211 toggle @Override
77    public List<Block> filter(Block block)
78    {
79  211 if (VALID_PLAINTEXT_BLOCKS.contains(block.getClass())) {
80  208 return Collections.singletonList(block);
81  3 } else if (LinkBlock.class.isAssignableFrom(block.getClass()) && block.getChildren().size() == 0) {
82  1 ResourceReference reference = ((LinkBlock) block).getReference();
83   
84  1 try {
85  1 String label;
86   
87  1 ResourceType resourceType = reference.getType();
88  1 if (ResourceType.DOCUMENT.equals(resourceType) || ResourceType.SPACE.equals(resourceType)) {
89  0 label = this.linkLabelGenerator.generate(reference);
90    } else {
91  1 label = reference.getReference();
92    }
93   
94  1 return this.plainTextParser.parse(new StringReader(label)).getChildren().get(0).getChildren();
95    } catch (ParseException e) {
96    // This shouldn't happen since the parser cannot throw an exception since the source is a memory
97    // String.
98  0 throw new RuntimeException("Failed to parse link label as plain text", e);
99    }
100    } else {
101  2 return Collections.emptyList();
102    }
103    }
104    }