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

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