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

File StyleFilter.java

 

Coverage histogram

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

Code metrics

16
27
3
1
123
69
12
0.44
9
3
4

Classes

Class Line # Actions
StyleFilter 47 27 0% 12 5
0.891304489.1%
 

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.HashMap;
24    import java.util.List;
25    import java.util.Map;
26   
27    import javax.inject.Named;
28    import javax.inject.Singleton;
29   
30    import org.w3c.dom.Document;
31    import org.w3c.dom.Element;
32    import org.w3c.dom.NamedNodeMap;
33    import org.w3c.dom.Node;
34    import org.w3c.dom.NodeList;
35    import org.xwiki.component.annotation.Component;
36    import org.xwiki.xml.html.filter.AbstractHTMLFilter;
37   
38    /**
39    * This particular filter searches HTML tags containing style attributes and removes such attributes if present.
40    *
41    * @version $Id: ddd046d7a4851ef67f9e4d93725c4f0421240a08 $
42    * @since 1.8M1
43    */
44    @Component
45    @Named("officeimporter/style")
46    @Singleton
 
47    public class StyleFilter extends AbstractHTMLFilter
48    {
49    /**
50    * Separator character used for grouping attribute names.
51    */
52    private static final String ATTRIBUTE_SEPARATOR = "|";
53   
54    /**
55    * The html_tag_name->allowed_attribute_names mappings for strict filtering mode. This is used to filter out all
56    * unnecessary attributes. The mapped object is a '|' separated string of all allowed attributes. If a particular
57    * tag name is not present in this map, all of it's attributes will be filtered.
58    */
59    private Map<String, String> attributeMappingsStrict;
60   
61    /**
62    * Constructs a {@link StyleFilter}.
63    */
 
64  20 toggle public StyleFilter()
65    {
66  20 this.attributeMappingsStrict = new HashMap<String, String>();
67  20 this.attributeMappingsStrict.put(TAG_A, "|href|name|");
68  20 this.attributeMappingsStrict.put(TAG_IMG, "|alt|src|height|width|");
69  20 this.attributeMappingsStrict.put(TAG_TD, "|colspan|rowspan|");
70  20 this.attributeMappingsStrict.put(TAG_TH, "|colspan|");
71    }
72   
 
73  66 toggle @Override
74    public void filter(Document document, Map<String, String> cleaningParams)
75    {
76  66 String mode = cleaningParams.get("filterStyles");
77  66 if (null != mode && mode.equals("strict")) {
78  35 filter(document.getDocumentElement(), this.attributeMappingsStrict);
79    }
80    }
81   
82    /**
83    * Removes style attributes from this node and it's children recursively.
84    *
85    * @param node node being filtered.
86    * @param attributeMappings attribute map to be used for filtering.
87    */
 
88  378 toggle private void filter(Node node, Map<String, String> attributeMappings)
89    {
90  378 if (node instanceof Element) {
91  268 Element element = (Element) node;
92  268 String allowedAttributes = attributeMappings.get(element.getNodeName().toLowerCase());
93  268 NamedNodeMap currentAttributes = element.getAttributes();
94  268 if (null == allowedAttributes) {
95    // Strip off all attributes.
96  257 while (currentAttributes.getLength() > 0) {
97  0 currentAttributes.removeNamedItem(currentAttributes.item(0).getNodeName());
98    }
99    } else {
100    // Collect those attributes that need to be removed.
101  11 List<String> attributesToBeRemoved = new ArrayList<String>();
102  14 for (int i = 0; i < currentAttributes.getLength(); i++) {
103  3 String attributeName = currentAttributes.item(i).getNodeName();
104  3 String pattern = ATTRIBUTE_SEPARATOR + attributeName.toLowerCase() + ATTRIBUTE_SEPARATOR;
105  3 if (allowedAttributes.indexOf(pattern) == -1) {
106  0 attributesToBeRemoved.add(attributeName);
107    }
108    }
109   
110    // Remove those attributes collected above.
111  11 for (String attribute : attributesToBeRemoved) {
112  0 currentAttributes.removeNamedItem(attribute);
113    }
114    }
115  268 if (node.hasChildNodes()) {
116  232 NodeList children = node.getChildNodes();
117  575 for (int i = 0; i < children.getLength(); i++) {
118  343 filter(children.item(i), attributeMappings);
119    }
120    }
121    }
122    }
123    }