forked from JabRef/jabref
-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #14 from subhramit/olly-improvements
Refactor out class ReferenceMark
- Loading branch information
Showing
6 changed files
with
178 additions
and
78 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
90 changes: 90 additions & 0 deletions
90
src/main/java/org/jabref/logic/openoffice/ReferenceMark.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
package org.jabref.logic.openoffice; | ||
|
||
import java.util.Optional; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import org.jabref.logic.openoffice.oocsltext.CSLCitationOOAdapter; | ||
|
||
import io.github.thibaultmeyer.cuid.CUID; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* See {@link CSLCitationOOAdapter#PREFIXES} for the prefix(es) used to identify reference marks. | ||
*/ | ||
public class ReferenceMark { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(ReferenceMark.class); | ||
|
||
private static final Pattern REFERENCE_MARK_FORMAT = Pattern.compile("^JABREF_(\\w+) CID_(\\w+) (\\w+)$"); | ||
private final String name; | ||
|
||
private String citationKey; | ||
private Integer citationNumber; | ||
private String uniqueId; | ||
|
||
/** | ||
* @param name Format: <code>JABREF_{citationKey} CID_{citationNumber} {uniqueId}</code> | ||
*/ | ||
public ReferenceMark(String name) { | ||
this.name = name; | ||
|
||
Matcher matcher = getMatcher(name); | ||
if (!matcher.matches()) { | ||
LOGGER.warn("CSLReferenceMark: name={} does not match pattern. Assuming random values", name); | ||
this.citationKey = CUID.randomCUID2(8).toString(); | ||
this.citationNumber = 0; | ||
this.uniqueId = this.citationKey; | ||
return; | ||
} | ||
|
||
this.citationKey = matcher.group(1); | ||
this.citationNumber = Integer.parseInt(matcher.group(2)); | ||
this.uniqueId = matcher.group(3); | ||
|
||
LOGGER.debug("CSLReferenceMark: citationKey={} citationNumber={} uniqueId={}", getCitationKey(), getCitationNumber(), getUniqueId()); | ||
} | ||
|
||
public ReferenceMark(String name, String citationKey, Integer citationNumber, String uniqueId) { | ||
this.name = name; | ||
this.citationKey = citationKey; | ||
this.citationNumber = citationNumber; | ||
this.uniqueId = uniqueId; | ||
} | ||
|
||
private ReferenceMark(String name, String citationKey, String citationNumber, String uniqueId) { | ||
this(name, citationKey, Integer.parseInt(citationNumber), uniqueId); | ||
} | ||
|
||
private static Matcher getMatcher(String name) { | ||
return REFERENCE_MARK_FORMAT.matcher(name); | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
/** | ||
* The BibTeX citation key | ||
*/ | ||
public String getCitationKey() { | ||
return citationKey; | ||
} | ||
|
||
public Integer getCitationNumber() { | ||
return citationNumber; | ||
} | ||
|
||
public String getUniqueId() { | ||
return uniqueId; | ||
} | ||
|
||
public static Optional<ReferenceMark> of(String name) { | ||
Matcher matcher = getMatcher(name); | ||
if (!matcher.matches()) { | ||
return Optional.empty(); | ||
} | ||
|
||
return Optional.of(new ReferenceMark(name, matcher.group(1), matcher.group(2), matcher.group(3))); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
32 changes: 32 additions & 0 deletions
32
src/test/java/org/jabref/logic/openoffice/ReferenceMarkTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package org.jabref.logic.openoffice; | ||
|
||
import java.util.stream.Stream; | ||
|
||
import org.junit.jupiter.api.DisplayName; | ||
import org.junit.jupiter.params.ParameterizedTest; | ||
import org.junit.jupiter.params.provider.Arguments; | ||
import org.junit.jupiter.params.provider.MethodSource; | ||
|
||
import static org.junit.jupiter.api.Assertions.assertEquals; | ||
|
||
class ReferenceMarkTest { | ||
|
||
@ParameterizedTest | ||
@MethodSource | ||
@DisplayName("Test parsing of valid reference marks") | ||
void validParsing(String name, String expectedCitationKey, String expectedCitationNumber, String expectedUniqueId) { | ||
ReferenceMark referenceMark = new ReferenceMark(name); | ||
|
||
assertEquals(expectedCitationKey, referenceMark.getCitationKey()); | ||
assertEquals(expectedCitationNumber, referenceMark.getCitationNumber()); | ||
assertEquals(expectedUniqueId, referenceMark.getUniqueId()); | ||
} | ||
|
||
private static Stream<Arguments> validParsing() { | ||
return Stream.of( | ||
Arguments.of("JABREF_key1 CID_12345 uniqueId1", "key1", "12345", "uniqueId1"), | ||
Arguments.of("JABREF_key2 CID_67890 uniqueId2", "key2", "67890", "uniqueId2"), | ||
Arguments.of("JABREF_key3 CID_54321 uniqueId3", "key3", "54321", "uniqueId3") | ||
); | ||
} | ||
} |