Clover Coverage Report - XWiki Rendering - Parent POM 4.0-SNAPSHOT (Aggregated)
Coverage timestamp: Mon Mar 12 2012 18:03:13 CET
../../../../img/srcFileCovDistChart9.png 55% of files have more coverage
18   103   8   6
6   52   0.44   3
3     2.67  
1    
 
  PlainTextBlockFilter       Line # 40 18 0% 8 3 88.9% 0.8888889
 
  (15)
 
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: ffb41b4a0a40a98a9d2dde503f7c39b83486c728 $
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  2 toggle {
48  2 add(WordBlock.class);
49  2 add(SpaceBlock.class);
50  2 add(SpecialSymbolBlock.class);
51  2 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  15 toggle public PlainTextBlockFilter(Parser plainTextParser, LinkLabelGenerator linkLabelGenerator)
71    {
72  15 this.plainTextParser = plainTextParser;
73  15 this.linkLabelGenerator = linkLabelGenerator;
74    }
75   
 
76  134 toggle @Override
77    public List<Block> filter(Block block)
78    {
79  134 if (VALID_PLAINTEXT_BLOCKS.contains(block.getClass())) {
80  131 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 if (reference.getType().equals(ResourceType.DOCUMENT)) {
88  0 label = this.linkLabelGenerator.generate(reference);
89    } else {
90  1 label = reference.getReference();
91    }
92   
93  1 return this.plainTextParser.parse(new StringReader(label)).getChildren().get(0).getChildren();
94    } catch (ParseException e) {
95    // This shouldn't happen since the parser cannot throw an exception since the source is a memory
96    // String.
97  0 throw new RuntimeException("Failed to parse link label as plain text", e);
98    }
99    } else {
100  2 return Collections.emptyList();
101    }
102    }
103    }