39 | * Synchronized with the commit on 28-April-2019. 40 | */ 41 | public interface Configuration extends ConfigurationSection { 42 | 43 | /** 44 | * Sets the default value of the given path as provided. 45 | *
46 | * If no source configuration was provided as a default collection, then a 47 | * new memory configuration will be created to hold the given default value. 48 | *
49 | * If value is {@code null}, the value will be removed from the default 50 | * configuration source. 51 | * 52 | * @param path The path of the value to set. 53 | * @param value The value to set the default to. 54 | * @throws IllegalArgumentException Thrown if the path is {@code null}. 55 | */ 56 | @Override 57 | void addDefault(@NotNull final String path, @Nullable final Object value) throws IllegalArgumentException; 58 | 59 | /** 60 | * Sets the default values of the given paths as provided. 61 | *
62 | * If no source configuration was provided as a default collection, then a
63 | * new memory configuration will be created to hold the given default
64 | * values.
65 | *
66 | * @param defs A map of paths to values to add to the defaults.
67 | */
68 | void addDefaults(@NotNull final Map
73 | * If no source configuration was provided as a default collection, then a
74 | * new memory configuration will be created to hold the given default
75 | * values.
76 | *
77 | * This method will not hold a reference to the given configuration, nor
78 | * will it automatically update if the given configuration ever changes. If
79 | * the automatic updates are required, please set the default source with
80 | * {@link Configuration#setDefaults(Configuration)}.
81 | *
82 | * @param defs A configuration holding a map of defaults to copy.
83 | */
84 | void addDefaults(@NotNull final Configuration defs);
85 |
86 | /**
87 | * Sets the source of all default values for this configuration.
88 | *
89 | * If a previous source was set, or previous default values were defined,
90 | * then they will not be copied to the new source.
91 | *
92 | * @param defs New source of default values for this configuration.
93 | */
94 | void setDefaults(@NotNull final Configuration defs);
95 |
96 | /**
97 | * Gets the default configuration for this configuration.
98 | *
99 | * If no configuration source was set, but default values were added, then
100 | * a memory configuration will be returned. If no source was set and no
101 | * defaults were set, then this method will return {@code null}.
102 | *
103 | * @return The configuration source for default values, or {@code null} if
104 | * none exist.
105 | */
106 | @Nullable
107 | Configuration getDefaults();
108 |
109 | /**
110 | * Gets the options for this configuration.
111 | *
112 | * All setters through this method are chainable.
113 | *
114 | * @return The options for this configuration.
115 | */
116 | @NotNull
117 | ConfigurationOptions getOptions();
118 | }
119 |
--------------------------------------------------------------------------------
/src/main/java/org/bspfsystems/yamlconfiguration/configuration/ConfigurationOptions.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of YamlConfiguration.
3 | *
4 | * Implementation of SnakeYAML to be easy to use with files.
5 | *
6 | * Copyright (C) 2010-2014 The Bukkit Project (https://bukkit.org/)
7 | * Copyright (C) 2014-2024 SpigotMC Pty. Ltd. (https://www.spigotmc.org/)
8 | * Copyright (C) 2020-2025 BSPF Systems, LLC (https://bspfsystems.org/)
9 | *
10 | * Many of the files in this project are sourced from the Bukkit API as
11 | * part of The Bukkit Project (https://bukkit.org/), now maintained by
12 | * SpigotMC Pty. Ltd. (https://www.spigotmc.org/). These files can be found
13 | * at https://github.com/Bukkit/Bukkit/ and https://hub.spigotmc.org/stash/,
14 | * respectively.
15 | *
16 | * This program is free software: you can redistribute it and/or modify
17 | * it under the terms of the GNU General Public License as published by
18 | * the Free Software Foundation, either version 3 of the License, or
19 | * (at your option) any later version.
20 | *
21 | * This program is distributed in the hope that it will be useful,
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 | * GNU General Public License for more details.
25 | *
26 | * You should have received a copy of the GNU General Public License
27 | * along with this program. If not, see
38 | * Synchronized with the commit on 13-March-2019.
39 | */
40 | public class ConfigurationOptions {
41 |
42 | public static final char DEFAULT_PATH_SEPARATOR = '.';
43 | public static final boolean DEFAULT_COPY_DEFAULTS = false;
44 |
45 | private final Configuration configuration;
46 |
47 | private char pathSeparator;
48 | private boolean copyDefaults;
49 |
50 | /**
51 | * Constructs a set of configuration options.
52 | *
53 | * @param configuration The configuration to create the options for.
54 | */
55 | protected ConfigurationOptions(@NotNull final Configuration configuration) {
56 | this.configuration = configuration;
57 | this.pathSeparator = DEFAULT_PATH_SEPARATOR;
58 | this.copyDefaults = DEFAULT_COPY_DEFAULTS;
59 | }
60 |
61 | /**
62 | * Gets the configuration that these options controls.
63 | *
64 | * @return The configuration that these options controls.
65 | */
66 | @NotNull
67 | public Configuration getConfiguration() {
68 | return this.configuration;
69 | }
70 |
71 | /**
72 | * Gets the {@code char} that will be used to separate configuration
73 | * sections.
74 | *
75 | * This value does not affect how the configuration is stored, only in how
76 | * you access the data.
77 | *
78 | * The default value is {@code .}.
79 | *
80 | * @return The {@code char} used to separate configuration sections.
81 | */
82 | public final char getPathSeparator() {
83 | return this.pathSeparator;
84 | }
85 |
86 | /**
87 | * Sets the {@code char} that will be used to separate configuration
88 | * sections.
89 | *
90 | * This value does not affect how the configuration is stored, only in how
91 | * you access the data.
92 | *
93 | * The default value is {@code .}.
94 | *
95 | * @param pathSeparator The {@code char} used to separate configuration
96 | * sections.
97 | * @return These options, for chaining.
98 | */
99 | @NotNull
100 | public ConfigurationOptions setPathSeparator(final char pathSeparator) {
101 | this.pathSeparator = pathSeparator;
102 | return this;
103 | }
104 |
105 | /**
106 | * Checks if the configuration should copy values from its default
107 | * configuration directly.
108 | *
109 | * If this is {@code true}, all values in the default configuration will be
110 | * directly copied, making it impossible to distinguish between values that
111 | * were set and values that are provided by default. As a result,
112 | * {@link ConfigurationSection#contains(String)} will always return the same
113 | * value as {@link ConfigurationSection#isSet(String)}.
114 | *
115 | * The default value is {@code false}.
116 | *
117 | * @return {@code true} if the default values should be copied,
118 | * {@code false} otherwise.
119 | */
120 | public final boolean getCopyDefaults() {
121 | return this.copyDefaults;
122 | }
123 |
124 | /**
125 | * Sets if the configuration should copy values from its default
126 | * configuration directly.
127 | *
128 | * If this is {@code true}, all values in the default configuration will be
129 | * directly copied, making it impossible to distinguish between values that
130 | * were set and values that are provided by default. As a result,
131 | * {@link ConfigurationSection#contains(String)} will always return the same
132 | * value as {@link ConfigurationSection#isSet(String)}.
133 | *
134 | * The default value is {@code false}.
135 | *
136 | * @param copyDefaults {@code true} if the default values should be copied,
137 | * {@code false} otherwise.
138 | * @return These options, for chaining.
139 | */
140 | @NotNull
141 | public ConfigurationOptions setCopyDefaults(final boolean copyDefaults) {
142 | this.copyDefaults = copyDefaults;
143 | return this;
144 | }
145 | }
146 |
--------------------------------------------------------------------------------
/src/main/java/org/bspfsystems/yamlconfiguration/configuration/InvalidConfigurationException.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of YamlConfiguration.
3 | *
4 | * Implementation of SnakeYAML to be easy to use with files.
5 | *
6 | * Copyright (C) 2010-2014 The Bukkit Project (https://bukkit.org/)
7 | * Copyright (C) 2014-2024 SpigotMC Pty. Ltd. (https://www.spigotmc.org/)
8 | * Copyright (C) 2020-2025 BSPF Systems, LLC (https://bspfsystems.org/)
9 | *
10 | * Many of the files in this project are sourced from the Bukkit API as
11 | * part of The Bukkit Project (https://bukkit.org/), now maintained by
12 | * SpigotMC Pty. Ltd. (https://www.spigotmc.org/). These files can be found
13 | * at https://github.com/Bukkit/Bukkit/ and https://hub.spigotmc.org/stash/,
14 | * respectively.
15 | *
16 | * This program is free software: you can redistribute it and/or modify
17 | * it under the terms of the GNU General Public License as published by
18 | * the Free Software Foundation, either version 3 of the License, or
19 | * (at your option) any later version.
20 | *
21 | * This program is distributed in the hope that it will be useful,
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 | * GNU General Public License for more details.
25 | *
26 | * You should have received a copy of the GNU General Public License
27 | * along with this program. If not, see
38 | * Synchronized with the commit on 15-December-2013.
39 | */
40 | public final class InvalidConfigurationException extends Exception {
41 |
42 | @Serial
43 | private static final long serialVersionUID = 685592388091335686L;
44 |
45 | /**
46 | * Constructs an exception without a message or cause.
47 | *
48 | * @see Exception#Exception()
49 | */
50 | public InvalidConfigurationException() {
51 | super();
52 | }
53 |
54 | /**
55 | * Constructs an exception with a message.
56 | *
57 | * @param message The details of the exception.
58 | * @see Exception#Exception(String)
59 | */
60 | public InvalidConfigurationException(@NotNull final String message) {
61 | super(message);
62 | }
63 |
64 | /**
65 | * Constructs an exception with an upstream cause.
66 | *
67 | * @param cause The cause of the new exception.
68 | * @see Exception#Exception(Throwable)
69 | */
70 | public InvalidConfigurationException(@NotNull final Throwable cause) {
71 | super(cause);
72 | }
73 |
74 | /**
75 | * Constructs an exception with a message and upstream cause.
76 | *
77 | * @param message The details of the exception.
78 | * @param cause The cause of the new exception.
79 | * @see Exception#Exception(String, Throwable)
80 | */
81 | public InvalidConfigurationException(@NotNull final String message, @NotNull final Throwable cause) {
82 | super(message, cause);
83 | }
84 | }
85 |
--------------------------------------------------------------------------------
/src/main/java/org/bspfsystems/yamlconfiguration/configuration/MemoryConfiguration.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of YamlConfiguration.
3 | *
4 | * Implementation of SnakeYAML to be easy to use with files.
5 | *
6 | * Copyright (C) 2010-2014 The Bukkit Project (https://bukkit.org/)
7 | * Copyright (C) 2014-2024 SpigotMC Pty. Ltd. (https://www.spigotmc.org/)
8 | * Copyright (C) 2020-2025 BSPF Systems, LLC (https://bspfsystems.org/)
9 | *
10 | * Many of the files in this project are sourced from the Bukkit API as
11 | * part of The Bukkit Project (https://bukkit.org/), now maintained by
12 | * SpigotMC Pty. Ltd. (https://www.spigotmc.org/). These files can be found
13 | * at https://github.com/Bukkit/Bukkit/ and https://hub.spigotmc.org/stash/,
14 | * respectively.
15 | *
16 | * This program is free software: you can redistribute it and/or modify
17 | * it under the terms of the GNU General Public License as published by
18 | * the Free Software Foundation, either version 3 of the License, or
19 | * (at your option) any later version.
20 | *
21 | * This program is distributed in the hope that it will be useful,
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 | * GNU General Public License for more details.
25 | *
26 | * You should have received a copy of the GNU General Public License
27 | * along with this program. If not, see
41 | * Synchronized with the commit on 07-June-2022.
42 | */
43 | public class MemoryConfiguration extends MemorySection implements Configuration {
44 |
45 | protected Configuration defs;
46 | protected MemoryConfigurationOptions options;
47 |
48 | /**
49 | * Constructs an empty memory configuration with no default values.
50 | *
51 | * @see MemorySection#MemorySection()
52 | */
53 | public MemoryConfiguration() {
54 | super();
55 | }
56 |
57 | /**
58 | * Constructs an empty memory configuration using the given configuration as
59 | * a source for all default values.
60 | *
61 | * @param defs The default value provider configuration.
62 | * @see MemorySection#MemorySection()
63 | */
64 | public MemoryConfiguration(@Nullable final Configuration defs) {
65 | super();
66 | this.defs = defs;
67 | }
68 |
69 | /**
70 | * {@inheritDoc}
71 | */
72 | @Override
73 | public final void addDefault(@NotNull final String path, @Nullable final Object value) {
74 | if (this.defs == null) {
75 | this.defs = new MemoryConfiguration();
76 | }
77 | this.defs.set(path, value);
78 | }
79 |
80 | /**
81 | * {@inheritDoc}
82 | */
83 | @Override
84 | public final void addDefaults(@NotNull final Map
38 | * Synchronized with the commit on 13-March-2019.
39 | */
40 | public class MemoryConfigurationOptions extends ConfigurationOptions {
41 |
42 | /**
43 | * Constructs a set of memory configuration options.
44 | *
45 | * @param configuration The memory configuration to create the options for.
46 | * @see ConfigurationOptions#ConfigurationOptions(Configuration)
47 | */
48 | protected MemoryConfigurationOptions(@NotNull final MemoryConfiguration configuration) {
49 | super(configuration);
50 | }
51 |
52 | /**
53 | * {@inheritDoc}
54 | */
55 | @Override
56 | @NotNull
57 | public MemoryConfiguration getConfiguration() {
58 | return (MemoryConfiguration) super.getConfiguration();
59 | }
60 |
61 | /**
62 | * {@inheritDoc}
63 | */
64 | @Override
65 | @NotNull
66 | public MemoryConfigurationOptions setPathSeparator(final char pathSeparator) {
67 | super.setPathSeparator(pathSeparator);
68 | return this;
69 | }
70 |
71 | /**
72 | * {@inheritDoc}
73 | */
74 | @Override
75 | @NotNull
76 | public MemoryConfigurationOptions setCopyDefaults(final boolean copyDefaults) {
77 | super.setCopyDefaults(copyDefaults);
78 | return this;
79 | }
80 | }
81 |
--------------------------------------------------------------------------------
/src/main/java/org/bspfsystems/yamlconfiguration/configuration/SectionPathData.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of YamlConfiguration.
3 | *
4 | * Implementation of SnakeYAML to be easy to use with files.
5 | *
6 | * Copyright (C) 2010-2014 The Bukkit Project (https://bukkit.org/)
7 | * Copyright (C) 2014-2024 SpigotMC Pty. Ltd. (https://www.spigotmc.org/)
8 | * Copyright (C) 2020-2025 BSPF Systems, LLC (https://bspfsystems.org/)
9 | *
10 | * Many of the files in this project are sourced from the Bukkit API as
11 | * part of The Bukkit Project (https://bukkit.org/), now maintained by
12 | * SpigotMC Pty. Ltd. (https://www.spigotmc.org/). These files can be found
13 | * at https://github.com/Bukkit/Bukkit/ and https://hub.spigotmc.org/stash/,
14 | * respectively.
15 | *
16 | * This program is free software: you can redistribute it and/or modify
17 | * it under the terms of the GNU General Public License as published by
18 | * the Free Software Foundation, either version 3 of the License, or
19 | * (at your option) any later version.
20 | *
21 | * This program is distributed in the hope that it will be useful,
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 | * GNU General Public License for more details.
25 | *
26 | * You should have received a copy of the GNU General Public License
27 | * along with this program. If not, see
43 | * Synchronized with the commit on 20-December-2021.
44 | */
45 | final class SectionPathData {
46 |
47 | private Object data;
48 | private List
75 | * This will override any previously-set value, regardless of what it was.
76 | *
77 | * @param data The updated data to store.
78 | */
79 | void setData(@Nullable final Object data) {
80 | this.data = data;
81 | }
82 |
83 | /**
84 | * Gets the comments on this section path data as a list of strings. If no
85 | * comments exist, an empty list will be returned.
86 | *
87 | * For the individual string entries in the list; a {@code null} entry
88 | * represents an empty line, whereas an empty string entry represents an
89 | * empty comment line ({@code #} and nothing else). Each entry in the list
90 | * represents 1 line of comments.
91 | *
92 | * The list cannot be modified. The returned list represents a snapshot of
93 | * the comments at the time the list was returned; any changes to the
94 | * actual comments will not be reflected in this list.
95 | *
96 | * @return The comments for this section path data, where each list entry
97 | * represents 1 line.
98 | */
99 | @NotNull
100 | @UnmodifiableView
101 | List
109 | * For the individual string entries in the list; a {@code null} entry
110 | * represents an empty line, whereas an empty string entry represents an
111 | * empty comment line ({@code #} and nothing else). Each entry in the list
112 | * represents 1 line of comments.
113 | *
114 | * The given list will not be directly saved; instead, a snapshot will be
115 | * taken and used to create an unmodifiable copy internally. Further updates
116 | * to the given list will not result in changes to the comments stored in
117 | * this section path data after this method completes.
118 | *
119 | * Any existing comments will be replaced, regardless of their value(s)
120 | * compared to the new comments.
121 | *
122 | * @param comments The comments to assign to this section path data.
123 | */
124 | void setComments(@Nullable final List
132 | * For the individual string entries in the list; a {@code null} entry
133 | * represents an empty line, whereas an empty string entry represents an
134 | * empty inline comment line ({@code #} and nothing else). Each entry in the
135 | * list represents 1 line of inline comments.
136 | *
137 | * The list cannot be modified. The returned list represents a snapshot of
138 | * the inline comments at the time the list was returned; any changes to the
139 | * actual inline comments will not be reflected in this list.
140 | *
141 | * @return The inline comments for this section path data, where each list
142 | * entry represents 1 line.
143 | */
144 | @NotNull
145 | List
153 | * For the individual string entries in the list; a {@code null} entry
154 | * represents an empty line, whereas an empty string entry represents an
155 | * empty inline comment line ({@code #} and nothing else). Each entry in the
156 | * list represents 1 line of inline comments.
157 | *
158 | * The given list will not be directly saved; instead, a snapshot will be
159 | * taken and used to create an unmodifiable copy internally. Further updates
160 | * to the given list will not result in changes to the inline comments
161 | * stored in this section path data after this method completes.
162 | *
163 | * Any existing inline comments will be replaced, regardless of their
164 | * value(s) compared to the new inline comments.
165 | *
166 | * @param inLineComments The inline comments to assign to this section path
167 | * data.
168 | */
169 | void setInlineComments(@Nullable final List
51 | * Synchronized with the commit on 24-November-2024.
52 | */
53 | public abstract class FileConfiguration extends MemoryConfiguration {
54 |
55 | /**
56 | * Constructs an empty file configuration with no default values.
57 | *
58 | * @see MemoryConfiguration#MemoryConfiguration()
59 | */
60 | protected FileConfiguration() {
61 | super();
62 | }
63 |
64 | /**
65 | * Constructs an empty file configuration using the given configuration as a
66 | * source for all default values.
67 | *
68 | * @param defs The default value provider configuration.
69 | * @see MemoryConfiguration#MemoryConfiguration(Configuration)
70 | */
71 | protected FileConfiguration(@Nullable final Configuration defs) {
72 | super(defs);
73 | }
74 |
75 | /**
76 | * Saves this file configuration to the given file.
77 | *
78 | * If the given file does not exist, it will attempt to be created. If it
79 | * already exists, any contents will be overwritten, regardless of the old
80 | * or new contents. If it cannot be created or overwritten, an I/O exception
81 | * will be thrown.
82 | *
83 | * This method will save using the UTF-8 charset.
84 | *
85 | * @param file The file to save to.
86 | * @throws IOException If this file configuration cannot be written.
87 | * @see FileConfiguration#saveToString()
88 | */
89 | public final void save(@NotNull final File file) throws IOException {
90 |
91 | if (!file.exists()) {
92 | if (!file.createNewFile()) {
93 | throw new IOException("File has not been created at " + file.getPath());
94 | }
95 | }
96 |
97 | final Writer writer = new OutputStreamWriter(Files.newOutputStream(file.toPath()), StandardCharsets.UTF_8);
98 | writer.write(this.saveToString());
99 | writer.close();
100 | }
101 |
102 | /**
103 | * Saves this file configuration to the given file.
104 | *
105 | * If the file at the given path does not exist, it will attempt to be
106 | * created. If it already exists, any contents will be overwritten,
107 | * regardless of the old or new contents. If it cannot be created or
108 | * overwritten, an I/O exception will be thrown.
109 | *
110 | * This method will save using the UTF-8 charset.
111 | *
112 | * @param path The path of the file to save to.
113 | * @throws IOException If this file configuration cannot be written.
114 | * @see FileConfiguration#save(File)
115 | */
116 | public final void save(@NotNull final String path) throws IOException {
117 | this.save(new File(path));
118 | }
119 |
120 | /**
121 | * Converts this file configuration to a string.
122 | *
123 | * @return A string representing this file configuration.
124 | */
125 | @NotNull
126 | public abstract String saveToString();
127 |
128 | /**
129 | * Loads this file configuration from the given reader.
130 | *
131 | * All values contained in-memory in this file configuration will be
132 | * removed, leaving only the file configuration options as well as any
133 | * defaults. The new values will be loaded into memory from the given
134 | * reader.
135 | *
136 | * @param reader The reader used to load this file configuration.
137 | * @throws IOException If the given reader encounters an error.
138 | * @throws InvalidConfigurationException If the data in the reader cannot be
139 | * parsed as a file configuration.
140 | * @see FileConfiguration#loadFromString(String)
141 | */
142 | public final void load(@NotNull final Reader reader) throws IOException, InvalidConfigurationException {
143 |
144 | final BufferedReader bufferedReader = reader instanceof BufferedReader ? (BufferedReader) reader : new BufferedReader(reader);
145 | final StringBuilder builder = new StringBuilder();
146 |
147 | try {
148 | String line;
149 | while ((line = bufferedReader.readLine()) != null) {
150 | builder.append(line).append('\n');
151 | }
152 | } finally {
153 | bufferedReader.close();
154 | }
155 |
156 | this.loadFromString(builder.toString());
157 | }
158 |
159 | /**
160 | * Loads this file configuration from the given file.
161 | *
162 | * All values contained in-memory in this file configuration will be
163 | * removed, leaving only the file configuration options as well as any
164 | * defaults. The new values will be loaded into memory from the given file.
165 | *
166 | * @param file The file used to load this file configuration.
167 | * @throws IOException If the given file cannot be read.
168 | * @throws InvalidConfigurationException If the data in the file cannot be
169 | * parsed as a file configuration.
170 | * @see FileConfiguration#load(Reader)
171 | */
172 | public final void load(@NotNull final File file) throws IOException, InvalidConfigurationException {
173 | this.load(new InputStreamReader(Files.newInputStream(file.toPath()), StandardCharsets.UTF_8));
174 | }
175 |
176 | /**
177 | * Loads this file configuration from the file at the given path.
178 | *
179 | * All values contained in-memory in this file configuration will be
180 | * removed, leaving only the file configuration options as well as any
181 | * defaults. The new values will be loaded into memory from the file at the
182 | * given path.
183 | *
184 | * @param path The path of the file to load from.
185 | * @throws IOException If the file cannot be read.
186 | * @throws InvalidConfigurationException If the data in the file cannot be
187 | * parsed as a file configuration.
188 | * @see FileConfiguration#load(File)
189 | */
190 | public final void load(@NotNull final String path) throws IOException, InvalidConfigurationException {
191 | this.load(new File(path));
192 | }
193 |
194 | /**
195 | * Loads this file configuration from the given string.
196 | *
197 | * All values contained in-memory in this file configuration will be
198 | * removed, leaving only the file configuration options as well as any
199 | * defaults. The new values will be loaded into memory from the given
200 | * string.
201 | *
202 | * @param data The string representation of the file configuration data to
203 | * load.
204 | * @throws InvalidConfigurationException If the given string cannot be
205 | * parsed as a file configuration.
206 | */
207 | public abstract void loadFromString(@NotNull final String data) throws InvalidConfigurationException;
208 |
209 | /**
210 | * {@inheritDoc}
211 | */
212 | @Override
213 | @NotNull
214 | public FileConfigurationOptions getOptions() {
215 | if (this.options == null) {
216 | this.options = new FileConfigurationOptions(this);
217 | }
218 | return (FileConfigurationOptions) this.options;
219 | }
220 | }
221 |
--------------------------------------------------------------------------------
/src/main/java/org/bspfsystems/yamlconfiguration/file/FileConfigurationOptions.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of YamlConfiguration.
3 | *
4 | * Implementation of SnakeYAML to be easy to use with files.
5 | *
6 | * Copyright (C) 2010-2014 The Bukkit Project (https://bukkit.org/)
7 | * Copyright (C) 2014-2024 SpigotMC Pty. Ltd. (https://www.spigotmc.org/)
8 | * Copyright (C) 2020-2025 BSPF Systems, LLC (https://bspfsystems.org/)
9 | *
10 | * Many of the files in this project are sourced from the Bukkit API as
11 | * part of The Bukkit Project (https://bukkit.org/), now maintained by
12 | * SpigotMC Pty. Ltd. (https://www.spigotmc.org/). These files can be found
13 | * at https://github.com/Bukkit/Bukkit/ and https://hub.spigotmc.org/stash/,
14 | * respectively.
15 | *
16 | * This program is free software: you can redistribute it and/or modify
17 | * it under the terms of the GNU General Public License as published by
18 | * the Free Software Foundation, either version 3 of the License, or
19 | * (at your option) any later version.
20 | *
21 | * This program is distributed in the hope that it will be useful,
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 | * GNU General Public License for more details.
25 | *
26 | * You should have received a copy of the GNU General Public License
27 | * along with this program. If not, see
45 | * Synchronized with the commit on 24-November-2024.
46 | */
47 | public class FileConfigurationOptions extends MemoryConfigurationOptions {
48 |
49 | public static final List
104 | * For the individual string entries in the list; a {@code null} entry
105 | * represents an empty line, whereas an empty string entry represents an
106 | * empty header comment line ({@code #} and nothing else). Each entry in the
107 | * list represents 1 line of header comments.
108 | *
109 | * The list cannot be modified. The returned list represents a snapshot of
110 | * the header comments at the time the list was returned; any changes to the
111 | * actual header comments will not be reflected in this list.
112 | *
113 | * @return The header comments for the file configuration controlled by this
114 | * file configuration options.
115 | */
116 | @NotNull
117 | @UnmodifiableView
118 | public final List
126 | * For the individual string entries in the list; a {@code null} entry
127 | * represents an empty line, whereas an empty string entry represents an
128 | * empty header comment line ({@code #} and nothing else). Each entry in the
129 | * list represents 1 line of header comments.
130 | *
131 | * The given list will not be directly saved; instead, a snapshot will be
132 | * taken and used to create an unmodifiable copy internally. Further updates
133 | * to the given list will not result in changes to the inline comments
134 | * stored in these file configuration options after this method completes.
135 | *
136 | * Any existing header comments will be replaced, regardless of their
137 | * value(s) compared to the new header comments.
138 | *
139 | * @param header The header comments to assign to these file configuration
140 | * options.
141 | * @return These file configuration options, for chaining.
142 | */
143 | @NotNull
144 | public FileConfigurationOptions setHeader(@Nullable final List
154 | * For the individual string entries in the list; a {@code null} entry
155 | * represents an empty line, whereas an empty string entry represents an
156 | * empty footer comment line ({@code #} and nothing else). Each entry in the
157 | * list represents 1 line of footer comments.
158 | *
159 | * The list cannot be modified. The returned list represents a snapshot of
160 | * the footer comments at the time the list was returned; any changes to the
161 | * actual footer comments will not be reflected in this list.
162 | *
163 | * @return The footer comments for the file configuration controlled by this
164 | * file configuration options.
165 | */
166 | @NotNull
167 | @UnmodifiableView
168 | public final List
176 | * For the individual string entries in the list; a {@code null} entry
177 | * represents an empty line, whereas an empty string entry represents an
178 | * empty footer comment line ({@code #} and nothing else). Each entry in the
179 | * list represents 1 line of footer comments.
180 | *
181 | * The given list will not be directly saved; instead, a snapshot will be
182 | * taken and used to create an unmodifiable copy internally. Further updates
183 | * to the given list will not result in changes to the inline comments
184 | * stored in these file configuration options after this method completes.
185 | *
186 | * Any existing footer comments will be replaced, regardless of their
187 | * value(s) compared to the new footer comments.
188 | *
189 | * @param footer The footer comments to assign to these file configuration
190 | * options.
191 | * @return These file configuration options, for chaining.
192 | */
193 | @NotNull
194 | public FileConfigurationOptions setFooter(@Nullable final List
203 | * If this returns {@code true}, and if a default file configuration is
204 | * passed to {@link Configuration#setDefaults(Configuration)}, then upon
205 | * saving, the default comments will be parsed from the passed default file
206 | * configuration, instead of the ones provided in here.
207 | *
208 | * If no default is set on the configuration, or the default is not a file
209 | * configuration, or that configuration has no comments, then the comments
210 | * specified in the original configuration will be used.
211 | *
212 | * The default value is {@code true}.
213 | *
214 | * @return {@code true} if the comments are to be parsed, {@code false}
215 | * otherwise.
216 | */
217 | public final boolean getParseComments() {
218 | return this.parseComments;
219 | }
220 |
221 | /**
222 | * Sets whether the comments (header, block, inline, and/or footer) in a
223 | * file configuration should be loaded and saved.
224 | *
225 | * If this is {@code true}, and if a default file configuration is passed to
226 | * {@link Configuration#setDefaults(Configuration)}, then upon saving, the
227 | * default comments will be parsed from the passed default file
228 | * configuration, instead of the ones provided in here.
229 | *
230 | * If no default is set on the configuration, or the default is not a file
231 | * configuration, or that configuration has no comments, then the comments
232 | * specified in the original configuration will be used.
233 | *
234 | * The default value is {@code true}.
235 | *
236 | * @param parseComments {@code true} if the comments are to be parsed,
237 | * {@code false} otherwise.
238 | * @return This file configuration options, for chaining.
239 | */
240 | @NotNull
241 | public FileConfigurationOptions setParseComments(final boolean parseComments) {
242 | this.parseComments = parseComments;
243 | return this;
244 | }
245 | }
246 |
--------------------------------------------------------------------------------
/src/main/java/org/bspfsystems/yamlconfiguration/file/YamlConfiguration.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of YamlConfiguration.
3 | *
4 | * Implementation of SnakeYAML to be easy to use with files.
5 | *
6 | * Copyright (C) 2010-2014 The Bukkit Project (https://bukkit.org/)
7 | * Copyright (C) 2014-2024 SpigotMC Pty. Ltd. (https://www.spigotmc.org/)
8 | * Copyright (C) 2020-2025 BSPF Systems, LLC (https://bspfsystems.org/)
9 | *
10 | * Many of the files in this project are sourced from the Bukkit API as
11 | * part of The Bukkit Project (https://bukkit.org/), now maintained by
12 | * SpigotMC Pty. Ltd. (https://www.spigotmc.org/). These files can be found
13 | * at https://github.com/Bukkit/Bukkit/ and https://hub.spigotmc.org/stash/,
14 | * respectively.
15 | *
16 | * This program is free software: you can redistribute it and/or modify
17 | * it under the terms of the GNU General Public License as published by
18 | * the Free Software Foundation, either version 3 of the License, or
19 | * (at your option) any later version.
20 | *
21 | * This program is distributed in the hope that it will be useful,
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 | * GNU General Public License for more details.
25 | *
26 | * You should have received a copy of the GNU General Public License
27 | * along with this program. If not, see
68 | * Synchronized with the commit on 24-November-2024.
69 | */
70 | public final class YamlConfiguration extends FileConfiguration {
71 |
72 | private final YamlConstructor yamlConstructor;
73 | private final YamlRepresenter yamlRepresenter;
74 | private final DumperOptions dumperOptions;
75 | private final LoaderOptions loaderOptions;
76 | private final Yaml yaml;
77 |
78 | /**
79 | * Constructs an empty YAML configuration with no default values.
80 | *
81 | * @see FileConfiguration#FileConfiguration()
82 | */
83 | public YamlConfiguration() {
84 | super();
85 |
86 | this.dumperOptions = new DumperOptions();
87 | this.dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
88 |
89 | this.loaderOptions = new LoaderOptions();
90 |
91 | this.yamlConstructor = new YamlConstructor(this.loaderOptions);
92 |
93 | this.yamlRepresenter = new YamlRepresenter(this.dumperOptions);
94 | this.yamlRepresenter.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
95 |
96 | this.yaml = new Yaml(this.yamlConstructor, this.yamlRepresenter, this.dumperOptions, this.loaderOptions);
97 | }
98 |
99 | /**
100 | * Constructs an empty YAML configuration using the given configuration as a
101 | * source for all default values.
102 | *
103 | * @param defs The default value provider configuration
104 | * @see FileConfiguration#FileConfiguration(Configuration)
105 | */
106 | public YamlConfiguration(@Nullable final Configuration defs) {
107 | super(defs);
108 |
109 | this.dumperOptions = new DumperOptions();
110 | this.dumperOptions.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
111 |
112 | this.loaderOptions = new LoaderOptions();
113 |
114 | this.yamlConstructor = new YamlConstructor(this.loaderOptions);
115 |
116 | this.yamlRepresenter = new YamlRepresenter(this.dumperOptions);
117 | this.yamlRepresenter.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
118 |
119 | this.yaml = new Yaml(this.yamlConstructor, this.yamlRepresenter, this.dumperOptions, this.loaderOptions);
120 | }
121 |
122 | /**
123 | * {@inheritDoc}
124 | */
125 | @Override
126 | @NotNull
127 | public String saveToString() {
128 |
129 | this.dumperOptions.setIndent(this.getOptions().getIndent());
130 | this.dumperOptions.setWidth(this.getOptions().getWidth());
131 | this.dumperOptions.setProcessComments(this.getOptions().getParseComments());
132 |
133 | final MappingNode mappingNode = this.toNodeTree(this);
134 | mappingNode.setBlockComments(this.getCommentLines(this.saveHeader(this.getOptions().getHeader()), CommentType.BLOCK));
135 | mappingNode.setEndComments(this.getCommentLines(this.getOptions().getFooter(), CommentType.BLOCK));
136 |
137 | final StringWriter writer = new StringWriter();
138 | if (mappingNode.getBlockComments().isEmpty() && mappingNode.getEndComments().isEmpty() && mappingNode.getValue().isEmpty()) {
139 | writer.write("");
140 | } else {
141 | if (mappingNode.getValue().isEmpty()) {
142 | mappingNode.setFlowStyle(DumperOptions.FlowStyle.FLOW);
143 | }
144 | this.yaml.serialize(mappingNode, writer);
145 | }
146 |
147 | return writer.toString();
148 | }
149 |
150 | /**
151 | * Creates a mapping node containing a serialized representation of the data
152 | * in the given configuration section, and then returns the newly-generated
153 | * mapping node.
154 | *
155 | * @param section The configuration section to serialize.
156 | * @return The serialized configuration section as a mapping node.
157 | */
158 | @NotNull
159 | private MappingNode toNodeTree(@NotNull final ConfigurationSection section) {
160 |
161 | final List
412 | * Any errors loading the YAML configuration will be logged and then
413 | * otherwise ignored. If the given input is not a valid YAML configuration,
414 | * an empty YAML configuration will be returned.
415 | *
416 | * This will only load up to the default number of aliases
417 | * ({@link YamlConfigurationOptions#getMaxAliases()}) to prevent a Billion
418 | * Laughs Attack.
419 | *
420 | * The encoding used may follow the system dependent default.
421 | *
422 | * @param file The file to load.
423 | * @return The loaded YAML configuration.
424 | */
425 | @NotNull
426 | public static YamlConfiguration loadConfiguration(@NotNull final File file) {
427 |
428 | final YamlConfiguration config = new YamlConfiguration();
429 | try {
430 | config.load(file);
431 | } catch (IOException | InvalidConfigurationException e) {
432 | LoggerFactory.getLogger(YamlConfiguration.class).error("Cannot load config from file: " + file.getPath(), e);
433 | }
434 |
435 | return config;
436 | }
437 |
438 | /**
439 | * Creates a new YAML configuration, loading from the given reader.
440 | *
441 | * Any errors loading the YAML configuration will be logged and then
442 | * otherwise ignored. If the given input is not a valid YAML configuration,
443 | * an empty YAML configuration will be returned.
444 | *
445 | * This will only load up to the default number of aliases
446 | * ({@link YamlConfigurationOptions#getMaxAliases()}) to prevent a Billion
447 | * Laughs Attack.
448 | *
449 | * The encoding used may follow the system dependent default.
450 | *
451 | * @param reader The reader to load.
452 | * @return The loaded YAML configuration.
453 | */
454 | @NotNull
455 | public static YamlConfiguration loadConfiguration(@NotNull final Reader reader) {
456 |
457 | final YamlConfiguration config = new YamlConfiguration();
458 | try {
459 | config.load(reader);
460 | } catch (IOException | InvalidConfigurationException e) {
461 | LoggerFactory.getLogger(YamlConfiguration.class).error("Cannot load config from reader.", e);
462 | }
463 |
464 | return config;
465 | }
466 | }
467 |
--------------------------------------------------------------------------------
/src/main/java/org/bspfsystems/yamlconfiguration/file/YamlConfigurationOptions.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of YamlConfiguration.
3 | *
4 | * Implementation of SnakeYAML to be easy to use with files.
5 | *
6 | * Copyright (C) 2010-2014 The Bukkit Project (https://bukkit.org/)
7 | * Copyright (C) 2014-2024 SpigotMC Pty. Ltd. (https://www.spigotmc.org/)
8 | * Copyright (C) 2020-2025 BSPF Systems, LLC (https://bspfsystems.org/)
9 | *
10 | * Many of the files in this project are sourced from the Bukkit API as
11 | * part of The Bukkit Project (https://bukkit.org/), now maintained by
12 | * SpigotMC Pty. Ltd. (https://www.spigotmc.org/). These files can be found
13 | * at https://github.com/Bukkit/Bukkit/ and https://hub.spigotmc.org/stash/,
14 | * respectively.
15 | *
16 | * This program is free software: you can redistribute it and/or modify
17 | * it under the terms of the GNU General Public License as published by
18 | * the Free Software Foundation, either version 3 of the License, or
19 | * (at your option) any later version.
20 | *
21 | * This program is distributed in the hope that it will be useful,
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 | * GNU General Public License for more details.
25 | *
26 | * You should have received a copy of the GNU General Public License
27 | * along with this program. If not, see
41 | * Synchronized with the commit on 24-November-2024.
42 | */
43 | public final class YamlConfigurationOptions extends FileConfigurationOptions {
44 |
45 | public static final int DEFAULT_INDENT = 2;
46 | public static final int DEFAULT_WIDTH = 80;
47 | public static final int DEFAULT_MAX_ALIASES = 50;
48 | public static final int DEFAULT_CODE_POINT_LIMIT = 3 * 1024 * 1024; // 3 MB
49 |
50 | private int indent;
51 | private int width;
52 | private int maxAliases;
53 | private int codePointLimit;
54 |
55 | /**
56 | * Constructs a set of YAML configuration options.
57 | *
58 | * @param configuration The YAML configuration to create the options for.
59 | * @see FileConfigurationOptions#FileConfigurationOptions(MemoryConfiguration)
60 | */
61 | YamlConfigurationOptions(@NotNull final YamlConfiguration configuration) {
62 | super(configuration);
63 | this.indent = DEFAULT_INDENT;
64 | this.width = DEFAULT_WIDTH;
65 | this.maxAliases = DEFAULT_MAX_ALIASES;
66 | this.codePointLimit = DEFAULT_CODE_POINT_LIMIT;
67 | }
68 |
69 | /**
70 | * {@inheritDoc}
71 | */
72 | @Override
73 | @NotNull
74 | public YamlConfiguration getConfiguration() {
75 | return (YamlConfiguration) super.getConfiguration();
76 | }
77 |
78 | /**
79 | * {@inheritDoc}
80 | */
81 | @Override
82 | @NotNull
83 | public YamlConfigurationOptions setPathSeparator(final char pathSeparator) {
84 | super.setPathSeparator(pathSeparator);
85 | return this;
86 | }
87 |
88 | /**
89 | * {@inheritDoc}
90 | */
91 | @Override
92 | @NotNull
93 | public YamlConfigurationOptions setCopyDefaults(final boolean copyDefaults) {
94 | super.setCopyDefaults(copyDefaults);
95 | return this;
96 | }
97 |
98 | /**
99 | * {@inheritDoc}
100 | */
101 | @Override
102 | @NotNull
103 | public YamlConfigurationOptions setHeader(@Nullable final List
131 | * The minimum value this may be is {@code 2}, and the maximum is {@code 9}.
132 | *
133 | * The default value is {@code 2}.
134 | *
135 | * @return The number of spaces used to represent an indent.
136 | */
137 | public int getIndent() {
138 | return this.indent;
139 | }
140 |
141 | /**
142 | * Sets the number of spaces used to represent an indent.
143 | *
144 | * The minimum value this may be is {@code 2}, and the maximum is {@code 9}.
145 | *
146 | * The default value is {@code 2}.
147 | *
148 | * @param indent The number of spaces used to represent an indent.
149 | * @return These YAML configuration options, for chaining.
150 | * @throws IllegalArgumentException If the given value is less than
151 | * {@code 2} or greater than {@code 9}.
152 | */
153 | @NotNull
154 | public YamlConfigurationOptions setIndent(final int indent) throws IllegalArgumentException {
155 | if (indent < 2) {
156 | throw new IllegalArgumentException("Indent must be at least 2 characters.");
157 | }
158 | if (indent > 9) {
159 | throw new IllegalArgumentException("Indent cannot be greater than 9 characters.");
160 | }
161 | this.indent = indent;
162 | return this;
163 | }
164 |
165 | /**
166 | * Gets how long a line can be before it gets split.
167 | *
168 | * The minimum value this may be is {@code 8}, and the maximum is
169 | * {@code 1000}.
170 | *
171 | * The default value is {@code 80}.
172 | *
173 | * @return The number of characters a line can be before it gets split.
174 | */
175 | public int getWidth() {
176 | return this.width;
177 | }
178 |
179 | /**
180 | * Sets how long a line can be before it gets split.
181 | *
182 | * The minimum value this may be is {@code 8}, and the maximum is
183 | * {@code 1000}.
184 | *
185 | * The default value is {@code 80}.
186 | *
187 | * @param width The number of characters a line can be before it gets split.
188 | * @return These YAML configuration options, for chaining.
189 | * @throws IllegalArgumentException If the given value is less than
190 | * {@code 8} or greater than {@code 1000}.
191 | */
192 | @NotNull
193 | public YamlConfigurationOptions setWidth(final int width) {
194 | if (width < 8) {
195 | throw new IllegalArgumentException("Width must be at least 8 characters.");
196 | }
197 | if (width > 1000) {
198 | throw new IllegalArgumentException("Width cannot be greater than 1000 characters.");
199 | }
200 | this.width = width;
201 | return this;
202 | }
203 |
204 | /**
205 | * Gets the maximum number of aliases for collections.
206 | *
207 | * The minimum value this may be is {@code 10}, and the maximum is
208 | * {@link Integer#MAX_VALUE}.
209 | *
210 | * The default value is {@code 50}. It is recommended to keep this value as
211 | * low as possible for your use case as to prevent a Denial-of-Service known
212 | * Billion Laughs Attack.
213 | *
214 | * @return The maximum number of aliases for collections.
215 | */
216 | public int getMaxAliases() {
217 | return this.maxAliases;
218 | }
219 |
220 | /**
221 | * Sets the maximum number of aliases for collections.
222 | *
223 | * The minimum value this may be is {@code 10}, and the maximum is
224 | * {@link Integer#MAX_VALUE} (please use this wisely).
225 | *
226 | * A recommended value is {@code 50}. It is recommended to keep this value
227 | * as low as possible for your use case as to prevent a Denial-of-Service
228 | * known
229 | * Billion Laughs Attack.
230 | *
231 | * @param maxAliases The maximum number of aliases for collections.
232 | * @return These YAML configuration options, for chaining.
233 | * @throws IllegalArgumentException If the given value is less than {@code 10}.
234 | */
235 | @NotNull
236 | public YamlConfigurationOptions setMaxAliases(final int maxAliases) throws IllegalArgumentException {
237 | if (maxAliases < 10) {
238 | throw new IllegalArgumentException("Max aliases must be at least 10.");
239 | }
240 | this.maxAliases = maxAliases;
241 | return this;
242 | }
243 |
244 | /**
245 | * Gets the maximum number of code points that can be loaded in at one time.
246 | *
247 | * The minimum is {@code 1kB (1024)}, and the maximum is
248 | * {@link Integer#MAX_VALUE} (please use this wisely).
249 | *
250 | * A recommended value is {@code 3 MB (1024 * 1024 * 3)}.
251 | *
252 | * @return The maximum number of code points.
253 | */
254 | public int getCodePointLimit() {
255 | return this.codePointLimit;
256 | }
257 |
258 | /**
259 | * Sets the maximum number of code points that can be loaded in at one time.
260 | *
261 | * The minimum is {@code 1kB (1024)}, and the maximum is
262 | * {@link Integer#MAX_VALUE} (please use this wisely).
263 | *
264 | * A recommended value is {@code 3 MB (1024 * 1024 * 3)}.
265 | *
266 | * @param codePointLimit The maximum number of code points for loading.
267 | * @return These YAML configuration options, for chaining.
268 | * @throws IllegalArgumentException If the given value is less than
269 | * {@code 1kB (1024)}.
270 | */
271 | @NotNull
272 | public YamlConfigurationOptions setCodePointLimit(final int codePointLimit) throws IllegalArgumentException {
273 | if (codePointLimit < 1024) {
274 | throw new IllegalArgumentException("Code point limit must be at least 1kB (1024).");
275 | }
276 | this.codePointLimit = codePointLimit;
277 | return this;
278 | }
279 | }
280 |
--------------------------------------------------------------------------------
/src/main/java/org/bspfsystems/yamlconfiguration/file/YamlConstructor.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of YamlConfiguration.
3 | *
4 | * Implementation of SnakeYAML to be easy to use with files.
5 | *
6 | * Copyright (C) 2010-2014 The Bukkit Project (https://bukkit.org/)
7 | * Copyright (C) 2014-2024 SpigotMC Pty. Ltd. (https://www.spigotmc.org/)
8 | * Copyright (C) 2020-2025 BSPF Systems, LLC (https://bspfsystems.org/)
9 | *
10 | * Many of the files in this project are sourced from the Bukkit API as
11 | * part of The Bukkit Project (https://bukkit.org/), now maintained by
12 | * SpigotMC Pty. Ltd. (https://www.spigotmc.org/). These files can be found
13 | * at https://github.com/Bukkit/Bukkit/ and https://hub.spigotmc.org/stash/,
14 | * respectively.
15 | *
16 | * This program is free software: you can redistribute it and/or modify
17 | * it under the terms of the GNU General Public License as published by
18 | * the Free Software Foundation, either version 3 of the License, or
19 | * (at your option) any later version.
20 | *
21 | * This program is distributed in the hope that it will be useful,
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 | * GNU General Public License for more details.
25 | *
26 | * You should have received a copy of the GNU General Public License
27 | * along with this program. If not, see
47 | * Synchronized with the commit on 24-November-2024.
48 | */
49 | public final class YamlConstructor extends SafeConstructor {
50 |
51 | /**
52 | * Represents a custom construct YAML map for use with a YAML configuration.
53 | */
54 | private final class ConstructCustomObject extends ConstructYamlMap {
55 |
56 | /**
57 | * Constructs an empty construct custom object.
58 | *
59 | * @see ConstructYamlMap#ConstructYamlMap()
60 | */
61 | private ConstructCustomObject() {
62 | super();
63 | }
64 |
65 | /**
66 | * Converts the given node into an object.
67 | *
68 | * @param node The node to convert.
69 | * @return The object converted from the given node.
70 | */
71 | @Nullable
72 | @Override
73 | public Object construct(@NotNull final Node node) {
74 |
75 | if (node.isTwoStepsConstruction()) {
76 | throw new YAMLException("Unexpected referential mapping structure. Node: " + node);
77 | }
78 |
79 | final Map, ?> raw = (Map, ?>) super.construct(node);
80 | if (raw.containsKey(ConfigurationSerialization.SERIALIZED_TYPE_KEY)) {
81 |
82 | final Map
46 | * Synchronized with the commit on 24-November-2024.
47 | */
48 | public final class YamlRepresenter extends Representer {
49 |
50 | /**
51 | * Represents a represent map that works with configuration sections.
52 | */
53 | private final class RepresentConfigurationSection extends RepresentMap {
54 |
55 | /**
56 | * Constructs a basic represent configuration section.
57 | *
58 | * @see RepresentMap#RepresentMap()
59 | */
60 | private RepresentConfigurationSection() {
61 | super();
62 | }
63 |
64 | /**
65 | * Converts the given object (known to be a configuration section) into
66 | * a node.
67 | *
68 | * @param object The object to represent.
69 | * @return The node converted from the given object.
70 | * @see RepresentMap#representData(Object)
71 | */
72 | @NotNull
73 | @Override
74 | public Node representData(@NotNull final Object object) {
75 | return super.representData(((ConfigurationSection) object).getValues(false));
76 | }
77 | }
78 |
79 | /**
80 | * Represents a represent map that works with configuration serializables.
81 | */
82 | private class RepresentConfigurationSerializable extends RepresentMap {
83 |
84 | /**
85 | * Constructs a basic represent configuration serializable.
86 | *
87 | * @see RepresentMap#RepresentMap()
88 | */
89 | private RepresentConfigurationSerializable() {
90 | super();
91 | }
92 |
93 | /**
94 | * Converts the given object (known to be a configuration serializable)
95 | * into a node.
96 | *
97 | * @param object The object to represent.
98 | * @return The node converted from the given object.
99 | * @see RepresentMap#representData(Object)
100 | */
101 | @NotNull
102 | @Override
103 | public Node representData(@NotNull final Object object) {
104 |
105 | final ConfigurationSerializable serializable = (ConfigurationSerializable) object;
106 | final Map
38 | * These objects MUST implement one of the following, in addition to the
39 | * methods as defined by this interface:
40 | *
50 | * * Synchronized with the commit on 23-April-2019.
51 | *
52 | * @see DelegateDeserialization
53 | * @see SerializableAs
54 | */
55 | public interface ConfigurationSerializable {
56 |
57 | /**
58 | * Creates a map representation of this configuration serializable.
59 | *
60 | * This class must provide a method to restore this class, as defined in the
61 | * configuration serializable interface javadocs.
62 | *
63 | * The map cannot be modified. The map will also only represent a snapshot
64 | * of this configuration serializable when it was taken.
65 | *
66 | * @return A map containing the current state of this configuration
67 | * serializable.
68 | */
69 | @NotNull
70 | Map
45 | * Synchronized with the commit on 19-November-2023.
46 | */
47 | public final class ConfigurationSerialization {
48 |
49 | public static final String SERIALIZED_TYPE_KEY = "==";
50 |
51 | private static final Map
196 | * The class must implement configuration serializable, including the extra
197 | * methods and/or constructor as specified in the javadocs of a
198 | * configuration serializable.
199 | *
200 | * If a new instance could not be made (an example being the class not fully
201 | * implementing the interface), {@code null} will be returned.
202 | *
203 | * @param map The map to deserialize.
204 | * @param clazz The class to deserialize into.
205 | * @return The new instance of the specified class.
206 | */
207 | @Nullable
208 | public static ConfigurationSerializable deserializeObject(@NotNull final Map
217 | * The class must implement configuration serializable, including the extra
218 | * methods and/or constructor as specified in the javadocs of a
219 | * configuration serializable.
220 | *
221 | * If a new instance could not be made (an example being the class not fully
222 | * implementing the interface), {@code null} will be returned.
223 | *
224 | * @param map The map to deserialize.
225 | * @return The new instance of the given data.
226 | */
227 | @Nullable
228 | public static ConfigurationSerializable deserializeObject(@NotNull final Map
42 | * Synchronized with the commit on 23-April-2019.
43 | */
44 | @Retention(RetentionPolicy.RUNTIME)
45 | @Target(ElementType.TYPE)
46 | public @interface DelegateDeserialization {
47 |
48 | /**
49 | * Gets the configuration serializable class that is delegated to.
50 | *
51 | * @return The delegated configuration serializable class.
52 | */
53 | @NotNull
54 | Class extends ConfigurationSerializable> value();
55 | }
56 |
--------------------------------------------------------------------------------
/src/main/java/org/bspfsystems/yamlconfiguration/serialization/SerializableAs.java:
--------------------------------------------------------------------------------
1 | /*
2 | * This file is part of YamlConfiguration.
3 | *
4 | * Implementation of SnakeYAML to be easy to use with files.
5 | *
6 | * Copyright (C) 2010-2014 The Bukkit Project (https://bukkit.org/)
7 | * Copyright (C) 2014-2024 SpigotMC Pty. Ltd. (https://www.spigotmc.org/)
8 | * Copyright (C) 2020-2025 BSPF Systems, LLC (https://bspfsystems.org/)
9 | *
10 | * Many of the files in this project are sourced from the Bukkit API as
11 | * part of The Bukkit Project (https://bukkit.org/), now maintained by
12 | * SpigotMC Pty. Ltd. (https://www.spigotmc.org/). These files can be found
13 | * at https://github.com/Bukkit/Bukkit/ and https://hub.spigotmc.org/stash/,
14 | * respectively.
15 | *
16 | * This program is free software: you can redistribute it and/or modify
17 | * it under the terms of the GNU General Public License as published by
18 | * the Free Software Foundation, either version 3 of the License, or
19 | * (at your option) any later version.
20 | *
21 | * This program is distributed in the hope that it will be useful,
22 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 | * GNU General Public License for more details.
25 | *
26 | * You should have received a copy of the GNU General Public License
27 | * along with this program. If not, see
41 | * If this annotation is no present on a configuration serializable class, the
42 | * class's fully-qualified name ({@link Class#getName()}) will be used.
43 | *
44 | * This value will be stored in the configuration serialization so that it can
45 | * determine what type it is during serialization and deserialization
46 | * operations.
47 | *
48 | * Using this annotation on a class that does not extend or implement a
49 | * configuration serializable will have no effect.
50 | *
51 | * Synchronized with the commit on 23-April-2019.
52 | *
53 | * @see ConfigurationSerialization#registerClass(Class, String)
54 | */
55 | @Retention(RetentionPolicy.RUNTIME)
56 | @Target(ElementType.TYPE)
57 | public @interface SerializableAs {
58 |
59 | /**
60 | * This is the name the configuration serializable class will be known by.
61 | *
62 | * This name MUST be unique. It is recommended to use names such as
63 | * "MyApplicationThing" instead of "Thing".
64 | *
65 | * @return The name to serialize the class as.
66 | */
67 | @NotNull
68 | String value();
69 | }
70 |
--------------------------------------------------------------------------------
41 | *
47 | * In addition to implementing this interface, you must register the class
48 | * with {@link ConfigurationSerialization#registerClass(Class)}.
49 | *