From 0febf5ceab567100f98a8b0b549afffe90c99e8d Mon Sep 17 00:00:00 2001 From: jensp Date: Fri, 12 Oct 2012 06:21:28 +0000 Subject: [PATCH] =?UTF-8?q?CLI=20Interface=20zum=20Ausf=C3=BChren=20von=20?= =?UTF-8?q?Importern,=20einige=20kleinere=20Korrekturen=20am=20Code=20f?= =?UTF-8?q?=C3=BCr=20Im-/Exporterinfrastruktur?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit git-svn-id: https://svn.libreccm.org/ccm/trunk@1880 8810af33-2d31-482b-a856-94f89814c4df --- .../imexporter/PublicationFormat.java | 28 ++-- .../scipublications/importer/ImporterCli.java | 144 ++++++++++++++++++ .../importer/SciPublicationsImporters.java | 13 +- 3 files changed, 164 insertions(+), 21 deletions(-) create mode 100644 ccm-sci-publications/src/com/arsdigita/cms/scipublications/importer/ImporterCli.java diff --git a/ccm-sci-publications/src/com/arsdigita/cms/scipublications/imexporter/PublicationFormat.java b/ccm-sci-publications/src/com/arsdigita/cms/scipublications/imexporter/PublicationFormat.java index bbd05a86c..500c6d6cf 100644 --- a/ccm-sci-publications/src/com/arsdigita/cms/scipublications/imexporter/PublicationFormat.java +++ b/ccm-sci-publications/src/com/arsdigita/cms/scipublications/imexporter/PublicationFormat.java @@ -79,35 +79,35 @@ public class PublicationFormat { } @Override - public boolean equals(Object obj) { + public boolean equals(final Object obj) { if (obj == null) { return false; } - if (getClass() != obj.getClass()) { + if (!(obj instanceof PublicationFormat)) { return false; } + final PublicationFormat other = (PublicationFormat) obj; - if ((this.name == null) ? (other.name != null) - : !this.name.equals(other.name)) { + if ((name == null) && other.getName() != null) { return false; - } - if (this.mimeType != other.mimeType && (this.mimeType == null || !this.mimeType. - equals(other.mimeType))) { + } else if((name != null) && other.getName() == null) { return false; + } else { + return name.equals(other.getName()); } - if ((this.fileExtension == null) ? (other.fileExtension != null) - : !this.fileExtension.equals(other.fileExtension)) { - return false; - } - return true; } @Override public int hashCode() { - int hash = 7; + int hash = 5; + if (this.name == null) { + hash *= 37; + } else { + hash = 37 * hash + this.name.hashCode(); + } return hash; } - + @Override public String toString() { return String.format("PublicationFormat = {name = \"%s\"; " diff --git a/ccm-sci-publications/src/com/arsdigita/cms/scipublications/importer/ImporterCli.java b/ccm-sci-publications/src/com/arsdigita/cms/scipublications/importer/ImporterCli.java new file mode 100644 index 000000000..4a7e7cb4a --- /dev/null +++ b/ccm-sci-publications/src/com/arsdigita/cms/scipublications/importer/ImporterCli.java @@ -0,0 +1,144 @@ +/* + * Copyright (c) 2012 Jens Pelzetter, ScientificCMS.org team + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public License + * as published by the Free Software Foundation; either version 2.1 of + * the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ +package com.arsdigita.cms.scipublications.importer; + +import com.arsdigita.cms.scipublications.imexporter.PublicationFormat; +import com.arsdigita.util.cmd.Program; +import java.io.File; +import java.io.IOException; +import java.util.List; +import org.apache.commons.cli.CommandLine; +import org.apache.commons.cli.OptionBuilder; +import org.apache.commons.cli.Options; +import org.apache.commons.io.FileUtils; +import org.apache.log4j.Logger; + +/** + * + * @author Jens Pelzetter + * @version $Id$ + */ +public class ImporterCli extends Program { + + private static final Logger LOGGER = Logger.getLogger(ImporterCli.class); + private static final String PRETEND = "pretend"; + private static final String PUBLISH = "publish"; + private static final String LIST = "list"; + + public ImporterCli() { + super("ImporterCli", "1.0.0", "ImporterCli [--pretend] [--publish] file | dir OR ImporterCLI --list"); + + final Options options = getOptions(); + + options.addOption(OptionBuilder + .withLongOpt(PRETEND) + .withDescription("Do not perform import, only print results.") + .create()); + options.addOption(OptionBuilder + .withLongOpt(PUBLISH) + .withDescription("Publish created publications") + .create()); + options.addOption(OptionBuilder + .withLongOpt(LIST) + .withDescription("List all available importers and exit") + .create()); + } + + public static void main(final String args[]) { + new ImporterCli().run(args); + } + + @Override + protected void doRun(final CommandLine cmdLine) { + LOGGER.info("Publications Importer CLI tool."); + if (cmdLine.hasOption(LIST)) { + final List formats = SciPublicationsImporters.getInstance().getSupportedFormats(); + LOGGER.info("Supported formats:"); + for(PublicationFormat format : formats) { + LOGGER.info(String.format("%s, MIME type: %s, file extension: %s", format.getName(), + format.getMimeType().toString(), + format.getFileExtension())); + } + return; + } + + final boolean pretend = cmdLine.hasOption(PRETEND); + final boolean publish = cmdLine.hasOption(PUBLISH); + + if (cmdLine.getArgs().length != 1) { + LOGGER.error("Missing file/directory to import."); + help(System.err); + return; + } + + final String sourceName = cmdLine.getArgs()[0]; + final File source = new File(sourceName); + importFile(source, pretend, publish); + } + + protected void importFile(final File file, final boolean pretend, final boolean publish) { + if (file == null) { + throw new IllegalArgumentException("File object is null."); + } + + if (file.isDirectory()) { + final File[] files = file.listFiles(); + for (File f : files) { + importFile(f, pretend, publish); + } + } else if (file.isFile()) { + final String fileName = file.getName(); + final String extension = fileName.substring(fileName.lastIndexOf('.')); + final PublicationFormat format = findFormatForExtension(extension); + if (format == null) { + LOGGER.error(String.format("No importer for publication format identified " + + "by file extension '%s' available.", + extension)); + return; + } + + final SciPublicationsImporter importer = SciPublicationsImporters.getInstance().getImporterForFormat( + format.getName()); + final String data; + try { + data = FileUtils.readFileToString(file); + } catch (IOException ex) { + LOGGER.error(String.format("Failed to read file '%s'.", file.getAbsolutePath()), ex); + return; + } + LOGGER.info(String.format("Importing publications from file '%s'...", file.getAbsolutePath())); + importer.importPublications(data, pretend, publish); + } else { + LOGGER.info(String.format("File %s does not exist.", file.getAbsolutePath())); + } + } + + protected PublicationFormat findFormatForExtension(final String extension) { + final List formats = SciPublicationsImporters.getInstance().getSupportedFormats(); + + for (PublicationFormat format : formats) { + if (format.getFileExtension().equals(extension)) { + return format; + } + } + + return null; + } + +} diff --git a/ccm-sci-publications/src/com/arsdigita/cms/scipublications/importer/SciPublicationsImporters.java b/ccm-sci-publications/src/com/arsdigita/cms/scipublications/importer/SciPublicationsImporters.java index 554fd5e25..e731d04fb 100644 --- a/ccm-sci-publications/src/com/arsdigita/cms/scipublications/importer/SciPublicationsImporters.java +++ b/ccm-sci-publications/src/com/arsdigita/cms/scipublications/importer/SciPublicationsImporters.java @@ -1,6 +1,5 @@ package com.arsdigita.cms.scipublications.importer; -import com.arsdigita.cms.scipublications.exporter.SciPublicationsExporter; import com.arsdigita.cms.scipublications.imexporter.PublicationFormat; import java.util.ArrayList; import java.util.HashMap; @@ -17,7 +16,7 @@ import org.apache.log4j.Logger; */ public class SciPublicationsImporters { - private static final Logger logger = Logger.getLogger(SciPublicationsImporters.class); + private static final Logger LOGGER = Logger.getLogger(SciPublicationsImporters.class); /** * Asscociation of the format the responsible importer. */ @@ -33,18 +32,18 @@ public class SciPublicationsImporters { * {@link SciPublicationsImporters} and puts them into the {@link #importers} map. */ private SciPublicationsImporters() { - logger.debug("Creating SciPublicationsImporter instance..."); + LOGGER.debug("Creating SciPublicationsImporter instance..."); final ServiceLoader importerServices; - logger.debug("Loading all available implementations of the SciPublicationsImporter interface..."); + LOGGER.debug("Loading all available implementations of the SciPublicationsImporter interface..."); importerServices = ServiceLoader.load(SciPublicationsImporter.class); for (SciPublicationsImporter importer : importerServices) { - logger.debug(String.format("Found importer for format '%s'...", + LOGGER.debug(String.format("Found importer for format '%s'...", importer.getSupportedFormat().getName().toLowerCase())); importers.put(importer.getSupportedFormat().getName().toLowerCase(), importer); } - logger.debug(String.format("Found %d importers.", importers.size())); + LOGGER.debug(String.format("Found %d importers.", importers.size())); } public static SciPublicationsImporters getInstance() { @@ -55,7 +54,7 @@ public class SciPublicationsImporters { * Retrieves the importer for the specified format. * * @param format The format which should be supported by the importer. - * @return The importer for the specified format, or null if there is no export which supports the + * @return The importer for the specified format, or null if there is no importer which supports the * specified format. */ public SciPublicationsImporter getImporterForFormat(final String format) {