1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.wysiwyg.server.internal.cleaner

File StandAloneMacroFilter.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart6.png
69% of files have more coverage

Code metrics

10
18
2
1
90
49
12
0.67
9
2
6

Classes

Class Line # Actions
StandAloneMacroFilter 46 18 0% 12 14
0.5333333653.3%
 

Contributing tests

This file is covered by 3 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.wysiwyg.server.internal.cleaner;
21   
22    import java.util.ArrayList;
23    import java.util.List;
24    import java.util.Map;
25   
26    import javax.inject.Named;
27    import javax.inject.Singleton;
28   
29    import org.w3c.dom.Document;
30    import org.w3c.dom.Element;
31    import org.w3c.dom.Node;
32    import org.w3c.dom.NodeList;
33    import org.xwiki.component.annotation.Component;
34   
35    /**
36    * Looks for stand alone macros that are wrapped in paragraphs and unwraps them. In other words, looks for paragraphs
37    * that contain just a macro call and replaces these paragraphs with those macro calls. For instance {@code <p>
38    * <!--startmacro:toc|-||-|><!--stopmacro-->
39    * </p>} is replaced by {@code <!--startmacro:toc|-||-|><!--stopmacro-->}.
40    *
41    * @version $Id: cc11b64ac45bd857a9269a6c8d973f4f55241f35 $
42    */
43    @Component(roles = {HTMLFilter.class })
44    @Named("standAloneMacro")
45    @Singleton
 
46    public class StandAloneMacroFilter extends AbstractHTMLFilter
47    {
 
48  13 toggle @Override
49    public void filter(Document document, Map<String, String> parameters)
50    {
51  13 List<Element> wrappers = getStandAloneMacroWrappers(document);
52  13 for (int i = 0; i < wrappers.size(); i++) {
53  0 Element paragraph = wrappers.get(i);
54    // Replace the paragraph with the macro comments.
55  0 paragraph.getParentNode().insertBefore(paragraph.getFirstChild(), paragraph);
56  0 paragraph.getParentNode().insertBefore(paragraph.getLastChild(), paragraph);
57  0 paragraph.getParentNode().removeChild(paragraph);
58    }
59    }
60   
61    /**
62    * @param document a DOM document
63    * @return the list of paragraphs that contain just a macro call
64    */
 
65  13 toggle private List<Element> getStandAloneMacroWrappers(Document document)
66    {
67  13 NodeList paragraphs = document.getElementsByTagName("p");
68  13 List<Element> wrappers = new ArrayList<Element>();
69  16 for (int i = 0; i < paragraphs.getLength(); i++) {
70  3 Element paragraph = (Element) paragraphs.item(i);
71    // See if the paragraph contains only a macro call.
72    // Look for the start macro comment.
73  3 Node child = paragraph.getFirstChild();
74  3 if (child == null || child.getNodeType() != Node.COMMENT_NODE
75    || !child.getNodeValue().startsWith("startmacro:")) {
76  3 continue;
77    }
78    // Look for the stop macro comment.
79  0 do {
80  0 child = child.getNextSibling();
81  0 } while (child != null
82    && !(child.getNodeType() == Node.COMMENT_NODE && child.getNodeValue().equals("stopmacro")));
83    // See if there's something else inside the paragraph.
84  0 if (child != null && child.getNextSibling() == null) {
85  0 wrappers.add(paragraph);
86    }
87    }
88  13 return wrappers;
89    }
90    }