1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.officeimporter.internal.filter

File RedundancyFilter.java

 

Coverage histogram

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

Code metrics

2
11
3
1
93
53
4
0.36
3.67
3
1.33

Classes

Class Line # Actions
RedundancyFilter 46 11 0% 4 2
0.87587.5%
 

Contributing tests

This file is covered by 20 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.officeimporter.internal.filter;
21   
22    import java.util.List;
23    import java.util.Map;
24   
25    import javax.inject.Named;
26    import javax.inject.Singleton;
27   
28    import org.w3c.dom.Document;
29    import org.w3c.dom.Element;
30    import org.xwiki.component.annotation.Component;
31    import org.xwiki.xml.html.filter.AbstractHTMLFilter;
32    import org.xwiki.xml.html.filter.ElementSelector;
33   
34    /**
35    * This filter is used to remove those tags that doesn't play any role with the representation of information. This type
36    * of tags can result from other filters (like the style filter) or Open Office specific formatting choices (like
37    * newlines being represented by empty paragraphs). For an example, empty {@code <span/>} or {@code <div/>} tags will be
38    * ripped off within this filter.
39    *
40    * @version $Id: d44cf476ea96ac774cc2aa4d03bba8a1fc46f78a $
41    * @since 1.8M1
42    */
43    @Component
44    @Named("officeimporter/redundancy")
45    @Singleton
 
46    public class RedundancyFilter extends AbstractHTMLFilter
47    {
48    /**
49    * List of those tags which will be filtered if no attributes are present.
50    */
51    private static final String[] FILTERED_IF_NO_ATTRIBUTES_TAGS = new String[] {TAG_SPAN, TAG_DIV};
52   
53    /**
54    * List of those tags which will be filtered if no textual content is present inside them.
55    */
56    private static final String[] FILTERED_IF_NO_CONTENT_TAGS = new String[] {
57    TAG_EM, TAG_STRONG, TAG_DFN, TAG_CODE, TAG_SAMP, TAG_KBD, TAG_VAR, TAG_CITE, TAG_ABBR,
58    TAG_ACRONYM, TAG_ADDRESS, TAG_BLOCKQUOTE, TAG_Q, TAG_PRE, TAG_H1, TAG_H2, TAG_H3, TAG_H4, TAG_H5, TAG_H6};
59   
 
60  66 toggle @Override
61    public void filter(Document document, Map<String, String> cleaningParams)
62    {
63  66 List<Element> elementsWithNoAttributes =
64    filterDescendants(document.getDocumentElement(), FILTERED_IF_NO_ATTRIBUTES_TAGS, new ElementSelector()
65    {
 
66  33 toggle @Override
67    public boolean isSelected(Element element)
68    {
69  33 return !element.hasAttributes();
70    }
71    });
72  66 for (Element element : elementsWithNoAttributes) {
73  33 replaceWithChildren(element);
74    }
75  66 List<Element> elementsWithNoContent =
76    filterDescendants(document.getDocumentElement(), FILTERED_IF_NO_CONTENT_TAGS, new ElementSelector()
77    {
 
78  68 toggle @Override
79    public boolean isSelected(Element element)
80    {
81  68 return element.getTextContent().trim().equals("");
82    }
83    });
84  66 for (Element element : elementsWithNoContent) {
85  20 String textContent = element.getTextContent();
86  20 if (textContent.equals("")) {
87  20 element.getParentNode().removeChild(element);
88    } else {
89  0 element.setTextContent(textContent.replaceAll(" ", "&nbsp;"));
90    }
91    }
92    }
93    }