|
11 | 11 | package org.springframework.ide.vscode.boot.java.requestmapping; |
12 | 12 |
|
13 | 13 | import java.io.File; |
| 14 | +import java.io.FileInputStream; |
14 | 15 | import java.io.IOException; |
| 16 | +import java.io.InputStreamReader; |
| 17 | +import java.io.Reader; |
15 | 18 | import java.nio.charset.Charset; |
16 | 19 | import java.nio.file.Path; |
| 20 | +import java.util.ArrayList; |
17 | 21 | import java.util.HashMap; |
18 | 22 | import java.util.List; |
19 | 23 | import java.util.Map; |
|
48 | 52 | import org.springframework.ide.vscode.commons.languageserver.java.JavaProjectFinder; |
49 | 53 | import org.springframework.ide.vscode.commons.util.BadLocationException; |
50 | 54 | import org.springframework.ide.vscode.commons.util.text.TextDocument; |
| 55 | +import org.springframework.ide.vscode.commons.yaml.ast.NodeUtil; |
51 | 56 | import org.springframework.ide.vscode.java.properties.antlr.parser.AntlrParser; |
52 | 57 | import org.springframework.ide.vscode.java.properties.parser.ParseResults; |
53 | 58 | import org.springframework.ide.vscode.java.properties.parser.Parser; |
| 59 | +import org.yaml.snakeyaml.LoaderOptions; |
| 60 | +import org.yaml.snakeyaml.Yaml; |
| 61 | +import org.yaml.snakeyaml.constructor.SafeConstructor; |
| 62 | +import org.yaml.snakeyaml.nodes.MappingNode; |
| 63 | +import org.yaml.snakeyaml.nodes.Node; |
| 64 | +import org.yaml.snakeyaml.nodes.NodeTuple; |
| 65 | +import org.yaml.snakeyaml.nodes.ScalarNode; |
| 66 | +import org.yaml.snakeyaml.nodes.SequenceNode; |
54 | 67 |
|
55 | 68 | public class WebConfigCodeLensProvider implements CodeLensProvider { |
56 | 69 |
|
@@ -215,9 +228,63 @@ private Stream<PropertyKeyValue> getPropertiesFromPropertiesFile(File file) { |
215 | 228 | } |
216 | 229 |
|
217 | 230 | private Stream<PropertyKeyValue> getPropertiesFromYamlFile(File file) { |
| 231 | + if (file.isFile()) { |
| 232 | + Yaml yaml = new Yaml(new SafeConstructor(new LoaderOptions())); |
| 233 | + |
| 234 | + try (Reader reader = new InputStreamReader(new FileInputStream(file), "UTF8")) { |
| 235 | + |
| 236 | + List<PropertyKeyValue> result = new ArrayList<>(); |
| 237 | + for (Node node : yaml.composeAll(reader)) { |
| 238 | + flattenProperties("", node, result); |
| 239 | + } |
| 240 | + |
| 241 | + return result.stream(); |
| 242 | + } catch (Exception e ) { |
| 243 | + //ignore failed attempt to read bad file |
| 244 | + } |
| 245 | + } |
218 | 246 | return Stream.empty(); |
| 247 | + } |
| 248 | + |
| 249 | + private void flattenProperties(String prefix, Node node, List<PropertyKeyValue> props) { |
| 250 | + switch (node.getNodeId()) { |
219 | 251 |
|
220 | | - // TODO !!! |
| 252 | + case mapping: |
| 253 | + if (!prefix.isEmpty()) { |
| 254 | + prefix = prefix + "."; |
| 255 | + } |
| 256 | + MappingNode mapping = (MappingNode)node; |
| 257 | + for (NodeTuple tup : mapping.getValue()) { |
| 258 | + String key = NodeUtil.asScalar(tup.getKeyNode()); |
| 259 | + if (key != null) { |
| 260 | + flattenProperties(prefix + key, tup.getValueNode(), props); |
| 261 | + } |
| 262 | + } |
| 263 | + break; |
| 264 | + |
| 265 | + case scalar: |
| 266 | + //End of the line. |
| 267 | + props.add(new PropertyKeyValue(prefix, NodeUtil.asScalar(node))); |
| 268 | + break; |
| 269 | + |
| 270 | + case sequence: |
| 271 | + SequenceNode sequenceNode = (SequenceNode) node; |
| 272 | + List<String> values = sequenceNode.getValue().stream() |
| 273 | + .filter(valueNode -> valueNode instanceof ScalarNode) |
| 274 | + .map(scalarNode -> NodeUtil.asScalar(scalarNode)) |
| 275 | + .toList(); |
| 276 | + |
| 277 | + props.add(new PropertyKeyValue(prefix, String.join(", ", values))); |
| 278 | + |
| 279 | + break; |
| 280 | + |
| 281 | + default: |
| 282 | + if (!prefix.isEmpty()) { |
| 283 | + props.add(new PropertyKeyValue(prefix, "<object>")); |
| 284 | + } |
| 285 | + |
| 286 | + break; |
| 287 | + } |
221 | 288 | } |
222 | 289 |
|
223 | 290 | record PropertyKeyValue (String key, String value) {} |
|
0 commit comments