com.xmlcalabash.functions.IterationPosition Maven / Gradle / Ivy
The newest version!
package com.xmlcalabash.functions;
import com.xmlcalabash.runtime.XCompoundStep;
import net.sf.saxon.expr.XPathContext;
import net.sf.saxon.lib.ExtensionFunctionCall;
import net.sf.saxon.lib.ExtensionFunctionDefinition;
import net.sf.saxon.om.Sequence;
import net.sf.saxon.trans.XPathException;
import net.sf.saxon.om.StructuredQName;
import net.sf.saxon.om.SequenceIterator;
import com.xmlcalabash.core.XProcRuntime;
import com.xmlcalabash.core.XProcConstants;
import com.xmlcalabash.core.XProcData;
import com.xmlcalabash.core.XProcException;
import com.xmlcalabash.runtime.XStep;
import net.sf.saxon.tree.iter.SingletonIterator;
import net.sf.saxon.value.Int64Value;
import net.sf.saxon.value.SequenceType;
//
// The contents of this file are subject to the Mozilla Public License Version 1.0 (the "License");
// you may not use this file except in compliance with the License. You may obtain a copy of the
// License at http://www.mozilla.org/MPL/
//
// Software distributed under the License is distributed on an "AS IS" basis,
// WITHOUT WARRANTY OF ANY KIND, either express or implied.
// See the License for the specific language governing rights and limitations under the License.
//
// The Original Code is: all this file.
//
// The Initial Developer of the Original Code is Michael H. Kay.
//
// Portions created by Norman Walsh are Copyright (C) Mark Logic Corporation. All Rights Reserved.
//
// Contributor(s): Norman Walsh.
//
/**
* Implementation of the XProc p:iteration-position function
*/
public class IterationPosition extends XProcExtensionFunctionDefinition {
private static StructuredQName funcname = new StructuredQName("p", XProcConstants.NS_XPROC, "iteration-position");
protected IterationPosition() {
// you can't call this one
}
public IterationPosition(XProcRuntime runtime) {
registry.registerRuntime(this, runtime);
}
public StructuredQName getFunctionQName() {
return funcname;
}
public int getMinimumNumberOfArguments() {
return 0;
}
public int getMaximumNumberOfArguments() {
return 0;
}
public SequenceType[] getArgumentTypes() {
return new SequenceType[]{SequenceType.OPTIONAL_ATOMIC};
}
public SequenceType getResultType(SequenceType[] suppliedArgumentTypes) {
return SequenceType.SINGLE_ATOMIC;
}
public ExtensionFunctionCall makeCallExpression() {
return new IterationPositionCall(this);
}
private class IterationPositionCall extends ExtensionFunctionCall {
private XProcExtensionFunctionDefinition xdef = null;
public IterationPositionCall(XProcExtensionFunctionDefinition def) {
xdef = def;
}
public Sequence call(XPathContext xPathContext, Sequence[] sequences) throws XPathException {
XProcRuntime runtime = registry.getRuntime(xdef);
XProcData data = runtime.getXProcData();
XStep step = data.getStep();
// FIXME: this can't be the best way to do this...
// step == null in use-when
if (step != null && !(step instanceof XCompoundStep)) {
throw XProcException.dynamicError(23);
}
return new Int64Value(runtime.getXProcData().getIterationPosition());
}
}
}