KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > icl > saxon > tree > LineNumberMap


1 package com.icl.saxon.tree;
2
3 /**
4   * Line numbers are not held in nodes in the tree, because they are not usually needed.
5   * This class provides a map from element sequence numbers to line numbers: it is
6   * linked to the root node of the tree.
7   *
8   * @author <A HREF="mailto:mhkay@iclway.co.uk>Michael H. Kay</A>
9   */

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 //
59
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
60
// you may not use this file except in compliance with the License. You may obtain a copy of the
61
// License at http://www.mozilla.org/MPL/
62
//
63
// Software distributed under the License is distributed on an "AS IS" basis,
64
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
65
// See the License for the specific language governing rights and limitations under the License.
66
//
67
// The Original Code is: all this file.
68
//
69
// The Initial Developer of the Original Code is
70
// Michael Kay of International Computers Limited (mhkay@iclway.co.uk).
71
//
72
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
73
//
74
// Contributor(s): none.
75
//
76
Popular Tags