/*
 * Copyright (c) 2006-2009 Chris Smith, Shane Mc Cormack, Gregory Holmes
 *
 * 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;

import com.dmdirc.logger.ErrorLevel;
import com.dmdirc.logger.Logger;
import com.dmdirc.parser.interfaces.Parser;
import com.dmdirc.parser.irc.IRCParser;
import com.dmdirc.parser.common.MyInfo;
import com.dmdirc.parser.irc.ServerInfo;
import com.dmdirc.plugins.ExportedService;
import com.dmdirc.plugins.NoSuchProviderException;
import com.dmdirc.plugins.PluginManager;
import com.dmdirc.plugins.Service;
import com.dmdirc.plugins.ServiceProvider;
import com.dmdirc.util.IrcAddress;

/**
 * Provides a method to retrieve a parser.
 *
 * @since 0.6
 * @author chris
 */
public class ParserFactory {
    
    /**
     * Retrieves a parser instance.
     * 
     * @param myInfo The client information to use
     * @param address The address of the server to connect to
     * @return An appropriately configured parser
     * @since 0.6.3m2
     */
    public Parser getParser(final MyInfo myInfo, final IrcAddress address) {
        // TODO: Hacky Hack McHack
        final ServerInfo info = new ServerInfo(address.getServer(), address.getPort(6667),
                address.getPassword());
        info.setSSL(address.isSSL());

        if ("irc-test".equals(address.getProtocol())) {
            try {
                return (Parser) Class.forName("com.dmdirc.harness.parser.TestParser")
                        .getConstructor(MyInfo.class, ServerInfo.class)
                        .newInstance(myInfo, info);
            } catch (Exception ex) {
                Logger.userError(ErrorLevel.UNKNOWN, "Unable to create parser", ex);
            }
        }

        try {
            if (address.getProtocol() != null) {
                final Service service = PluginManager.getPluginManager().getService("parser", address.getProtocol());
                final ServiceProvider provider = service.getProviders().get(0);
                provider.activateServices();
                if (provider != null) {
                    final ExportedService exportService = provider.getExportedService("getParser");
                    final Object obj = exportService.execute(myInfo, address);
                    if (obj != null && obj instanceof Parser) {
                        return (Parser)obj;
                    } else {
                        Logger.userError(ErrorLevel.UNKNOWN, "Unable to create parser for: "+address.getProtocol());
                    }
                }
            }
        } catch (NoSuchProviderException nspe) {
            Logger.userError(ErrorLevel.UNKNOWN, "No parser found for: "+address.getProtocol());
        }

        return new IRCParser(myInfo, info);
    }

}

@@ -2731,34 +2731,16 @@
      %7D%0A%0A
-        try %7B%0A    
         
@@ -2768,32 +2768,50 @@
 ol() != null) %7B%0A
+            try %7B%0A
                 
@@ -2904,32 +2904,76 @@
 getProtocol());%0A
+%0A                if (service != null) %7B%0A    
                 
@@ -3036,16 +3036,18 @@
 get(0);%0A
+%0A 
         
@@ -3054,24 +3054,31 @@
         
+   if (
 provider
 .activat
@@ -3073,69 +3073,78 @@
 ider
-.activateServices();%0A                if (provider != null) %7B%0A
+ != null) %7B%0A                        provider.activateServices();%0A%0A    
     
@@ -3255,24 +3255,28 @@
             
+  
+  
 final Object
@@ -3322,16 +3322,20 @@
 dress);%0A
+    
         
@@ -3406,32 +3406,36 @@
                 
+    
 return (Parser)o
@@ -3458,16 +3458,20 @@
         
+ 
+   
 %7D else %7B
@@ -3471,16 +3471,20 @@
  else %7B%0A
+    
         
@@ -3563,25 +3563,27 @@
 arser for: %22
-+
+ + 
 address.getP
@@ -3606,34 +3606,42 @@
                 
+   
+ 
 %7D%0A
+    
                 
@@ -3654,26 +3654,32 @@
         
-%7D%0A
+    %7D%0A  
         
 %7D catch 
@@ -3662,32 +3662,34 @@
     %7D%0A          
+  
 %7D catch (NoSuchP
@@ -3709,24 +3709,28 @@
 ion nspe) %7B%0A
+    
             
@@ -3789,17 +3789,19 @@
 d for: %22
-+
+ + 
 address.
@@ -3808,32 +3808,46 @@
 getProtocol());%0A
+            %7D%0A
         %7D%0A%0A     

Fix issue 2820. Shane code rocks.