/* * 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.ServerManager; import com.dmdirc.addons.serverlists.ServerGroup; 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 serverManager 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 ServerManager serverManager, final IdentityController identityController, final ConfigProvider identity) { this.identity = identity; this.identityController = identityController; this.entryReader = new ServerEntryReader(serverManager, 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 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); } }