1. Project Clover database Sat Feb 2 2019 06:45:20 CET
  2. Package org.xwiki.rendering.wikimodel.util

File ListBuilder.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart8.png
56% of files have more coverage

Code metrics

10
32
13
2
152
99
19
0.59
2.46
6.5
1.46

Classes

Class Line # Actions
ListBuilder 34 20 0% 12 2
0.941176594.1%
ListBuilder.CharPos 36 12 0% 7 13
0.380952438.1%
 

Contributing tests

This file is covered by 185 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.rendering.wikimodel.util;
21   
22    import java.util.ArrayList;
23    import java.util.List;
24   
25    import org.apache.commons.lang3.builder.HashCodeBuilder;
26   
27    /**
28    * This is an internal utility class used as a context to keep in memory the
29    * current state of parsed trees (list items).
30    *
31    * @version $Id: 820612eb891de387d6892bad2381eb1ef6ccdd89 $
32    * @since 4.0M1
33    */
 
34    public class ListBuilder
35    {
 
36    static class CharPos implements TreeBuilder.IPos<CharPos>
37    {
38    private int fPos;
39   
40    private char fRowChar;
41   
42    private char fTreeChar;
43   
 
44  57896 toggle public CharPos(char treeChar, char rowChar, int pos)
45    {
46  57896 fPos = pos;
47  57896 fTreeChar = treeChar;
48  57896 fRowChar = rowChar;
49    }
50   
 
51  0 toggle @Override
52    public boolean equals(Object obj)
53    {
54  0 if (obj == this) {
55  0 return true;
56    }
57  0 if (!(obj instanceof CharPos)) {
58  0 return false;
59    }
60  0 CharPos pos = (CharPos) obj;
61  0 return equalsData(pos) && pos.fPos == fPos;
62    }
63   
 
64  0 toggle @Override
65    public int hashCode()
66    {
67  0 return new HashCodeBuilder()
68    .append(fTreeChar)
69    .append(fPos)
70    .toHashCode();
71    }
72   
73   
 
74  45060 toggle public boolean equalsData(CharPos pos)
75    {
76  45060 return pos.fTreeChar == fTreeChar;
77    }
78   
 
79  90432 toggle public int getPos()
80    {
81  90432 return fPos;
82    }
83    }
84   
85    TreeBuilder<CharPos> fBuilder = new TreeBuilder<CharPos>(
86    new TreeBuilder.ITreeListener<CharPos>()
87    {
 
88  43365 toggle public void onBeginRow(CharPos pos)
89    {
90  43365 fListener.beginRow(pos.fTreeChar, pos.fRowChar);
91    }
92   
 
93  12855 toggle public void onBeginTree(CharPos pos)
94    {
95  12855 fListener.beginTree(pos.fTreeChar);
96    }
97   
 
98  43365 toggle public void onEndRow(CharPos pos)
99    {
100  43365 fListener.endRow(pos.fTreeChar, pos.fRowChar);
101    }
102   
 
103  12855 toggle public void onEndTree(CharPos pos)
104    {
105  12855 fListener.endTree(pos.fTreeChar);
106    }
107    });
108   
109    private IListListener fListener;
110   
 
111  8257 toggle public ListBuilder(IListListener listener)
112    {
113  8257 fListener = listener;
114    }
115   
116    /**
117    * @param row the parameters of the row
118    */
 
119  51595 toggle public void alignContext(String row)
120    {
121  51595 List<CharPos> list = getCharPositions(row);
122  51595 fBuilder.align(list);
123    }
124   
 
125  51595 toggle private List<CharPos> getCharPositions(String s)
126    {
127  51595 List<CharPos> list = new ArrayList<CharPos>();
128  51595 char[] array = s.toCharArray();
129  51595 int pos = 0;
130  110251 for (int i = 0; i < array.length; i++) {
131  58656 char ch = array[i];
132  58656 if (ch == '\r' || ch == '\n') {
133  0 continue;
134    }
135  58656 if (!Character.isSpaceChar(ch)) {
136  57896 char treeChar = getTreeType(ch);
137  57896 list.add(new CharPos(treeChar, ch, pos));
138    }
139  58656 pos++;
140    }
141  51595 return list;
142    }
143   
144    /**
145    * @param rowType the type of the row
146    * @return the type of the tree corresponding to the given row type
147    */
 
148  453 toggle protected char getTreeType(char rowType)
149    {
150  453 return rowType;
151    }
152    }