Debian Codegraph @

net/sf/saxon/functions/CopyOfFn.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.*;
11 import net.sf.saxon.expr.instruct.CopyOf;
12 import net.sf.saxon.lib.Validation;
13 import net.sf.saxon.om.*;
14 import net.sf.saxon.trans.XPathException;
15 import net.sf.saxon.tree.wrapper.VirtualCopy;
16
17 /**
18 * XSLT 3.0 function copy-of(). This compiles into an xsl:copy-of instruction, except when called dynamically.
19 */
20 public class CopyOfFn extends SystemFunction {
21
22 public int getCardinality(Expression[] arguments) {
23 return arguments[0].getCardinality();
24 }
25
26 /**
27 * Evaluate the expression (used only for dynamic calls)
28 *
29 * @param context the dynamic evaluation context
30 * @param arguments the values of the arguments, supplied as Sequences
31 * @return the result of the evaluation, in the form of a Sequence
32 * @throws net.sf.saxon.trans.XPathException
33 * if a dynamic error occurs during the evaluation of the expression
34 */
35 public Sequence<?> call(final XPathContext context, Sequence[] arguments) throws XPathException {
36 Sequence<?> in = arguments.length == 0 ? context.getContextItem() : arguments[0];
37 SequenceIterator<?> input = in.iterate();
38 SequenceIterator<?> output = new ItemMappingIterator<>(input, item -> {
39 if (!(item instanceof NodeInfo)) {
40 return item;
41 } else {
42 VirtualCopy vc = VirtualCopy.makeVirtualCopy((NodeInfo) item);
43 vc.getTreeInfo().setCopyAccumulators(true);
44 // TODO: set the base URI
45 return vc;
46 }
47 });
48 return new LazySequence<>(output);
49 }
50
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 Expression arg;
64 if (arguments.length == 0) {
65 arg = new ContextItemExpression();
66 } else {
67 arg = arguments[0];
68 }
69 CopyOf fn = new CopyOf(arg, true, Validation.PRESERVE, null, false);
70 fn.setCopyAccumulators(true);
71 fn.setSchemaAware(false);
72 return fn;
73 }
74 }
75
76
77
78