Debian Codegraph @

net/sf/saxon/lib/StandardURIChecker.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.lib;
9
10 import net.sf.saxon.expr.sort.LRUCache;
11 import net.sf.saxon.functions.IriToUri;
12 import net.sf.saxon.value.Whitespace;
13
14 import java.net.URI;
15 import java.net.URISyntaxException;
16
17 /**
18 * This class checks whether a string is a valid URI. Different checking rules can be chosen by including
19 * a different URIChecker in the {@link ConversionRules} used when the value is checked.
20 */
21 public class StandardURIChecker implements URIChecker {
22
23 private static StandardURIChecker THE_INSTANCE = new StandardURIChecker();
24
25 public static StandardURIChecker getInstance() {
26 return THE_INSTANCE;
27 }
28
29 /**
30 * To prevent repeated validation of commonly used URIs (especially namespaces)
31 * we keep a small cache. This is especially useful in the case of URIs that are
32 * valid only after escaping, as otherwise an exception occurs during the validation process
33 */
34
35 /*@NotNull*/ private static ThreadLocal<LRUCache<CharSequence, Boolean>> caches =
36 new ThreadLocal<LRUCache<CharSequence, Boolean>>();
37
38 /**
39 * Protected constructor to allow subclassing
40 */
41
42 protected StandardURIChecker() {
43 }
44
45 /**
46 * Validate a string to determine whether it is a valid URI
47 *
48 * @param value the string to be checked
49 * @return true if the string is considered to be a valid URI
50 */
51
52 public boolean isValidURI(CharSequence value) {
53 LRUCache<CharSequence, Boolean> cache = caches.get();
54 if (cache == null) {
55 cache = new LRUCache<CharSequence, Boolean>(50);
56 caches.set(cache);
57 }
58
59 if (cache.get(value) != null) {
60 return true;
61 }
62
63 String sv = Whitespace.trim(value);
64
65 // Allow zero-length strings (RFC2396 is ambivalent on this point)
66 if (sv.isEmpty()) {
67 return true;
68 }
69
70 // Allow a string if the java.net.URI class accepts it
71 try {
72 new URI(sv);
73 cache.put(value, Boolean.TRUE);
74 return true;
75 } catch (URISyntaxException e) {
76 // keep trying
77 // Note: it's expensive to throw exceptions on a success path, so we keep a cache.
78 }
79
80 // Allow a string if it can be escaped into a form that java.net.URI accepts
81 sv = IriToUri.iriToUri(sv).toString();
82 try {
83 new URI(sv);
84 cache.put(value, Boolean.TRUE);
85 return true;
86 } catch (URISyntaxException e) {
87 return false;
88 }
89 }
90
91 public static void main(String[] args) {
92 System.err.println(args[0] + " is valid? - " + getInstance().isValidURI(args[0]));
93 }
94 }
95