KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > util > StringCharCursor


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.util;
30
31 /* The text cursor is purposely lightweight. It does not update with the
32  * text, nor does is allow changes.
33  */

34 public class StringCharCursor extends CharCursor {
35   String JavaDoc string;
36   int pos;
37
38   public StringCharCursor(String JavaDoc string)
39   {
40     this.string = string;
41     this.pos = 0;
42   }
43
44   public StringCharCursor(String JavaDoc string, int offset)
45   {
46     this.string = string;
47     this.pos = offset;
48   }
49
50   /**
51    * returns the current location of the cursor
52    */

53   public int getIndex() { return pos; }
54
55   public int getBeginIndex() { return 0; }
56   public int getEndIndex() { return string.length(); }
57   /**
58    * sets the cursor to the position
59    */

60   public char setIndex(int pos)
61   {
62     if (pos < 0) {
63       this.pos = 0;
64       return DONE;
65     }
66     else if (pos >= string.length()) {
67       this.pos = string.length();
68       return DONE;
69     }
70     else {
71       this.pos = pos;
72       return string.charAt(pos);
73     }
74   }
75
76   /**
77    * reads a character from the cursor
78    *
79    * @return -1 on EOF
80    */

81   public char next()
82   {
83     if (++pos >= string.length()) {
84       pos = string.length();
85       return DONE;
86     }
87     else
88       return string.charAt(pos);
89   }
90
91   /**
92    * reads a character from the cursor
93    *
94    * @return -1 on EOF
95    */

96   public char previous()
97   {
98     if (--pos < 0) {
99       pos = 0;
100       return DONE;
101     }
102     else
103       return string.charAt(pos);
104   }
105
106   public char current()
107   {
108     if (pos >= string.length())
109       return DONE;
110     else
111       return string.charAt(pos);
112   }
113
114   /**
115    * Skips the next n characters
116    */

117   public char skip(int n)
118   {
119     pos += n;
120     if (pos >= string.length()) {
121       pos = string.length();
122       return DONE;
123     } else
124       return string.charAt(pos);
125   }
126
127   public void init(String JavaDoc string)
128   {
129     this.string = string;
130     this.pos = 0;
131   }
132
133   public Object JavaDoc clone()
134   {
135     return new StringCharCursor(string);
136   }
137 }
138
Popular Tags