-
-
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
Support RFC links in Quick Import functionality (#11949) #12043
Merged
koppor
merged 19 commits into
JabRef:main
from
AU0430:Issue11949-quick-import-for-RFC-link
Oct 23, 2024
Merged
Changes from 11 commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
1672342
Add RfcId to handle RFC url link to support quick import
ChengPuPu 365e50a
Refactor RFC URL parsing
ChengPuPu f24ec13
Refactor RFC URL parsing
ChengPuPu 401f8ce
Adjust code style
ChengPuPu ddadb92
Adjust code style
ChengPuPu 787dc35
Merge branch 'main' into Issue11949-quick-import-for-RFC-link
AU0430 11a57f1
According to CI test feedback:
ChengPuPu 0649019
Merge remote-tracking branch 'origin/Issue11949-quick-import-for-RFC-…
ChengPuPu bc78c9c
Accorfing to maintainer feedback to fix:
ChengPuPu 2c16f73
Refactored RFCTest to adhere to the suggestions provided:
ChengPuPu 74ed902
Merge branch 'main' into Issue11949-quick-import-for-RFC-link
AU0430 d7300a4
adjust code format according to CI test(checkstyle)
ChengPuPu 846e64b
Merge branch 'main' into Issue11949-quick-import-for-RFC-link
AU0430 90d769c
Merge remote-tracking branch 'origin/main' into Issue11949-quick-impo…
koppor 03f7ea0
Merge remote-tracking branch 'origin/main' into Issue11949-quick-impo…
koppor e0abc10
Swap expected and input
koppor 29a3e5e
Fix OpenRewrite issues
koppor 891e1f5
fix code style according to checkstyle
ChengPuPu 98e4414
Merge remote-tracking branch 'origin/Issue11949-quick-import-for-RFC-…
ChengPuPu File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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
36
src/test/java/org/jabref/model/entry/identifier/RFCTest.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,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) { | ||
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 | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Please be consistent.
assertEquals first gets expected, thus please, first
expected
theninput
I fix it for myself. Too much context switches.