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.

CategoryReader.java 3.5KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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 com.dmdirc.config.prefs.PreferencesSetting;
  20. import java.util.LinkedList;
  21. import java.util.List;
  22. import java.util.Map;
  23. import static com.dmdirc.util.io.yaml.YamlReaderUtils.optionalString;
  24. import static com.dmdirc.util.io.yaml.YamlReaderUtils.requiredString;
  25. import static com.google.common.base.Preconditions.checkNotNull;
  26. /**
  27. * Reads a single category.
  28. * <p>
  29. * Categories must be specified as a map containing some information about the category itself, and
  30. * a list of items.
  31. * <p>
  32. * Each category must have both a title (specified with the key {@link #TITLE_KEY}) and a list of
  33. * items (specified with {@link #ITEMS_KEY}). It may optionally have a description
  34. * ({@link #DESCRIPTION_KEY}), parent ({@link #PARENT_KEY}), icon ({@link #ICON_KEY}) and a default
  35. * domain for its items ({@link #DOMAIN_KEY}).
  36. * <p>
  37. * A category expressed in YAML will resemble:
  38. * <pre><code>
  39. * - name: FooBar settings
  40. * description: Settings that control how the Foo interacts with Bar
  41. * icon: foo-bar-settings
  42. * items:
  43. * - [item]
  44. * - [item]
  45. * </code></pre>
  46. */
  47. public class CategoryReader {
  48. private static final String TITLE_KEY = "name";
  49. private static final String DESCRIPTION_KEY = "description";
  50. private static final String PARENT_KEY = "parent";
  51. private static final String ICON_KEY = "icon";
  52. private static final String DOMAIN_KEY = "domain";
  53. private static final String ITEMS_KEY = "items"; // NOPMD
  54. private final Map<Object, Object> data;
  55. private final List<PreferencesSetting> items = new LinkedList<>();
  56. private String title;
  57. private String description;
  58. private String icon;
  59. private String parent; // NOPMD
  60. private String domain; // NOPMD
  61. public CategoryReader(final Map<Object, Object> data) {
  62. this.data = checkNotNull(data);
  63. }
  64. public void read() {
  65. title = requiredString(data, TITLE_KEY);
  66. description = optionalString(data, DESCRIPTION_KEY);
  67. icon = optionalString(data, ICON_KEY);
  68. parent = optionalString(data, PARENT_KEY);
  69. domain = optionalString(data, DOMAIN_KEY);
  70. }
  71. public PreferencesCategory getCategory() {
  72. final PreferencesCategory category = new PreferencesCategory(title, description, icon);
  73. items.forEach(category::addSetting);
  74. return category;
  75. }
  76. }