Commits
md_5 authored 9cb65d72a3a
1 1 | package org.bukkit; |
2 2 | |
3 3 | import com.google.common.base.Preconditions; |
4 4 | import java.util.Locale; |
5 5 | import java.util.UUID; |
6 + | import java.util.regex.Pattern; |
6 7 | import org.bukkit.plugin.Plugin; |
7 8 | |
8 9 | /** |
9 10 | * Represents a String based key which consists of two components - a namespace |
10 11 | * and a key. |
12 + | * |
13 + | * Namespaces may only contain lowercase alphanumeric characters, periods, |
14 + | * underscores, and hyphens. |
15 + | * <p> |
16 + | * Keys may only contain lowercase alphanumeric characters, periods, |
17 + | * underscores, hyphens, and forward slashes. |
18 + | * |
11 19 | */ |
12 20 | public final class NamespacedKey { |
13 21 | |
14 22 | /** |
15 23 | * The namespace representing all inbuilt keys. |
16 24 | */ |
17 25 | public static final String MINECRAFT = "minecraft"; |
18 26 | /** |
19 27 | * The namespace representing all keys generated by Bukkit for backwards |
20 28 | * compatibility measures. |
21 29 | */ |
22 30 | public static final String BUKKIT = "bukkit"; |
23 31 | // |
32 + | private static final Pattern VALID_NAMESPACE = Pattern.compile("[a-z0-9._-]+"); |
33 + | private static final Pattern VALID_KEY = Pattern.compile("[a-z0-9/._-]+"); |
34 + | // |
24 35 | private final String namespace; |
25 36 | private final String key; |
26 37 | |
27 38 | /** |
28 39 | * Create a key in a specific namespace. |
29 40 | * |
30 41 | * @param namespace |
31 42 | * @param key |
32 43 | * @deprecated should never be used by plugins, for internal use only!! |
33 44 | */ |
34 45 | |
35 46 | public NamespacedKey(String namespace, String key) { |
36 - | Preconditions.checkArgument(namespace != null && !namespace.isEmpty(), "namespace"); |
37 - | Preconditions.checkArgument(key != null, "key"); |
47 + | Preconditions.checkArgument(namespace != null && VALID_NAMESPACE.matcher(namespace).matches(), "namespace"); |
48 + | Preconditions.checkArgument(key != null && VALID_KEY.matcher(key).matches(), "key"); |
38 49 | |
39 50 | this.namespace = namespace; |
40 51 | this.key = key; |
41 52 | |
42 53 | String string = toString(); |
43 - | Preconditions.checkArgument(string.indexOf(' ') == -1, "NamespacedKey cannot contain spaces (%s)", string); |
44 54 | Preconditions.checkArgument(string.length() < 256, "NamespacedKey must be less than 256 characters", string); |
45 55 | } |
46 56 | |
47 57 | /** |
48 58 | * Create a key in the plugin's namespace. |
49 59 | * |
50 60 | * @param plugin the plugin to use for the namespace |
51 61 | * @param key the key to create |
52 62 | */ |
53 63 | public NamespacedKey(Plugin plugin, String key) { |
54 64 | Preconditions.checkArgument(plugin != null, "plugin"); |
55 65 | Preconditions.checkArgument(key != null, "key"); |
56 66 | |
57 - | // Plugin names cannot have spaces anymore (SimplePluginManager) |
58 - | Preconditions.checkArgument(key.indexOf(' ') == -1, "key cannot contain spaces (%s)", key); |
59 - | |
60 67 | this.namespace = plugin.getName().toLowerCase(Locale.ROOT); |
61 68 | this.key = key.toLowerCase().toLowerCase(Locale.ROOT); |
62 69 | |
70 + | // Check validity after normalization |
71 + | Preconditions.checkArgument(VALID_NAMESPACE.matcher(namespace).matches(), "namespace"); |
72 + | Preconditions.checkArgument(VALID_KEY.matcher(key).matches(), "key"); |
73 + | |
63 74 | String string = toString(); |
64 75 | Preconditions.checkArgument(string.length() < 256, "NamespacedKey must be less than 256 characters (%s)", string); |
65 76 | } |
66 77 | |
67 78 | public String getNamespace() { |
68 79 | return namespace; |
69 80 | } |
70 81 | |
71 82 | public String getKey() { |
72 83 | return key; |