1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
package org.yaml.snakeyaml.resolver; |
17 | |
|
18 | |
import java.util.ArrayList; |
19 | |
import java.util.HashMap; |
20 | |
import java.util.List; |
21 | |
import java.util.Map; |
22 | |
import java.util.regex.Pattern; |
23 | |
|
24 | |
import org.yaml.snakeyaml.nodes.NodeId; |
25 | |
import org.yaml.snakeyaml.nodes.Tag; |
26 | |
|
27 | |
|
28 | |
|
29 | |
|
30 | |
|
31 | |
public class Resolver { |
32 | 1 | public static final Pattern BOOL = Pattern |
33 | |
.compile("^(?:yes|Yes|YES|no|No|NO|true|True|TRUE|false|False|FALSE|on|On|ON|off|Off|OFF)$"); |
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | 1 | public static final Pattern FLOAT = Pattern |
40 | |
.compile("^([-+]?(\\.[0-9]+|[0-9_]+(\\.[0-9_]*)?)([eE][-+]?[0-9]+)?|[-+]?[0-9][0-9_]*(?::[0-5]?[0-9])+\\.[0-9_]*|[-+]?\\.(?:inf|Inf|INF)|\\.(?:nan|NaN|NAN))$"); |
41 | 1 | public static final Pattern INT = Pattern |
42 | |
.compile("^(?:[-+]?0b[0-1_]+|[-+]?0[0-7_]+|[-+]?(?:0|[1-9][0-9_]*)|[-+]?0x[0-9a-fA-F_]+|[-+]?[1-9][0-9_]*(?::[0-5]?[0-9])+)$"); |
43 | 1 | public static final Pattern MERGE = Pattern.compile("^(?:<<)$"); |
44 | 1 | public static final Pattern NULL = Pattern.compile("^(?:~|null|Null|NULL| )$"); |
45 | 1 | public static final Pattern EMPTY = Pattern.compile("^$"); |
46 | 1 | public static final Pattern TIMESTAMP = Pattern |
47 | |
.compile("^(?:[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]|[0-9][0-9][0-9][0-9]-[0-9][0-9]?-[0-9][0-9]?(?:[Tt]|[ \t]+)[0-9][0-9]?:[0-9][0-9]:[0-9][0-9](?:\\.[0-9]*)?(?:[ \t]*(?:Z|[-+][0-9][0-9]?(?::[0-9][0-9])?))?)$"); |
48 | 1 | public static final Pattern VALUE = Pattern.compile("^(?:=)$"); |
49 | 1 | public static final Pattern YAML = Pattern.compile("^(?:!|&|\\*)$"); |
50 | |
|
51 | 133131 | protected Map<Character, List<ResolverTuple>> yamlImplicitResolvers = new HashMap<Character, List<ResolverTuple>>(); |
52 | |
|
53 | |
|
54 | |
|
55 | |
|
56 | |
|
57 | |
|
58 | |
|
59 | |
|
60 | 133131 | public Resolver(boolean respectDefaultImplicitScalars) { |
61 | 133131 | if (respectDefaultImplicitScalars) { |
62 | 133130 | addImplicitResolvers(); |
63 | |
} |
64 | 133131 | } |
65 | |
|
66 | |
protected void addImplicitResolvers() { |
67 | 133128 | addImplicitResolver(Tag.BOOL, BOOL, "yYnNtTfFoO"); |
68 | |
|
69 | |
|
70 | |
|
71 | |
|
72 | |
|
73 | 133128 | addImplicitResolver(Tag.INT, INT, "-+0123456789"); |
74 | 133128 | addImplicitResolver(Tag.FLOAT, FLOAT, "-+0123456789."); |
75 | 133128 | addImplicitResolver(Tag.MERGE, MERGE, "<"); |
76 | 133128 | addImplicitResolver(Tag.NULL, NULL, "~nN\0"); |
77 | 133128 | addImplicitResolver(Tag.NULL, EMPTY, null); |
78 | 133128 | addImplicitResolver(Tag.TIMESTAMP, TIMESTAMP, "0123456789"); |
79 | 133128 | addImplicitResolver(Tag.VALUE, VALUE, "="); |
80 | |
|
81 | |
|
82 | |
|
83 | |
|
84 | 133128 | addImplicitResolver(Tag.YAML, YAML, "!&*"); |
85 | 133128 | } |
86 | |
|
87 | |
public Resolver() { |
88 | 133130 | this(true); |
89 | 133130 | } |
90 | |
|
91 | |
public void addImplicitResolver(Tag tag, Pattern regexp, String first) { |
92 | 1198174 | if (first == null) { |
93 | 133132 | List<ResolverTuple> curr = yamlImplicitResolvers.get(null); |
94 | 133132 | if (curr == null) { |
95 | 0 | curr = new ArrayList<ResolverTuple>(); |
96 | 0 | yamlImplicitResolvers.put(null, curr); |
97 | |
} |
98 | 133132 | curr.add(new ResolverTuple(tag, regexp)); |
99 | 133132 | } else { |
100 | 1065042 | char[] chrs = first.toCharArray(); |
101 | 8254055 | for (int i = 0, j = chrs.length; i < j; i++) { |
102 | 7189013 | Character theC = Character.valueOf(chrs[i]); |
103 | 7189013 | if (theC == 0) { |
104 | |
|
105 | 133131 | theC = null; |
106 | |
} |
107 | 7189013 | List<ResolverTuple> curr = yamlImplicitResolvers.get(theC); |
108 | 7189013 | if (curr == null) { |
109 | 3993895 | curr = new ArrayList<ResolverTuple>(); |
110 | 3993895 | yamlImplicitResolvers.put(theC, curr); |
111 | |
} |
112 | 7189013 | curr.add(new ResolverTuple(tag, regexp)); |
113 | |
} |
114 | |
} |
115 | 1198174 | } |
116 | |
|
117 | |
public Tag resolve(NodeId kind, String value, boolean implicit) { |
118 | 1905523 | if (kind == NodeId.scalar && implicit) { |
119 | 813013 | List<ResolverTuple> resolvers = null; |
120 | 813013 | if (value.length() == 0) { |
121 | 136 | resolvers = yamlImplicitResolvers.get('\0'); |
122 | |
} else { |
123 | 812877 | resolvers = yamlImplicitResolvers.get(value.charAt(0)); |
124 | |
} |
125 | 813013 | if (resolvers != null) { |
126 | 484961 | for (ResolverTuple v : resolvers) { |
127 | 534765 | Tag tag = v.getTag(); |
128 | 534765 | Pattern regexp = v.getRegexp(); |
129 | 534765 | if (regexp.matcher(value).matches()) { |
130 | 458882 | return tag; |
131 | |
} |
132 | 75883 | } |
133 | |
} |
134 | 354131 | if (yamlImplicitResolvers.containsKey(null)) { |
135 | 354130 | for (ResolverTuple v : yamlImplicitResolvers.get(null)) { |
136 | 708270 | Tag tag = v.getTag(); |
137 | 708270 | Pattern regexp = v.getRegexp(); |
138 | 708270 | if (regexp.matcher(value).matches()) { |
139 | 141 | return tag; |
140 | |
} |
141 | 708129 | } |
142 | |
} |
143 | |
} |
144 | 1 | switch (kind) { |
145 | |
case scalar: |
146 | 1346829 | return Tag.STR; |
147 | |
case sequence: |
148 | 27003 | return Tag.SEQ; |
149 | |
default: |
150 | 72668 | return Tag.MAP; |
151 | |
} |
152 | |
} |
153 | |
} |