1 21 package au.id.jericho.lib.html; 22 23 import java.util.*; 24 25 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 47 public int getRow() { 48 return row; 49 } 50 51 55 public int getColumn() { 56 return column; 57 } 58 59 63 public int getPos() { 64 return pos; 65 } 66 67 74 public String toString() { 75 return appendTo(new StringBuffer (20)).toString(); 76 } 77 78 StringBuffer appendTo(final StringBuffer 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 |