-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
Add parser for German months (#3536) #3734
Changes from 10 commits
19223c8
a1daf61
94fb247
b5dabe7
ef98eb9
87b6273
5b8f3f7
bf88e94
7cf796c
7c585ba
d84ec09
52b153c
5d56874
73a7ee7
0124fe1
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,5 +1,9 @@ | ||
package org.jabref.model.entry; | ||
|
||
import java.time.YearMonth; | ||
import java.time.format.DateTimeFormatter; | ||
import java.time.format.DateTimeParseException; | ||
import java.util.Locale; | ||
import java.util.Optional; | ||
|
||
import org.jabref.model.strings.StringUtil; | ||
|
@@ -85,9 +89,13 @@ public static Optional<Month> parse(String value) { | |
testString = testString.substring(0, 3); | ||
} | ||
Optional<Month> month = Month.getMonthByShortName(testString); | ||
Optional<Month> monthGerman = Month.parseGermanShortMonth(testString); | ||
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. Please move that after line 95. the German month only has to be determined if there is no English month. 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. Done. |
||
if (month.isPresent()) { | ||
return month; | ||
} | ||
if (monthGerman.isPresent()) { | ||
return monthGerman; | ||
} | ||
|
||
try { | ||
int number = Integer.parseInt(value); | ||
|
@@ -97,6 +105,29 @@ public static Optional<Month> parse(String value) { | |
} | ||
} | ||
|
||
/** | ||
* Parses a month having the string in German standard form such as | ||
* "Oktober" or in German short form such as "Okt" | ||
* | ||
* @param value, | ||
* a String that represents a month in German form | ||
* @return the corresponding month instance, empty if input is not in German | ||
* form | ||
*/ | ||
private static Optional<Month> parseGermanShortMonth(String value) { | ||
if ("Mae".equals(value)) { | ||
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. What if it is "mae" in lowercase? 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. The implementation should use 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. Please fix indent. |
||
return Month.getMonthByNumber(3); | ||
} | ||
|
||
try { | ||
YearMonth yearMonth = YearMonth.parse("1969-" + value, | ||
DateTimeFormatter.ofPattern("yyyy-MMM", Locale.GERMAN)); | ||
return Month.getMonthByNumber(yearMonth.getMonthValue()); | ||
} catch (DateTimeParseException e) { | ||
return Optional.empty(); | ||
} | ||
} | ||
|
||
/** | ||
* Returns the name of a Month in a short (3-letter) format. (jan, feb, mar, ...) | ||
* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -100,4 +100,38 @@ public void parseReturnsEmptyOptionalForInvalidInput() { | |
public void parseReturnsEmptyOptionalForEmptyInput() { | ||
assertEquals(Optional.empty(), Month.parse("")); | ||
} | ||
|
||
@Test | ||
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. I adjusted it. |
||
public void parseCorrectlyByShortNameGerman() { | ||
assertEquals(Optional.of(Month.JANUARY), Month.parse("Jan")); | ||
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. Also add tests for lowercase? 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. The German short forms are in uppercase. So what should be tested? 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. Well, then just copy the test cases to a new method and then just call month. Parse with the lowercase name 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. Tests were added. |
||
assertEquals(Optional.of(Month.FEBRUARY), Month.parse("Feb")); | ||
assertEquals(Optional.of(Month.MARCH), Month.parse("M�r")); | ||
assertEquals(Optional.of(Month.MARCH), Month.parse("Mae")); | ||
assertEquals(Optional.of(Month.APRIL), Month.parse("Apr")); | ||
assertEquals(Optional.of(Month.MAY), Month.parse("Mai")); | ||
assertEquals(Optional.of(Month.JUNE), Month.parse("Jun")); | ||
assertEquals(Optional.of(Month.JULY), Month.parse("Jul")); | ||
assertEquals(Optional.of(Month.AUGUST), Month.parse("Aug")); | ||
assertEquals(Optional.of(Month.SEPTEMBER), Month.parse("Sep")); | ||
assertEquals(Optional.of(Month.OCTOBER), Month.parse("Okt")); | ||
assertEquals(Optional.of(Month.NOVEMBER), Month.parse("Nov")); | ||
assertEquals(Optional.of(Month.DECEMBER), Month.parse("Dez")); | ||
} | ||
|
||
@Test | ||
public void parseCorrectlyByFullNameGerman() { | ||
assertEquals(Optional.of(Month.JANUARY), Month.parse("Januar")); | ||
assertEquals(Optional.of(Month.FEBRUARY), Month.parse("Februar")); | ||
assertEquals(Optional.of(Month.MARCH), Month.parse("M�rz")); | ||
assertEquals(Optional.of(Month.MARCH), Month.parse("Maerz")); | ||
assertEquals(Optional.of(Month.APRIL), Month.parse("April")); | ||
assertEquals(Optional.of(Month.MAY), Month.parse("Mai")); | ||
assertEquals(Optional.of(Month.JUNE), Month.parse("Juni")); | ||
assertEquals(Optional.of(Month.JULY), Month.parse("Juli")); | ||
assertEquals(Optional.of(Month.AUGUST), Month.parse("August")); | ||
assertEquals(Optional.of(Month.SEPTEMBER), Month.parse("September")); | ||
assertEquals(Optional.of(Month.OCTOBER), Month.parse("Oktober")); | ||
assertEquals(Optional.of(Month.NOVEMBER), Month.parse("November")); | ||
assertEquals(Optional.of(Month.DECEMBER), Month.parse("Dezember")); | ||
} | ||
} |
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.
The indent is wrong here. I have no clue, what is going wrong on your side. Do you use tabs instead of spaces?
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.
I don't know what went wrong. I used the auto formatter of Eclipse, so it could be the tabs. Now it looks better after replacing the tabs with spaces by hand. I should definetly change the IDE if this is a common problem.