001: package net.sf.saxon.instruct;
002:
003: import net.sf.saxon.expr.Expression;
004: import net.sf.saxon.om.NamespaceResolver;
005: import net.sf.saxon.trace.InstructionInfo;
006:
007: import java.util.HashMap;
008: import java.util.Iterator;
009:
010: /**
011: * A subclass of TraceWrapper used to trace expressions in XPath and XQuery. Unlike
012: * the TraceInstruction class, this class contains all information needed for tracing,
013: * rather than referencing a separate InstructionDetails object.
014: */
015:
016: // This class is used for tracing
017: // within XPath/XQuery expressions. The TraceExpression itself holds all the information
018: // about the source needing to be traced (the InstructionInfo).
019: public class TraceExpression extends TraceWrapper implements
020: InstructionInfo {
021:
022: private int lineNumber = -1;
023: private int columnNumber = -1;
024: private String systemId = null;
025: private int objectNameCode = -1;
026: private int constructType;
027: private NamespaceResolver namespaceResolver = null;
028: private HashMap properties = new HashMap(10);
029:
030: /**
031: * Create a trace expression that traces execution of a given child expression
032: * @param child the expression to be traced. This will be available to the TraceListener
033: * as the value of the "expression" property of the InstructionInfo.
034: */
035: public TraceExpression(Expression child) {
036: this .child = child;
037: setProperty("expression", child);
038: }
039:
040: /**
041: * Set the line number of the expression being traced
042: * @param line
043: */
044: public void setLineNumber(int line) {
045: lineNumber = line;
046: }
047:
048: /**
049: * Set the column number of the expression being traced
050: * @param column
051: */
052: public void setColumnNumber(int column) {
053: columnNumber = column;
054: }
055:
056: /**
057: * Set the type of construct. This will generally be a constant
058: * in class {@link net.sf.saxon.trace.Location}
059: */
060:
061: public void setConstructType(int type) {
062: constructType = type;
063: }
064:
065: /**
066: * Get the construct type. This will generally be a constant
067: * in class {@link net.sf.saxon.trace.Location}
068: */
069: public int getConstructType() {
070: return constructType;
071: }
072:
073: /**
074: * Set the namespace context for the instruction being traced. This is needed if the
075: * tracelistener wants to evaluate XPath expressions in the context of the current instruction
076: */
077:
078: public void setNamespaceResolver(NamespaceResolver resolver) {
079: namespaceResolver = resolver;
080: }
081:
082: /**
083: * Get the namespace resolver to supply the namespace context of the instruction
084: * that is being traced
085: */
086:
087: public NamespaceResolver getNamespaceResolver() {
088: return namespaceResolver;
089: }
090:
091: /**
092: * Set the URI of the module containing the instruction
093: * @param systemId the module's URI
094: */
095:
096: public void setSystemId(String systemId) {
097: this .systemId = systemId;
098: }
099:
100: /**
101: * Get the URI of the module containing the instruction
102: * @return the module's URI
103: */
104:
105: public String getSystemId() {
106: return systemId;
107: }
108:
109: /**
110: * Get the line number of the instruction within its module
111: * @return the line number
112: */
113:
114: public int getLineNumber() {
115: return lineNumber;
116: }
117:
118: /**
119: * Set a name identifying the object of the expression, for example a function name, template name,
120: * variable name, key name, element name, etc. This is used only where the name is known statically.
121: */
122:
123: public void setObjectNameCode(int nameCode) {
124: objectNameCode = nameCode;
125: }
126:
127: /**
128: * Get a name identifying the object of the expression, for example a function name, template name,
129: * variable name, key name, element name, etc. This is used only where the name is known statically.
130: */
131:
132: public int getObjectNameCode() {
133: return objectNameCode;
134: }
135:
136: /**
137: * Set a named property of the instruction/expression
138: */
139:
140: public void setProperty(String name, Object value) {
141: properties.put(name, value);
142: }
143:
144: /**
145: * Get a named property of the instruction/expression
146: */
147:
148: public Object getProperty(String name) {
149: return properties.get(name);
150: }
151:
152: /**
153: * Get an iterator over all the properties available. The values returned by the iterator
154: * will be of type String, and each string can be supplied as input to the getProperty()
155: * method to retrieve the value of the property.
156: */
157:
158: public Iterator getProperties() {
159: return properties.keySet().iterator();
160: }
161:
162: /**
163: * Get the column number identifying the position of the instruction. This method
164: * is provided to satisfy the SourceLocator interface. However, the column number is
165: * not maintained by Saxon, and the method always returns -1
166: * @return -1
167: */
168:
169: public int getColumnNumber() {
170: return columnNumber;
171: }
172:
173: /**
174: * Get the InstructionInfo details about the construct. This is to satisfy the InstructionInfoProvider
175: * interface.
176: */
177:
178: public InstructionInfo getInstructionInfo() {
179: return this ;
180: }
181:
182: /**
183: * Get the system identifier (that is the base URI) of the static context of the expression being
184: * traced. This returns the same result as getSystemId(), it is provided to satisfy the
185: * {@link net.sf.saxon.event.LocationProvider} interface.
186: * @param locationId not used
187: * @return the URI of the module containing the expression
188: */
189: public String getSystemId(int locationId) {
190: return getSystemId();
191: }
192:
193: /**
194: * Get the line number of the expression being
195: * traced. This returns the same result as getLineNumber(), it is provided to satisfy the
196: * {@link net.sf.saxon.event.LocationProvider} interface.
197: * @param locationId not used
198: * @return the line number of the expression within its module
199: */
200:
201: public int getLineNumber(int locationId) {
202: return getLineNumber();
203: }
204: }
205:
206: //
207: // The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
208: // you may not use this file except in compliance with the License. You may obtain a copy of the
209: // License at http://www.mozilla.org/MPL/
210: //
211: // Software distributed under the License is distributed on an "AS IS" basis,
212: // WITHOUT WARRANTY OF ANY KIND, either express or implied.
213: // See the License for the specific language governing rights and limitations under the License.
214: //
215: // The Original Code is: all this file.
216: //
217: // The Initial Developer of the Original Code is Michael H. Kay
218: //
219: // Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
220: //
221: // Contributor(s): none.
222: //
|