Debian Codegraph @

net/sf/saxon/style/TextValueTemplateContext.java

raw symbols SCIP
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.style;
9
10 import net.sf.saxon.expr.Expression;
11 import net.sf.saxon.expr.LocalVariableReference;
12 import net.sf.saxon.expr.VariableReference;
13 import net.sf.saxon.om.AxisInfo;
14 import net.sf.saxon.om.NodeInfo;
15 import net.sf.saxon.om.StructuredQName;
16 import net.sf.saxon.trans.XPathException;
17 import net.sf.saxon.tree.iter.AxisIterator;
18
19 /**
20 * Define the static context for expressions appearing between curly braces in a text value template.
21 * The rules for binding local variables are slightly different from the rules for variables appearing
22 * within attribute nodes.
23 */
24 public class TextValueTemplateContext extends ExpressionContext {
25
26 TextValueTemplateNode textNode;
27
28 public TextValueTemplateContext(StyleElement parent, TextValueTemplateNode textNode) {
29 super(parent, null);
30 this.textNode = textNode;
31 }
32
33 /**
34 * Bind a variable to an object that can be used to refer to it
35 *
36 * @param qName the name of the variable
37 * @return a VariableDeclaration object that can be used to identify it in the Bindery,
38 * @throws net.sf.saxon.trans.XPathException
39 * if the variable has not been declared
40 */
41 @Override
42 public Expression bindVariable(StructuredQName qName) throws XPathException {
43 SourceBinding siblingVar = bindLocalVariable(qName);
44 if (siblingVar == null) {
45 return super.bindVariable(qName);
46 } else {
47 VariableReference var = new LocalVariableReference(qName);
48 siblingVar.registerReference(var);
49 return var;
50 }
51 }
52
53 private SourceBinding bindLocalVariable(StructuredQName qName) {
54 NodeInfo curr = textNode;
55
56 // first search for a local variable declaration
57
58 AxisIterator preceding = curr.iterateAxis(AxisInfo.PRECEDING_SIBLING);
59 while ((curr = preceding.next()) != null) {
60 if (curr instanceof XSLGeneralVariable) {
61 SourceBinding sourceBinding = ((XSLGeneralVariable) curr).getBindingInformation(qName);
62 if (sourceBinding != null) {
63 return sourceBinding;
64 }
65 }
66
67 }
68 return null;
69 }
70
71 }
72