KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > saxon > functions > Trace


1 package net.sf.saxon.functions;
2 import net.sf.saxon.expr.Expression;
3 import net.sf.saxon.expr.ExpressionTool;
4 import net.sf.saxon.expr.StaticContext;
5 import net.sf.saxon.expr.XPathContext;
6 import net.sf.saxon.instruct.InstructionDetails;
7 import net.sf.saxon.om.*;
8 import net.sf.saxon.trace.Location;
9 import net.sf.saxon.trace.TraceListener;
10 import net.sf.saxon.trans.XPathException;
11 import net.sf.saxon.type.Type;
12 import net.sf.saxon.value.Value;
13
14 /**
15 * This class supports the XPath 2.0 function trace().
16 * The value is traced to the System.err stream, unless a TraceListener is in use,
17 * in which case the information is sent to the TraceListener
18 */

19
20
21 public class Trace extends SystemFunction {
22
23     NamespaceResolver resolver;
24         // This is retained so that the static namespace context is available if required by the TraceListener
25

26
27     /**
28      * Simplify the function call. This implementation saves the static namespace context, in case it is
29      * needed by the TraceListener.
30      */

31
32     public Expression simplify(StaticContext env) throws XPathException {
33         resolver = env.getNamespaceResolver();
34         return super.simplify(env);
35     }
36
37     /**
38     * preEvaluate: this method suppresses compile-time evaluation by doing nothing
39     */

40
41     public Expression preEvaluate(StaticContext env) {
42         return this;
43     }
44
45     /**
46     * Get the static properties of this expression (other than its type). The result is
47     * bit-significant. These properties are used for optimizations. In general, if
48     * property bit is set, it is true, but if it is unset, the value is unknown.
49     */

50
51     public int computeSpecialProperties() {
52         return argument[0].getSpecialProperties();
53     }
54
55     /**
56     * Get the static cardinality
57     */

58
59     public int computeCardinality() {
60         return argument[0].getCardinality();
61     }
62
63     /**
64     * Evaluate the function
65     */

66
67     public Item evaluateItem(XPathContext context) throws XPathException {
68         Item val = argument[0].evaluateItem(context);
69         String JavaDoc label = argument[1].evaluateAsString(context);
70         if (context.getController().isTracing()) {
71             notifyListener(label, Value.asValue(val), context);
72         } else {
73             traceItem(val, label);
74         }
75         return val;
76     }
77
78     private void notifyListener(String JavaDoc label, Value val, XPathContext context) {
79         InstructionDetails info = (InstructionDetails)getInstructionInfo();
80         info.setConstructType(Location.TRACE_CALL);
81         info.setNamespaceResolver(resolver);
82         info.setProperty("label", label);
83         info.setProperty("value", val);
84         TraceListener listener = context.getController().getTraceListener();
85         listener.enter(info, context);
86         listener.leave(info);
87     }
88
89     private void traceItem(Item val, String JavaDoc label) {
90         if (val==null) {
91             System.err.println(label + ": empty sequence");
92         } else {
93             if (val instanceof NodeInfo) {
94                 System.err.println(label + ": " + Type.displayTypeName(val) + ": "
95                                     + Navigator.getPath((NodeInfo)val));
96             } else {
97                 System.err.println(label + ": " + Type.displayTypeName(val) + ": "
98                                     + val.getStringValue());
99             }
100         }
101     }
102
103     /**
104     * Iterate over the results of the function
105     */

106
107     public SequenceIterator iterate(XPathContext context) throws XPathException {
108         if (context.getController().isTracing()) {
109             String JavaDoc label = argument[1].evaluateAsString(context);
110             Value value = ExpressionTool.eagerEvaluate(argument[0], context);
111             notifyListener(label, value, context);
112             return value.iterate(context);
113         } else {
114             return new TracingIterator(argument[0].iterate(context), argument[1].evaluateAsString(context));
115         }
116     }
117
118
119     /**
120     * Tracing Iterator class
121     */

122
123     public class TracingIterator implements SequenceIterator {
124
125         SequenceIterator base;
126         String JavaDoc label;
127         boolean empty = true;
128
129
130         public TracingIterator(SequenceIterator base, String JavaDoc label) {
131             this.base = base;
132             this.label = label;
133         }
134
135         public Item next() throws XPathException {
136             Item n = base.next();
137             if (n==null) {
138                 if (empty) {
139                     traceItem(null, label);
140                 }
141             } else {
142                 traceItem(n, label + " [" + position() + ']');
143                 empty = false;
144             }
145             return n;
146         }
147
148         public Item current() {
149             return base.current();
150         }
151
152         public int position() {
153             return base.position();
154         }
155
156         public SequenceIterator getAnother() throws XPathException {
157             return new TracingIterator(base.getAnother(), label);
158         }
159
160         /**
161          * Get properties of this iterator, as a bit-significant integer.
162          *
163          * @return the properties of this iterator. This will be some combination of
164          * properties such as {@link GROUNDED}, {@link LAST_POSITION_FINDER},
165          * and {@link LOOKAHEAD}. It is always
166          * acceptable to return the value zero, indicating that there are no known special properties.
167          * It is acceptable for the properties of the iterator to change depending on its state.
168          */

169
170         public int getProperties() {
171             return 0;
172         }
173     }
174
175 }
176
177 //
178
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
179
// you may not use this file except in compliance with the License. You may obtain a copy of the
180
// License at http://www.mozilla.org/MPL/
181
//
182
// Software distributed under the License is distributed on an "AS IS" basis,
183
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
184
// See the License for the specific language governing rights and limitations under the License.
185
//
186
// The Original Code is: all this file.
187
//
188
// The Initial Developer of the Original Code is Michael H. Kay.
189
//
190
// Portions created by (your name) are Copyright (C) (your legal entity). All Rights Reserved.
191
//
192
// Contributor(s): none.
193
//
194
Popular Tags