| 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.pattern; |
| 9 | |
| 10 | import net.sf.saxon.Configuration; |
| 11 | import net.sf.saxon.expr.*; |
| 12 | import net.sf.saxon.expr.parser.ExpressionTool; |
| 13 | import net.sf.saxon.om.AxisInfo; |
| 14 | import net.sf.saxon.trans.XPathException; |
| 15 | |
| 16 | /** |
| 17 | * This is a singleton class used to convert an expression to an equivalent pattern. |
| 18 | * This version of the class is used to generate conventional XSLT match patterns; |
| 19 | * there is another version used to generate patterns suitable for streamed evaluation |
| 20 | * in Saxon-EE. |
| 21 | */ |
| 22 | public class PatternMaker { |
| 23 | |
| 24 | /** |
| 25 | * Static factory method to make a pattern by converting an expression. The supplied |
| 26 | * expression is the equivalent expression to the pattern, in the sense that it takes |
| 27 | * the same syntactic form. |
| 28 | * <p>Note that this method does NOT check all the rules for XSLT patterns; it deliberately allows |
| 29 | * a (slightly) wider class of expressions to be converted than XSLT allows.</p> |
| 30 | * <p>The expression root() at the start of the expression has a special meaning: it refers to |
| 31 | * the root of the subtree in which the pattern must match, which can be supplied at run-time |
| 32 | * during pattern matching. This is used for patterns that represent streamable path expressions.</p> |
| 33 | * |
| 34 | * @param expression the expression to be converted, which must already have been simplified and |
| 35 | * type-checked |
| 36 | * @param config the Saxon configuration |
| 37 | * @param is30 set to true if XSLT 3.0 syntax is to be accepted |
| 38 | * @return the compiled pattern |
| 39 | * @throws net.sf.saxon.trans.XPathException |
| 40 | * if the expression cannot be converted |
| 41 | */ |
| 42 | |
| 43 | /*@NotNull*/ |
| 44 | public static Pattern fromExpression(Expression expression, Configuration config, boolean is30) throws XPathException { |
| 45 | Pattern result = expression.toPattern(config); |
| 46 | ExpressionTool.copyLocationInfo(expression, result); |
| 47 | // result.setOriginalText(expression.toString()); |
| 48 | // result.setSystemId(expression.getSystemId()); |
| 49 | // result.setLineNumber(expression.getLineNumber()); |
| 50 | //result.setExecutable(expression.getExecutable()); |
| 51 | return result; |
| 52 | } |
| 53 | |
| 54 | public static byte getAxisForPathStep(Expression step) throws XPathException { |
| 55 | if (step instanceof AxisExpression) { |
| 56 | return AxisInfo.inverseAxis[((AxisExpression) step).getAxis()]; |
| 57 | } else if (step instanceof FilterExpression) { |
| 58 | return getAxisForPathStep(((FilterExpression) step).getSelectExpression()); |
| 59 | } else if (step instanceof FirstItemExpression) { |
| 60 | return getAxisForPathStep(((FirstItemExpression) step).getBaseExpression()); |
| 61 | } else if (step instanceof SubscriptExpression) { |
| 62 | return getAxisForPathStep(((SubscriptExpression) step).getBaseExpression()); |
| 63 | } else if (step instanceof SlashExpression) { |
| 64 | return getAxisForPathStep(((SlashExpression) step).getFirstStep()); |
| 65 | } else if (step instanceof ContextItemExpression) { |
| 66 | return AxisInfo.SELF; |
| 67 | } else { |
| 68 | throw new XPathException("The path in a pattern must contain simple steps"); |
| 69 | } |
| 70 | } |
| 71 | } |
| 72 |