| 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.expr.parser; |
| 9 | |
| 10 | import org.xml.sax.Locator; |
| 11 | |
| 12 | import javax.xml.transform.SourceLocator; |
| 13 | |
| 14 | /** |
| 15 | * Class to hold details of the location of an expression, of an error in a source file, etc. |
| 16 | * The object is immutable. |
| 17 | */ |
| 18 | |
| 19 | public class ExplicitLocation implements Location { |
| 20 | |
| 21 | private String systemId; |
| 22 | private int lineNumber; |
| 23 | private int columnNumber = -1; |
| 24 | |
| 25 | public static ExplicitLocation UNKNOWN_LOCATION = new ExplicitLocation(null, -1, -1); |
| 26 | |
| 27 | /** |
| 28 | * Create an ExpressionLocation, taking the data from a supplied JAXP SourceLocator |
| 29 | * |
| 30 | * @param loc the JAXP SourceLocator |
| 31 | */ |
| 32 | |
| 33 | public ExplicitLocation(SourceLocator loc) { |
| 34 | systemId = loc.getSystemId(); |
| 35 | lineNumber = loc.getLineNumber(); |
| 36 | columnNumber = loc.getColumnNumber(); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Create an ExpressionLocation, taking the data from a supplied SAX Locator |
| 41 | * |
| 42 | * @param loc the SAX Locator |
| 43 | */ |
| 44 | |
| 45 | public static ExplicitLocation makeFromSax(Locator loc) { |
| 46 | return new ExplicitLocation(loc.getSystemId(), loc.getLineNumber(), loc.getColumnNumber()); |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * Create an ExpressionLocation corresponding to a given module, line number, and column number |
| 51 | * |
| 52 | * @param systemId the module URI |
| 53 | * @param lineNumber the line number (starting at 1; -1 means unknown) |
| 54 | * @param columnNumber the column number (starting at 1; -1 means unknown) |
| 55 | */ |
| 56 | |
| 57 | public ExplicitLocation(String systemId, int lineNumber, int columnNumber) { |
| 58 | this.systemId = systemId; |
| 59 | this.lineNumber = lineNumber; |
| 60 | this.columnNumber = columnNumber; |
| 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Get the system ID (the module URI) |
| 65 | * |
| 66 | * @return the system ID |
| 67 | */ |
| 68 | |
| 69 | public String getSystemId() { |
| 70 | return systemId; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Get the Public ID |
| 75 | * |
| 76 | * @return always null in this implementation |
| 77 | */ |
| 78 | |
| 79 | /*@Nullable*/ |
| 80 | public String getPublicId() { |
| 81 | return null; |
| 82 | } |
| 83 | |
| 84 | /** |
| 85 | * Get the line number |
| 86 | * |
| 87 | * @return the line number |
| 88 | */ |
| 89 | |
| 90 | public int getLineNumber() { |
| 91 | return lineNumber; |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Get the column number |
| 96 | * |
| 97 | * @return the column number |
| 98 | */ |
| 99 | |
| 100 | public int getColumnNumber() { |
| 101 | return columnNumber; |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Get an immutable copy of this Location object. By default Location objects may be mutable, so they |
| 106 | * should not be saved for later use. The result of this operation holds the same location information, |
| 107 | * but in an immutable form. |
| 108 | */ |
| 109 | public Location saveLocation() { |
| 110 | return this; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Ask whether this is an "unknown location" |
| 115 | */ |
| 116 | |
| 117 | public static boolean isUnknown(Location location) { |
| 118 | return location == null || (location.getSystemId() == null || location.getSystemId().isEmpty()) |
| 119 | && location.getLineNumber() == -1; |
| 120 | } |
| 121 | |
| 122 | |
| 123 | } |