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