Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ExportFormats: Use FileExtension enum #3565

Merged
merged 6 commits into from
Dec 22, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/gui/exporter/ExportAction.java
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ public void actionPerformed(ActionEvent e) {

ExportFileFilter eff = (ExportFileFilter) ff;
String path = file.getPath();
if (!path.endsWith(eff.getExtension())) {
if (!path.endsWith(eff.getExtension().getExtensionsAsList().toString())) {
path = path + eff.getExtension();
}
file = new File(path);
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/org/jabref/gui/exporter/ExportFileFilter.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import javax.swing.filechooser.FileFilter;

import org.jabref.logic.exporter.IExportFormat;
import org.jabref.logic.util.FileExtensions;

/**
* File filter that lets the user choose export format while choosing file to
Expand All @@ -14,7 +15,7 @@
public class ExportFileFilter extends FileFilter implements Comparable<ExportFileFilter> {

private final IExportFormat format;
private final String extension;
private final FileExtensions extension;
private final String name;


Expand All @@ -29,7 +30,7 @@ public IExportFormat getExportFormat() {
return format;
}

public String getExtension() {
public FileExtensions getExtension() {
return extension;
}

Expand All @@ -38,7 +39,7 @@ public boolean accept(File file) {
if (file.isDirectory()) {
return true;
} else {
return file.getPath().toLowerCase(Locale.ROOT).endsWith(extension);
return file.getPath().toLowerCase(Locale.ROOT).endsWith(extension.getExtensionsAsList().toString());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
import org.jabref.logic.importer.fileformat.bibtexml.Proceedings;
import org.jabref.logic.importer.fileformat.bibtexml.Techreport;
import org.jabref.logic.importer.fileformat.bibtexml.Unpublished;
import org.jabref.logic.util.FileExtensions;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;

Expand All @@ -54,7 +55,7 @@ public class BibTeXMLExportFormat extends ExportFormat {


public BibTeXMLExportFormat() {
super("BibTeXML", "bibtexml", null, null, ".xml");
super("BibTeXML", "bibtexml", null, null, FileExtensions.XML);
}

@Override
Expand Down
9 changes: 5 additions & 4 deletions src/main/java/org/jabref/logic/exporter/ExportFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.jabref.logic.layout.Layout;
import org.jabref.logic.layout.LayoutFormatterPreferences;
import org.jabref.logic.layout.LayoutHelper;
import org.jabref.logic.util.FileExtensions;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;

Expand All @@ -38,7 +39,7 @@ public class ExportFormat implements IExportFormat {
private String consoleName;
private String lfFileName;
private String directory;
private String extension;
private FileExtensions extension;
private Charset encoding; // If this value is set, it will be used to override
// the default encoding for the getCurrentBasePanel.
private LayoutFormatterPreferences layoutPreferences;
Expand All @@ -56,7 +57,7 @@ public class ExportFormat implements IExportFormat {
* @param directory Directory in which to find the layout file.
* @param extension Should contain the . (for instance .txt).
*/
public ExportFormat(String displayName, String consoleName, String lfFileName, String directory, String extension) {
public ExportFormat(String displayName, String consoleName, String lfFileName, String directory, FileExtensions extension) {
this.displayName = displayName;
this.consoleName = consoleName;
this.lfFileName = lfFileName;
Expand All @@ -76,7 +77,7 @@ public ExportFormat(String displayName, String consoleName, String lfFileName, S
* @param layoutPreferences Preferences for layout
* @param savePreferences Preferences for saving
*/
public ExportFormat(String displayName, String consoleName, String lfFileName, String directory, String extension,
public ExportFormat(String displayName, String consoleName, String lfFileName, String directory, FileExtensions extension,
LayoutFormatterPreferences layoutPreferences, SavePreferences savePreferences) {
this(displayName, consoleName, lfFileName, directory, extension);
this.layoutPreferences = layoutPreferences;
Expand Down Expand Up @@ -131,7 +132,7 @@ public void setEncoding(Charset encoding) {
* @see IExportFormat#getExtension()
*/
@Override
public String getExtension() {
public FileExtensions getExtension() {
return extension;
}

Expand Down
61 changes: 36 additions & 25 deletions src/main/java/org/jabref/logic/exporter/ExportFormats.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

import org.jabref.logic.l10n.Localization;
import org.jabref.logic.layout.LayoutFormatterPreferences;
import org.jabref.logic.util.FileExtensions;

public class ExportFormats {

Expand All @@ -19,42 +20,42 @@ private ExportFormats() {
}

public static void initAllExports(Map<String, ExportFormat> customFormats,
LayoutFormatterPreferences layoutPreferences, SavePreferences savePreferences) {
LayoutFormatterPreferences layoutPreferences, SavePreferences savePreferences) {

ExportFormats.EXPORT_FORMATS.clear();

// Initialize Build-In Export Formats
ExportFormats
.putFormat(new ExportFormat("HTML", "html", "html", null, ".html", layoutPreferences, savePreferences));
.putFormat(new ExportFormat("HTML", "html", "html", null, FileExtensions.HTML, layoutPreferences, savePreferences));
ExportFormats.putFormat(new ExportFormat(Localization.lang("Simple HTML"), "simplehtml", "simplehtml", null,
".html", layoutPreferences, savePreferences));
ExportFormats.putFormat(new ExportFormat("DocBook 4.4", "docbook", "docbook", null, ".xml", layoutPreferences,
FileExtensions.HTML, layoutPreferences, savePreferences));
ExportFormats.putFormat(new ExportFormat("DocBook 4.4", "docbook", "docbook", null, FileExtensions.XML, layoutPreferences,
savePreferences));
ExportFormats.putFormat(new ExportFormat("DIN 1505", "din1505", "din1505winword", "din1505", ".rtf",
ExportFormats.putFormat(new ExportFormat("DIN 1505", "din1505", "din1505winword", "din1505", FileExtensions.RTF,
layoutPreferences, savePreferences));
ExportFormats.putFormat(
new ExportFormat("BibO RDF", "bibordf", "bibordf", null, ".rdf", layoutPreferences, savePreferences));
new ExportFormat("BibO RDF", "bibordf", "bibordf", null, FileExtensions.RDF, layoutPreferences, savePreferences));
ExportFormats.putFormat(new ExportFormat(Localization.lang("HTML table"), "tablerefs", "tablerefs", "tablerefs",
".html", layoutPreferences, savePreferences));
FileExtensions.HTML, layoutPreferences, savePreferences));
ExportFormats.putFormat(new ExportFormat(Localization.lang("HTML list"), "listrefs", "listrefs", "listrefs",
".html", layoutPreferences, savePreferences));
FileExtensions.HTML, layoutPreferences, savePreferences));
ExportFormats.putFormat(new ExportFormat(Localization.lang("HTML table (with Abstract & BibTeX)"),
"tablerefsabsbib", "tablerefsabsbib", "tablerefsabsbib", ".html", layoutPreferences, savePreferences));
ExportFormats.putFormat(new ExportFormat("Harvard RTF", "harvard", "harvard", "harvard", ".rtf",
"tablerefsabsbib", "tablerefsabsbib", "tablerefsabsbib", FileExtensions.HTML, layoutPreferences, savePreferences));
ExportFormats.putFormat(new ExportFormat("Harvard RTF", "harvard", "harvard", "harvard", FileExtensions.RDF,
layoutPreferences, savePreferences));
ExportFormats.putFormat(new ExportFormat("ISO 690 RTF", "iso690rtf", "iso690RTF", "iso690rtf", ".rtf",
ExportFormats.putFormat(new ExportFormat("ISO 690 RTF", "iso690rtf", "iso690RTF", "iso690rtf", FileExtensions.RTF,
layoutPreferences, savePreferences));
ExportFormats.putFormat(new ExportFormat("ISO 690", "iso690txt", "iso690", "iso690txt", ".txt",
ExportFormats.putFormat(new ExportFormat("ISO 690", "iso690txt", "iso690", "iso690txt", FileExtensions.TXT,
layoutPreferences, savePreferences));
ExportFormats.putFormat(new ExportFormat("Endnote", "endnote", "EndNote", "endnote", ".txt", layoutPreferences,
ExportFormats.putFormat(new ExportFormat("Endnote", "endnote", "EndNote", "endnote", FileExtensions.TXT, layoutPreferences,
savePreferences));
ExportFormats.putFormat(new ExportFormat("OpenOffice/LibreOffice CSV", "oocsv", "openoffice-csv", "openoffice",
".csv", layoutPreferences, savePreferences));
ExportFormat ef = new ExportFormat("RIS", "ris", "ris", "ris", ".ris", layoutPreferences, savePreferences);
FileExtensions.CSV, layoutPreferences, savePreferences));
ExportFormat ef = new ExportFormat("RIS", "ris", "ris", "ris", FileExtensions.RIS, layoutPreferences, savePreferences);
ef.setEncoding(StandardCharsets.UTF_8);
ExportFormats.putFormat(ef);
ExportFormats.putFormat(
new ExportFormat("MIS Quarterly", "misq", "misq", "misq", ".rtf", layoutPreferences, savePreferences));
new ExportFormat("MIS Quarterly", "misq", "misq", "misq", FileExtensions.RTF, layoutPreferences, savePreferences));

ExportFormats.putFormat(new BibTeXMLExportFormat());
ExportFormats.putFormat(new OpenOfficeDocumentCreator());
Expand All @@ -71,11 +72,9 @@ public static void initAllExports(Map<String, ExportFormat> customFormats,
/**
* Build a string listing of all available export formats.
*
* @param maxLineLength
* The max line length before a line break must be added.
* @param linePrefix
* If a line break is added, this prefix will be inserted at the
* beginning of the next line.
* @param maxLineLength The max line length before a line break must be added.
* @param linePrefix If a line break is added, this prefix will be inserted at the
* beginning of the next line.
* @return The string describing available formats.
*/
public static String getConsoleExportList(int maxLineLength, int firstLineSubtr, String linePrefix) {
Expand All @@ -98,6 +97,7 @@ public static String getConsoleExportList(int maxLineLength, int firstLineSubtr,

/**
* Get a Map of all export formats.
*
* @return A Map containing all export formats, mapped to their console names.
*/
public static Map<String, IExportFormat> getExportFormats() {
Expand All @@ -108,17 +108,28 @@ public static Map<String, IExportFormat> getExportFormats() {
/**
* Look up the named export format.
*
* @param consoleName
* The export name given in the JabRef console help information.
* @param consoleName The export name given in the JabRef console help information.
* @return The ExportFormat, or null if no exportformat with that name is
* registered.
* registered.
*/
public static IExportFormat getExportFormat(String consoleName) {
return ExportFormats.EXPORT_FORMATS.get(consoleName);
}

public static FileExtensions getFileExtension(String consoleName) {
if (checkExportFormatExisit(consoleName)) {
ExportFormat exportFormat = (ExportFormat) EXPORT_FORMATS.get(consoleName);
return exportFormat.getExtension();
} else {
return FileExtensions.DEFAULT;
}
}

private static boolean checkExportFormatExisit(String consoleName) {
return EXPORT_FORMATS.keySet().contains(consoleName);
}

private static void putFormat(IExportFormat format) {
ExportFormats.EXPORT_FORMATS.put(format.getConsoleName(), format);
}

}
3 changes: 2 additions & 1 deletion src/main/java/org/jabref/logic/exporter/IExportFormat.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.nio.charset.Charset;
import java.util.List;

import org.jabref.logic.util.FileExtensions;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;

Expand All @@ -19,7 +20,7 @@ public interface IExportFormat {
*/
String getDisplayName();

String getExtension();
FileExtensions getExtension();

/**
* Perform the export.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import javax.xml.transform.stream.StreamResult;

import org.jabref.logic.msbib.MSBibDatabase;
import org.jabref.logic.util.FileExtensions;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;

Expand All @@ -25,7 +26,7 @@
class MSBibExportFormat extends ExportFormat {

public MSBibExportFormat() {
super("MS Office 2007", "MSBib", null, null, ".xml");
super("MS Office 2007", "MSBib", null, null, FileExtensions.XML);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.jabref.logic.importer.fileformat.mods.TitleInfoDefinition;
import org.jabref.logic.importer.fileformat.mods.TypeOfResourceDefinition;
import org.jabref.logic.importer.fileformat.mods.UrlDefinition;
import org.jabref.logic.util.FileExtensions;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.FieldName;
Expand All @@ -61,7 +62,7 @@ class ModsExportFormat extends ExportFormat {


public ModsExportFormat() {
super("MODS", "mods", null, null, ".xml");
super("MODS", "mods", null, null, FileExtensions.XML);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import javax.xml.transform.stream.StreamResult;

import org.jabref.logic.l10n.Localization;
import org.jabref.logic.util.FileExtensions;
import org.jabref.model.database.BibDatabase;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
Expand All @@ -45,7 +46,7 @@ public class OpenDocumentSpreadsheetCreator extends ExportFormat {
* Creates a new instance of OpenOfficeDocumentCreator
*/
public OpenDocumentSpreadsheetCreator() {
super(Localization.lang("OpenDocument spreadsheet"), "ods", null, null, ".ods");
super(Localization.lang("OpenDocument spreadsheet"), "ods", null, null, FileExtensions.ODS);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.jabref.logic.util.FileExtensions;
import org.jabref.model.database.BibDatabase;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
Expand All @@ -43,7 +44,7 @@ public class OpenOfficeDocumentCreator extends ExportFormat {
* Creates a new instance of OpenOfficeDocumentCreator
*/
public OpenOfficeDocumentCreator() {
super("OpenOffice/LibreOffice Calc", "oocalc", null, null, ".sxc");
super("OpenOffice/LibreOffice Calc", "oocalc", null, null, FileExtensions.SXC);
}

@Override
Expand Down
9 changes: 8 additions & 1 deletion src/main/java/org/jabref/logic/util/FileExtensions.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,14 @@ public enum FileExtensions {
CLASS(Localization.lang("%0 file", "CLASS"), "class"),
JAR(Localization.lang("%0 file", "JAR"), "jar"),
XML(Localization.lang("%0 file", "XML"), "xml"),
ZIP(Localization.lang("%0 file", "ZIP"), "zip");
ZIP(Localization.lang("%0 file", "ZIP"), "zip"),
ODS(Localization.lang("%0 file", "ODS"), "ods"),
SXC(Localization.lang("%0 file", "SXC"), "sxc"),
HTML(Localization.lang("%0 file", "HTML"), "html"),
RTF(Localization.lang("%0 file", "RTF"), "rtf"),
RDF(Localization.lang("%0 file", "RDF"), "rdf"),
CSV(Localization.lang("%0 file", "CSV"), "csv"),
DEFAULT(Localization.lang("%0 file", "DEFAULT"), "default");

private final String[] extension;
private final String description;
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/org/jabref/preferences/CustomExportList.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.TreeMap;

import org.jabref.logic.exporter.ExportFormat;
import org.jabref.logic.exporter.ExportFormats;
import org.jabref.logic.exporter.SavePreferences;
import org.jabref.logic.journals.JournalAbbreviationLoader;
import org.jabref.logic.layout.LayoutFormatterPreferences;
Expand Down Expand Up @@ -90,7 +91,7 @@ private Optional<ExportFormat> createFormat(List<String> s, LayoutFormatterPrefe
} else {
lfFileName = s.get(1);
}
ExportFormat format = new ExportFormat(s.get(0), s.get(0), lfFileName, null, s.get(2), layoutPreferences,
ExportFormat format = new ExportFormat(s.get(0), s.get(0), lfFileName, null, ExportFormats.getFileExtension(s.get(2)), layoutPreferences,
savePreferences);
format.setCustomExport(true);
return Optional.of(format);
Expand Down