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

File AbstractHTMLFilter.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart9.png
38% 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 6
0.9090909490.9%
 

Contributing tests

This file is covered by 113 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  12 toggle protected List<Element> filterChildren(Element parent, String tagName)
46    {
47  12 List<Element> result = new ArrayList<Element>();
48  12 Node current = parent.getFirstChild();
49  21 while (current != null) {
50  9 if (current.getNodeName().equals(tagName)) {
51  2 result.add((Element) current);
52    }
53  9 current = current.getNextSibling();
54    }
55  12 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  20351 toggle protected List<Element> filterDescendants(Element parent, String[] tagNames)
66    {
67  20351 List<Element> result = new ArrayList<Element>();
68  20351 for (String tagName : tagNames) {
69  25468 NodeList nodes = parent.getElementsByTagName(tagName);
70  31721 for (int i = 0; i < nodes.getLength(); i++) {
71  6253 Element element = (Element) nodes.item(i);
72  6253 result.add(element);
73    }
74    }
75  20351 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  330 toggle protected List<Element> filterDescendants(Element parent, String[] tagNames, ElementSelector elementSelector)
87    {
88  330 List<Element> result = new ArrayList<Element>();
89  330 for (String tagName : tagNames) {
90  1650 NodeList nodes = parent.getElementsByTagName(tagName);
91  1874 for (int i = 0; i < nodes.getLength(); i++) {
92  224 Element element = (Element) nodes.item(i);
93  224 if (elementSelector.isSelected(element)) {
94  119 result.add(element);
95    }
96    }
97    }
98  330 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  10 toggle protected boolean hasAttribute(List<Element> elements, String attributeName, boolean checkValue)
111    {
112  10 boolean hasAttribute = true;
113  10 if (!checkValue) {
114  0 for (Element e : elements) {
115  0 hasAttribute = e.hasAttribute(attributeName) ? hasAttribute : false;
116    }
117    } else {
118  10 String attributeValue = null;
119  10 for (Element e : elements) {
120  9 attributeValue = attributeValue == null ? e.getAttribute(attributeName) : attributeValue;
121  9 hasAttribute = e.getAttribute(attributeName).equals(attributeValue) ? hasAttribute : false;
122    }
123    }
124  10 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  40 toggle protected void replaceWithChildren(Element element)
133    {
134  40 Element parent = (Element) element.getParentNode();
135  152 while (element.getFirstChild() != null) {
136  112 parent.insertBefore(element.removeChild(element.getFirstChild()), element);
137    }
138  40 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  2 toggle protected void moveChildren(Element parent, Element destination)
148    {
149  2 NodeList children = parent.getChildNodes();
150  4 while (children.getLength() > 0) {
151  2 destination.appendChild(parent.removeChild(parent.getFirstChild()));
152    }
153    }
154    }