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

File AnchorFilter.java

 

Coverage histogram

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

Code metrics

4
12
2
1
102
42
4
0.33
6
2
2

Classes

Class Line # Actions
AnchorFilter 66 12 0% 4 1
0.944444494.4%
 

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.ArrayList;
23    import java.util.HashSet;
24    import java.util.List;
25    import java.util.Map;
26    import java.util.Set;
27   
28    import javax.inject.Named;
29    import javax.inject.Singleton;
30   
31    import org.w3c.dom.Document;
32    import org.w3c.dom.Element;
33    import org.w3c.dom.Node;
34    import org.xwiki.component.annotation.Component;
35    import org.xwiki.xml.html.filter.AbstractHTMLFilter;
36   
37    /**
38    * Filters duplicated anchors.
39    * <p>
40    * The HTML generated by the office server includes anchors of the form:
41    *
42    * <pre>
43    * {@code <a name="table1">
44    * <h1>Sheet 1: <em>Hello</em></h1>
45    * </a>}
46    * </pre>
47    *
48    * and the default HTML cleaner converts them to:
49    *
50    * <pre>
51    * {@code <a name="table1"/>
52    * <h1>
53    * <a name="table1">Sheet 1: <em>Hello</em></a>
54    * </h1>}
55    * </pre>
56    *
57    * this is because of the close-before-copy-inside behavior of default HTML cleaner. Thus the additional (copy-inside)
58    * anchor needs to be ripped off.
59    *
60    * @version $Id: b28ccd7da39771c4c2c4b6a7d3b500e3e896086b $
61    * @since 1.8M1
62    */
63    @Component
64    @Named("officeimporter/anchor")
65    @Singleton
 
66    public class AnchorFilter extends AbstractHTMLFilter
67    {
 
68  66 toggle @Override
69    public void filter(Document document, Map<String, String> cleaningParameters)
70    {
71  66 List<Element> links = filterDescendants(document.getDocumentElement(), new String[] {TAG_A});
72  66 Set<String> fragmentIdentifiers = new HashSet<String>();
73  66 List<Element> anchorsToRemove = new ArrayList<Element>();
74  66 for (Element link : links) {
75  3 if (isAnchor(link)) {
76  3 String fragmentIdentifier = link.getAttribute(ATTRIBUTE_NAME);
77  3 if (fragmentIdentifiers.contains(fragmentIdentifier)) {
78  1 anchorsToRemove.add(link);
79    }
80  3 fragmentIdentifiers.add(fragmentIdentifier);
81    }
82    }
83  66 for (Element anchor : anchorsToRemove) {
84  1 replaceWithChildren(anchor);
85    }
86    }
87   
88    /**
89    * Checks whether the given node represents an HTML anchor.
90    *
91    * <pre>
92    * {@code <a name="Chapter1"/>}
93    * </pre>
94    *
95    * @param node the {@link Node}
96    * @return {@code true} if the node represents an anchor, {@code false} otherwise
97    */
 
98  3 toggle private boolean isAnchor(Node node)
99    {
100  3 return node instanceof Element && !"".equals(((Element) node).getAttribute(ATTRIBUTE_NAME));
101    }
102    }