1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package org.xwiki.rendering.wikimodel.util.tmp

File ListBuilder1.java

 

Coverage histogram

../../../../../../img/srcFileCovDistChart0.png
83% of files have more coverage

Code metrics

20
44
11
2
160
106
22
0.5
4
5.5
2

Classes

Class Line # Actions
ListBuilder1 34 35 0% 18 60
0.00%
ListBuilder1.CharPos 36 9 0% 4 15
0.00%
 

Contributing tests

No tests hitting this source file were found.

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.tmp;
21   
22    import java.util.ArrayList;
23    import java.util.List;
24   
25    import org.xwiki.rendering.wikimodel.util.IListListener;
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: 52cac276a313e0c960e7d290963e360299761646 $
32    * @since 4.0M1
33    */
 
34    public class ListBuilder1
35    {
 
36    private static class CharPos
37    {
38    public final int pos;
39   
40    public final char rowChar;
41   
42    public final char treeChar;
43   
 
44  0 toggle public CharPos(char treeChar, char rowChar, int pos)
45    {
46  0 this.treeChar = treeChar;
47  0 this.rowChar = rowChar;
48  0 this.pos = pos;
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 p = (CharPos) obj;
61  0 return p.pos == pos && p.treeChar == treeChar;
62    }
63    }
64   
65    private TreeBuilder1<CharPos> fBuilder = new TreeBuilder1<CharPos>(
66    new TreeBuilder1.ITreeBuilderListener<CharPos>()
67    {
 
68  0 toggle public void beginItem(int depth, CharPos data)
69    {
70  0 fListener.beginRow(data.treeChar, data.rowChar);
71    }
72   
 
73  0 toggle public void beginLevel(int depth, CharPos prev)
74    {
75  0 fListener.beginTree(prev.treeChar);
76    }
77   
 
78  0 toggle public void endItem(int depth, CharPos data)
79    {
80  0 fListener.endRow(data.treeChar, data.rowChar);
81    }
82   
 
83  0 toggle public void endLevel(int i, CharPos prev)
84    {
85  0 fListener.endTree(prev.treeChar);
86    }
87    });
88   
89    private IListListener fListener;
90   
91    private List<CharPos> fPrevRow;
92   
93    /**
94    * @param listener
95    */
 
96  0 toggle public ListBuilder1(IListListener listener)
97    {
98  0 fListener = listener;
99    }
100   
101    /**
102    * @param rowParams the parameters of the row
103    */
 
104  0 toggle public void alignContext(String row)
105    {
106  0 List<CharPos> list = getCharPositions(row);
107  0 int prevLen = fPrevRow != null ? fPrevRow.size() : 0;
108  0 int currentLen = list.size();
109  0 int len = Math.min(currentLen, prevLen);
110  0 int i;
111  0 for (i = 0; i < len - 1; i++) {
112  0 CharPos prev = fPrevRow.get(i);
113  0 CharPos second = list.get(i);
114  0 if (!prev.equals(second)) {
115  0 break;
116    }
117    }
118  0 fBuilder.trim(i, true);
119  0 if (i < currentLen) {
120  0 for (; i < currentLen; i++) {
121  0 CharPos p = list.get(i);
122  0 fBuilder.align(p.pos, p);
123    }
124    }
125  0 fPrevRow = list;
126    }
127   
 
128  0 toggle public void finish()
129    {
130  0 fBuilder.finish();
131    }
132   
 
133  0 toggle private List<CharPos> getCharPositions(String s)
134    {
135  0 List<CharPos> list = new ArrayList<CharPos>();
136  0 char[] array = s.toCharArray();
137  0 int pos = 0;
138  0 for (int i = 0; i < array.length; i++) {
139  0 char ch = array[i];
140  0 if (ch == '\r' || ch == '\n') {
141  0 continue;
142    }
143  0 if (!Character.isSpaceChar(ch)) {
144  0 char treeChar = getTreeType(ch);
145  0 list.add(new CharPos(treeChar, ch, pos));
146    }
147  0 pos++;
148    }
149  0 return list;
150    }
151   
152    /**
153    * @param rowType the type of the row
154    * @return the type of the tree corresponding to the given row type
155    */
 
156  0 toggle protected char getTreeType(char rowType)
157    {
158  0 return rowType;
159    }
160    }