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

Support RFC links in Quick Import functionality (#11949) #12043

Merged
merged 19 commits into from
Oct 23, 2024
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
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
10 changes: 9 additions & 1 deletion src/main/java/org/jabref/logic/importer/CompositeIdFetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@

import org.jabref.logic.importer.fetcher.ArXivFetcher;
import org.jabref.logic.importer.fetcher.DoiFetcher;
import org.jabref.logic.importer.fetcher.RfcFetcher;
import org.jabref.logic.importer.fetcher.isbntobibtex.IsbnFetcher;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.identifier.ArXivIdentifier;
import org.jabref.model.entry.identifier.DOI;
import org.jabref.model.entry.identifier.ISBN;
import org.jabref.model.entry.identifier.SSRN;
import org.jabref.model.entry.identifier.RFC;

public class CompositeIdFetcher {

Expand Down Expand Up @@ -49,6 +51,11 @@ public Optional<BibEntry> performSearchById(String identifier) throws FetcherExc
return new DoiFetcher(importFormatPreferences).performSearchById(ssrn.get().toDoi().getNormalized());
}

Optional<RFC> rfcId = RFC.parse(identifier);
if (rfcId.isPresent()) {
return new RfcFetcher(importFormatPreferences).performSearchById(rfcId.get().getNormalized());
}

return Optional.empty();
}

Expand All @@ -61,7 +68,8 @@ public static boolean containsValidId(String identifier) {
Optional<ArXivIdentifier> arXivIdentifier = ArXivIdentifier.parse(identifier);
Optional<ISBN> isbn = ISBN.parse(identifier);
Optional<SSRN> ssrn = SSRN.parse(identifier);
Optional<RFC> rfcId = RFC.parse(identifier);

return Stream.of(doi, arXivIdentifier, isbn, ssrn).anyMatch(Optional::isPresent);
return Stream.of(doi, arXivIdentifier, isbn, ssrn, rfcId).anyMatch(Optional::isPresent);
}
}
68 changes: 68 additions & 0 deletions src/main/java/org/jabref/model/entry/identifier/RFC.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
package org.jabref.model.entry.identifier;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Objects;
import java.util.Optional;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Represents an RFC identifier, which can be used to fetch bibliographic information about the RFC document.
* This class supports both plain RFC IDs (e.g., "rfc7276") and full URLs (e.g., "https://www.rfc-editor.org/rfc/" + rfc****.html).
*/
public class RFC extends EprintIdentifier {
private static final String RFC_URL_REGEX = "(rfc\\d+)|(https?://)?(www\\.)?rfc-editor\\.org/rfc/rfc(?<id>\\d+)(\\.html)?.*";
private static final Pattern RFC_URL_MATCH = Pattern.compile(RFC_URL_REGEX, Pattern.CASE_INSENSITIVE);
private final String rfcString;

/**
* Constructs an RFC object from a given string.
*
* @param rfcString The RFC ID or URL, which will be converted to lowercase and trimmed.
* @throws NullPointerException if the given rfcString is null.
*/
public RFC(String rfcString) {
this.rfcString = Objects.requireNonNull(rfcString).trim().toLowerCase();
}

/**
* Parses the given input string to extract a valid RFC identifier.
* The input can be a plain RFC ID or a full URL.
*
* @param input The input string, which could be in the form of an RFC ID (e.g., "rfc1234") or a complete URL.
* @return An Optional containing a valid RFC if the input matches the expected pattern, otherwise an empty Optional.
*/
public static Optional<RFC> parse(String input) {
Matcher matcher = RFC_URL_MATCH.matcher(input);
if (matcher.matches()) {
String rfcId;
if (input.startsWith("rfc")) {
rfcId = input;
} else {
rfcId = "rfc" + matcher.group("id");
}
return Optional.of(new RFC(rfcId));
}
return Optional.empty();
}

@Override
public String getNormalized() {
return rfcString;
}

/**
* Generates an external URI that points to the RFC document on the rfc-editor website.
*
* @return An Optional containing the URI if the construction succeeds, otherwise an empty Optional.
*/
@Override
public Optional<URI> getExternalURI() {
try {
return Optional.of(new URI("https://www.rfc-editor.org/rfc/" + rfcString));
} catch (URISyntaxException e) {
return Optional.empty();
}
}
}
36 changes: 36 additions & 0 deletions src/test/java/org/jabref/model/entry/identifier/RFCTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package org.jabref.model.entry.identifier;

import static org.junit.jupiter.api.Assertions.*;

import java.net.URI;
import java.util.Optional;

import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.CsvSource;
import org.junit.jupiter.api.Test;

class RFCTest {

@ParameterizedTest
@CsvSource({
"rfc7276, rfc7276",
"https://www.rfc-editor.org/rfc/rfc7276.html, rfc7276"
})
void testParseValidRfcIdAndUrl(String input, String expected) {
Copy link
Member

Choose a reason for hiding this comment

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

Please be consistent.

assertEquals first gets expected, thus please, first expected then input

I fix it for myself. Too much context switches.

Optional<RFC> rfc = RFC.parse(input);
assertTrue(rfc.isPresent());
assertEquals(expected, rfc.get().getNormalized()); // Use asString() method
}

@Test
void testInvalidRfc() {
Optional<RFC> rfc = RFC.parse("invalidRfc");
assertEquals(Optional.empty(), rfc); // Replace assertFalse with assertEquals
}

@Test
void testGetExternalUri() {
RFC rfc = new RFC("rfc7276");
assertEquals(Optional.of(URI.create("https://www.rfc-editor.org/rfc/rfc7276")), rfc.getExternalURI()); // Use Optional.of for expected value
}
}
Loading