KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > xmlc > misc > LineNumberMapReader


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: LineNumberMapReader.java,v 1.2 2005/01/26 08:29:24 jkjome Exp $
22  */

23
24 package org.enhydra.xml.xmlc.misc;
25
26 import java.io.IOException JavaDoc;
27 import java.io.LineNumberReader JavaDoc;
28 import java.io.Reader JavaDoc;
29
30 import org.enhydra.xml.io.InputSourceOps;
31 import org.xml.sax.InputSource JavaDoc;
32
33 /**
34  * A file reader that keeps a map of chararacter offsets to line numbers. This
35  * is used by the Swing HTML parser interface to provide reasonable error
36  * messages.
37  */

38 public final class LineNumberMapReader extends Reader JavaDoc {
39     /**
40      * Input file for file name.
41      */

42     private InputSource JavaDoc fInputSource;
43
44     /**
45      * Map of stream position to source file and line number.
46      */

47     private LineNumberRecorder fLineNumbers;
48
49     /**
50      * Contained reader.
51      */

52     private Reader JavaDoc fReader;
53
54     /**
55      * Construct a new reader for the specified file.
56      */

57     public LineNumberMapReader(InputSource JavaDoc inputSource) throws IOException JavaDoc {
58         fInputSource = inputSource;
59         fLineNumbers = new LineNumberRecorder(fInputSource.getSystemId());
60         fReader = new LineNumberReader JavaDoc(InputSourceOps.open(fInputSource));
61     }
62
63     /**
64      * Get the line number map.
65      */

66     public final LineNumberMap getLineNumberMap() {
67         return fLineNumbers;
68     }
69
70     /**
71      * Read a character.
72      *
73      * @see Reader#read
74      * @exception IOException If an I/O error occurs
75      */

76     public int read() throws IOException JavaDoc {
77         int ch = fReader.read();
78         if (ch >= 0) {
79             fLineNumbers.countChar((char)ch);
80         }
81         return ch;
82     }
83
84     /**
85      * Read characters into a portion of an array.
86      *
87      * @see Reader#read
88      * @exception IOException If an I/O error occurs
89      */

90     public int read(char cbuf[], int off, int len) throws IOException JavaDoc {
91         int num = fReader.read(cbuf, off, len);
92         if (num < 0) {
93             return num;
94         }
95         fLineNumbers.countChars(cbuf, off, len);
96         return num;
97     }
98
99     /**
100      * Close the stream.
101      *
102      * @see Reader#close
103      * @exception IOException If an I/O error occurs
104      */

105     public void close() throws IOException JavaDoc {
106         fReader.close();
107     }
108 }
109
Popular Tags