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

File TableFilter.java

 

Coverage histogram

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

Code metrics

4
15
2
1
80
48
4
0.27
7.5
2
2

Classes

Class Line # Actions
TableFilter 43 15 0% 4 1
0.9523809695.2%
 

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 rip-off or modify HTML tables so that they can be rendered properly.
36    *
37    * @version $Id: b996f90707b02570eafb5e3784888a64ac38ea4c $
38    * @since 1.8M1
39    */
40    @Component
41    @Named("officeimporter/table")
42    @Singleton
 
43    public class TableFilter extends AbstractHTMLFilter
44    {
 
45  66 toggle @Override
46    public void filter(Document document, Map<String, String> cleaningParams)
47    {
48    // Remove isolated paragraphs inside cell items / table header items.
49  66 List<Element> tableCells = filterDescendants(document.getDocumentElement(), new String[] {TAG_TD, TAG_TH});
50  66 for (Element cell : tableCells) {
51  12 List<Element> paragraphs = filterChildren(cell, TAG_P);
52  12 if (paragraphs.size() == 1) {
53  2 replaceWithChildren(paragraphs.get(0));
54    }
55    }
56    // Strip off empty table rows. See http://jira.xwiki.org/jira/browse/XWIKI-3136.
57  66 List<Element> emptyRows =
58    filterDescendants(document.getDocumentElement(), new String[] {TAG_TR}, new ElementSelector()
59    {
 
60  11 toggle @Override
61    public boolean isSelected(Element element)
62    {
63  11 return element.getChildNodes().getLength() == 0;
64    }
65    });
66  66 for (Element emptyRow : emptyRows) {
67  1 emptyRow.getParentNode().removeChild(emptyRow);
68    }
69    // Remove problematic rowspan attributes.
70  66 List<Element> rows = filterDescendants(document.getDocumentElement(), new String[] {TAG_TR});
71  66 for (Element row : rows) {
72  10 List<Element> childCells = filterDescendants(row, new String[] {TAG_TD});
73  10 if (hasAttribute(childCells, ATTRIBUTE_ROWSPAN, true)) {
74  10 for (Element childCell : childCells) {
75  9 childCell.removeAttribute(ATTRIBUTE_ROWSPAN);
76    }
77    }
78    }
79    }
80    }