Commits
md_5 authored 006049902d4
1 - | package org.bukkit.configuration.file; |
2 - | |
3 - | import java.io.IOException; |
4 - | import java.io.Writer; |
5 - | import java.lang.reflect.Field; |
6 - | import java.util.ArrayDeque; |
7 - | import java.util.Queue; |
8 - | import org.jetbrains.annotations.NotNull; |
9 - | import org.yaml.snakeyaml.DumperOptions; |
10 - | import org.yaml.snakeyaml.LoaderOptions; |
11 - | import org.yaml.snakeyaml.Yaml; |
12 - | import org.yaml.snakeyaml.comments.CommentEventsCollector; |
13 - | import org.yaml.snakeyaml.comments.CommentType; |
14 - | import org.yaml.snakeyaml.constructor.BaseConstructor; |
15 - | import org.yaml.snakeyaml.emitter.Emitter; |
16 - | import org.yaml.snakeyaml.error.YAMLException; |
17 - | import org.yaml.snakeyaml.events.Event; |
18 - | import org.yaml.snakeyaml.nodes.Node; |
19 - | import org.yaml.snakeyaml.representer.Representer; |
20 - | import org.yaml.snakeyaml.serializer.Serializer; |
21 - | |
22 - | final class BukkitYaml extends Yaml { |
23 - | |
24 - | private static final Field events; |
25 - | private static final Field blockCommentsCollector; |
26 - | private static final Field inlineCommentsCollector; |
27 - | |
28 - | private static Field getEmitterField(String name) { |
29 - | Field field = null; |
30 - | try { |
31 - | field = Emitter.class.getDeclaredField(name); |
32 - | field.setAccessible(true); |
33 - | } catch (ReflectiveOperationException ex) { |
34 - | // Ignore as a fail-safe fallback |
35 - | } |
36 - | return field; |
37 - | } |
38 - | |
39 - | static { |
40 - | events = getEmitterField("events"); |
41 - | blockCommentsCollector = getEmitterField("blockCommentsCollector"); |
42 - | inlineCommentsCollector = getEmitterField("inlineCommentsCollector"); |
43 - | } |
44 - | |
45 - | public BukkitYaml( BaseConstructor constructor, Representer representer, DumperOptions dumperOptions, LoaderOptions loadingConfig) { |
46 - | super(constructor, representer, dumperOptions, loadingConfig); |
47 - | } |
48 - | |
49 - | |
50 - | public void serialize( Node node, Writer output) { |
51 - | Emitter emitter = new Emitter(output, dumperOptions); |
52 - | if (events != null && blockCommentsCollector != null && inlineCommentsCollector != null) { |
53 - | Queue<Event> newEvents = new ArrayDeque<>(100); |
54 - | |
55 - | try { |
56 - | events.set(emitter, newEvents); |
57 - | blockCommentsCollector.set(emitter, new CommentEventsCollector(newEvents, CommentType.BLANK_LINE, CommentType.BLOCK)); |
58 - | inlineCommentsCollector.set(emitter, new CommentEventsCollector(newEvents, CommentType.IN_LINE)); |
59 - | } catch (ReflectiveOperationException ex) { |
60 - | // Don't ignore this as we could be in an inconsistent state |
61 - | throw new RuntimeException("Could not update Yaml event queue", ex); |
62 - | } |
63 - | } |
64 - | |
65 - | Serializer serializer = new Serializer(emitter, resolver, dumperOptions, null); |
66 - | try { |
67 - | serializer.open(); |
68 - | serializer.serialize(node); |
69 - | serializer.close(); |
70 - | } catch (IOException ex) { |
71 - | throw new YAMLException(ex); |
72 - | } |
73 - | } |
74 - | } |