| 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.trace; |
| 9 | |
| 10 | import net.sf.saxon.expr.Expression; |
| 11 | import net.sf.saxon.expr.Literal; |
| 12 | import net.sf.saxon.expr.StaticContext; |
| 13 | import net.sf.saxon.expr.flwor.Clause; |
| 14 | import net.sf.saxon.expr.flwor.TraceClause; |
| 15 | import net.sf.saxon.expr.instruct.TraceExpression; |
| 16 | import net.sf.saxon.expr.parser.CodeInjector; |
| 17 | import net.sf.saxon.om.StandardNames; |
| 18 | import net.sf.saxon.om.StructuredQName; |
| 19 | |
| 20 | /** |
| 21 | * A code injector that wraps every expression (other than a literal) in a TraceExpression, which causes |
| 22 | * a TraceListener to be notified when the expression is evaluated |
| 23 | */ |
| 24 | public class TraceCodeInjector implements CodeInjector { |
| 25 | |
| 26 | /** |
| 27 | * If tracing, wrap an expression in a trace instruction |
| 28 | * |
| 29 | * @param exp the expression to be wrapped |
| 30 | * @param env the static context |
| 31 | * @param construct integer constant identifying the kind of construct |
| 32 | * @param qName the name of the construct (if applicable) |
| 33 | * @return the expression that does the tracing |
| 34 | */ |
| 35 | |
| 36 | public Expression inject(Expression exp, /*@NotNull*/ StaticContext env, int construct, StructuredQName qName) { |
| 37 | if (exp instanceof Literal && !(construct == StandardNames.XSL_TEMPLATE || construct == StandardNames.XSL_FUNCTION)) { |
| 38 | return exp; |
| 39 | } |
| 40 | TraceExpression trace = new TraceExpression(exp); |
| 41 | //ExpressionTool.copyLocationInfo(exp, trace); |
| 42 | trace.setNamespaceResolver(env.getNamespaceResolver()); |
| 43 | trace.setConstructType(construct); |
| 44 | trace.setObjectName(qName); |
| 45 | //trace.setObjectNameCode(objectNameCode); |
| 46 | return trace; |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * If tracing, add a clause to a FLWOR expression that can be used to monitor requests for |
| 51 | * tuples to be processed |
| 52 | * |
| 53 | * @param target the clause whose evaluation is to be traced (or otherwise monitored) |
| 54 | * @param env the static context of the containing FLWOR expression |
| 55 | * @return the new clause to do the tracing; or null if no tracing is required at this point |
| 56 | */ |
| 57 | |
| 58 | public Clause injectClause(Clause target, StaticContext env) { |
| 59 | return new TraceClause(target, env.getNamespaceResolver()); |
| 60 | } |
| 61 | } |
| 62 |