001: package net.sf.saxon.expr;
002:
003: import net.sf.saxon.event.LocationProvider;
004: import net.sf.saxon.event.SaxonLocator;
005: import net.sf.saxon.instruct.LocationMap;
006: import net.sf.saxon.instruct.InstructionDetails;
007:
008: import javax.xml.transform.SourceLocator;
009: import java.io.Serializable;
010:
011: /**
012: * Class to hold details of the location of an expression, of an error in a source file, etc.
013: */
014:
015: public class ExpressionLocation implements SaxonLocator, Serializable {
016:
017: public ExpressionLocation() {
018: }
019:
020: public ExpressionLocation(SourceLocator loc) {
021: systemId = loc.getSystemId();
022: lineNumber = loc.getLineNumber();
023: }
024:
025: public ExpressionLocation(LocationProvider provider, int locationId) {
026: systemId = provider.getSystemId(locationId);
027: lineNumber = provider.getLineNumber(locationId);
028: }
029:
030: public ExpressionLocation(String systemId, int lineNumber,
031: int columnNumber) {
032: this .systemId = systemId;
033: this .lineNumber = lineNumber;
034: this .columnNumber = columnNumber;
035: }
036:
037: private String systemId;
038: // private String publicId;
039: private int lineNumber;
040: private int columnNumber = -1;
041:
042: public String getSystemId() {
043: return systemId;
044: }
045:
046: public String getPublicId() {
047: return null;
048: }
049:
050: public int getLineNumber() {
051: return lineNumber;
052: }
053:
054: public int getColumnNumber() {
055: return columnNumber;
056: }
057:
058: public void setSystemId(String systemId) {
059: this .systemId = systemId;
060: }
061:
062: public void setPublicId(String publicId) {
063: // this.publicId = publicId;
064: }
065:
066: public void setLineNumber(int lineNumber) {
067: this .lineNumber = lineNumber;
068: }
069:
070: public void setColumnNumber(int columnNumber) {
071: this .columnNumber = columnNumber;
072: }
073:
074: public String getSystemId(int locationId) {
075: return getSystemId();
076: }
077:
078: public int getLineNumber(int locationId) {
079: return getLineNumber();
080: }
081:
082: /**
083: * Construct an object holding location information for a validation error message
084: * @param locationId The locationId as supplied with an event such as startElement or attribute
085: * @param locationProvider The object that understands how to interpret the locationId
086: * @return a SaxonLocator containing the location information
087: */
088: public static SaxonLocator getSourceLocator(int locationId,
089: LocationProvider locationProvider) {
090: SaxonLocator locator;
091: if (locationProvider instanceof LocationMap && locationId != 0) {
092: // this is typically true when validating output documents
093: ExpressionLocation loc = new ExpressionLocation();
094: loc.setLineNumber(locationProvider
095: .getLineNumber(locationId));
096: loc.setSystemId(locationProvider.getSystemId(locationId));
097: locator = loc;
098: } else if (locationProvider instanceof SaxonLocator) {
099: // this is typically true when validating input documents
100: locator = (SaxonLocator) locationProvider;
101: } else {
102: // return a dummy location object providing no information. This can happen for example
103: // if a built-in template rule writes invalid output before the transformation properly begins.
104: return new InstructionDetails();
105: }
106: return locator;
107: }
108: }
109: //
110: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
111: // you may not use this file except in compliance with the License. You may obtain a copy of the
112: // License at http://www.mozilla.org/MPL/
113: //
114: // Software distributed under the License is distributed on an "AS IS" basis,
115: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
116: // See the License for the specific language governing rights and limitations under the License.
117: //
118: // The Original Code is: all this file.
119: //
120: // The Initial Developer of the Original Code is Michael H. Kay.
121: //
122: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
123: //
124: // Contributor(s): none.
125: //
|