001: /*
002:
003: Licensed to the Apache Software Foundation (ASF) under one or more
004: contributor license agreements. See the NOTICE file distributed with
005: this work for additional information regarding copyright ownership.
006: The ASF licenses this file to You under the Apache License, Version 2.0
007: (the "License"); you may not use this file except in compliance with
008: the License. You may obtain a copy of the License at
009:
010: http://www.apache.org/licenses/LICENSE-2.0
011:
012: Unless required by applicable law or agreed to in writing, software
013: distributed under the License is distributed on an "AS IS" BASIS,
014: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
015: See the License for the specific language governing permissions and
016: limitations under the License.
017:
018: */
019: package org.apache.batik.util.io;
020:
021: import java.io.IOException;
022:
023: /**
024: * This class represents a NormalizingReader which handles Strings.
025: *
026: * @author <a href="mailto:stephane@hillion.org">Stephane Hillion</a>
027: * @version $Id: StringNormalizingReader.java 475477 2006-11-15 22:44:28Z cam $
028: */
029: public class StringNormalizingReader extends NormalizingReader {
030:
031: /**
032: * The characters.
033: */
034: protected String string;
035:
036: /**
037: * The length of the string.
038: */
039: protected int length;
040:
041: /**
042: * The index of the next character.
043: */
044: protected int next;
045:
046: /**
047: * The current line in the stream.
048: */
049: protected int line = 1;
050:
051: /**
052: * The current column in the stream.
053: */
054: protected int column;
055:
056: /**
057: * Creates a new StringNormalizingReader.
058: * @param s The string to read.
059: */
060: public StringNormalizingReader(String s) {
061: string = s;
062: length = s.length();
063: }
064:
065: /**
066: * Read a single character. This method will block until a
067: * character is available, an I/O error occurs, or the end of the
068: * stream is reached.
069: */
070: public int read() throws IOException {
071: int result = (length == next) ? -1 : string.charAt(next++);
072: if (result <= 13) {
073: switch (result) {
074: case 13:
075: column = 0;
076: line++;
077: int c = (length == next) ? -1 : string.charAt(next);
078: if (c == 10) {
079: next++;
080: }
081: return 10;
082:
083: case 10:
084: column = 0;
085: line++;
086: }
087: }
088: return result;
089: }
090:
091: /**
092: * Returns the current line in the stream.
093: */
094: public int getLine() {
095: return line;
096: }
097:
098: /**
099: * Returns the current column in the stream.
100: */
101: public int getColumn() {
102: return column;
103: }
104:
105: /**
106: * Close the stream.
107: */
108: public void close() throws IOException {
109: string = null;
110: }
111: }
|