-
-
Notifications
You must be signed in to change notification settings - Fork 2.9k
Simplify BibDatabaseWriter by removing unnecessary subclass #13408
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,7 @@ | ||
package org.jabref.logic.exporter; | ||
|
||
import java.io.IOException; | ||
import java.io.Writer; | ||
import java.nio.charset.Charset; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.ArrayList; | ||
|
@@ -16,7 +17,10 @@ | |
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import org.jabref.logic.bibtex.BibEntryWriter; | ||
import org.jabref.logic.bibtex.FieldPreferences; | ||
import org.jabref.logic.bibtex.FieldWriter; | ||
import org.jabref.logic.bibtex.InvalidFieldValueException; | ||
import org.jabref.logic.bibtex.comparator.BibtexStringComparator; | ||
import org.jabref.logic.bibtex.comparator.CrossRefEntryComparator; | ||
import org.jabref.logic.bibtex.comparator.FieldComparator; | ||
|
@@ -44,20 +48,25 @@ | |
import org.jabref.model.strings.StringUtil; | ||
|
||
import org.jooq.lambda.Unchecked; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* A generic writer for our database. This is independent of the concrete serialization format. | ||
* For instance, we could also write out YAML or XML by subclassing this class. | ||
* Writes a .bib file following the BibTeX / BibLaTeX format using the provided {@link BibWriter} | ||
* <p> | ||
* Currently, {@link BibtexDatabaseWriter} is the only subclass of this class (and that class writes a .bib file) | ||
* <p> | ||
* The opposite class is {@link org.jabref.logic.importer.fileformat.BibtexParser} | ||
* The opposite class is {@link org.jabref.logic.importer.fileformat.BibtexImporter} | ||
*/ | ||
public abstract class BibDatabaseWriter { | ||
|
||
public class BibDatabaseWriter { | ||
public enum SaveType { WITH_JABREF_META_DATA, PLAIN_BIBTEX } | ||
|
||
public static final String DATABASE_ID_PREFIX = "DBID:"; | ||
private static final Logger LOGGER = LoggerFactory.getLogger(BibDatabaseWriter.class); | ||
private static final Pattern REFERENCE_PATTERN = Pattern.compile("(#[A-Za-z]+#)"); // Used to detect string references in strings | ||
private static final String COMMENT_PREFIX = "@Comment"; | ||
private static final String PREAMBLE_PREFIX = "@Preamble"; | ||
|
||
private static final String STRING_PREFIX = "@String"; | ||
|
||
protected final BibWriter bibWriter; | ||
protected final SelfContainedSaveConfiguration saveConfiguration; | ||
protected final CitationKeyPatternPreferences keyPatternPreferences; | ||
|
@@ -78,6 +87,19 @@ public BibDatabaseWriter(BibWriter bibWriter, | |
assert saveConfiguration.getSaveOrder().getOrderType() != SaveOrder.OrderType.TABLE; | ||
} | ||
|
||
public BibDatabaseWriter(Writer writer, | ||
String newline, | ||
SelfContainedSaveConfiguration saveConfiguration, | ||
FieldPreferences fieldPreferences, | ||
CitationKeyPatternPreferences citationKeyPatternPreferences, | ||
BibEntryTypesManager entryTypesManager) { | ||
this(new BibWriter(writer, newline), | ||
saveConfiguration, | ||
fieldPreferences, | ||
citationKeyPatternPreferences, | ||
entryTypesManager); | ||
} | ||
|
||
private static List<FieldChange> applySaveActions(List<BibEntry> toChange, MetaData metaData, FieldPreferences fieldPreferences) { | ||
List<FieldChange> changes = new ArrayList<>(); | ||
|
||
|
@@ -224,11 +246,31 @@ public void savePartOfDatabase(BibDatabaseContext bibDatabaseContext, List<BibEn | |
writeEpilogue(bibDatabaseContext.getDatabase().getEpilog()); | ||
} | ||
|
||
protected abstract void writeProlog(BibDatabaseContext bibDatabaseContext, Charset encoding) throws IOException; | ||
protected void writeProlog(BibDatabaseContext bibDatabaseContext, Charset encoding) throws IOException { | ||
// We write the encoding if | ||
// - it is provided (!= null) | ||
// - explicitly set in the .bib file OR not equal to UTF_8 | ||
// Otherwise, we do not write anything and return | ||
if ((encoding == null) || (!bibDatabaseContext.getMetaData().getEncodingExplicitlySupplied() && (encoding.equals(StandardCharsets.UTF_8)))) { | ||
return; | ||
} | ||
|
||
// Writes the file encoding information. | ||
bibWriter.write("% "); | ||
bibWriter.writeLine(SaveConfiguration.ENCODING_PREFIX + encoding); | ||
} | ||
|
||
protected abstract void writeEntry(BibEntry entry, BibDatabaseMode mode) throws IOException; | ||
protected void writeEntry(BibEntry entry, BibDatabaseMode mode) throws IOException { | ||
BibEntryWriter bibtexEntryWriter = new BibEntryWriter(new FieldWriter(fieldPreferences), entryTypesManager); | ||
bibtexEntryWriter.write(entry, bibWriter, mode, saveConfiguration.shouldReformatFile()); | ||
} | ||
|
||
protected abstract void writeEpilogue(String epilogue) throws IOException; | ||
protected void writeEpilogue(String epilogue) throws IOException { | ||
if (!StringUtil.isNullOrEmpty(epilogue)) { | ||
bibWriter.write(epilogue); | ||
bibWriter.finishBlock(); | ||
} | ||
} | ||
|
||
/** | ||
* Writes all data to the specified writer, using each object's toString() method. | ||
|
@@ -244,11 +286,31 @@ protected void writeMetaData(MetaData metaData, GlobalCitationKeyPatterns global | |
} | ||
} | ||
|
||
protected abstract void writeMetaDataItem(Map.Entry<String, String> metaItem) throws IOException; | ||
protected void writeMetaDataItem(Map.Entry<String, String> metaItem) throws IOException { | ||
bibWriter.write(COMMENT_PREFIX + "{"); | ||
bibWriter.write(MetaData.META_FLAG); | ||
bibWriter.write(metaItem.getKey()); | ||
bibWriter.write(":"); | ||
bibWriter.write(metaItem.getValue()); | ||
bibWriter.write("}"); | ||
bibWriter.finishBlock(); | ||
} | ||
|
||
protected abstract void writePreamble(String preamble) throws IOException; | ||
protected void writePreamble(String preamble) throws IOException { | ||
if (!StringUtil.isNullOrEmpty(preamble)) { | ||
bibWriter.write(PREAMBLE_PREFIX + "{"); | ||
bibWriter.write(preamble); | ||
bibWriter.writeLine("}"); | ||
bibWriter.finishBlock(); | ||
} | ||
} | ||
|
||
protected abstract void writeDatabaseID(String sharedDatabaseID) throws IOException; | ||
protected void writeDatabaseID(String sharedDatabaseID) throws IOException { | ||
bibWriter.write("% "); | ||
bibWriter.write(DATABASE_ID_PREFIX); | ||
bibWriter.write(" "); | ||
bibWriter.writeLine(sharedDatabaseID); | ||
} | ||
|
||
/** | ||
* Write all strings in alphabetical order, modified to produce a safe (for BibTeX) order of the strings if they | ||
|
@@ -307,16 +369,49 @@ protected void writeString(BibtexString bibtexString, Map<String, BibtexString> | |
writeString(bibtexString, maxKeyLength); | ||
} | ||
|
||
protected abstract void writeString(BibtexString bibtexString, int maxKeyLength) | ||
throws IOException; | ||
protected void writeString(BibtexString bibtexString, int maxKeyLength) throws IOException { | ||
// If the string has not been modified, write it back as it was | ||
if (!saveConfiguration.shouldReformatFile() && !bibtexString.hasChanged()) { | ||
LOGGER.debug("Writing parsed serialization {}.", bibtexString.getParsedSerialization()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Debug logging statement provides no additional value beyond what's obvious from the code. This violates the principle that comments/logs should add new information. |
||
bibWriter.write(bibtexString.getParsedSerialization()); | ||
return; | ||
} | ||
|
||
// Write user comments | ||
String userComments = bibtexString.getUserComments(); | ||
Comment on lines
+380
to
+381
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Comment merely restates what the code does without providing additional context or reasoning. This type of trivial comment should be removed. |
||
if (!userComments.isEmpty()) { | ||
bibWriter.writeLine(userComments); | ||
} | ||
|
||
bibWriter.write(STRING_PREFIX + "{" + bibtexString.getName() + StringUtil | ||
.repeatSpaces(maxKeyLength - bibtexString.getName().length()) + " = "); | ||
if (bibtexString.getContent().isEmpty()) { | ||
bibWriter.write("{}"); | ||
} else { | ||
try { | ||
String formatted = new FieldWriter(fieldPreferences) | ||
.write(InternalField.BIBTEX_STRING, bibtexString.getContent()); | ||
bibWriter.write(formatted); | ||
} catch (InvalidFieldValueException ex) { | ||
throw new IOException(ex); | ||
} | ||
} | ||
|
||
bibWriter.writeLine("}"); | ||
} | ||
|
||
protected void writeEntryTypeDefinitions(SortedSet<BibEntryType> types) throws IOException { | ||
for (BibEntryType type : types) { | ||
writeEntryTypeDefinition(type); | ||
} | ||
} | ||
|
||
protected abstract void writeEntryTypeDefinition(BibEntryType customType) throws IOException; | ||
protected void writeEntryTypeDefinition(BibEntryType customType) throws IOException { | ||
bibWriter.write(COMMENT_PREFIX + "{"); | ||
bibWriter.write(MetaDataSerializer.serializeCustomEntryTypes(customType)); | ||
bibWriter.writeLine("}"); | ||
bibWriter.finishBlock(); | ||
} | ||
|
||
/** | ||
* Generate keys for all entries that are lacking keys. | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Comment is redundant and simply restates what the code does. It doesn't provide any additional information or reasoning about why this is necessary.