net.sf.saxon.functions.StringLength_1 Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of Saxon-HE Show documentation
Show all versions of Saxon-HE Show documentation
The XSLT and XQuery Processor
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Copyright (c) 2018-2023 Saxonica Limited
// This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
// If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/.
// This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0.
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
package net.sf.saxon.functions;
import net.sf.saxon.expr.elab.ItemEvaluator;
import net.sf.saxon.expr.elab.UnicodeStringEvaluator;
import net.sf.saxon.expr.elab.Elaborator;
import net.sf.saxon.expr.elab.ItemElaborator;
import net.sf.saxon.expr.Expression;
import net.sf.saxon.expr.SystemFunctionCall;
import net.sf.saxon.expr.XPathContext;
import net.sf.saxon.om.Item;
import net.sf.saxon.om.Sequence;
import net.sf.saxon.str.UnicodeString;
import net.sf.saxon.trans.XPathException;
import net.sf.saxon.value.AtomicValue;
import net.sf.saxon.value.Int64Value;
import net.sf.saxon.value.IntegerValue;
import net.sf.saxon.value.StringValue;
/**
* Implement the XPath string-length() function
*/
public class StringLength_1 extends ScalarSystemFunction {
/**
* For an expression that returns an integer or a sequence of integers, get
* a lower and upper bound on the values of the integers that may be returned, from
* static analysis. The default implementation returns null, meaning "unknown" or
* "not applicable". Other implementations return an array of two IntegerValue objects,
* representing the lower and upper bounds respectively. The values
* UNBOUNDED_LOWER and UNBOUNDED_UPPER are used by convention to indicate that
* the value may be arbitrarily large. The values MAX_STRING_LENGTH and MAX_SEQUENCE_LENGTH
* are used to indicate values limited by the size of a string or the size of a sequence.
*
* @return the lower and upper bounds of integer values in the result, or null to indicate
* unknown or not applicable.
*/
@Override
public IntegerValue[] getIntegerBounds() {
return new IntegerValue[]{Int64Value.ZERO, Expression.MAX_STRING_LENGTH};
}
@Override
public Sequence resultWhenEmpty() {
return Int64Value.ZERO;
}
@Override
public AtomicValue evaluate(Item arg, XPathContext context) throws XPathException {
if (arg instanceof StringValue) {
return Int64Value.makeIntegerValue(((StringValue) arg).length());
} else {
UnicodeString s;
try {
s = arg.getUnicodeStringValue();
} catch (UnsupportedOperationException e) {
throw new XPathException("Cannot get the string value of a function item", "FOTY0013");
}
return Int64Value.makeIntegerValue(s.length());
}
}
@Override
public String getCompilerName() {
return "StringLengthCompiler";
}
/**
* Make an elaborator for a system function call on this function
*
* @return a suitable elaborator; or null if no custom elaborator is available
*/
@Override
public Elaborator getElaborator() {
return new StringLengthFnElaborator();
}
public static class StringLengthFnElaborator extends ItemElaborator {
public ItemEvaluator elaborateForItem() {
SystemFunctionCall fnc = (SystemFunctionCall) getExpression();
Expression arg = fnc.getArg(0);
UnicodeStringEvaluator argEval = arg.makeElaborator().elaborateForUnicodeString(true);
return context -> {
UnicodeString str = argEval.eval(context);
return Int64Value.makeIntegerValue(str.length());
};
}
}
}