01: package net.sf.saxon.tree;
02:
03: /**
04: * Line numbers are not held in nodes in the tree, because they are not usually needed.
05: * This class provides a map from element sequence numbers to line numbers: it is
06: * linked to the root node of the tree.
07: *
08: * @author Michael H. Kay
09: */
10:
11: public class LineNumberMap {
12:
13: private int[] sequenceNumbers;
14: private int[] lineNumbers;
15: private int allocated;
16:
17: public LineNumberMap() {
18: sequenceNumbers = new int[1000];
19: lineNumbers = new int[1000];
20: allocated = 0;
21: }
22:
23: /**
24: * Set the line number corresponding to a given sequence number
25: */
26:
27: public void setLineNumber(int sequence, int line) {
28: if (sequenceNumbers.length <= allocated + 1) {
29: int[] s = new int[allocated * 2];
30: int[] l = new int[allocated * 2];
31: System.arraycopy(sequenceNumbers, 0, s, 0, allocated);
32: System.arraycopy(lineNumbers, 0, l, 0, allocated);
33: sequenceNumbers = s;
34: lineNumbers = l;
35: }
36: sequenceNumbers[allocated] = sequence;
37: lineNumbers[allocated] = line;
38: allocated++;
39: }
40:
41: /**
42: * Get the line number corresponding to a given sequence number
43: */
44:
45: public int getLineNumber(int sequence) {
46: // could use a binary chop, but it's not important
47: for (int i = 1; i < allocated; i++) {
48: if (sequenceNumbers[i] > sequence) {
49: return lineNumbers[i - 1];
50: }
51: }
52: return lineNumbers[allocated - 1];
53: }
54:
55: }
56:
57: //
58: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
59: // you may not use this file except in compliance with the License. You may obtain a copy of the
60: // License at http://www.mozilla.org/MPL/
61: //
62: // Software distributed under the License is distributed on an "AS IS" basis,
63: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
64: // See the License for the specific language governing rights and limitations under the License.
65: //
66: // The Original Code is: all this file.
67: //
68: // The Initial Developer of the Original Code is Michael H. Kay.
69: //
70: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
71: //
72: // Contributor(s): none.
73: //
|