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

File OfficeHTMLCleaner.java

 

Coverage histogram

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

Code metrics

0
9
3
1
155
79
3
0.33
3
3
1

Classes

Class Line # Actions
OfficeHTMLCleaner 47 9 0% 3 0
1.0100%
 

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.cleaner;
21   
22    import java.io.Reader;
23    import java.util.ArrayList;
24    import java.util.Arrays;
25    import java.util.Collections;
26    import java.util.List;
27   
28    import javax.inject.Inject;
29    import javax.inject.Named;
30    import javax.inject.Singleton;
31   
32    import org.w3c.dom.Document;
33    import org.xwiki.component.annotation.Component;
34    import org.xwiki.xml.html.HTMLCleaner;
35    import org.xwiki.xml.html.HTMLCleanerConfiguration;
36    import org.xwiki.xml.html.filter.HTMLFilter;
37   
38    /**
39    * {@link HTMLCleaner} for cleaning HTML generated from an office server.
40    *
41    * @version $Id: 6a1d4314372915b173afb416ecc6156ae9b87dc0 $
42    * @since 1.8M1
43    */
44    @Component
45    @Named("openoffice")
46    @Singleton
 
47    public class OfficeHTMLCleaner implements HTMLCleaner
48    {
49    /**
50    * Default html cleaner component used internally.
51    */
52    @Inject
53    private HTMLCleaner defaultHtmlCleaner;
54   
55    /**
56    * {@link HTMLFilter} for stripping various tags.
57    */
58    @Inject
59    @Named("officeimporter/stripper")
60    private HTMLFilter stripperFilter;
61   
62    /**
63    * {@link HTMLFilter} filtering styles.
64    */
65    @Inject
66    @Named("officeimporter/style")
67    private HTMLFilter styleFilter;
68   
69    /**
70    * {@link HTMLFilter} for stripping redundant tags.
71    */
72    @Inject
73    @Named("officeimporter/redundancy")
74    private HTMLFilter redundancyFilter;
75   
76    /**
77    * {@link HTMLFilter} for cleaning empty paragraphs.
78    */
79    @Inject
80    @Named("officeimporter/paragraph")
81    private HTMLFilter paragraphFilter;
82   
83    /**
84    * {@link HTMLFilter} for filtering image tags.
85    */
86    @Inject
87    @Named("officeimporter/image")
88    private HTMLFilter imageFilter;
89   
90    /**
91    * {@link HTMLFilter} for filtering HTML anchors.
92    */
93    @Inject
94    @Named("officeimporter/anchor")
95    private HTMLFilter anchorFilter;
96   
97    /**
98    * {@link HTMLFilter} for filtering lists.
99    */
100    @Inject
101    @Named("officeimporter/list")
102    private HTMLFilter listFilter;
103   
104    /**
105    * {@link HTMLFilter} for filtering tables.
106    */
107    @Inject
108    @Named("officeimporter/table")
109    private HTMLFilter tableFilter;
110   
111    /**
112    * {@link HTMLFilter} for filtering line breaks.
113    */
114    @Inject
115    @Named("officeimporter/linebreak")
116    private HTMLFilter lineBreakFilter;
117   
 
118  34 toggle @Override
119    public Document clean(Reader originalHtmlContent)
120    {
121    // Add special parameters used in filters
122  34 HTMLCleanerConfiguration configuration = getDefaultConfiguration();
123  34 configuration.setParameters(Collections.singletonMap("filterStyles", "strict"));
124   
125  34 return clean(originalHtmlContent, configuration);
126    }
127   
 
128  65 toggle @Override
129    public Document clean(Reader originalHtmlContent, HTMLCleanerConfiguration configuration)
130    {
131  65 return this.defaultHtmlCleaner.clean(originalHtmlContent, configuration);
132    }
133   
 
134  66 toggle @Override
135    public HTMLCleanerConfiguration getDefaultConfiguration()
136    {
137  66 HTMLCleanerConfiguration configuration = this.defaultHtmlCleaner.getDefaultConfiguration();
138   
139    // Add office cleaning filters after the default filters.
140  66 List<HTMLFilter> filters = new ArrayList<HTMLFilter>(configuration.getFilters());
141  66 filters.addAll(Arrays.asList(
142    this.stripperFilter,
143    this.styleFilter,
144    this.redundancyFilter,
145    this.paragraphFilter,
146    this.imageFilter,
147    this.anchorFilter,
148    this.listFilter,
149    this.tableFilter,
150    this.lineBreakFilter));
151  66 configuration.setFilters(filters);
152   
153  66 return configuration;
154    }
155    }