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

Unify month field formatting in test bib files (JabRef/jabref#5116) #5283

Closed
wants to merge 8 commits into from
Closed
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
15 changes: 15 additions & 0 deletions src/main/java/org/jabref/logic/exporter/BibTeXMLExporter.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;

import javax.xml.bind.JAXBContext;
Expand Down Expand Up @@ -40,6 +41,7 @@
import org.jabref.logic.util.StandardFileType;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.Month;
import org.jabref.model.entry.field.Field;
import org.jabref.model.entry.field.StandardField;
import org.jabref.model.entry.types.EntryType;
Expand Down Expand Up @@ -153,6 +155,13 @@ private void parseInbook(Inbook inbook, BibEntry bibEntry, Entry entry) {
JAXBElement<BigInteger> number = new JAXBElement<>(new QName(BIBTEXML_NAMESPACE_URI, "number"),
BigInteger.class, new BigInteger(value));
inbook.getContent().add(number);
} else if (StandardField.MONTH.equals(key)) {
Optional<Month> month = bibEntry.getMonth();
if (month.isPresent()) {
JAXBElement<String> element = new JAXBElement<>(new QName(BIBTEXML_NAMESPACE_URI, key.getName()),
String.class, month.get().getFullName());
inbook.getContent().add(element);
}
} else {
JAXBElement<String> element = new JAXBElement<>(new QName(BIBTEXML_NAMESPACE_URI, key.getName()), String.class,
value);
Expand Down Expand Up @@ -205,6 +214,12 @@ private <T> void parse(T entryType, BibEntry bibEntry, Entry entry) {
LOGGER.warn("The value %s of the 'number' field is not an integer and thus is ignored for the export", value);
}
break;
} else if (StandardField.MONTH.equals(key)) {
Optional<Month> month = bibEntry.getMonth();
if (month.isPresent()) {
method.invoke(entryType, month.get().getFullName());
}
break;
} else {
method.invoke(entryType, value);
break;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.regex.Pattern;

import javax.xml.bind.JAXBContext;
Expand All @@ -27,6 +28,7 @@
import org.jabref.logic.importer.fileformat.bibtexml.Incollection;
import org.jabref.logic.util.StandardFileType;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.Month;
import org.jabref.model.entry.field.Field;
import org.jabref.model.entry.field.FieldFactory;
import org.jabref.model.entry.field.StandardField;
Expand Down Expand Up @@ -176,6 +178,9 @@ private <T> void parse(T entryType, Map<Field, String> fields) {
} else if (method.getName().equals("getNumber")) {
putNumber(fields, (BigInteger) method.invoke(entryType));
continue;
} else if (method.getName().equals("getMonth")) {
putMonth(fields, Month.parse((String)method.invoke(entryType)));
continue;
} else if (isMethodToIgnore(method.getName())) {
continue;
} else if (method.getName().startsWith("get")) {
Expand Down Expand Up @@ -208,7 +213,11 @@ private void parseInbook(Inbook inbook, Map<Field, String> fields) {
Object elementValue = element.getValue();
if (elementValue instanceof String) {
String value = (String) elementValue;
putIfValueNotNull(fields, field, value);
if (StandardField.MONTH.equals(field)) {
putMonth(fields, Month.parse(value));
} else {
putIfValueNotNull(fields, field, value);
}
} else if (elementValue instanceof BigInteger) {
BigInteger value = (BigInteger) elementValue;
if (StandardField.NUMBER.equals(field)) {
Expand Down Expand Up @@ -241,6 +250,12 @@ private void putNumber(Map<Field, String> fields, BigInteger number) {
}
}

private void putMonth(Map<Field, String> fields, Optional<Month> month) {
if (month.isPresent()) {
fields.put(StandardField.MONTH, month.get().getJabRefFormat());
}
}

private void putIfValueNotNull(Map<Field, String> fields, Field field, String value) {
if (value != null) {
fields.put(field, value);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -618,7 +618,11 @@ private String parseFieldContent(Field field) throws IOException {
throw new IOException("Error in line " + line + " or above: "
+ "Empty text token.\nThis could be caused " + "by a missing comma between two fields.");
}
value.append('#').append(textToken).append('#');
if (field != StandardField.MONTH) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Although this change makes sense to me and we might change the logic to your version in the future, I would prefer if we don't touch parsing and writing of latex files as part of this PR. There are just too many things that can go wrong and we don't have extensive tests in place to capture them.

Thus, please keep the current behavior here (month = jan is stored as #jan# in BibEntry; moreover, #jan# should be written as month = jan).

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think, this logic is also used to handle string constants (see https://tex.stackexchange.com/a/303489/9075), so we should be very cautious when touching this.

In short: A user should be able to enter a reference to a constant.

  • BibTex: Journal = IEEE_M_COM <-> entry editor: #IEEE_M_COM#
  • BibTex: Journal = {IEEE_M_COM} <-> entry editor: IEEE_M_COM

This is a "quick hack" existing since the early days of JabRef. No one came up with another solution. Especially when joining constants with "real" text: BibTex: Journal = IEEE_M_COM # {some more text}

value.append('#').append(textToken).append('#');
} else {
value.append(textToken);
}
}
skipWhitespace();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
Expand Down Expand Up @@ -74,6 +75,7 @@
import org.jabref.logic.importer.fileformat.medline.Text;
import org.jabref.logic.util.StandardFileType;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.Month;
import org.jabref.model.entry.field.Field;
import org.jabref.model.entry.field.FieldFactory;
import org.jabref.model.entry.field.InternalField;
Expand Down Expand Up @@ -603,7 +605,10 @@ private void addPubDate(Map<Field, String> fields, PubDate pubDate) {
} else {
fields.put(StandardField.YEAR, pubDate.getYear());
if (pubDate.getMonth() != null) {
fields.put(StandardField.MONTH, pubDate.getMonth());
Optional<Month> month = Month.parse(pubDate.getMonth());
if (month.isPresent()) {
fields.put(StandardField.MONTH, month.get().getJabRefFormat());
}
} else if (pubDate.getSeason() != null) {
fields.put(new UnknownField("season"), pubDate.getSeason());
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/org/jabref/model/entry/Month.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,7 +175,7 @@ public String getShortName() {
* @return Month in JabRef format
*/
public String getJabRefFormat() {
return String.format("#%s#", shortName);
return String.format("%s", shortName);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't change this, but instead use getShortName in the exporters

}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -358,7 +358,7 @@ public void monthFieldSpecialSyntax() throws IOException {
// modify month field
Set<Field> fields = entry.getFields();
assertTrue(fields.contains(StandardField.MONTH));
assertEquals("#mar#", entry.getField(StandardField.MONTH).get());
assertEquals("mar", entry.getField(StandardField.MONTH).get());

//write out bibtex string
StringWriter stringWriter = new StringWriter();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ void cleanupMonthChangesNumberToBibtex() {
entry.setField(StandardField.MONTH, "01");

worker.cleanup(preset, entry);
assertEquals(Optional.of("#jan#"), entry.getField(StandardField.MONTH));
assertEquals(Optional.of("jan"), entry.getField(StandardField.MONTH));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public void cleanupMovesDateToYearAndMonth() {

assertEquals(Optional.empty(), entry.getField(StandardField.DATE));
assertEquals(Optional.of("2011"), entry.getField(StandardField.YEAR));
assertEquals(Optional.of("#jan#"), entry.getField(StandardField.MONTH));
assertEquals(Optional.of("jan"), entry.getField(StandardField.MONTH));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,6 @@ public void setUp() {

@Test
public void formatExample() {
assertEquals("#dec#", formatter.format(formatter.getExampleInput()));
assertEquals("dec", formatter.format(formatter.getExampleInput()));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ public void testImportEntriesINSPEC() throws IOException, URISyntaxException {
first.getField(StandardField.AUTHOR));
assertEquals(Optional.of("Applied Physics Letters"), first.getField(StandardField.JOURNAL));
assertEquals(Optional.of("2006"), first.getField(StandardField.YEAR));
assertEquals(Optional.of("#jul#"), first.getField(StandardField.MONTH));
assertEquals(Optional.of("jul"), first.getField(StandardField.MONTH));
assertEquals(Optional.of("89"), first.getField(StandardField.VOLUME));
assertEquals(Optional.of("4"), first.getField(StandardField.NUMBER));
assertEquals(Optional.of("Lorem ipsum abstract"), first.getField(StandardField.ABSTRACT));
Expand Down Expand Up @@ -235,14 +235,14 @@ public void testIsiAuthorsConvert() {

@Test
public void testMonthConvert() {
assertEquals("#jun#", IsiImporter.parseMonth("06"));
assertEquals("#jun#", IsiImporter.parseMonth("JUN"));
assertEquals("#jun#", IsiImporter.parseMonth("jUn"));
assertEquals("#may#", IsiImporter.parseMonth("MAY-JUN"));
assertEquals("#jun#", IsiImporter.parseMonth("2006 06"));
assertEquals("#jun#", IsiImporter.parseMonth("2006 06-07"));
assertEquals("#jul#", IsiImporter.parseMonth("2006 07 03"));
assertEquals("#may#", IsiImporter.parseMonth("2006 May-Jun"));
assertEquals("jun", IsiImporter.parseMonth("06"));
assertEquals("jun", IsiImporter.parseMonth("JUN"));
assertEquals("jun", IsiImporter.parseMonth("jUn"));
assertEquals("may", IsiImporter.parseMonth("MAY-JUN"));
assertEquals("jun", IsiImporter.parseMonth("2006 06"));
assertEquals("jun", IsiImporter.parseMonth("2006 06-07"));
assertEquals("jul", IsiImporter.parseMonth("2006 07 03"));
assertEquals("may", IsiImporter.parseMonth("2006 May-Jun"));
}

@Test
Expand Down Expand Up @@ -334,7 +334,7 @@ public void testImportEntriesMedline() throws IOException, URISyntaxException {
"Joffe, Hadine and Hall, Janet E. and Gruber, Staci and Sarmiento, Ingrid A. and Cohen, Lee S. and Yurgelun-Todd, Deborah and Martin, Kathryn A."),
second.getField(StandardField.AUTHOR));
assertEquals(Optional.of("2006"), second.getField(StandardField.YEAR));
assertEquals(Optional.of("#may#"), second.getField(StandardField.MONTH));
assertEquals(Optional.of("may"), second.getField(StandardField.MONTH));
assertEquals(Optional.of("13"), second.getField(StandardField.VOLUME));
assertEquals(Optional.of("3"), second.getField(StandardField.NUMBER));
assertEquals(Optional.of("411--22"), second.getField(StandardField.PAGES));
Expand Down
6 changes: 3 additions & 3 deletions src/test/java/org/jabref/model/entry/BibEntryTests.java
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ public void getFieldOrAliasDateWithYearNumericalMonthString() {
@Test
public void getFieldOrAliasDateWithYearAbbreviatedMonth() {
emptyEntry.setField(StandardField.YEAR, "2003");
emptyEntry.setField(StandardField.MONTH, "#mar#");
emptyEntry.setField(StandardField.MONTH, "mar");
assertEquals(Optional.of("2003-03"), emptyEntry.getFieldOrAlias(StandardField.DATE));
}

Expand Down Expand Up @@ -161,13 +161,13 @@ public void getFieldOrAliasMonthWithDateYYYYReturnsNull() {
@Test
public void getFieldOrAliasMonthWithDateYYYYMM() {
emptyEntry.setField(StandardField.DATE, "2003-03");
assertEquals(Optional.of("#mar#"), emptyEntry.getFieldOrAlias(StandardField.MONTH));
assertEquals(Optional.of("mar"), emptyEntry.getFieldOrAlias(StandardField.MONTH));
}

@Test
public void getFieldOrAliasMonthWithDateYYYYMMDD() {
emptyEntry.setField(StandardField.DATE, "2003-03-30");
assertEquals(Optional.of("#mar#"), emptyEntry.getFieldOrAlias(StandardField.MONTH));
assertEquals(Optional.of("mar"), emptyEntry.getFieldOrAlias(StandardField.MONTH));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ @Article{Mustermann2016
journal = {Java Journal},
year = {2016},
pages = {2},
month = {February},
month = feb,
keywords = {java}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ @Article{
journal = {Java Journal},
year = {2016},
pages = {2},
month = {February},
month = feb,
keywords = {java}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ @Book{Mustermann2016
year = {2016},
author = {Max Mustermann},
volume = {1},
month = {February},
month = feb,
keywords = {java}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ @Booklet{Mustermann2016
title = {Java Booklet},
author = {Max Mustermann},
address = {Stuttgart},
month = {February},
month = feb,
year = {2016},
keywords = {java}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ @Conference{Mustermann2016
series = {1},
pages = {3-9},
address = {Stuttgart},
month = {February},
month = feb,
organization = {Java Org},
publisher = {Java Publisher},
keywords = {java}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ @InBook{Mustermann2016
year = {2016},
author = {Max Mustermann},
volume = {1},
month = {February},
month = feb,
keywords = {java}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ @InCollection{Mustermann2016
number = {1},
chapter = {3},
pages = {18-26},
month = {February},
month = feb,
keywords = {java}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ @InProceedings{Mustermann2016
title = {Java InProceedings},
booktitle = {Java Book},
year = {2016},
month = {February},
month = feb,
organization = {Java Org},
publisher = {Java Publisher},
keywords = {java}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ @InBook{Mustermann2016
series = {1},
address = {Stuttgart},
edition = {10},
month = {February},
month = feb,
note = {some note},
keywords = {java},
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ @Manual{Mustermann2016
author = {Max Mustermann},
organization = {Java Users},
address = {Stuttgart},
month = {February},
month = feb,
year = {2016},
keywords = {java}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ @MastersThesis{Mustermann2016
year = {2016},
type = {Thesis},
address = {Stuttgart},
month = {February},
month = feb,
keywords = {java}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ @Misc{Mustermann2016
author = {Max Mustermann},
title = {Java Misc},
howpublished = {Internet},
month = {February},
month = feb,
year = {2016},
keywords = {java}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@ @PhdThesis{Mustermann2016
year = {2016},
type = {Thesis},
address = {Stuttgart},
month = {February},
month = feb,
keywords = {java}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ @Proceedings{Musterfrau2016
editor = {Maxima Musterfrau},
address = {Stuttgart},
publisher = {Java Pub},
month = {February},
month = feb,
organization = {Java Org}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ @TechReport{Mustermann2016
type = {Report},
number = {1},
address = {Stuttgart},
month = {February},
month = feb,
keywords = {java},
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
@Unpublished{Mustermann2016,
author = {Max Mustermann},
title = {Java Unpublished},
month = {February},
month = feb,
year = {2016},
keywords = {java}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,6 @@ @Article{Mustermann2016
journal = {Java Journal},
year = {2016},
pages = {2},
month = {February},
month = feb,
keywords = {java}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ @article{Mustermann2016
author = {Max Mustermann},
journal = {Java Journal},
keywords = {java},
month = {February},
month = feb,
pages = {2},
title = {Java tricks},
year = {2016}
Expand Down
Loading