From 4ea765b6898413913c0265923ccbd61c5015dd73 Mon Sep 17 00:00:00 2001 From: David Huynh Date: Sun, 22 Aug 2010 01:46:32 +0000 Subject: [PATCH] Factored out registries of importers and exporters. git-svn-id: http://google-refine.googlecode.com/svn/trunk@1183 7d457c2a-affb-35e4-300a-418c747d4874 --- .../project/CreateProjectCommand.java | 98 +---------------- .../commands/project/ExportRowsCommand.java | 29 +---- .../gridworks/exporters/ExporterRegistry.java | 31 ++++++ .../gridworks/importers/ImporterRegistry.java | 103 ++++++++++++++++++ 4 files changed, 141 insertions(+), 120 deletions(-) create mode 100644 main/src/com/google/gridworks/exporters/ExporterRegistry.java create mode 100644 main/src/com/google/gridworks/importers/ImporterRegistry.java diff --git a/main/src/com/google/gridworks/commands/project/CreateProjectCommand.java b/main/src/com/google/gridworks/commands/project/CreateProjectCommand.java index 80375549e..6a90e58c6 100644 --- a/main/src/com/google/gridworks/commands/project/CreateProjectCommand.java +++ b/main/src/com/google/gridworks/commands/project/CreateProjectCommand.java @@ -43,6 +43,7 @@ import com.google.gridworks.ProjectManager; import com.google.gridworks.ProjectMetadata; import com.google.gridworks.commands.Command; import com.google.gridworks.importers.Importer; +import com.google.gridworks.importers.ImporterRegistry; import com.google.gridworks.importers.ReaderImporter; import com.google.gridworks.importers.StreamImporter; import com.google.gridworks.importers.TsvCsvImporter; @@ -57,71 +58,6 @@ public class CreateProjectCommand extends Command { final static Logger logger = LoggerFactory.getLogger("create-project_command"); - static final private Map importers = new HashMap(); - - private static final String[][] importerNames = { - {"ExcelImporter", "com.google.gridworks.importers.ExcelImporter"}, - {"XmlImporter", "com.google.gridworks.importers.XmlImporter"}, - {"RdfTripleImporter", "com.google.gridworks.importers.RdfTripleImporter"}, - {"MarcImporter", "com.google.gridworks.importers.MarcImporter"}, - {"TsvCsvImporter", "com.google.gridworks.importers.TsvCsvImporter"}, - }; - - static { - registerImporters(importerNames); - } - - static public boolean registerImporters(String[][] importers) { - boolean status = true; - for (String[] importer : importerNames) { - String importerName = importer[0]; - String className = importer[1]; - logger.debug("Loading command " + importerName + " class: " + className); - Importer cmd; - try { - // TODO: May need to use the servlet container's class loader here - cmd = (Importer) Class.forName(className).newInstance(); - } catch (InstantiationException e) { - logger.error("Failed to load importer class " + className, e); - status = false; - continue; - } catch (IllegalAccessException e) { - logger.error("Failed to load importer class " + className, e); - status = false; - continue; - } catch (ClassNotFoundException e) { - logger.error("Failed to load importer class " + className, e); - status = false; - continue; - } - status |= registerImporter(importerName, cmd); - } - return status; - } - - /** - * Register a single importer. - * - * @param name - * importer verb for importer - * @param commandObject - * object implementing the importer - * @return true if importer was loaded and registered successfully - */ - static public boolean registerImporter(String name, - Importer importerObject) { - if (importers.containsKey(name)) { - return false; - } - importers.put(name, importerObject); - return true; - } - - // Currently only for test purposes - static protected boolean unregisterImporter(String verb) { - return importers.remove(verb) != null; - } - @Override public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { @@ -369,7 +305,7 @@ public class CreateProjectCommand extends Command { } private void load(Project project, Properties options, String fileName, InputStream inputStream) throws Exception { - Importer importer = guessImporter(null, fileName); + Importer importer = ImporterRegistry.guessImporter(null, fileName); internalInvokeImporter(project, importer, options, inputStream, null); } @@ -415,7 +351,7 @@ public class CreateProjectCommand extends Command { URLConnection connection = null; // Try for a URL importer first - Importer importer = guessUrlImporter(url); + Importer importer = ImporterRegistry.guessUrlImporter(url); if (importer instanceof UrlImporter) { ((UrlImporter) importer).read(url, project, options); } else { @@ -436,7 +372,7 @@ public class CreateProjectCommand extends Command { } try { - importer = guessImporter(connection.getContentType(), url.getPath()); + importer = ImporterRegistry.guessImporter(connection.getContentType(), url.getPath()); internalInvokeImporter(project, importer, options, inputStream, connection.getContentEncoding()); } finally { @@ -506,30 +442,4 @@ public class CreateProjectCommand extends Command { importer.read(reader, project, options); } - protected Importer guessImporter(String contentType, String fileName, boolean provideDefault) { - for (Importer i : importers.values()){ - if(i.canImportData(contentType, fileName)){ - return i; - } - } - if (provideDefault) { - return new TsvCsvImporter(); // default - } else { - return null; - } - } - - protected Importer guessImporter(String contentType, String filename) { - return guessImporter(contentType, filename, true); - } - - protected Importer guessUrlImporter(URL url) { - for (Importer importer : importers.values()){ - if (importer instanceof UrlImporter - && ((UrlImporter) importer).canImportData(url)) { - return importer; - } - } - return null; - } } diff --git a/main/src/com/google/gridworks/commands/project/ExportRowsCommand.java b/main/src/com/google/gridworks/commands/project/ExportRowsCommand.java index 61eba8362..34fc9da40 100644 --- a/main/src/com/google/gridworks/commands/project/ExportRowsCommand.java +++ b/main/src/com/google/gridworks/commands/project/ExportRowsCommand.java @@ -1,10 +1,8 @@ package com.google.gridworks.commands.project; - import java.io.IOException; +import java.io.IOException; import java.io.PrintWriter; import java.util.Enumeration; -import java.util.HashMap; -import java.util.Map; import java.util.Properties; import javax.servlet.ServletException; @@ -16,32 +14,11 @@ import com.google.gridworks.browsing.Engine; import com.google.gridworks.commands.Command; import com.google.gridworks.exporters.CsvExporter; import com.google.gridworks.exporters.Exporter; -import com.google.gridworks.exporters.HtmlTableExporter; -import com.google.gridworks.exporters.TemplatingExporter; -import com.google.gridworks.exporters.XlsExporter; -import com.google.gridworks.exporters.ProtographTransposeExporter.MqlwriteLikeExporter; -import com.google.gridworks.exporters.ProtographTransposeExporter.TripleLoaderExporter; +import com.google.gridworks.exporters.ExporterRegistry; import com.google.gridworks.model.Project; public class ExportRowsCommand extends Command { - static final protected Map s_formatToExporter = new HashMap(); - - static { - s_formatToExporter.put("html", new HtmlTableExporter()); - s_formatToExporter.put("xls", new XlsExporter()); - s_formatToExporter.put("csv", new CsvExporter()); - - s_formatToExporter.put("template", new TemplatingExporter()); - - s_formatToExporter.put("tripleloader", new TripleLoaderExporter()); - s_formatToExporter.put("mqlwrite", new MqlwriteLikeExporter()); - } - - static public void registerExporter(String format, Exporter exporter) { - s_formatToExporter.put(format, exporter); - } - @SuppressWarnings("unchecked") static public Properties getRequestParameters(HttpServletRequest request) { Properties options = new Properties(); @@ -64,7 +41,7 @@ public class ExportRowsCommand extends Command { String format = request.getParameter("format"); Properties options = getRequestParameters(request); - Exporter exporter = s_formatToExporter.get(format.toLowerCase()); + Exporter exporter = ExporterRegistry.getExporter(format); if (exporter == null){ exporter = new CsvExporter('\t'); } diff --git a/main/src/com/google/gridworks/exporters/ExporterRegistry.java b/main/src/com/google/gridworks/exporters/ExporterRegistry.java new file mode 100644 index 000000000..ea5578b55 --- /dev/null +++ b/main/src/com/google/gridworks/exporters/ExporterRegistry.java @@ -0,0 +1,31 @@ +package com.google.gridworks.exporters; + +import java.util.HashMap; +import java.util.Map; + +import com.google.gridworks.exporters.ProtographTransposeExporter.MqlwriteLikeExporter; +import com.google.gridworks.exporters.ProtographTransposeExporter.TripleLoaderExporter; + + +abstract public class ExporterRegistry { + static final private Map s_formatToExporter = new HashMap(); + + static { + s_formatToExporter.put("html", new HtmlTableExporter()); + s_formatToExporter.put("xls", new XlsExporter()); + s_formatToExporter.put("csv", new CsvExporter()); + + s_formatToExporter.put("template", new TemplatingExporter()); + + s_formatToExporter.put("tripleloader", new TripleLoaderExporter()); + s_formatToExporter.put("mqlwrite", new MqlwriteLikeExporter()); + } + + static public void registerExporter(String format, Exporter exporter) { + s_formatToExporter.put(format.toLowerCase(), exporter); + } + + static public Exporter getExporter(String format) { + return s_formatToExporter.get(format.toLowerCase()); + } +} diff --git a/main/src/com/google/gridworks/importers/ImporterRegistry.java b/main/src/com/google/gridworks/importers/ImporterRegistry.java new file mode 100644 index 000000000..d84afc0dc --- /dev/null +++ b/main/src/com/google/gridworks/importers/ImporterRegistry.java @@ -0,0 +1,103 @@ +package com.google.gridworks.importers; + +import java.net.URL; +import java.util.HashMap; +import java.util.Map; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + + +abstract public class ImporterRegistry { + final static Logger logger = LoggerFactory.getLogger("importer-registry"); + + static final private Map importers = new HashMap(); + + private static final String[][] importerNames = { + {"ExcelImporter", "com.google.gridworks.importers.ExcelImporter"}, + {"XmlImporter", "com.google.gridworks.importers.XmlImporter"}, + {"RdfTripleImporter", "com.google.gridworks.importers.RdfTripleImporter"}, + {"MarcImporter", "com.google.gridworks.importers.MarcImporter"}, + {"TsvCsvImporter", "com.google.gridworks.importers.TsvCsvImporter"} + }; + + static { + registerImporters(importerNames); + } + + static public boolean registerImporters(String[][] importers) { + boolean status = true; + for (String[] importer : importerNames) { + String importerName = importer[0]; + String className = importer[1]; + logger.debug("Loading command " + importerName + " class: " + className); + Importer cmd; + try { + // TODO: May need to use the servlet container's class loader here + cmd = (Importer) Class.forName(className).newInstance(); + } catch (InstantiationException e) { + logger.error("Failed to load importer class " + className, e); + status = false; + continue; + } catch (IllegalAccessException e) { + logger.error("Failed to load importer class " + className, e); + status = false; + continue; + } catch (ClassNotFoundException e) { + logger.error("Failed to load importer class " + className, e); + status = false; + continue; + } + status |= registerImporter(importerName, cmd); + } + return status; + } + + /** + * Register a single importer. + * + * @param name importer verb for importer + * @param importerObject object implementing the importer + * + * @return true if importer was loaded and registered successfully + */ + static public boolean registerImporter(String name, Importer importerObject) { + if (importers.containsKey(name)) { + return false; + } + importers.put(name, importerObject); + return true; + } + + // Currently only for test purposes + static protected boolean unregisterImporter(String verb) { + return importers.remove(verb) != null; + } + + static public Importer guessImporter(String contentType, String fileName, boolean provideDefault) { + for (Importer i : importers.values()){ + if(i.canImportData(contentType, fileName)){ + return i; + } + } + if (provideDefault) { + return new TsvCsvImporter(); // default + } else { + return null; + } + } + + static public Importer guessImporter(String contentType, String filename) { + return guessImporter(contentType, filename, true); + } + + static public Importer guessUrlImporter(URL url) { + for (Importer importer : importers.values()){ + if (importer instanceof UrlImporter + && ((UrlImporter) importer).canImportData(url)) { + return importer; + } + } + return null; + } +}