KickJava   Java API By Example, From Geeks To Geeks.

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


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: LineNumberMap.java,v 1.1.1.1 2003/03/10 16:36:23 taweili Exp $
22  */

23
24 package org.enhydra.xml.xmlc.misc;
25
26 import java.util.ArrayList JavaDoc;
27
28 /**
29  * Table that keeps a map of character offsets and line count in an input
30  * stream to file name and line numbers in source files. This is used to
31  * handle mapping of indexes or line numbers in a stream generted by including
32  * files to those in the original files.
33  */

34 public class LineNumberMap {
35     /**
36      * Structure to record the char offset in an input stream for
37      * a line. Objects of this class are immutable.
38      */

39     public final class Line {
40         /** File were encountered. */
41         private final String JavaDoc fFileName;
42
43         /** One-based line number in the source file. */
44         private final int fLineNum;
45
46         /** One-based line number in the char stream */
47         private final int fStreamLineNum;
48
49         /** Zero-based stream char offset. */
50         private final int fStreamCharOffset;
51
52         /**
53          * Constructor
54          */

55         Line(String JavaDoc fileName,
56              int lineNum,
57              int streamLineNum,
58              int streamCharOffset) {
59             fFileName = fileName;
60             fLineNum = lineNum;
61             fStreamLineNum = streamLineNum;
62             fStreamCharOffset = streamCharOffset;
63         }
64         
65         //FIXME: should really use the term systemId rather than fileName.
66
/** Get the file name. */
67         public String JavaDoc getFileName() {
68             return fFileName;
69         }
70
71         /** Get one-based source line number. */
72         public int getLineNum() {
73             return fLineNum;
74         }
75
76         /** Get one-based stream line number. */
77         public int getStreamLineNum() {
78             return fStreamLineNum;
79         }
80
81         /** Zero-based stream char offset. */
82         public int getStreamCharOffset() {
83             return fStreamCharOffset;
84         }
85
86         /** Get String reprsentation for debugging */
87         public String JavaDoc toString() {
88             return "(" + fFileName + " " + fLineNum + ") => ("
89                 + fStreamLineNum + " " + fStreamCharOffset + ")";
90
91         }
92     }
93
94     /**
95      * Array of line records. We don't currently provide indexing as
96      * this is normally search only on warnings or errors, so the overhead
97      * of keeping an index is avoided.
98      */

99     private ArrayList JavaDoc fLines = new ArrayList JavaDoc();
100
101     /**
102      * Add a line to the map. This object currently assumes that
103      * all lines are added in sequential order.
104      */

105     public final void addLine(String JavaDoc fileName,
106                               int lineNum,
107                               int streamLineNum,
108                               int streamCharOffset) {
109         fLines.add(new Line(fileName, lineNum,
110                             streamLineNum, streamCharOffset));
111     }
112
113     /**
114      * Get the filname end line number for a stream line number.
115      */

116     public final Line getLineFromOffset(int streamCharOffset) {
117         // Start from the end of the list, as this is normally
118
// used to find errors that just occured.
119
for (int idx = fLines.size() - 1; idx >= 0; idx--) {
120             Line line = (Line)fLines.get(idx);
121             if (streamCharOffset >= line.getStreamCharOffset()) {
122                 return line;
123             }
124         }
125         return null;
126     }
127
128     /**
129      * Get the filname end line number for a stream character offset.
130      */

131     public final Line getLineFromLineNum(int streamLineNum) {
132         // Start from the end of the list, as this is normally
133
// used to find errors that just occured.
134
for (int idx = fLines.size() - 1; idx >= 0; idx--) {
135             Line line = (Line)fLines.get(idx);
136             if (streamLineNum >= line.getStreamLineNum()) {
137                 return line;
138             }
139         }
140         return null;
141     }
142
143     /** Get String reprsentation for debugging */
144     public String JavaDoc toString() {
145         StringBuffer JavaDoc buf = new StringBuffer JavaDoc(4096); // Something bigger than default.
146

147         for (int idx = 0; idx < fLines.size(); idx++) {
148             buf.append(fLines.get(idx).toString());
149             buf.append('\n');
150         }
151         return buf.toString();
152     }
153 }
154
Popular Tags