001: /*
002: * StringSource.java: TokenizerSource implementation for strings.
003: *
004: * Copyright (C) 2004 Heiko Blau
005: *
006: * This file belongs to the JTopas Library.
007: * JTopas is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU Lesser General Public License as published by the
009: * Free Software Foundation; either version 2.1 of the License, or (at your
010: * option) any later version.
011: *
012: * This software is distributed in the hope that it will be useful, but WITHOUT
013: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014: * FITNESS FOR A PARTICULAR PURPOSE.
015: * See the GNU Lesser General Public License for more details.
016: *
017: * You should have received a copy of the GNU Lesser General Public License along
018: * with JTopas. If not, write to the
019: *
020: * Free Software Foundation, Inc.
021: * 59 Temple Place, Suite 330,
022: * Boston, MA 02111-1307
023: * USA
024: *
025: * or check the Internet: http://www.fsf.org
026: *
027: * Contact:
028: * email: heiko@susebox.de
029: */
030:
031: package de.susebox.jtopas;
032:
033: //-----------------------------------------------------------------------------
034: // Class StringSource
035: //
036:
037: /**
038: * Implementation of the {@link TokenizerSource} and its extension
039: * {@link CharSequenceTokenizerSource} for strings. It is a shortcut for:
040: *<block><pre>
041: * String myData = "...";
042: * TokenizerSource source = new ReaderSource(new StringReader(myData));
043: *</pre></block>
044: * The class also provides a faster access to the data in a string through the
045: * methods of the {@link java.lang.CharSequence} interface. Since this interface
046: * was only introduced with Java 1.4, this class can only be used with 1.4 or
047: * higher Java versions.
048: *
049: * @see TokenizerSource
050: * @see CharSequenceTokenizerSource
051: * @see CharArraySource
052: * @author Heiko Blau
053: */
054: public class StringSource implements CharSequenceTokenizerSource {
055:
056: //---------------------------------------------------------------------------
057: // Constructors
058: //
059:
060: /**
061: * Constructing a <code>StringSource</code> on the given {@link java.lang.String}.
062: * A <code>null</code> value is handled like an empty string.
063: *
064: * @param data the string
065: */
066: public StringSource(String data) {
067: _data = data;
068: }
069:
070: //---------------------------------------------------------------------------
071: // Methods of the TokenizerSource interface
072: //
073:
074: /**
075: * This method copies the available data into the given buffer according to
076: * the given offset and maximum character count. See {@link TokenizerSource#read}
077: * for details.
078: *
079: * @param cbuf buffer to receive data
080: * @param offset position from where the data should be inserted in <code>cbuf</code>
081: * @param maxChars maximum number of characters to be read into <code>cbuf</code>
082: * @return actually read characters or -1 on an end-of-file condition
083: */
084: public int read(char[] cbuf, final int offset, final int maxChars)
085: throws Exception {
086: int length = (_data != null) ? _data.length() : 0;
087: int left = Math.min(length - _readOffset, maxChars);
088:
089: if (left > 0) {
090: _data.getChars(_readOffset, _readOffset + left, cbuf,
091: offset);
092: _readOffset += left;
093: return left;
094: } else {
095: return -1;
096: }
097: }
098:
099: //---------------------------------------------------------------------------
100: // Methods of the CharSequence interface
101: //
102:
103: /**
104: * Implements {@link java.lang.CharSequence#charAt} for this class.
105: *
106: * @param index which character to retrieve
107: * @return the character at the given index
108: */
109: public char charAt(int index) {
110: return _data.charAt(index);
111: }
112:
113: /**
114: * Implements {@link java.lang.CharSequence#length} for this class.
115: *
116: * @return the number of available characters
117: */
118: public int length() {
119: return (_data != null) ? _data.length() : 0;
120: }
121:
122: /**
123: * Implements {@link java.lang.CharSequence#subSequence} for this class.
124: *
125: * @param start the new <code>CharSequence</code> contains the characters
126: * from and including this position
127: * @param end the new <code>CharSequence</code> contains the characters
128: * up to and excluding this position
129: * @return a part of this <code>CharSequence</code>
130: */
131: public CharSequence subSequence(int start, int end) {
132: return _data.subSequence(start, end);
133: }
134:
135: //---------------------------------------------------------------------------
136: // Members
137: //
138: private String _data = null;
139: private int _readOffset = 0;
140: }
|