
org.enhydra.xml.xmlc.misc.LineNumberMapReader Maven / Gradle / Ivy
The newest version!
/*
* Enhydra Java Application Server Project
*
* The contents of this file are subject to the Enhydra Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License on
* the Enhydra web site ( http://www.enhydra.org/ ).
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific terms governing rights and limitations
* under the License.
*
* The Initial Developer of the Enhydra Application Server is Lutris
* Technologies, Inc. The Enhydra Application Server and portions created
* by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
* All Rights Reserved.
*
* Contributor(s):
*
* $Id: LineNumberMapReader.java,v 1.2 2005/01/26 08:29:24 jkjome Exp $
*/
package org.enhydra.xml.xmlc.misc;
import java.io.IOException;
import java.io.LineNumberReader;
import java.io.Reader;
import org.enhydra.xml.io.InputSourceOps;
import org.xml.sax.InputSource;
/**
* A file reader that keeps a map of chararacter offsets to line numbers. This
* is used by the Swing HTML parser interface to provide reasonable error
* messages.
*/
public final class LineNumberMapReader extends Reader {
/**
* Input file for file name.
*/
private InputSource fInputSource;
/**
* Map of stream position to source file and line number.
*/
private LineNumberRecorder fLineNumbers;
/**
* Contained reader.
*/
private Reader fReader;
/**
* Construct a new reader for the specified file.
*/
public LineNumberMapReader(InputSource inputSource) throws IOException {
fInputSource = inputSource;
fLineNumbers = new LineNumberRecorder(fInputSource.getSystemId());
fReader = new LineNumberReader(InputSourceOps.open(fInputSource));
}
/**
* Get the line number map.
*/
public final LineNumberMap getLineNumberMap() {
return fLineNumbers;
}
/**
* Read a character.
*
* @see Reader#read
* @exception IOException If an I/O error occurs
*/
public int read() throws IOException {
int ch = fReader.read();
if (ch >= 0) {
fLineNumbers.countChar((char)ch);
}
return ch;
}
/**
* Read characters into a portion of an array.
*
* @see Reader#read
* @exception IOException If an I/O error occurs
*/
public int read(char cbuf[], int off, int len) throws IOException {
int num = fReader.read(cbuf, off, len);
if (num < 0) {
return num;
}
fLineNumbers.countChars(cbuf, off, len);
return num;
}
/**
* Close the stream.
*
* @see Reader#close
* @exception IOException If an I/O error occurs
*/
public void close() throws IOException {
fReader.close();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy