KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > au > id > jericho > lib > html > RowColumnVector


1 // Jericho HTML Parser - Java based library for analysing and manipulating HTML
2
// Version 2.2
3
// Copyright (C) 2006 Martin Jericho
4
// http://sourceforge.net/projects/jerichohtml/
5
//
6
// This library is free software; you can redistribute it and/or
7
// modify it under the terms of the GNU Lesser General Public
8
// License as published by the Free Software Foundation; either
9
// version 2.1 of the License, or (at your option) any later version.
10
// http://www.gnu.org/copyleft/lesser.html
11
//
12
// This library is distributed in the hope that it will be useful,
13
// but WITHOUT ANY WARRANTY; without even the implied warranty of
14
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15
// Lesser General Public License for more details.
16
//
17
// You should have received a copy of the GNU Lesser General Public
18
// License along with this library; if not, write to the Free Software
19
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20

21 package au.id.jericho.lib.html;
22
23 import java.util.*;
24
25 /**
26  * Represents the row and column number of a character position in the source document.
27  * <p>
28  * Obtained using the {@link Source#getRowColumnVector(int pos)} method.
29  */

30 public final class RowColumnVector {
31     private int row;
32     private int column;
33     private int pos;
34     
35     private static final RowColumnVector FIRST=new RowColumnVector(1,1,0);
36
37     private RowColumnVector(final int row, final int column, final int pos) {
38         this.row=row;
39         this.column=column;
40         this.pos=pos;
41     }
42
43     /**
44      * Returns the row number of this character position in the source document.
45      * @return the row number of this character position in the source document.
46      */

47     public int getRow() {
48         return row;
49     }
50     
51     /**
52      * Returns the column number of this character position in the source document.
53      * @return the column number of this character position in the source document.
54      */

55     public int getColumn() {
56         return column;
57     }
58
59     /**
60      * Returns the character position in the source document.
61      * @return the character position in the source document.
62      */

63     public int getPos() {
64         return pos;
65     }
66     
67     /**
68      * Returns a string representation of this character position.
69      * <p>
70      * The returned string has the format "<code>(</code><i>row</i><code>,</code><i>column</i><code>:</code><i>pos</i><code>)</code>".
71      *
72      * @return a string representation of this character position.
73      */

74     public String JavaDoc toString() {
75         return appendTo(new StringBuffer JavaDoc(20)).toString();
76     }
77
78     StringBuffer JavaDoc appendTo(final StringBuffer JavaDoc sb) {
79         return sb.append('(').append(row).append(',').append(column).append(':').append(pos).append(')');
80     }
81     
82     static RowColumnVector[] getCacheArray(final Source source) {
83         final int lastSourcePos=source.end-1;
84         final ArrayList list=new ArrayList();
85         int pos=0;
86         list.add(FIRST);
87         int row=1;
88         while (pos<=lastSourcePos) {
89             final char ch=source.charAt(pos);
90             if (ch=='\n' || (ch=='\r' && (pos==lastSourcePos || source.charAt(pos+1)!='\n'))) list.add(new RowColumnVector(++row,1,pos+1));
91             pos++;
92         }
93         return (RowColumnVector[])list.toArray(new RowColumnVector[list.size()]);
94     }
95
96     static RowColumnVector get(final RowColumnVector[] cacheArray, final int pos) {
97         int low=0;
98         int high=cacheArray.length-1;
99         while (true) {
100             int mid=(low+high) >> 1;
101             final RowColumnVector rowColumnVector=cacheArray[mid];
102             if (rowColumnVector.pos<pos) {
103                 if (mid==high) return new RowColumnVector(rowColumnVector.row,pos-rowColumnVector.pos+1,pos);
104                 low=mid+1;
105             } else if (rowColumnVector.pos>pos) {
106                 high=mid-1;
107             } else {
108                 return rowColumnVector;
109             }
110         }
111     }
112 }
113
Popular Tags