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

File AbstractFilterContentAlterer.java

 

Coverage histogram

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

Code metrics

10
20
1
1
78
39
6
0.3
20
1
6

Classes

Class Line # Actions
AbstractFilterContentAlterer 37 20 0% 6 0
1.0100%
 

Contributing tests

This file is covered by 128 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   
21    package org.xwiki.annotation.internal.content;
22   
23    import java.util.HashMap;
24    import java.util.Map;
25   
26    import org.xwiki.annotation.content.AlteredContent;
27    import org.xwiki.annotation.content.filter.Filter;
28   
29    /**
30    * Content alterer to filter out characters which are part of a wiki syntax. This class should be extended to provide
31    * the {@link Filter} used to determine which characters are not meaningful text and should be preserved (not
32    * syntax characters).
33    *
34    * @version $Id: d910da7b45db2288657fcd46784bf3d2533d3373 $
35    * @since 2.3M1
36    */
 
37    public abstract class AbstractFilterContentAlterer extends AbstractContentAlterer
38    {
39    /**
40    * @return Syntax filter used to determine the accepted characters in the content altered by this alterer.
41    */
42    protected abstract Filter getFilter();
43   
 
44  246 toggle @Override
45    public AlteredContent alter(CharSequence sequence)
46    {
47  246 StringBuffer buffer = new StringBuffer();
48  246 Map<Integer, Integer> initialToAltered = new HashMap<Integer, Integer>();
49  246 Map<Integer, Integer> alteredToInitial = new HashMap<Integer, Integer>();
50   
51    // index altered
52  246 int j = 0;
53    // number of refused chars
54  246 int z = 0;
55  246 Character c;
56  4683 for (int i = 0; i < sequence.length(); ++i) {
57  4437 c = sequence.charAt(i);
58  4437 if (getFilter().accept(c)) {
59  3702 buffer.append(c);
60  8092 for (int t = 0; t <= z; ++t) {
61    // 1+0;1 // 1+1;1
62  4390 initialToAltered.put(i - t, j);
63    }
64  3702 alteredToInitial.put(j, i);
65  3702 ++j;
66  3702 z = 0;
67    } else {
68  735 z++;
69    }
70    }
71  246 if (j != 0) {
72  245 for (int t = 0; t < z; ++t) {
73  15 initialToAltered.put(sequence.length() - 1 - t, j - 1);
74    }
75    }
76  246 return new OffsetsMapAlteredContent(buffer.toString(), sequence.length(), initialToAltered, alteredToInitial);
77    }
78    }