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.

ServerEntryReader.java 3.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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.ServerManager;
  24. import com.dmdirc.addons.serverlists.ServerEntry;
  25. import com.dmdirc.addons.serverlists.ServerGroup;
  26. import com.dmdirc.interfaces.config.ConfigProvider;
  27. import com.dmdirc.interfaces.config.IdentityController;
  28. import java.net.URI;
  29. import java.net.URISyntaxException;
  30. /**
  31. * Facilitates loading of a {@link com.dmdirc.addons.serverlists.ServerEntry} from a DMDirc
  32. * {@link com.dmdirc.interfaces.config.ConfigProvider}.
  33. *
  34. * @since 0.6.4
  35. */
  36. public class ServerEntryReader {
  37. /** ServerManager that ServerEntrys use to create servers */
  38. private final ServerManager serverManager;
  39. /** The controller to read/write settings with. */
  40. private final IdentityController identityController;
  41. /** The identity to read entries from. */
  42. private final ConfigProvider identity;
  43. public ServerEntryReader(final ServerManager serverManager,
  44. final IdentityController identityController, final ConfigProvider identity) {
  45. this.serverManager = serverManager;
  46. this.identityController = identityController;
  47. this.identity = identity;
  48. }
  49. /**
  50. * Attempts to read the details of the specified server from this reader's identity.
  51. *
  52. * @param group The group that owns this server
  53. * @param name The name of the server to be read
  54. *
  55. * @return A corresponding ServerEntry
  56. *
  57. * @throws URISyntaxException If the server doesn't specify a valid URI
  58. * @throws IllegalArgumentException If the server doesn't define a name or address
  59. */
  60. public ServerEntry read(final ServerGroup group, final String name) throws URISyntaxException,
  61. IllegalArgumentException {
  62. if (!identity.hasOptionString(name, "name")
  63. || !identity.hasOptionString(name, "address")) {
  64. throw new IllegalArgumentException("Server does not specify name or address: "
  65. + name);
  66. }
  67. final String serverName = identity.getOption(name, "name");
  68. final URI serverURI = new URI(identity.getOption(name, "address"));
  69. return new ServerEntry(identityController, serverManager, group, serverName, serverURI, null);
  70. }
  71. }