Cursor.java
1 // HTMLParser Library $Name: v1_6_20060319 $ - A java-based parser for HTML 2 // http://sourceforge.org/projects/htmlparser 3 // Copyright (C) 2004 Derrick Oswald 4 // 5 // Revision Control Information 6 // 7 // $Source: /cvsroot/htmlparser/htmlparser/src/org/htmlparser/lexer/Cursor.java,v $ 8 // $Author: derrickoswald $ 9 // $Date: 2005/05/15 11:49:04 $ 10 // $Revision: 1.20 $ 11 // 12 // This library is free software; you can redistribute it and/or 13 // modify it under the terms of the GNU Lesser General Public 14 // License as published by the Free Software Foundation; either 15 // version 2.1 of the License, or (at your option) any later version. 16 // 17 // This library is distributed in the hope that it will be useful, 18 // but WITHOUT ANY WARRANTY; without even the implied warranty of 19 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 20 // Lesser General Public License for more details. 21 // 22 // You should have received a copy of the GNU Lesser General Public 23 // License along with this library; if not, write to the Free Software 24 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 25 // 26 27 package org.htmlparser.lexer; 28 29 import java.io.Serializable; 30 import org.htmlparser.util.sort.Ordered; 31 32 /** 33 * A bookmark in a page. 34 * This class remembers the page it came from and its position within the page. 35 */ 36 public class Cursor 37 implements 38 Serializable, 39 Ordered, 40 Cloneable 41 { 42 /** 43 * This cursor's position. 44 */ 45 protected int mPosition; 46 47 /** 48 * This cursor's page. 49 */ 50 protected Page mPage; 51 52 /** 53 * Construct a <code>Cursor</code> from the page and position given. 54 * @param page The page this cursor is on. 55 * @param offset The character offset within the page. 56 */ 57 public Cursor (Page page, int offset) 58 { 59 mPage = page; 60 mPosition = offset; 61 } 62 63 /** 64 * Get this cursor's page. 65 * @return The page associated with this cursor. 66 */ 67 public Page getPage () 68 { 69 return (mPage); 70 } 71 72 /** 73 * Get the position of this cursor. 74 * @return The cursor position. 75 */ 76 public int getPosition () 77 { 78 return (mPosition); 79 } 80 81 /** 82 * Set the position of this cursor. 83 * @param position The new cursor position. 84 */ 85 public void setPosition (int position) 86 { 87 mPosition = position; 88 } 89 90 /** 91 * Move the cursor position ahead one character. 92 */ 93 public void advance () 94 { 95 mPosition++; 96 } 97 98 /** 99 * Move the cursor position back one character. 100 */ 101 public void retreat () 102 { 103 mPosition--; 104 if (0 > mPosition) 105 mPosition = 0; 106 } 107 108 /** 109 * Make a new cursor just like this one. 110 * @return The new cursor positioned where <code>this</code> one is, 111 * and referring to the same page. 112 */ 113 public Cursor dup () 114 { 115 try 116 { 117 return ((Cursor)clone ()); 118 } 119 catch (CloneNotSupportedException cnse) 120 { 121 return (new Cursor (getPage (), getPosition ())); 122 } 123 } 124 125 /** 126 * Return a string representation of this cursor 127 * @return A string of the form "n[r,c]", where n is the character position, 128 * r is the row (zero based) and c is the column (zero based) on the page. 129 */ 130 public String toString () 131 { 132 StringBuffer ret; 133 134 ret = new StringBuffer (9 * 3 + 3); // three ints and delimiters 135 ret.append (getPosition ()); 136 ret.append ("["); 137 if (null != mPage) 138 ret.append (mPage.row (this)); 139 else 140 ret.append ("?"); 141 ret.append (","); 142 if (null != mPage) 143 ret.append (mPage.column (this)); 144 else 145 ret.append ("?"); 146 ret.append ("]"); 147 148 return (ret.toString ()); 149 } 150 151 // 152 // Ordered interface 153 // 154 155 /** 156 * Compare one reference to another. 157 * @param that The object to compare this to. 158 * @return A negative integer, zero, or a positive 159 * integer as this object is less than, equal to, 160 * or greater than that object. 161 * @see org.htmlparser.util.sort.Ordered 162 */ 163 public int compare (Object that) 164 { 165 Cursor r = (Cursor)that; 166 return (getPosition () - r.getPosition ()); 167 } 168 }