Factored out registries of importers and exporters.
git-svn-id: http://google-refine.googlecode.com/svn/trunk@1183 7d457c2a-affb-35e4-300a-418c747d4874
This commit is contained in:
parent
7320f65935
commit
4ea765b689
@ -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<String, Importer> importers = new HashMap<String, Importer>();
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -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<String, Exporter> s_formatToExporter = new HashMap<String, Exporter>();
|
||||
|
||||
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');
|
||||
}
|
||||
|
@ -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<String, Exporter> s_formatToExporter = new HashMap<String, Exporter>();
|
||||
|
||||
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());
|
||||
}
|
||||
}
|
103
main/src/com/google/gridworks/importers/ImporterRegistry.java
Normal file
103
main/src/com/google/gridworks/importers/ImporterRegistry.java
Normal file
@ -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<String, Importer> importers = new HashMap<String, Importer>();
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user