KickJava   Java API By Example, From Geeks To Geeks.

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


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 import java.text.CharacterIterator JavaDoc;
32
33 /* The text cursor is purposely lightweight. It does not update with the
34  * text, nor does is allow changes.
35  */

36 public abstract class CharCursor implements CharacterIterator JavaDoc {
37   /**
38    * returns the current location of the cursor
39    */

40   public abstract int getIndex();
41
42   public abstract int getBeginIndex();
43   public abstract int getEndIndex();
44   /**
45    * sets the cursor to the position
46    */

47   public abstract char setIndex(int pos);
48
49   public abstract char next();
50
51   public abstract char previous();
52
53   public abstract char current();
54
55   public abstract Object JavaDoc clone();
56
57   public char first()
58   {
59     return setIndex(getBeginIndex());
60   }
61   public char last()
62   {
63     return setIndex(getEndIndex());
64   }
65
66   /**
67    * our stuff
68    */

69   public char read()
70   {
71     char value = current();
72     next();
73     return value;
74   }
75
76   public char prev()
77   {
78     int pos = getIndex();
79     char value = previous();
80     setIndex(pos);
81     return value;
82   }
83
84   /**
85    * Skips the next n characters
86    */

87   public char skip(int n)
88   {
89     for (int i = 0; i < n; i++)
90       next();
91     return current();
92   }
93
94   public void subseq(CharBuffer cb, int begin, int end)
95   {
96     int pos = getIndex();
97
98     char ch = setIndex(begin);
99     for (int i = begin; i < end; i++) {
100       if (ch != DONE)
101     cb.append(ch);
102       ch = next();
103     }
104
105     setIndex(pos);
106   }
107
108   public void subseq(CharBuffer cb, int length)
109   {
110     char ch = current();
111     for (int i = 0; i < length; i++) {
112       if (ch != DONE)
113     cb.append(ch);
114       ch = next();
115     }
116   }
117
118   /**
119    * True if the cursor matches the character buffer
120    *
121    * If match fails, return the pointer to its original.
122    */

123   public boolean regionMatches(char []cb, int offset, int length)
124   {
125     int pos = getIndex();
126
127     char ch = current();
128     for (int i = 0; i < length; i++) {
129       if (cb[i + offset] != ch) {
130     setIndex(pos);
131     return false;
132       }
133       ch = next();
134     }
135
136     return true;
137   }
138
139   /**
140    * True if the cursor matches the character buffer
141    *
142    * If match fails, return the pointer to its original.
143    */

144   public boolean regionMatchesIgnoreCase(char []cb, int offset, int length)
145   {
146     int pos = getIndex();
147
148     char ch = current();
149     for (int i = 0; i < length; i++) {
150       if (ch == DONE) {
151     setIndex(pos);
152     return false;
153       }
154
155       if (Character.toLowerCase(cb[i + offset]) != Character.toLowerCase(ch)) {
156     setIndex(pos);
157     return false;
158       }
159
160       ch = next();
161     }
162
163     return true;
164   }
165 }
166
Popular Tags