| 1 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 2 | // Copyright (c) 2018 Saxonica Limited. |
| 3 | // This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0. |
| 4 | // If a copy of the MPL was not distributed with this file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 5 | // This Source Code Form is "Incompatible With Secondary Licenses", as defined by the Mozilla Public License, v. 2.0. |
| 6 | //////////////////////////////////////////////////////////////////////////////////////////////////////////////////// |
| 7 | |
| 8 | package net.sf.saxon.functions; |
| 9 | |
| 10 | import net.sf.saxon.expr.Expression; |
| 11 | import net.sf.saxon.expr.XPathContext; |
| 12 | import net.sf.saxon.om.Item; |
| 13 | import net.sf.saxon.om.One; |
| 14 | import net.sf.saxon.om.ZeroOrOne; |
| 15 | import net.sf.saxon.trans.XPathException; |
| 16 | import net.sf.saxon.value.AtomicValue; |
| 17 | import net.sf.saxon.value.Int64Value; |
| 18 | import net.sf.saxon.value.IntegerValue; |
| 19 | import net.sf.saxon.value.StringValue; |
| 20 | |
| 21 | /** |
| 22 | * Implement the XPath string-length() function |
| 23 | */ |
| 24 | |
| 25 | public class StringLength_1 extends ScalarSystemFunction { |
| 26 | |
| 27 | |
| 28 | /** |
| 29 | * For an expression that returns an integer or a sequence of integers, get |
| 30 | * a lower and upper bound on the values of the integers that may be returned, from |
| 31 | * static analysis. The default implementation returns null, meaning "unknown" or |
| 32 | * "not applicable". Other implementations return an array of two IntegerValue objects, |
| 33 | * representing the lower and upper bounds respectively. The values |
| 34 | * UNBOUNDED_LOWER and UNBOUNDED_UPPER are used by convention to indicate that |
| 35 | * the value may be arbitrarily large. The values MAX_STRING_LENGTH and MAX_SEQUENCE_LENGTH |
| 36 | * are used to indicate values limited by the size of a string or the size of a sequence. |
| 37 | * |
| 38 | * @return the lower and upper bounds of integer values in the result, or null to indicate |
| 39 | * unknown or not applicable. |
| 40 | */ |
| 41 | @Override |
| 42 | public IntegerValue[] getIntegerBounds() { |
| 43 | return new IntegerValue[]{Int64Value.ZERO, Expression.MAX_STRING_LENGTH}; |
| 44 | } |
| 45 | |
| 46 | @Override |
| 47 | public ZeroOrOne<? extends AtomicValue> resultWhenEmpty() { |
| 48 | return One.integer(0); |
| 49 | } |
| 50 | |
| 51 | @Override |
| 52 | public AtomicValue evaluate(Item arg, XPathContext context) throws XPathException { |
| 53 | if (arg instanceof StringValue) { |
| 54 | return Int64Value.makeIntegerValue(((StringValue) arg).getStringLength()); |
| 55 | } else { |
| 56 | CharSequence s; |
| 57 | try { |
| 58 | s = arg.getStringValueCS(); |
| 59 | } catch (UnsupportedOperationException e) { |
| 60 | throw new XPathException("Cannot get the string value of a function item", "FOTY0013"); |
| 61 | } |
| 62 | return Int64Value.makeIntegerValue(StringValue.getStringLength(s)); |
| 63 | } |
| 64 | } |
| 65 | |
| 66 | public String getCompilerName() { |
| 67 | return "StringLengthCompiler"; |
| 68 | } |
| 69 | |
| 70 | } |
| 71 |