KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > instruct > LocationMap


1 package net.sf.saxon.instruct;
2
3 import net.sf.saxon.event.LocationProvider;
4
5 import java.io.Serializable JavaDoc;
6
7 /**
8  * A LocationMap allocates integer codes to (systemId, lineNumber) pairs. The integer
9  * 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 JavaDoc {
14
15     private String JavaDoc[] modules = new String JavaDoc[10];
16     private int numberOfModules = 0;
17
18     public LocationMap() {}
19
20     /**
21      * Allocate a location identifier to an expression
22      */

23
24     public int allocateLocationId(String JavaDoc module, int lineNumber) {
25         if (module == null) {
26             // the module has no base URI
27
module = "*module with no systemId*";
28         }
29         int mod = -1;
30         for (int m=numberOfModules-1; m>=0; m--) {
31             if (modules[m].equals(module)) {
32                 mod = m;
33             }
34         }
35         if (mod == -1) {
36             if (numberOfModules >= modules.length) {
37                 String JavaDoc[] m2 = new String JavaDoc[numberOfModules*2];
38                 System.arraycopy(modules, 0, m2, 0, numberOfModules);
39                 modules = m2;
40             }
41             mod = numberOfModules;
42             modules[numberOfModules++] = module;
43         }
44         if (mod >= 1024) {
45             modules[mod] = "*unknown module*";
46             mod = 1023;
47         }
48         if (lineNumber > 999999) {
49             lineNumber = 999999;
50         }
51         return (mod<<20) + lineNumber;
52     }
53
54     /**
55      * Get the system identifier corresponding to a locationId
56      */

57
58     public String JavaDoc getSystemId(int locationId) {
59         int m = locationId>>20;
60         if (m < 0 || m >= numberOfModules) {
61             return null;
62         }
63         return modules[m];
64     }
65
66     /**
67      * Get the line number corresponding to a locationId
68      */

69
70     public int getLineNumber(int locationId) {
71         return locationId & 0xfffff;
72     }
73
74 }
75
76 //
77
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
78
// you may not use this file except in compliance with the License. You may obtain a copy of the
79
// License at http://www.mozilla.org/MPL/
80
//
81
// Software distributed under the License is distributed on an "AS IS" basis,
82
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
83
// See the License for the specific language governing rights and limitations under the License.
84
//
85
// The Original Code is: all this file.
86
//
87
// The Initial Developer of the Original Code is Michael H. Kay.
88
//
89
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
90
//
91
// Contributor(s): none.
92
//
Popular Tags