|
45 | 45 | */
|
46 | 46 | package org.exist.xquery.functions.fn;
|
47 | 47 |
|
48 |
| -import org.exist.dom.QName; |
49 |
| -import org.exist.xquery.*; |
50 |
| -import org.exist.xquery.value.FunctionParameterSequenceType; |
51 |
| -import org.exist.xquery.value.FunctionReturnSequenceType; |
52 |
| -import org.exist.xquery.value.Item; |
| 48 | +import org.exist.xquery.BasicFunction; |
| 49 | +import org.exist.xquery.FunctionSignature; |
| 50 | +import org.exist.xquery.XPathException; |
| 51 | +import org.exist.xquery.XQueryContext; |
53 | 52 | import org.exist.xquery.value.Sequence;
|
54 |
| -import org.exist.xquery.value.SequenceIterator; |
55 |
| -import org.exist.xquery.value.SequenceType; |
56 |
| -import org.exist.xquery.value.Type; |
57 | 53 | import org.exist.xquery.value.ValueSequence;
|
| 54 | +import org.exist.xquery.value.Type; |
| 55 | + |
| 56 | +import static org.exist.xquery.FunctionDSL.optManyParam; |
| 57 | +import static org.exist.xquery.FunctionDSL.returnsOptMany; |
| 58 | +import static org.exist.xquery.functions.fn.FnModule.functionSignature; |
58 | 59 |
|
59 | 60 | /**
|
60 | 61 | * Implements the fn:reverse function.
|
61 | 62 | *
|
62 | 63 | * @author <a href="mailto:[email protected]">Piotr Kaminski</a>
|
| 64 | + * @author <a href="mailto:[email protected]">Adam Retter</a> |
63 | 65 | */
|
64 |
| -public class FunReverse extends Function { |
| 66 | +public class FunReverse extends BasicFunction { |
65 | 67 |
|
66 |
| - public final static FunctionSignature signature = |
67 |
| - new FunctionSignature( |
68 |
| - new QName("reverse", FnModule.NAMESPACE_URI), |
69 |
| - "Reverses the order of items in a sequence. If the argument is an empty" + |
70 |
| - "sequence, the empty sequence is returned.", |
71 |
| - new SequenceType[] {new FunctionParameterSequenceType("arg", Type.ITEM, Cardinality.ZERO_OR_MORE, "The sequence to reverse")}, |
72 |
| - new FunctionReturnSequenceType(Type.ITEM, Cardinality.ZERO_OR_MORE, "the reverse order sequence")); |
| 68 | + public static final FunctionSignature FS_REVERSE = functionSignature( |
| 69 | + "reverse", |
| 70 | + "Reverses the order of items in a sequence. If the argument is an empty sequence, the empty sequence is returned.", |
| 71 | + returnsOptMany(Type.ITEM, "The reverse order sequence"), |
| 72 | + optManyParam("arg", Type.ITEM, "The sequence to reverse") |
| 73 | + ); |
73 | 74 |
|
74 |
| - public FunReverse(XQueryContext context) { |
75 |
| - super(context, signature); |
76 |
| - } |
77 |
| - |
78 |
| - public void analyze(final AnalyzeContextInfo contextInfo) throws XPathException { |
79 |
| - inPredicate = (contextInfo.getFlags() & IN_PREDICATE) > 0; |
80 |
| - contextId = contextInfo.getContextId(); |
81 |
| - contextInfo.setParent(this); |
82 |
| - |
83 |
| - final SequenceType[] argumentTypes = getSignature().getArgumentTypes(); |
84 |
| - for (int i = 0; i < getArgumentCount(); i++) { |
85 |
| - final Expression arg = getArgument(i); |
86 |
| - |
87 |
| - // call analyze for each argument |
88 |
| - final AnalyzeContextInfo argContextInfo = new AnalyzeContextInfo(contextInfo); |
89 |
| - arg.analyze(argContextInfo); |
90 |
| - if (i == 0) { |
91 |
| - contextInfo.setStaticReturnType(argContextInfo.getStaticReturnType()); |
92 |
| - } |
93 |
| - |
94 |
| - if (!argumentsChecked) { |
95 |
| - // statically check the argument |
96 |
| - SequenceType argType = null; |
97 |
| - if (argumentTypes != null && i < argumentTypes.length) { |
98 |
| - argType = argumentTypes[i]; |
99 |
| - } |
100 |
| - checkArgument(arg, argType, argContextInfo, i + 1); |
101 |
| - } |
102 |
| - } |
103 |
| - argumentsChecked = true; |
| 75 | + public FunReverse(final XQueryContext context, final FunctionSignature signature) { |
| 76 | + super(context, signature); |
104 | 77 | }
|
105 | 78 |
|
106 |
| - public Sequence eval(Sequence contextSequence, Item contextItem) throws XPathException { |
107 |
| - if (context.getProfiler().isEnabled()) { |
108 |
| - context.getProfiler().start(this); |
109 |
| - context.getProfiler().message(this, Profiler.DEPENDENCIES, "DEPENDENCIES", Dependency.getDependenciesName(this.getDependencies())); |
110 |
| - if (contextSequence != null) |
111 |
| - {context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT SEQUENCE", contextSequence);} |
112 |
| - if (contextItem != null) |
113 |
| - {context.getProfiler().message(this, Profiler.START_SEQUENCES, "CONTEXT ITEM", contextItem.toSequence());} |
114 |
| - } |
115 |
| - |
116 |
| - Sequence result; |
117 |
| - final Sequence seq = getArguments(contextSequence, contextItem)[0]; |
118 |
| - if (seq.isEmpty()) |
119 |
| - {result = Sequence.EMPTY_SEQUENCE;} |
120 |
| - else { |
121 |
| - final Sequence tmp = new ValueSequence(); |
122 |
| - Item item; |
123 |
| - for(final SequenceIterator i = seq.iterate(); i.hasNext(); ) { |
124 |
| - item = i.nextItem(); |
125 |
| - tmp.add(item); |
126 |
| - } |
127 |
| - result = new ValueSequence(); |
128 |
| - for (int i = seq.getItemCount() - 1; i >= 0; i--) { |
129 |
| - result.add(tmp.itemAt(i)); |
130 |
| - } |
| 79 | + @Override |
| 80 | + public Sequence eval(final Sequence[] args, final Sequence contextSequence) throws XPathException { |
| 81 | + final Sequence seq = args[0]; |
| 82 | + final Sequence result; |
| 83 | + if (seq.isEmpty()) { |
| 84 | + result = Sequence.EMPTY_SEQUENCE; |
| 85 | + } else { |
| 86 | + result = new ValueSequence(); |
| 87 | + for (int i = seq.getItemCount() - 1; i >= 0; i--) { |
| 88 | + result.add(seq.itemAt(i)); |
| 89 | + } |
131 | 90 | }
|
132 |
| - |
133 |
| - if (context.getProfiler().isEnabled()) |
134 |
| - {context.getProfiler().end(this, "", result);} |
135 |
| - |
136 | 91 | return result;
|
137 |
| - } |
138 |
| - |
| 92 | + } |
139 | 93 | }
|
0 commit comments