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.

ParserFactory.java 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Copyright (c) 2006-2010 Chris Smith, Shane Mc Cormack, Gregory Holmes
  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;
  23. import com.dmdirc.logger.ErrorLevel;
  24. import com.dmdirc.logger.Logger;
  25. import com.dmdirc.parser.interfaces.Parser;
  26. import com.dmdirc.parser.irc.IRCParser;
  27. import com.dmdirc.parser.common.MyInfo;
  28. import com.dmdirc.parser.irc.ServerInfo;
  29. import com.dmdirc.plugins.ExportedService;
  30. import com.dmdirc.plugins.NoSuchProviderException;
  31. import com.dmdirc.plugins.PluginManager;
  32. import com.dmdirc.plugins.Service;
  33. import com.dmdirc.plugins.ServiceProvider;
  34. import java.net.URI;
  35. /**
  36. * Provides a method to retrieve a parser.
  37. *
  38. * @since 0.6
  39. * @author chris
  40. */
  41. public class ParserFactory {
  42. /**
  43. * Retrieves a parser instance.
  44. *
  45. * @param myInfo The client information to use
  46. * @param address The address of the server to connect to
  47. * @return An appropriately configured parser
  48. * @since 0.6.4
  49. */
  50. public Parser getParser(final MyInfo myInfo, final URI address) {
  51. // TODO: Hacky Hack McHack
  52. if ("irc-test".equals(address.getScheme())) {
  53. try {
  54. return (Parser) Class.forName("com.dmdirc.harness.parser.TestParser")
  55. .getConstructor(MyInfo.class, ServerInfo.class)
  56. .newInstance(myInfo, address);
  57. } catch (Exception ex) {
  58. Logger.userError(ErrorLevel.UNKNOWN, "Unable to create parser", ex);
  59. }
  60. }
  61. if (address.getScheme() == null || "irc".equals(address.getScheme())
  62. || "ircs".equals(address.getScheme())) {
  63. return new IRCParser(myInfo, address);
  64. } else {
  65. try {
  66. final Service service = PluginManager.getPluginManager()
  67. .getService("parser", address.getScheme());
  68. if (service != null && !service.getProviders().isEmpty()) {
  69. final ServiceProvider provider = service.getProviders().get(0);
  70. if (provider != null) {
  71. provider.activateServices();
  72. final ExportedService exportService = provider
  73. .getExportedService("getParser");
  74. final Object obj = exportService.execute(myInfo, address);
  75. if (obj != null && obj instanceof Parser) {
  76. return (Parser)obj;
  77. } else {
  78. Logger.userError(ErrorLevel.MEDIUM,
  79. "Unable to create parser for: " + address.getScheme());
  80. }
  81. }
  82. }
  83. } catch (NoSuchProviderException nspe) {
  84. Logger.userError(ErrorLevel.MEDIUM,
  85. "No parser found for: " + address.getScheme(), nspe);
  86. }
  87. return null;
  88. }
  89. }
  90. }