You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

PreferencesReader.java 4.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. /*
  2. * Copyright (c) 2006-2017 DMDirc Developers
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
  5. * documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
  6. * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
  7. * permit persons to whom the Software is furnished to do so, subject to the following conditions:
  8. *
  9. * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
  10. * Software.
  11. *
  12. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  13. * WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
  14. * OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
  15. * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  16. */
  17. package com.dmdirc.config.prefs.reader;
  18. import com.dmdirc.config.prefs.PreferencesCategory;
  19. import java.io.IOException;
  20. import java.io.InputStream;
  21. import java.io.InputStreamReader;
  22. import java.util.ArrayList;
  23. import java.util.Collections;
  24. import java.util.List;
  25. import java.util.Map;
  26. import javax.annotation.WillClose;
  27. import org.slf4j.Logger;
  28. import org.slf4j.LoggerFactory;
  29. import com.esotericsoftware.yamlbeans.YamlReader;
  30. import static com.dmdirc.util.io.yaml.YamlReaderUtils.asList;
  31. import static com.dmdirc.util.io.yaml.YamlReaderUtils.asMap;
  32. import static com.google.common.base.Preconditions.checkNotNull;
  33. /**
  34. * Reads preferences information from a meta-data file.
  35. * <p>
  36. * Meta-data files are expected to be valid YAML with UTF-8 encoding, containing a dictionary with a
  37. * single entry with the key {@link #CATEGORIES_KEY}. This key contains a list of preferences
  38. * categories.
  39. * <p>
  40. * The outer level of a preferences meta-data file should look like:
  41. * <pre><code>
  42. * ----
  43. * categories:
  44. * - [category data]
  45. * - [category data]
  46. * </code></pre>
  47. *
  48. * @see CategoryReader
  49. */
  50. public class PreferencesReader {
  51. /** The charset to use when reading files. */
  52. private static final String CHARSET = "UTF-8";
  53. /** The top-level key that contains a list of categories. */
  54. private static final String CATEGORIES_KEY = "categories";
  55. private static final Logger LOG = LoggerFactory.getLogger(PreferencesReader.class);
  56. private final InputStream input;
  57. private final List<PreferencesCategory> categories = new ArrayList<>();
  58. /**
  59. * Creates a new instance of {@link PreferencesReader} that will read from the given stream.
  60. *
  61. * @param input The input stream to read preferences meta-data from. Will be closed once read.
  62. */
  63. public PreferencesReader(@WillClose final InputStream input) {
  64. this.input = checkNotNull(input);
  65. }
  66. /**
  67. * Gets a list of all categories that were defined in the given input.
  68. *
  69. * @return A list of all categories given in the input.
  70. */
  71. public List<PreferencesCategory> getCategories() {
  72. return Collections.unmodifiableList(categories);
  73. }
  74. /**
  75. * Reads the preferences data.
  76. *
  77. * @throws PreferencesReaderException If the stream cannot be read, or if preferences cannot be
  78. * parsed.
  79. */
  80. public void read() throws PreferencesReaderException {
  81. try (final InputStreamReader reader = new InputStreamReader(input, CHARSET)) {
  82. final YamlReader yamlReader = new YamlReader(reader);
  83. final Object top = yamlReader.read();
  84. LOG.debug("Found top-level {} element", top.getClass().getName());
  85. readMap(asMap(top));
  86. } catch (IOException ex) {
  87. throw new PreferencesReaderException("Unable to read input stream", ex);
  88. } catch (IllegalArgumentException ex) {
  89. throw new PreferencesReaderException(ex.getMessage(), ex);
  90. }
  91. }
  92. private void readMap(final Map<Object, Object> map) throws PreferencesReaderException {
  93. if (!map.containsKey(CATEGORIES_KEY)) {
  94. throw new PreferencesReaderException("No categories element found");
  95. }
  96. final Object value = map.get(CATEGORIES_KEY);
  97. LOG.debug("Found categories value: {}", value.getClass().getName());
  98. readCategoriesList(asList(value));
  99. }
  100. private void readCategoriesList(final List<Object> list) {
  101. for (Object category : list) {
  102. LOG.debug("Found category entry: {}", category.getClass().getName());
  103. final CategoryReader reader = new CategoryReader(asMap(category));
  104. reader.read();
  105. categories.add(reader.getCategory());
  106. }
  107. }
  108. }