Debian Codegraph @

net/sf/saxon/functions/NormalizeUnicode.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.XPathContext;
11 import net.sf.saxon.om.Sequence;
12 import net.sf.saxon.serialize.codenorm.Normalizer;
13 import net.sf.saxon.trans.XPathException;
14 import net.sf.saxon.tree.tiny.CompressedWhitespace;
15 import net.sf.saxon.value.StringValue;
16 import net.sf.saxon.value.Whitespace;
17
18 /**
19 * Implement the XPath normalize-unicode() function (both the 1-argument and 2-argument versions)
20 */
21
22 public class NormalizeUnicode extends SystemFunction {
23
24 /**
25 * Evaluate the expression
26 *
27 * @param context the dynamic evaluation context
28 * @param arguments the values of the arguments, supplied as SequenceIterators
29 * @return the result of the evaluation, in the form of a SequenceIterator
30 * @throws net.sf.saxon.trans.XPathException
31 * if a dynamic error occurs during the evaluation of the expression
32 */
33 public StringValue call(XPathContext context, Sequence[] arguments) throws XPathException {
34 StringValue sv = (StringValue) arguments[0].head();
35 if (sv == null) {
36 return StringValue.EMPTY_STRING;
37 }
38 String nf = arguments.length == 1 ? "NFC" : Whitespace.trim(arguments[1].head().getStringValue());
39 return normalize(sv, nf, context);
40 }
41
42 public static StringValue normalize(StringValue sv, String form, XPathContext c) throws XPathException {
43 byte fb;
44
45 if (form.equalsIgnoreCase("NFC")) {
46 fb = Normalizer.C;
47 } else if (form.equalsIgnoreCase("NFD")) {
48 fb = Normalizer.D;
49 } else if (form.equalsIgnoreCase("NFKC")) {
50 fb = Normalizer.KC;
51 } else if (form.equalsIgnoreCase("NFKD")) {
52 fb = Normalizer.KD;
53 } else if (form.isEmpty()) {
54 return sv;
55 } else {
56 String msg = "Normalization form " + form + " is not supported";
57 XPathException err = new XPathException(msg);
58 err.setErrorCode("FOCH0003");
59 err.setXPathContext(c);
60 throw err;
61 }
62
63 // fast path for ASCII strings: normalization is a no-op
64 boolean allASCII = true;
65 CharSequence chars = sv.getStringValueCS();
66 if (chars instanceof CompressedWhitespace) {
67 return sv;
68 }
69 for (int i = chars.length() - 1; i >= 0; i--) {
70 if (chars.charAt(i) > 127) {
71 allASCII = false;
72 break;
73 }
74 }
75 if (allASCII) {
76 return sv;
77 }
78
79 Normalizer norm = new Normalizer(fb, c.getConfiguration());
80 CharSequence result = norm.normalize(sv.getStringValueCS());
81 return StringValue.makeStringValue(result);
82 }
83
84 }
85