Database Reference
In-Depth Information
Type . STRING ,
Cardinality . ZERO_OR_ONE ,
"An optional greeting, if omitted then 'hello' is used."
),
new FunctionParameterSequenceType ( "visitors" ,
Type . STRING ,
Cardinality . ONE_OR_MORE ,
"The visitors, i.e. the names of the people that the greeter is "
+ "saying 'hello' to."
),
},
new FunctionReturnSequenceType ( Type . DOCUMENT ,
Cardinality . ONE ,
"The hello!"
)
);
As our new function takes parameters, we define an array of
org.exist.xquery.FunctionParameterSequenceType objects in our Function
Signature . You can see each of the parameters named, its type and cardinality
defined, and a description provided for documentation purposes.
Now that we have written a signature for our function, we can implement the actual
processing of the function. We can do this within the same class of our h:hello-
world function from Example 16-14 by simply checking for a different calling signa‐
ture within our eval function, handling the parameters we are interested in, and then
executing our business logic. For example:
@Override
public Sequence eval ( final Sequence [] args ,
final Sequence contextSequence ) throws XPathException {
final Sequence result ;
//act on the invoked function name
if ( isCalledAs ( qnHelloWorld . getLocalName ())) {
result = sayHelloWorld ();
} else if ( isCalledAs ( qnSayHello . getLocalName ())) {
final String greeter = args [ 0 ]. itemAt ( 0 ). getStringValue ();
final String greeting ;
if ( args [ 1 ]. hasOne ()) {
greeting = args [ 1 ]. itemAt ( 0 ). getStringValue ();
} else {
greeting = "hello" ;
}
final List < String > visitors =
new ArrayList < String >( args [ 2 ]. getItemCount ());
Search WWH ::




Custom Search