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
53
import org .exist .xquery .value .ValueSequence ;
55
- import org .exist .xquery .value .SequenceType ;
56
54
import org .exist .xquery .value .Type ;
57
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 ;
59
+
58
60
/**
59
61
* Implements the fn:reverse function.
60
62
*
61
63
* @author <a href="mailto:[email protected] ">Piotr Kaminski</a>
62
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
79
@ Override
107
- public Sequence eval (final Sequence contextSequence , final Item contextItem ) throws XPathException {
108
- if (context .getProfiler ().isEnabled ()) {
109
- context .getProfiler ().start (this );
110
- context .getProfiler ().message (this , Profiler .DEPENDENCIES , "DEPENDENCIES" , Dependency .getDependenciesName (this .getDependencies ()));
111
- if (contextSequence != null ) {
112
- context .getProfiler ().message (this , Profiler .START_SEQUENCES , "CONTEXT SEQUENCE" , contextSequence );
113
- }
114
- if (contextItem != null ) {
115
- context .getProfiler ().message (this , Profiler .START_SEQUENCES , "CONTEXT ITEM" , contextItem .toSequence ());
116
- }
117
- }
118
-
119
- final Sequence seq = getArguments (contextSequence , contextItem )[0 ];
80
+ public Sequence eval (final Sequence [] args , final Sequence contextSequence ) throws XPathException {
81
+ final Sequence seq = args [0 ];
120
82
final Sequence result ;
121
83
if (seq .isEmpty ()) {
122
84
result = Sequence .EMPTY_SEQUENCE ;
@@ -126,11 +88,6 @@ public Sequence eval(final Sequence contextSequence, final Item contextItem) thr
126
88
result .add (seq .itemAt (i ));
127
89
}
128
90
}
129
-
130
- if (context .getProfiler ().isEnabled ()) {
131
- context .getProfiler ().end (this , "" , result );
132
- }
133
-
134
91
return result ;
135
92
}
136
93
}
0 commit comments