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.

ServerGroupReader.java 5.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /*
  2. * Copyright (c) 2006-2014 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.addons.serverlists.io;
  23. import com.dmdirc.addons.serverlists.ServerGroup;
  24. import com.dmdirc.interfaces.ConnectionManager;
  25. import com.dmdirc.interfaces.config.ConfigProvider;
  26. import com.dmdirc.interfaces.config.IdentityController;
  27. import java.net.URI;
  28. import java.net.URISyntaxException;
  29. import java.util.Map;
  30. /**
  31. * Facilitates loading of a {@link ServerGroup} from a DMDirc {@link ConfigProvider}.
  32. *
  33. * @since 0.6.4
  34. */
  35. public class ServerGroupReader {
  36. /** The config provider this reader should read from. */
  37. private final ConfigProvider identity;
  38. /** The reader we'll use for individual servers. */
  39. private final ServerEntryReader entryReader;
  40. /** The controller to read/write settings with. */
  41. private final IdentityController identityController;
  42. /**
  43. * Creates a new ServerGroupReader that will read from the specified identity.
  44. *
  45. * @param connectionManager The server manager to use to connect to servers.
  46. * @param identityController The identity controller to use.
  47. * @param identity The identity describing the server group
  48. */
  49. public ServerGroupReader(
  50. final ConnectionManager connectionManager,
  51. final IdentityController identityController,
  52. final ConfigProvider identity) {
  53. this.identity = identity;
  54. this.identityController = identityController;
  55. this.entryReader = new ServerEntryReader(connectionManager, identityController, identity);
  56. }
  57. /**
  58. * Reads the default server group from this reader's identity.
  59. *
  60. * @see #read(ServerGroup, java.lang.String)
  61. * @return A ServerGroup corresponding to the identity's default group
  62. *
  63. * @throws IllegalArgumentException If the identity doesn't define a group
  64. */
  65. public ServerGroup read() {
  66. if (identity.hasOptionString("identity", "name")) {
  67. return read(null, identity.getOption("identity", "name"));
  68. }
  69. throw new IllegalArgumentException("Identity has no name");
  70. }
  71. /**
  72. * Reads a named server group from this reader's identity.
  73. *
  74. * @param parent The parent of the group being read
  75. * @param name The name of the server group to read
  76. *
  77. * @return A corresponding ServerGroup
  78. *
  79. * @throws IllegalArgumentException If the server group doesn't define a name
  80. */
  81. public ServerGroup read(final ServerGroup parent, final String name)
  82. throws IllegalArgumentException {
  83. if (!identity.hasOptionString(name, "name")) {
  84. throw new IllegalArgumentException("ServerGroup '" + name + "' not defined");
  85. }
  86. final ServerGroup group = new ServerGroup(identityController, parent,
  87. identity.getOption(name, "name"));
  88. if (identity.hasOptionString(name, "description")) {
  89. group.setDescription(identity.getOption(name, "description"));
  90. }
  91. if (identity.hasOptionString(name, "links")) {
  92. readLinks(group, identity.getOption(name, "links"));
  93. }
  94. for (String item : identity.getOptionList(name, "contents", true)) {
  95. if (item.endsWith(" servergroup")) {
  96. try {
  97. group.addItem(read(group, item));
  98. } catch (IllegalArgumentException ex) {
  99. // TODO: Raise an error about malformed group
  100. }
  101. } else if (item.endsWith(" server")) {
  102. try {
  103. group.addItem(entryReader.read(group, item));
  104. } catch (URISyntaxException | IllegalArgumentException ex) {
  105. // TODO: Raise an error about malformed server
  106. }
  107. }
  108. }
  109. return group;
  110. }
  111. /**
  112. * Reads a set of links from the named domain and adds them to the specified group.
  113. *
  114. * @param group The group to add links to
  115. * @param domain The domain in the identity containing links
  116. */
  117. private void readLinks(final ServerGroup group, final String domain) {
  118. for (Map.Entry<String, String> entry : identity.getOptions(domain).entrySet()) {
  119. try {
  120. group.addLink(entry.getKey(), new URI(entry.getValue()));
  121. } catch (URISyntaxException ex) {
  122. // TODO: Raise an error about illegal URI?
  123. }
  124. }
  125. }
  126. /**
  127. * Returns a writer which may be used to write updated data.
  128. *
  129. * @return An appropriately configured {@link ServerGroupWriter}
  130. */
  131. public ServerGroupWriter getWriter() {
  132. return new ServerGroupWriter(identity);
  133. }
  134. }