1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.xml.html.filter

File AbstractHTMLFilter.java

 

Coverage histogram

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

Code metrics

22
38
6
1
154
79
17
0.45
6.33
6
2.83

Classes

Class Line # Actions
AbstractHTMLFilter 36 38 0% 17 0
1.0100%
 

Contributing tests

This file is covered by 121 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.xml.html.filter;
21   
22    import java.util.ArrayList;
23    import java.util.List;
24   
25    import org.w3c.dom.Element;
26    import org.w3c.dom.Node;
27    import org.w3c.dom.NodeList;
28    import org.xwiki.xml.html.HTMLConstants;
29   
30    /**
31    * Abstract implementation of {@link HTMLFilter} providing utility methods for various common w3c dom operations.
32    *
33    * @version $Id: 170817c44b0263039e89f5345a5504390e3be7ef $
34    * @since 1.8M2
35    */
 
36    public abstract class AbstractHTMLFilter implements HTMLFilter, HTMLConstants
37    {
38    /**
39    * Utility method for filtering an element's children with a tagName.
40    *
41    * @param parent the parent {@link Element}.
42    * @param tagName expected tagName of the children elements.
43    * @return list of children elements with the provided tagName.
44    */
 
45  18 toggle protected List<Element> filterChildren(Element parent, String tagName)
46    {
47  18 List<Element> result = new ArrayList<Element>();
48  18 Node current = parent.getFirstChild();
49  37 while (current != null) {
50  19 if (current.getNodeName().equals(tagName)) {
51  9 result.add((Element) current);
52    }
53  19 current = current.getNextSibling();
54    }
55  18 return result;
56    }
57   
58    /**
59    * Utility method for filtering an element's descendants by their tag names.
60    *
61    * @param parent the parent {@link Element}.
62    * @param tagNames an array of tagNames.
63    * @return list of descendants of the parent element having one of given tag names.
64    */
 
65  153999 toggle protected List<Element> filterDescendants(Element parent, String[] tagNames)
66    {
67  154000 List<Element> result = new ArrayList<Element>();
68  154000 for (String tagName : tagNames) {
69  192534 NodeList nodes = parent.getElementsByTagName(tagName);
70  225324 for (int i = 0; i < nodes.getLength(); i++) {
71  32785 Element element = (Element) nodes.item(i);
72  32785 result.add(element);
73    }
74    }
75  154007 return result;
76    }
77   
78    /**
79    * Utility method for filtering an element's descendants by their tag names and an {@link ElementSelector}.
80    *
81    * @param parent the parent {@link Element}.
82    * @param tagNames an array of tagNames.
83    * @param elementSelector an {@link ElementSelector} that allows further filtering of elements.
84    * @return list of descendants of the parent element having one of given tag names.
85    */
 
86  331 toggle protected List<Element> filterDescendants(Element parent, String[] tagNames, ElementSelector elementSelector)
87    {
88  331 List<Element> result = new ArrayList<Element>();
89  331 for (String tagName : tagNames) {
90  1651 NodeList nodes = parent.getElementsByTagName(tagName);
91  1877 for (int i = 0; i < nodes.getLength(); i++) {
92  226 Element element = (Element) nodes.item(i);
93  226 if (elementSelector.isSelected(element)) {
94  120 result.add(element);
95    }
96    }
97    }
98  331 return result;
99    }
100   
101    /**
102    * Utility method for checking if a list of elements have the same attribute set. If the checkValue is true, the
103    * values of the given attribute will be checked for equivalency.
104    *
105    * @param elements the list of elements.
106    * @param attributeName Name of the attribute.
107    * @param checkValue flag indicating if the value of the attribute should be equal among all the elements.
108    * @return true if the given attribute is present and the value check is passing.
109    */
 
110  14 toggle protected boolean hasAttribute(List<Element> elements, String attributeName, boolean checkValue)
111    {
112  14 boolean hasAttribute = true;
113  14 if (!checkValue) {
114  2 for (Element e : elements) {
115  4 hasAttribute = e.hasAttribute(attributeName) ? hasAttribute : false;
116    }
117    } else {
118  12 String attributeValue = null;
119  12 for (Element e : elements) {
120  13 attributeValue = attributeValue == null ? e.getAttribute(attributeName) : attributeValue;
121  13 hasAttribute = e.getAttribute(attributeName).equals(attributeValue) ? hasAttribute : false;
122    }
123    }
124  14 return hasAttribute;
125    }
126   
127    /**
128    * Replaces the given {@link Element} with it's children.
129    *
130    * @param element the {@link Element} to be replaced.
131    */
 
132  41 toggle protected void replaceWithChildren(Element element)
133    {
134  41 Element parent = (Element) element.getParentNode();
135  155 while (element.getFirstChild() != null) {
136  114 parent.insertBefore(element.removeChild(element.getFirstChild()), element);
137    }
138  41 parent.removeChild(element);
139    }
140   
141    /**
142    * Moves all child elements of the parent into destination element.
143    *
144    * @param parent the parent {@link Element}.
145    * @param destination the destination {@link Element}.
146    */
 
147  3 toggle protected void moveChildren(Element parent, Element destination)
148    {
149  3 NodeList children = parent.getChildNodes();
150  7 while (children.getLength() > 0) {
151  4 destination.appendChild(parent.removeChild(parent.getFirstChild()));
152    }
153    }
154    }