Debian Codegraph @

net/sf/saxon/functions/IriToUri.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.Item;
12 import net.sf.saxon.om.ZeroOrOne;
13 import net.sf.saxon.trans.XPathException;
14 import net.sf.saxon.tree.util.FastStringBuffer;
15 import net.sf.saxon.value.AtomicValue;
16 import net.sf.saxon.value.StringValue;
17
18 import java.util.Arrays;
19
20 /**
21 * This class supports the functions encode-for-uri() and iri-to-uri()
22 */
23
24 public class IriToUri extends ScalarSystemFunction {
25
26 public static boolean[] allowedASCII = new boolean[128];
27
28 static {
29 Arrays.fill(allowedASCII, 0, 32, false);
30 Arrays.fill(allowedASCII, 33, 127, true);
31 allowedASCII[(int) '"'] = false;
32 allowedASCII[(int) '<'] = false;
33 allowedASCII[(int) '>'] = false;
34 allowedASCII[(int) '\\'] = false;
35 allowedASCII[(int) '^'] = false;
36 allowedASCII[(int) '`'] = false;
37 allowedASCII[(int) '{'] = false;
38 allowedASCII[(int) '|'] = false;
39 allowedASCII[(int) '}'] = false;
40 }
41
42 @Override
43 public AtomicValue evaluate(Item arg, XPathContext context) throws XPathException {
44 final CharSequence s = arg.getStringValueCS();
45 return StringValue.makeStringValue(iriToUri(s));
46 }
47
48 @Override
49 public ZeroOrOne<? extends AtomicValue> resultWhenEmpty() {
50 return ZERO_LENGTH_STRING;
51 }
52
53 /**
54 * Escape special characters in a URI. The characters that are %HH-encoded are
55 * all non-ASCII characters
56 *
57 * @param s the URI to be escaped
58 * @return the %HH-encoded string
59 */
60
61 public static CharSequence iriToUri(CharSequence s) {
62 // NOTE: implements a late spec change which says that characters that are illegal in an IRI,
63 // for example "\", must be %-encoded.
64 if (allAllowedAscii(s)) {
65 // it's worth doing a prescan to avoid the cost of copying in the common all-ASCII case
66 return s;
67 }
68 FastStringBuffer sb = new FastStringBuffer(s.length() + 20);
69 for (int i = 0; i < s.length(); i++) {
70 final char c = s.charAt(i);
71 if (c >= 0x7f || !allowedASCII[(int) c]) {
72 EncodeForUri.escapeChar(c, (i + 1) < s.length() ? s.charAt(i + 1) : ' ', sb);
73 } else {
74 sb.append(c);
75 }
76 }
77 return sb;
78 }
79
80 private static boolean allAllowedAscii(CharSequence s) {
81 for (int i = 0; i < s.length(); i++) {
82 final char c = s.charAt(i);
83 if (c >= 0x7f || !allowedASCII[(int) c]) {
84 return false;
85 }
86 }
87 return true;
88 }
89
90
91 private static final String hex = "0123456789ABCDEF";
92
93 }
94