KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > htmlparser > lexer > Cursor


1 // HTMLParser Library $Name: v1_5_20050313 $ - 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: 2004/07/31 16:42:31 $
10
// $Revision: 1.18 $
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 JavaDoc;
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 JavaDoc,
39         Ordered,
40         Cloneable JavaDoc
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 JavaDoc cnse)
120         {
121             return (new Cursor (getPage (), getPosition ()));
122         }
123     }
124
125     public String JavaDoc toString ()
126     {
127         StringBuffer JavaDoc ret;
128
129         ret = new StringBuffer JavaDoc (9 * 3 + 3); // three ints and delimiters
130
ret.append (getPosition ());
131         ret.append ("[");
132         if (null != mPage)
133             ret.append (mPage.row (this));
134         else
135             ret.append ("?");
136         ret.append (",");
137         if (null != mPage)
138             ret.append (mPage.column (this));
139         else
140             ret.append ("?");
141         ret.append ("]");
142
143         return (ret.toString ());
144     }
145
146     //
147
// Ordered interface
148
//
149

150     /**
151      * Compare one reference to another.
152      * @see org.htmlparser.util.sort.Ordered
153      */

154     public int compare (Object JavaDoc that)
155     {
156         Cursor r = (Cursor)that;
157         return (getPosition () - r.getPosition ());
158     }
159 }
160
161        
Popular Tags