123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151 |
- /*
- * Copyright (c) 2006-2014 DMDirc Developers
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in
- * all copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-
- package com.dmdirc.addons.serverlists.io;
-
- import com.dmdirc.addons.serverlists.ServerGroup;
- import com.dmdirc.interfaces.ConnectionManager;
- import com.dmdirc.interfaces.config.ConfigProvider;
- import com.dmdirc.interfaces.config.IdentityController;
-
- import java.net.URI;
- import java.net.URISyntaxException;
- import java.util.Map;
-
- /**
- * Facilitates loading of a {@link ServerGroup} from a DMDirc {@link ConfigProvider}.
- *
- * @since 0.6.4
- */
- public class ServerGroupReader {
-
- /** The config provider this reader should read from. */
- private final ConfigProvider identity;
- /** The reader we'll use for individual servers. */
- private final ServerEntryReader entryReader;
- /** The controller to read/write settings with. */
- private final IdentityController identityController;
-
- /**
- * Creates a new ServerGroupReader that will read from the specified identity.
- *
- * @param connectionManager The server manager to use to connect to servers.
- * @param identityController The identity controller to use.
- * @param identity The identity describing the server group
- */
- public ServerGroupReader(
- final ConnectionManager connectionManager,
- final IdentityController identityController,
- final ConfigProvider identity) {
- this.identity = identity;
- this.identityController = identityController;
- this.entryReader = new ServerEntryReader(connectionManager, identityController, identity);
- }
-
- /**
- * Reads the default server group from this reader's identity.
- *
- * @see #read(ServerGroup, java.lang.String)
- * @return A ServerGroup corresponding to the identity's default group
- *
- * @throws IllegalArgumentException If the identity doesn't define a group
- */
- public ServerGroup read() {
- if (identity.hasOptionString("identity", "name")) {
- return read(null, identity.getOption("identity", "name"));
- }
-
- throw new IllegalArgumentException("Identity has no name");
- }
-
- /**
- * Reads a named server group from this reader's identity.
- *
- * @param parent The parent of the group being read
- * @param name The name of the server group to read
- *
- * @return A corresponding ServerGroup
- *
- * @throws IllegalArgumentException If the server group doesn't define a name
- */
- public ServerGroup read(final ServerGroup parent, final String name)
- throws IllegalArgumentException {
- if (!identity.hasOptionString(name, "name")) {
- throw new IllegalArgumentException("ServerGroup '" + name + "' not defined");
- }
-
- final ServerGroup group = new ServerGroup(identityController, parent,
- identity.getOption(name, "name"));
-
- if (identity.hasOptionString(name, "description")) {
- group.setDescription(identity.getOption(name, "description"));
- }
-
- if (identity.hasOptionString(name, "links")) {
- readLinks(group, identity.getOption(name, "links"));
- }
-
- for (String item : identity.getOptionList(name, "contents", true)) {
- if (item.endsWith(" servergroup")) {
- try {
- group.addItem(read(group, item));
- } catch (IllegalArgumentException ex) {
- // TODO: Raise an error about malformed group
- }
- } else if (item.endsWith(" server")) {
- try {
- group.addItem(entryReader.read(group, item));
- } catch (URISyntaxException | IllegalArgumentException ex) {
- // TODO: Raise an error about malformed server
- }
- }
- }
-
- return group;
- }
-
- /**
- * Reads a set of links from the named domain and adds them to the specified group.
- *
- * @param group The group to add links to
- * @param domain The domain in the identity containing links
- */
- private void readLinks(final ServerGroup group, final String domain) {
- for (Map.Entry<String, String> entry : identity.getOptions(domain).entrySet()) {
- try {
- group.addLink(entry.getKey(), new URI(entry.getValue()));
- } catch (URISyntaxException ex) {
- // TODO: Raise an error about illegal URI?
- }
- }
- }
-
- /**
- * Returns a writer which may be used to write updated data.
- *
- * @return An appropriately configured {@link ServerGroupWriter}
- */
- public ServerGroupWriter getWriter() {
- return new ServerGroupWriter(identity);
- }
-
- }
|