| 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 net.sf.saxon.Configuration; |
| 11 | import net.sf.saxon.expr.StaticContext; |
| 12 | import net.sf.saxon.expr.XPathContext; |
| 13 | |
| 14 | /** |
| 15 | * The ExpressionVisitor supports the various phases of processing of an expression tree which require |
| 16 | * a recursive walk of the tree structure visiting each node in turn. In maintains a stack holding the |
| 17 | * ancestor nodes of the node currently being visited. |
| 18 | */ |
| 19 | |
| 20 | public class ExpressionVisitor { |
| 21 | |
| 22 | private StaticContext staticContext; |
| 23 | private boolean optimizeForStreaming = false; |
| 24 | private boolean optimizeForPatternMatching = false; |
| 25 | private Configuration config; |
| 26 | private Optimizer optimizer; |
| 27 | private int depth = 0; |
| 28 | |
| 29 | private boolean suppressWarnings = false; |
| 30 | |
| 31 | private final static int MAX_DEPTH = 500; |
| 32 | |
| 33 | |
| 34 | /** |
| 35 | * Create an ExpressionVisitor |
| 36 | * @param config the Saxon configuration |
| 37 | */ |
| 38 | |
| 39 | public ExpressionVisitor(Configuration config) { |
| 40 | this.config = config; |
| 41 | } |
| 42 | |
| 43 | /** |
| 44 | * Get the Configuration |
| 45 | * @return the configuration |
| 46 | */ |
| 47 | public Configuration getConfiguration() { |
| 48 | return config; |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Get the static context for the expressions being visited. Note: this may not reflect all changes |
| 53 | * in static context (e.g. namespace context, base URI) applying to nested expressions |
| 54 | * |
| 55 | * @return the static context |
| 56 | */ |
| 57 | |
| 58 | public StaticContext getStaticContext() { |
| 59 | return staticContext; |
| 60 | } |
| 61 | |
| 62 | /** |
| 63 | * Set the static context for the expressions being visited. Note: this may not reflect all changes |
| 64 | * in static context (e.g. namespace context, base URI) applying to nested expressions |
| 65 | * |
| 66 | * @param staticContext the static context |
| 67 | */ |
| 68 | |
| 69 | public void setStaticContext(StaticContext staticContext) { |
| 70 | this.staticContext = staticContext; |
| 71 | } |
| 72 | |
| 73 | /** |
| 74 | * Factory method: make an expression visitor |
| 75 | * |
| 76 | * @param env the static context |
| 77 | * @return the new expression visitor |
| 78 | */ |
| 79 | |
| 80 | public static ExpressionVisitor make(StaticContext env) { |
| 81 | ExpressionVisitor visitor = new ExpressionVisitor(env.getConfiguration()); |
| 82 | visitor.setStaticContext(env); |
| 83 | return visitor; |
| 84 | } |
| 85 | |
| 86 | /** |
| 87 | * Issue a warning message |
| 88 | * |
| 89 | * @param message the message |
| 90 | * @param locator the query/stylesheet location associated with the message |
| 91 | */ |
| 92 | |
| 93 | public void issueWarning(String message, Location locator) { |
| 94 | if (!isSuppressWarnings()) { |
| 95 | staticContext.issueWarning(message, locator); |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | /** |
| 100 | * Create a dynamic context suitable for early evaluation of constant subexpressions |
| 101 | * @return a dynamic evaluation context |
| 102 | */ |
| 103 | |
| 104 | public XPathContext makeDynamicContext() { |
| 105 | return staticContext.makeEarlyEvaluationContext(); |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Obtain an optimizer. The optimizer is created on the first call of this method and is |
| 110 | * retained for use in subsequent calls. |
| 111 | * @return a suitable optimizer, taking into account the optimization options defined by the |
| 112 | * Configuration and by the static context |
| 113 | */ |
| 114 | |
| 115 | public Optimizer obtainOptimizer() { |
| 116 | if (optimizer == null) { |
| 117 | optimizer = config.obtainOptimizer(staticContext.getOptimizerOptions()); |
| 118 | } |
| 119 | return optimizer; |
| 120 | } |
| 121 | |
| 122 | /** |
| 123 | * Tell the visitor to optimize expressions for evaluation in a streaming environment |
| 124 | * |
| 125 | * @param option true if optimizing for streaming |
| 126 | */ |
| 127 | |
| 128 | public void setOptimizeForStreaming(boolean option) { |
| 129 | optimizeForStreaming = option; |
| 130 | } |
| 131 | |
| 132 | /** |
| 133 | * Ask whether the visitor is to optimize expressions for evaluation in a streaming environment |
| 134 | * |
| 135 | * @return true if optimizing for streaming |
| 136 | */ |
| 137 | |
| 138 | public boolean isOptimizeForStreaming() { |
| 139 | return optimizeForStreaming; |
| 140 | } |
| 141 | |
| 142 | /** |
| 143 | * Tell the visitor to optimize expressions for evaluation as an XSLT match pattern |
| 144 | * |
| 145 | * @param option true if optimizing for pattern matching |
| 146 | */ |
| 147 | |
| 148 | public void setOptimizeForPatternMatching(boolean option) { |
| 149 | optimizeForPatternMatching = option; |
| 150 | } |
| 151 | |
| 152 | /** |
| 153 | * Ask whether the visitor is to optimize expressions for evaluation as an XSLT match pattern |
| 154 | * |
| 155 | * @return true if optimizing for pattern matching |
| 156 | */ |
| 157 | |
| 158 | public boolean isOptimizeForPatternMatching() { |
| 159 | return optimizeForPatternMatching; |
| 160 | } |
| 161 | |
| 162 | /** |
| 163 | * Get the target environment for which we are optimizing |
| 164 | * @return the target environment (for example "EE" or "JS"). Defaults to the edition |
| 165 | * of the current configuration |
| 166 | */ |
| 167 | |
| 168 | public String getTargetEdition() { |
| 169 | return staticContext.getPackageData().getTargetEdition(); |
| 170 | } |
| 171 | |
| 172 | /** |
| 173 | * Increment and test depth |
| 174 | * @return true if the current depth is less than a maximum limit; and increment the current depth |
| 175 | */ |
| 176 | |
| 177 | public boolean incrementAndTestDepth() { |
| 178 | return depth++ < MAX_DEPTH; |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Decrement depth |
| 183 | */ |
| 184 | |
| 185 | public void decrementDepth() { |
| 186 | depth--; |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Ask if warnings have been suppressed. This typically happens when processing code that has been copied, |
| 191 | * to prevent multiple warnings for the same code |
| 192 | * @return true if warnings are suppressed |
| 193 | */ |
| 194 | |
| 195 | public boolean isSuppressWarnings() { |
| 196 | return suppressWarnings; |
| 197 | } |
| 198 | |
| 199 | /** |
| 200 | * Say whether warnings should be suppressed. This is typically done when processing code that has been |
| 201 | * copied, to prevent multiple warnings for the same code |
| 202 | * @param suppressWarnings set to true to suppress warnings |
| 203 | */ |
| 204 | |
| 205 | public void setSuppressWarnings(boolean suppressWarnings) { |
| 206 | this.suppressWarnings = suppressWarnings; |
| 207 | } |
| 208 | |
| 209 | } |
| 210 |