Debian Codegraph @

net/sf/saxon/tree/linked/LineNumberMap.java

raw symbols SCIP
1 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
2 // Copyright (c) 2018 Saxonica Limited.
3 // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
4 // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
5 // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
6 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
7
8 package net.sf.saxon.tree.linked;
9
10 import java.util.Arrays;
11
12 /**
13 * Line numbers are not held in nodes in the tree, because they are not usually needed.
14 * This class provides a map from element sequence numbers to line numbers: it is
15 * linked to the root node of the tree.
16 *
17 * @author Michael H. Kay
18 */
19
20 public class LineNumberMap {
21
22 private int[] sequenceNumbers;
23 private int[] lineNumbers;
24 private int[] columnNumbers;
25 private int allocated;
26
27 /**
28 * Create a LineNumberMap with an initial capacity of 200 nodes, which is expanded as necessary
29 */
30
31 public LineNumberMap() {
32 sequenceNumbers = new int[200];
33 lineNumbers = new int[200];
34 columnNumbers = new int[200];
35 allocated = 0;
36 }
37
38 /**
39 * Set the line number corresponding to a given sequence number
40 *
41 * @param sequence the sequence number of the node
42 * @param line the line number position of the node
43 * @param column the column position of the node
44 */
45
46 public void setLineAndColumn(int sequence, int line, int column) {
47 if (sequenceNumbers.length <= allocated + 1) {
48 sequenceNumbers = Arrays.copyOf(sequenceNumbers, allocated * 2);
49 lineNumbers = Arrays.copyOf(lineNumbers, allocated * 2);
50 columnNumbers = Arrays.copyOf(columnNumbers, allocated * 2);
51 }
52 sequenceNumbers[allocated] = sequence;
53 lineNumbers[allocated] = line;
54 columnNumbers[allocated] = column;
55 allocated++;
56 }
57
58 /**
59 * Get the line number corresponding to a given sequence number
60 *
61 * @param sequence the sequence number held in the node
62 * @return the corresponding line number
63 */
64
65 public int getLineNumber(int sequence) {
66 if (sequenceNumbers.length > allocated) {
67 condense();
68 }
69 int index = Arrays.binarySearch(sequenceNumbers, sequence);
70 if (index < 0) {
71 index = -index - 1;
72 if (index > lineNumbers.length - 1) {
73 index = lineNumbers.length - 1;
74 }
75 }
76 return lineNumbers[index];
77 }
78
79 /**
80 * Get the column number corresponding to a given sequence number
81 *
82 * @param sequence the sequence number held in the node
83 * @return the corresponding column number
84 */
85
86 public int getColumnNumber(int sequence) {
87 if (sequenceNumbers.length > allocated) {
88 condense();
89 }
90 int index = Arrays.binarySearch(sequenceNumbers, sequence);
91 if (index < 0) {
92 index = -index - 1;
93 if (index >= columnNumbers.length) {
94 index = columnNumbers.length - 1;
95 }
96 }
97 return columnNumbers[index];
98 }
99
100 private synchronized void condense() {
101 sequenceNumbers = Arrays.copyOf(sequenceNumbers, allocated);
102 lineNumbers = Arrays.copyOf(lineNumbers, allocated);
103 columnNumbers = Arrays.copyOf(columnNumbers, allocated);
104 }
105
106
107 }
108