1 /*
2 * Copyright (c) 2001-2005,
3 * RedVerst Group, ISP RAS http://www.ispras.ru
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are met:
8 *
9 * 1. Redistributions of source code must retain the above copyright notice, this
10 * list of conditions and the following disclaimer.
11 *
12 * 2. Redistributions in binary form must reproduce the above copyright notice,
13 * this list of conditions and the following disclaimer in the documentation
14 * and/or other materials provided with the distribution.
15 *
16 * 3. The names "ATP", "TreeDL", "RedVerst", "ISP RAS"
17 * may not be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
22 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
24 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
27 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
29 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31
32 package com.unitesk.atp.text.filters;
33
34 import com.unitesk.atp.text.location.Position;
35
36 /***
37 * Calculates line and position in output text.
38 * Line is counted from 1.
39 * Column is counted from 0.
40 *
41 * @author <A href="mailto:demakov@ispras.ru">Alexey Demakov</A>
42 * @version $Id: PositionFilter.java,v 1.3 2005/07/06 10:24:06 all-x Exp $
43 * @since 3.3-beta-050628
44 */
45 public class PositionFilter extends Filter
46 {
47 public static final int DEFAULT_TAB_SIZE = 8;
48 public static final boolean DEFAULT_FIXED_TAB_SIZE = false;
49
50 /***
51 * Filter constructor.
52 *
53 * @param client Filter client.
54 * @param tabSize Tab size.
55 * @param fixedTabSize If <code>true</code>, tab char always increases
56 * column by <code>tabSize</code>.
57 * Otherwise column is increased to the next value
58 * divisible by <code>tabSize</code>.
59 */
60 public PositionFilter( TextReceiver client, int tabSize, boolean fixedTabSize )
61 {
62 super( client );
63 this.tabSize = tabSize;
64 this.fixedTabSize = fixedTabSize;
65 line = 1;
66 column = 0;
67 }
68
69 public PositionFilter( TextReceiver client )
70 {
71 this( client, DEFAULT_TAB_SIZE, DEFAULT_FIXED_TAB_SIZE );
72 }
73
74 public int getLine() { return line; }
75
76 public void setLine( int l ) { line = l; }
77
78 public int getColumn() { return column; }
79
80 public void setColumn( int c ) { column = c; }
81
82 public Position getPosition() { return new Position( line, column ); }
83
84 public void setPosition( Position position )
85 {
86 line = position.getLine();
87 column = position.getColumn();
88 }
89
90 public static int lineLength( String s ) { return updateColumn( 0, s ); }
91
92 public static int updateColumn( int currentColumn, String s )
93 {
94 int len = s.length();
95 if( len == 0 ) return currentColumn;
96
97 for( int i = 0; i < len; )
98 {
99 // skip all before '\t'
100 int j = s.indexOf( "\t", i );
101 if( j < 0 ) j = len;
102 if( i < j )
103 {
104 currentColumn += j - i;
105 i = j;
106 }
107 if( i == len ) continue;
108 // skip '\t'
109 i++;
110 // update currentColumn by actual length of '\t'
111 currentColumn += 8 - currentColumn % 8;
112 }
113 return currentColumn;
114 }
115
116 //----- TextReceiver -------------------------------------------------------
117
118 public void txt( String s )
119 {
120 column = updateColumn( column, s );
121 super.txt( s );
122 }
123
124 public void nl()
125 {
126 column = 0;
127 line++;
128 super.nl();
129 }
130
131 //----- Implementation -----------------------------------------------------
132
133 protected int tabSize;
134 protected boolean fixedTabSize;
135 private int line;
136 private int column;
137 }