|
1 | 1 | package org.w3id.cwl.cwl1_2.utils;
|
2 | 2 |
|
| 3 | +import java.util.ArrayList; |
| 4 | +import java.util.HashMap; |
3 | 5 | import java.util.List;
|
4 | 6 | import java.util.Map;
|
5 |
| -import org.yaml.snakeyaml.Yaml; |
6 |
| -import org.yaml.snakeyaml.constructor.SafeConstructor; |
| 7 | +import java.util.Objects; |
| 8 | +import java.util.regex.Pattern; |
| 9 | + |
| 10 | +import org.snakeyaml.engine.v2.api.Load; |
| 11 | +import org.snakeyaml.engine.v2.api.LoadSettings; |
| 12 | +import org.snakeyaml.engine.v2.nodes.Tag; |
| 13 | +import org.snakeyaml.engine.v2.resolver.ScalarResolver; |
| 14 | + |
| 15 | +// Copied from org.snakeyaml.engine.v2.resolver.ResolverTuple because it was marked non-public |
| 16 | +/** |
| 17 | + * Copyright (c) 2018, http://www.snakeyaml.org |
| 18 | + * <p> |
| 19 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 20 | + * you may not use this file except in compliance with the License. |
| 21 | + * You may obtain a copy of the License at |
| 22 | + * <p> |
| 23 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 24 | + * <p> |
| 25 | + * Unless required by applicable law or agreed to in writing, software |
| 26 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 27 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 28 | + * See the License for the specific language governing permissions and |
| 29 | + * limitations under the License. |
| 30 | + */ |
| 31 | +final class ResolverTuple { |
| 32 | + private final Tag tag; |
| 33 | + private final Pattern regexp; |
| 34 | + |
| 35 | + public ResolverTuple(Tag tag, Pattern regexp) { |
| 36 | + Objects.requireNonNull(tag, "Tag must be provided"); |
| 37 | + Objects.requireNonNull(regexp, "regexp must be provided"); |
| 38 | + this.tag = tag; |
| 39 | + this.regexp = regexp; |
| 40 | + } |
| 41 | + |
| 42 | + public Tag getTag() { |
| 43 | + return tag; |
| 44 | + } |
| 45 | + |
| 46 | + public Pattern getRegexp() { |
| 47 | + return regexp; |
| 48 | + } |
| 49 | + |
| 50 | + @Override |
| 51 | + public String toString() { |
| 52 | + return "Tuple tag=" + tag + " regexp=" + regexp; |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | + |
| 57 | +// Adapted from org.snakeyaml.engine.v2.resolver.JsonScalarResolver |
| 58 | +// Not guaranteed to be complete coverage of the YAML 1.2 Core Schema |
| 59 | +// 2021-02-03 Supports 'True'/'False'/'TRUE','FALSE' as boolean; 'Null', 'NULL', an '~' as null |
| 60 | +/** |
| 61 | + * Copyright (c) 2018, http://www.snakeyaml.org |
| 62 | + * <p> |
| 63 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 64 | + * you may not use this file except in compliance with the License. |
| 65 | + * You may obtain a copy of the License at |
| 66 | + * <p> |
| 67 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 68 | + * <p> |
| 69 | + * Unless required by applicable law or agreed to in writing, software |
| 70 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 71 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 72 | + * See the License for the specific language governing permissions and |
| 73 | + * limitations under the License. |
| 74 | + */ |
| 75 | +class CoreScalarResolver implements ScalarResolver { |
| 76 | + public final Pattern BOOL = Pattern.compile("^(?:true|false|True|False|TRUE|FALSE)$"); |
| 77 | + public static final Pattern FLOAT = Pattern |
| 78 | + .compile("^(-?(0?\\.[0-9]+|[1-9][0-9]*(\\.[0-9]*)?)(e[-+]?[0-9]+)?)|-?\\.(?:inf)|\\.(?:nan)$"); // NOSONAR |
| 79 | + public static final Pattern INT = Pattern.compile("^(?:-?(?:0|[1-9][0-9]*))$"); |
| 80 | + public static final Pattern NULL = Pattern.compile("^(?:null|Null|NULL|~)$"); |
| 81 | + public static final Pattern EMPTY = Pattern.compile("^$"); |
| 82 | + |
| 83 | + public static final Pattern ENV_FORMAT = Pattern |
| 84 | + .compile("^\\$\\{\\s*((?<name>\\w+)((?<separator>:?(-|\\?))(?<value>\\w+)?)?)\\s*\\}$"); |
| 85 | + |
| 86 | + protected Map<Character, List<ResolverTuple>> yamlImplicitResolvers = new HashMap<Character, List<ResolverTuple>>(); |
| 87 | + |
| 88 | + public void addImplicitResolver(Tag tag, Pattern regexp, String first) { |
| 89 | + if (first == null) { |
| 90 | + List<ResolverTuple> curr = yamlImplicitResolvers.computeIfAbsent(null, c -> new ArrayList<ResolverTuple>()); |
| 91 | + curr.add(new ResolverTuple(tag, regexp)); |
| 92 | + } else { |
| 93 | + char[] chrs = first.toCharArray(); |
| 94 | + for (int i = 0, j = chrs.length; i < j; i++) { |
| 95 | + Character theC = Character.valueOf(chrs[i]); |
| 96 | + if (theC == 0) { |
| 97 | + // special case: for null |
| 98 | + theC = null; |
| 99 | + } |
| 100 | + List<ResolverTuple> curr = yamlImplicitResolvers.get(theC); |
| 101 | + if (curr == null) { |
| 102 | + curr = new ArrayList<ResolverTuple>(); |
| 103 | + yamlImplicitResolvers.put(theC, curr); |
| 104 | + } |
| 105 | + curr.add(new ResolverTuple(tag, regexp)); |
| 106 | + } |
| 107 | + } |
| 108 | + } |
| 109 | + |
| 110 | + protected void addImplicitResolvers() { |
| 111 | + addImplicitResolver(Tag.NULL, EMPTY, null); |
| 112 | + addImplicitResolver(Tag.BOOL, BOOL, "tfTF"); |
| 113 | + /* |
| 114 | + * INT must be before FLOAT because the regular expression for FLOAT matches INT |
| 115 | + * (see issue 130) http://code.google.com/p/snakeyaml/issues/detail?id=130 |
| 116 | + */ |
| 117 | + addImplicitResolver(Tag.INT, INT, "-0123456789"); |
| 118 | + addImplicitResolver(Tag.FLOAT, FLOAT, "-0123456789."); |
| 119 | + addImplicitResolver(Tag.NULL, NULL, "nN~\u0000"); |
| 120 | + addImplicitResolver(Tag.ENV_TAG, ENV_FORMAT, "$"); |
| 121 | + } |
| 122 | + |
| 123 | + public CoreScalarResolver() { |
| 124 | + addImplicitResolvers(); |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public Tag resolve(String value, Boolean implicit) { |
| 129 | + if (!implicit) { |
| 130 | + return Tag.STR; |
| 131 | + } |
| 132 | + final List<ResolverTuple> resolvers; |
| 133 | + if (value.length() == 0) { |
| 134 | + resolvers = yamlImplicitResolvers.get('\0'); |
| 135 | + } else { |
| 136 | + resolvers = yamlImplicitResolvers.get(value.charAt(0)); |
| 137 | + } |
| 138 | + if (resolvers != null) { |
| 139 | + for (ResolverTuple v : resolvers) { |
| 140 | + Tag tag = v.getTag(); |
| 141 | + Pattern regexp = v.getRegexp(); |
| 142 | + if (regexp.matcher(value).matches()) { |
| 143 | + return tag; |
| 144 | + } |
| 145 | + } |
| 146 | + } |
| 147 | + if (yamlImplicitResolvers.containsKey(null)) { |
| 148 | + for (ResolverTuple v : yamlImplicitResolvers.get(null)) { |
| 149 | + Tag tag = v.getTag(); |
| 150 | + Pattern regexp = v.getRegexp(); |
| 151 | + if (regexp.matcher(value).matches()) { |
| 152 | + return tag; |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + return Tag.STR; |
| 157 | + } |
| 158 | +} |
7 | 159 |
|
8 | 160 | public class YamlUtils {
|
9 | 161 |
|
10 |
| - public static Map<String, Object> mapFromString(final String text) { |
11 |
| - Yaml yaml = new Yaml(new SafeConstructor()); |
12 |
| - final Map<String, Object> result = yaml.load(text); |
13 |
| - return result; |
14 |
| - } |
15 |
| - |
16 |
| - public static List<Object> listFromString(final String text) { |
17 |
| - Yaml yaml = new Yaml(new SafeConstructor()); |
18 |
| - final List<Object> result = yaml.load(text); |
19 |
| - return result; |
20 |
| - } |
| 162 | + public static Map<String, Object> mapFromString(final String text) { |
| 163 | + LoadSettings settings = LoadSettings.builder().setScalarResolver(new CoreScalarResolver()).build(); |
| 164 | + Load load = new Load(settings); |
| 165 | + final Map<String, Object> result = (Map<String, Object>) load.loadFromString(text); |
| 166 | + return result; |
| 167 | + } |
| 168 | + |
| 169 | + public static List<Object> listFromString(final String text) { |
| 170 | + LoadSettings settings = LoadSettings.builder().setScalarResolver(new CoreScalarResolver()).build(); |
| 171 | + Load load = new Load(settings); |
| 172 | + final List<Object> result = (List<Object>) load.loadFromString(text); |
| 173 | + return result; |
| 174 | + } |
21 | 175 | }
|
0 commit comments