| 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.instruct; |
| 9 | |
| 10 | import net.sf.saxon.expr.Expression; |
| 11 | import net.sf.saxon.expr.parser.ExplicitLocation; |
| 12 | import net.sf.saxon.expr.parser.Location; |
| 13 | import net.sf.saxon.om.StructuredQName; |
| 14 | import net.sf.saxon.trace.InstructionInfo; |
| 15 | import net.sf.saxon.trace.LocationKind; |
| 16 | |
| 17 | import java.util.HashMap; |
| 18 | import java.util.Iterator; |
| 19 | |
| 20 | /** |
| 21 | * Details about an instruction, used when reporting errors and when tracing |
| 22 | */ |
| 23 | |
| 24 | public final class InstructionDetails implements InstructionInfo { |
| 25 | |
| 26 | private int constructType = LocationKind.UNCLASSIFIED; |
| 27 | /*@Nullable*/ private String systemId = null; |
| 28 | private int lineNumber = -1; |
| 29 | private int columnNumber = -1; |
| 30 | /*@Nullable*/ private StructuredQName objectName; |
| 31 | private HashMap<String, Object> properties = new HashMap<String, Object>(5); |
| 32 | |
| 33 | public InstructionDetails() { |
| 34 | } |
| 35 | |
| 36 | public InstructionDetails(Expression expr) { |
| 37 | setConstructType(expr.getConstructType()); |
| 38 | setLineNumber(expr.getLocation().getLineNumber()); |
| 39 | setSystemId(expr.getLocation().getSystemId()); |
| 40 | setColumnNumber(expr.getLocation().getColumnNumber()); |
| 41 | setProperty("name", expr.getExpressionName()); |
| 42 | } |
| 43 | |
| 44 | /** |
| 45 | * Set the type of construct |
| 46 | * |
| 47 | * @param type the type of contruct |
| 48 | */ |
| 49 | |
| 50 | public void setConstructType(int type) { |
| 51 | constructType = type; |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Get the construct type |
| 56 | */ |
| 57 | public int getConstructType() { |
| 58 | return constructType; |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * Set the URI of the module containing the instruction |
| 63 | * |
| 64 | * @param systemId the module's URI, or null indicating unknown |
| 65 | */ |
| 66 | |
| 67 | public void setSystemId(/*@Nullable*/ String systemId) { |
| 68 | this.systemId = systemId; |
| 69 | } |
| 70 | |
| 71 | /** |
| 72 | * Get the URI of the module containing the instruction |
| 73 | * |
| 74 | * @return the module's URI, or null indicating unknown |
| 75 | */ |
| 76 | |
| 77 | /*@Nullable*/ |
| 78 | public String getSystemId() { |
| 79 | return systemId; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Set the line number of the instruction within the module |
| 84 | * |
| 85 | * @param lineNumber the line number |
| 86 | */ |
| 87 | |
| 88 | public void setLineNumber(int lineNumber) { |
| 89 | this.lineNumber = lineNumber; |
| 90 | } |
| 91 | |
| 92 | /** |
| 93 | * Get the line number of the instruction within its module |
| 94 | * |
| 95 | * @return the line number |
| 96 | */ |
| 97 | |
| 98 | public int getLineNumber() { |
| 99 | return lineNumber; |
| 100 | } |
| 101 | |
| 102 | /** |
| 103 | * Get an immutable copy of this Location object. By default Location objects may be mutable, so they |
| 104 | * should not be saved for later use. The result of this operation holds the same location information, |
| 105 | * but in an immutable form. |
| 106 | */ |
| 107 | public Location saveLocation() { |
| 108 | return new ExplicitLocation(this); |
| 109 | } |
| 110 | |
| 111 | /** |
| 112 | * Set a name identifying the object of the expression, for example a function name, template name, |
| 113 | * variable name, key name, element name, etc. This is used only where the name is known statically. |
| 114 | * |
| 115 | * @param qName the name of the object, for example a function or variable name, or null to indicate |
| 116 | * that it has no name |
| 117 | */ |
| 118 | |
| 119 | public void setObjectName(/*@Nullable*/ StructuredQName qName) { |
| 120 | objectName = qName; |
| 121 | } |
| 122 | |
| 123 | /** |
| 124 | * Get a name identifying the object of the expression, for example a function name, template name, |
| 125 | * variable name, key name, element name, etc. This is used only where the name is known statically. |
| 126 | * |
| 127 | * @return the name of the object, or null to indicate that it has no name |
| 128 | */ |
| 129 | |
| 130 | /*@Nullable*/ |
| 131 | public StructuredQName getObjectName() { |
| 132 | if (objectName != null) { |
| 133 | return objectName; |
| 134 | } else { |
| 135 | return null; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Set a named property of the instruction |
| 141 | * |
| 142 | * @param name the name of the property |
| 143 | * @param value the value of the property |
| 144 | */ |
| 145 | |
| 146 | public void setProperty(String name, Object value) { |
| 147 | properties.put(name, value); |
| 148 | } |
| 149 | |
| 150 | /** |
| 151 | * Get a named property of the instruction |
| 152 | * |
| 153 | * @param name name of the property |
| 154 | * @return the value of the named property |
| 155 | */ |
| 156 | |
| 157 | public Object getProperty(String name) { |
| 158 | return properties.get(name); |
| 159 | } |
| 160 | |
| 161 | /** |
| 162 | * Get an iterator over all the properties available. The values returned by the iterator |
| 163 | * will be of type String, and each string can be supplied as input to the getProperty() |
| 164 | * method to retrieve the value of the property. |
| 165 | * |
| 166 | * @return an iterator over the names of the properties |
| 167 | */ |
| 168 | |
| 169 | public Iterator<String> getProperties() { |
| 170 | return properties.keySet().iterator(); |
| 171 | } |
| 172 | |
| 173 | /** |
| 174 | * Get the public ID of the module containing the instruction. This method |
| 175 | * is provided to satisfy the SourceLocator interface. However, the public ID is |
| 176 | * not maintained by Saxon, and the method always returns null |
| 177 | * |
| 178 | * @return null |
| 179 | */ |
| 180 | |
| 181 | public String getPublicId() { |
| 182 | return null; |
| 183 | } |
| 184 | |
| 185 | /** |
| 186 | * Set the column number |
| 187 | * |
| 188 | * @param column the column number of the instruction in the source module |
| 189 | */ |
| 190 | |
| 191 | public void setColumnNumber(int column) { |
| 192 | columnNumber = column; |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Get the column number identifying the position of the instruction. |
| 197 | * |
| 198 | * @return -1 if column number is not known |
| 199 | */ |
| 200 | |
| 201 | public int getColumnNumber() { |
| 202 | return columnNumber; |
| 203 | } |
| 204 | |
| 205 | } |
| 206 |