Debian Codegraph @

net/sf/saxon/functions/NotFn.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.functions;
9
10 import net.sf.saxon.expr.Expression;
11 import net.sf.saxon.expr.Negatable;
12 import net.sf.saxon.expr.SystemFunctionCall;
13 import net.sf.saxon.expr.XPathContext;
14 import net.sf.saxon.expr.parser.ContextItemStaticInfo;
15 import net.sf.saxon.expr.parser.ExpressionTool;
16 import net.sf.saxon.expr.parser.ExpressionVisitor;
17 import net.sf.saxon.expr.parser.TypeChecker;
18 import net.sf.saxon.om.Sequence;
19 import net.sf.saxon.pattern.NodeTest;
20 import net.sf.saxon.trans.XPathException;
21 import net.sf.saxon.type.TypeHierarchy;
22 import net.sf.saxon.value.BooleanValue;
23
24 /**
25 * This class supports the XPath functions boolean(), not(), true(), and false()
26 */
27
28
29 public class NotFn extends SystemFunction {
30
31
32 @Override
33 public void supplyTypeInformation(ExpressionVisitor visitor, ContextItemStaticInfo contextItemType, Expression[] arguments) throws XPathException {
34 XPathException err = TypeChecker.ebvError(arguments[0], visitor.getConfiguration().getTypeHierarchy());
35 if (err != null) {
36 throw err;
37 }
38 }
39
40 /**
41 * Evaluate the expression
42 *
43 * @param context the dynamic evaluation context
44 * @param arguments the values of the arguments, supplied as Sequences
45 * @return the result of the evaluation, in the form of a Sequence
46 * @throws net.sf.saxon.trans.XPathException
47 * if a dynamic error occurs during the evaluation of the expression
48 */
49 public BooleanValue call(XPathContext context, Sequence[] arguments) throws XPathException {
50 return BooleanValue.get(!ExpressionTool.effectiveBooleanValue(arguments[0].iterate()));
51 }
52
53 /**
54 * Make an expression that either calls this function, or that is equivalent to a call
55 * on this function
56 *
57 * @param arguments the supplied arguments to the function call
58 * @return either a function call on this function, or an expression that delivers
59 * the same result
60 */
61 @Override
62 public Expression makeFunctionCall(Expression[] arguments) {
63 return new SystemFunctionCall(this, arguments) {
64 /**
65 * Evaluate the effective boolean value
66 */
67
68 public boolean effectiveBooleanValue(XPathContext c) throws XPathException {
69 try {
70 return !getArg(0).effectiveBooleanValue(c);
71 } catch (XPathException e) {
72 e.maybeSetLocation(getLocation());
73 e.maybeSetContext(c);
74 throw e;
75 }
76 }
77 };
78 }
79
80 /**
81 * Allow the function to create an optimized call based on the values of the actual arguments
82 *
83 * @param visitor the expression visitor
84 * @param contextInfo information about the context item
85 * @param arguments the supplied arguments to the function call
86 * @return either a function call on this function, or an expression that delivers
87 * the same result, or null indicating that no optimization has taken place
88 * @throws net.sf.saxon.trans.XPathException if an error is detected
89 */
90 @Override
91 public Expression makeOptimizedFunctionCall(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo, Expression... arguments) throws XPathException {
92 TypeHierarchy th = visitor.getStaticContext().getConfiguration().getTypeHierarchy();
93 if (arguments[0] instanceof Negatable && ((Negatable) arguments[0]).isNegatable(th)) {
94 return ((Negatable) arguments[0]).negate();
95 }
96 if (arguments[0].getItemType() instanceof NodeTest) {
97 SystemFunction empty = SystemFunction.makeFunction("empty", getRetainedStaticContext(), 1);
98 return empty.makeFunctionCall(arguments[0]).optimize(visitor, contextInfo);
99 }
100 return null;
101 }
102
103 public String getCompilerName() {
104 return "NotFnCompiler";
105 }
106
107 public String getStreamerName() {
108 return "NotFn";
109 }
110 }
111