KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > expr > ExpressionLocation


1 package net.sf.saxon.expr;
2
3 import net.sf.saxon.event.LocationProvider;
4 import net.sf.saxon.event.SaxonLocator;
5 import net.sf.saxon.instruct.LocationMap;
6 import net.sf.saxon.instruct.InstructionDetails;
7
8 import javax.xml.transform.SourceLocator JavaDoc;
9 import java.io.Serializable JavaDoc;
10
11 /**
12  * Class to hold details of the location of an expression, of an error in a source file, etc.
13  */

14
15 public class ExpressionLocation implements SaxonLocator, Serializable JavaDoc {
16
17     public ExpressionLocation() {}
18
19     public ExpressionLocation(SourceLocator JavaDoc loc) {
20         systemId = loc.getSystemId();
21         lineNumber = loc.getLineNumber();
22     }
23
24     public ExpressionLocation(LocationProvider provider, int locationId) {
25         systemId = provider.getSystemId(locationId);
26         lineNumber = provider.getLineNumber(locationId);
27     }
28
29     private String JavaDoc systemId;
30 // private String publicId;
31
private int lineNumber;
32     private int columnNumber = -1;
33
34     public String JavaDoc getSystemId() {
35         return systemId;
36     }
37
38     public String JavaDoc getPublicId() {
39         return null;
40     }
41
42     public int getLineNumber() {
43         return lineNumber;
44     }
45
46     public int getColumnNumber() {
47         return columnNumber;
48     }
49
50     public void setSystemId(String JavaDoc systemId) {
51         this.systemId = systemId;
52     }
53
54     public void setPublicId(String JavaDoc publicId) {
55 // this.publicId = publicId;
56
}
57
58     public void setLineNumber(int lineNumber) {
59         this.lineNumber = lineNumber;
60     }
61
62     public void setColumnNumber(int columnNumber) {
63         this.columnNumber = columnNumber;
64     }
65
66     public String JavaDoc getSystemId(int locationId) {
67         return getSystemId();
68     }
69
70     public int getLineNumber(int locationId) {
71         return getLineNumber();
72     }
73
74     /**
75      * Construct an object holding location information for a validation error message
76      * @param locationId The locationId as supplied with an event such as startElement or attribute
77      * @param locationProvider The object that understands how to interpret the locationId
78      * @return a SaxonLocator containing the location information
79      */

80     public static SaxonLocator getSourceLocator(int locationId, LocationProvider locationProvider) {
81         SaxonLocator locator;
82         if (locationProvider instanceof LocationMap && locationId != 0) {
83             // this is typically true when validating output documents
84
ExpressionLocation loc = new ExpressionLocation();
85             loc.setLineNumber(locationProvider.getLineNumber(locationId));
86             loc.setSystemId(locationProvider.getSystemId(locationId));
87             locator = loc;
88         } else if (locationProvider instanceof SaxonLocator) {
89             // this is typically true when validating input documents
90
locator = (SaxonLocator)locationProvider;
91         } else {
92             // return a dummy location object providing no information. This can happen for example
93
// if a built-in template rule writes invalid output before the transformation properly begins.
94
return new InstructionDetails();
95         }
96         return locator;
97     }
98 }
99 //
100
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
101
// you may not use this file except in compliance with the License. You may obtain a copy of the
102
// License at http://www.mozilla.org/MPL/
103
//
104
// Software distributed under the License is distributed on an "AS IS" basis,
105
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
106
// See the License for the specific language governing rights and limitations under the License.
107
//
108
// The Original Code is: all this file.
109
//
110
// The Initial Developer of the Original Code is Michael H. Kay.
111
//
112
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
113
//
114
// Contributor(s): none.
115
//
Popular Tags