Debian Codegraph @

net/sf/saxon/functions/CodepointsToString.java

raw symbols SCIP
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.Callable;
11 import net.sf.saxon.expr.XPathContext;
12 import net.sf.saxon.om.Sequence;
13 import net.sf.saxon.om.SequenceIterator;
14 import net.sf.saxon.trans.XPathException;
15 import net.sf.saxon.tree.util.FastStringBuffer;
16 import net.sf.saxon.value.NumericValue;
17 import net.sf.saxon.value.StringValue;
18 import java.util.function.IntPredicate;
19
20 /**
21 * This class implements the function fn:codepoints-to-string()
22 */
23
24 public class CodepointsToString extends SystemFunction implements Callable {
25
26 /**
27 * Return the Unicode string corresponding to a given sequence of Unicode code values
28 *
29 * @param chars iterator delivering the characters as integer values
30 * @param checker used to test whether a character is valid in the appropriate XML version
31 * @return the sequence of characters as a CharSequence
32 * @throws net.sf.saxon.trans.XPathException
33 * if any of the integers is not the codepoint of a valid XML character
34 */
35
36 public static CharSequence unicodeToString(SequenceIterator chars, IntPredicate checker) throws XPathException {
37 FastStringBuffer sb = new FastStringBuffer(FastStringBuffer.C64);
38 while (true) {
39 NumericValue nextInt = (NumericValue) chars.next();
40 if (nextInt == null) {
41 return sb.condense();
42 }
43 long next = nextInt.longValue();
44 if (next < 0 || next > Integer.MAX_VALUE || !checker.test((int) next)) {
45 throw new XPathException("codepoints-to-string(): invalid XML character [x" + Integer.toHexString((int) next) + ']', "FOCH0001");
46 }
47 sb.appendWideChar((int) next);
48 }
49 }
50
51 /**
52 * Evaluate the expression
53 *
54 * @param context the dynamic evaluation context
55 * @param arguments the values of the arguments, supplied as Sequences
56 * @return the result of the evaluation, in the form of a Sequence
57 * @throws net.sf.saxon.trans.XPathException
58 * if a dynamic error occurs during the evaluation of the expression
59 */
60 public StringValue call(XPathContext context, Sequence[] arguments) throws XPathException {
61 SequenceIterator chars = arguments[0].iterate();
62 return new StringValue(unicodeToString(chars, context.getConfiguration().getValidCharacterChecker()));
63 }
64
65 public String getStreamerName() {
66 return "CodepointsToString";
67 }
68
69 }
70