01: package net.sf.saxon.instruct;
02:
03: import net.sf.saxon.event.LocationProvider;
04:
05: import java.io.Serializable;
06:
07: /**
08: * A LocationMap allocates integer codes to (systemId, lineNumber) pairs. The integer
09: * codes are held inside an Expression object to track the location of the expression
10: * in the source code
11: */
12:
13: public class LocationMap implements LocationProvider, Serializable {
14:
15: private String[] modules = new String[10];
16: private int numberOfModules = 0;
17:
18: public LocationMap() {
19: }
20:
21: /**
22: * Allocate a location identifier to an expression
23: */
24:
25: public int allocateLocationId(String module, int lineNumber) {
26: if (module == null) {
27: // the module has no base URI
28: module = "*module with no systemId*";
29: }
30: int mod = -1;
31: for (int m = numberOfModules - 1; m >= 0; m--) {
32: if (modules[m].equals(module)) {
33: mod = m;
34: }
35: }
36: if (mod == -1) {
37: if (numberOfModules >= modules.length) {
38: String[] m2 = new String[numberOfModules * 2];
39: System.arraycopy(modules, 0, m2, 0, numberOfModules);
40: modules = m2;
41: }
42: mod = numberOfModules;
43: modules[numberOfModules++] = module;
44: }
45: if (mod >= 1024) {
46: modules[mod] = "*unknown module*";
47: mod = 1023;
48: }
49: if (lineNumber > 999999) {
50: lineNumber = 999999;
51: }
52: return (mod << 20) + lineNumber;
53: }
54:
55: /**
56: * Get the system identifier corresponding to a locationId
57: */
58:
59: public String getSystemId(int locationId) {
60: int m = locationId >> 20;
61: if (m < 0 || m >= numberOfModules) {
62: return null;
63: }
64: return modules[m];
65: }
66:
67: /**
68: * Get the line number corresponding to a locationId
69: */
70:
71: public int getLineNumber(int locationId) {
72: return locationId & 0xfffff;
73: }
74:
75: }
76:
77: //
78: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
79: // you may not use this file except in compliance with the License. You may obtain a copy of the
80: // License at http://www.mozilla.org/MPL/
81: //
82: // Software distributed under the License is distributed on an "AS IS" basis,
83: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
84: // See the License for the specific language governing rights and limitations under the License.
85: //
86: // The Original Code is: all this file.
87: //
88: // The Initial Developer of the Original Code is Michael H. Kay.
89: //
90: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
91: //
92: // Contributor(s): none.
93: //
|