Debian Codegraph @

net/sf/saxon/expr/HomogeneityChecker.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.expr;
9
10 import net.sf.saxon.expr.parser.ContextItemStaticInfo;
11 import net.sf.saxon.expr.parser.ExpressionTool;
12 import net.sf.saxon.expr.parser.ExpressionVisitor;
13 import net.sf.saxon.expr.parser.RebindingMap;
14 import net.sf.saxon.expr.sort.DocumentSorter;
15 import net.sf.saxon.om.SequenceIterator;
16 import net.sf.saxon.pattern.AnyNodeTest;
17 import net.sf.saxon.trans.XPathException;
18 import net.sf.saxon.tree.iter.HomogeneityCheckerIterator;
19 import net.sf.saxon.type.ErrorType;
20 import net.sf.saxon.type.ItemType;
21 import net.sf.saxon.type.TypeHierarchy;
22
23 /**
24 * This class is an expression that does a run-time check of the result of a "/" expression
25 * to ensure that (a) the results consists entirely of atomic values and function items, or entirely of nodes,
26 * and (b) if the results are nodes, then they are deduplicated and sorted into document order.
27 */
28 public class HomogeneityChecker extends UnaryExpression {
29
30 public HomogeneityChecker(Expression base) {
31 super(base);
32 }
33
34 protected OperandRole getOperandRole() {
35 return OperandRole.INSPECT;
36 }
37
38 /**
39 * Type-check the expression. Default implementation for unary operators that accept
40 * any kind of operand
41 */
42 /*@NotNull*/
43 @Override
44 public Expression typeCheck(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException {
45 if (getBaseExpression() instanceof HomogeneityChecker) {
46 return getBaseExpression().typeCheck(visitor, contextInfo);
47 }
48 getOperand().typeCheck(visitor, contextInfo);
49 TypeHierarchy th = visitor.getConfiguration().getTypeHierarchy();
50 ItemType type = getBaseExpression().getItemType();
51 if (type.equals(ErrorType.getInstance())) {
52 return Literal.makeEmptySequence();
53 }
54 int rel = th.relationship(type, AnyNodeTest.getInstance());
55 if (rel == TypeHierarchy.DISJOINT) {
56 // expression cannot return nodes, so this checker is redundant
57 // if (getBaseExpression() instanceof SlashExpression && ((SlashExpression) getBaseExpression()).getLeadingSteps() instanceof SlashExpression &&
58 // (((SlashExpression) getBaseExpression()).getLeadingSteps().getSpecialProperties() & StaticProperty.ORDERED_NODESET) == 0) {
59 // DocumentSorter ds = new DocumentSorter(((SlashExpression) getBaseExpression()).getLeadingSteps());
60 // SlashExpression se = new SlashExpression(ds, ((SlashExpression) getBaseExpression()).getLastStep());
61 // ExpressionTool.copyLocationInfo(this, se);
62 // return se;
63 // } else {
64 return getBaseExpression();
65 // }
66 } else if (rel == TypeHierarchy.SAME_TYPE || rel == TypeHierarchy.SUBSUMED_BY) {
67 // expression always returns nodes, so replace this expression with a DocumentSorter
68 Expression savedBase = getBaseExpression();
69 Expression parent = getParentExpression();
70 getOperand().detachChild();
71 DocumentSorter ds = new DocumentSorter(savedBase);
72 ExpressionTool.copyLocationInfo(this, ds);
73 ds.setParentExpression(parent);
74 //ds.verifyParentPointers();
75 return ds;
76 }
77 return this;
78 }
79
80 /*@NotNull*/
81 @Override
82 public Expression optimize(ExpressionVisitor visitor, ContextItemStaticInfo contextInfo) throws XPathException {
83 if (getBaseExpression() instanceof HomogeneityChecker) {
84 return getBaseExpression().optimize(visitor, contextInfo);
85 }
86 return super.optimize(visitor, contextInfo);
87 }
88
89 /**
90 * Copy an expression. This makes a deep copy.
91 *
92 * @return the copy of the original expression
93 * @param rebindings
94 */
95 /*@NotNull*/
96 @Override
97 public Expression copy(RebindingMap rebindings) {
98 HomogeneityChecker hc = new HomogeneityChecker(getBaseExpression().copy(rebindings));
99 ExpressionTool.copyLocationInfo(this, hc);
100 return hc;
101 }
102
103 /**
104 * An implementation of Expression must provide at least one of the methods evaluateItem(), iterate(), or process().
105 * This method indicates which of these methods is provided directly. The other methods will always be available
106 * indirectly, using an implementation that relies on one of the other methods.
107 *
108 * @return the implementation method, for example {@link #ITERATE_METHOD} or {@link #EVALUATE_METHOD} or
109 * {@link #PROCESS_METHOD}
110 */
111 @Override
112 public int getImplementationMethod() {
113 return ITERATE_METHOD;
114 }
115
116 /**
117 * Iterate the path-expression in a given context
118 *
119 * @param context the evaluation context
120 */
121
122 /*@NotNull*/
123 public SequenceIterator<?> iterate(final XPathContext context) throws XPathException {
124
125 // This class delivers the result of the path expression in unsorted order,
126 // without removal of duplicates. If sorting and deduplication are needed,
127 // this is achieved by wrapping the path expression in a DocumentSorter
128
129 SequenceIterator<?> base = getBaseExpression().iterate(context);
130 return new HomogeneityCheckerIterator(base, getLocation());
131 }
132
133 /**
134 * Get a name identifying the kind of expression, in terms meaningful to a user.
135 *
136 * @return a name identifying the kind of expression, in terms meaningful to a user.
137 * The name will always be in the form of a lexical XML QName, and should match the name used
138 * in explain() output displaying the expression.
139 */
140 @Override
141 public String getExpressionName() {
142 return "homCheck";
143 }
144 }
145