| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 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 |
|
| 22 |
|
| 23 |
|
| 24 |
public class CodepointsToString extends SystemFunction implements Callable { |
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 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 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 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 |
|