-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
issue(8448): allow URL with + sign in entry tabs
* clean code * add java doc * test issue: #8448
- Loading branch information
1 parent
b33ebfb
commit 4423300
Showing
2 changed files
with
55 additions
and
12 deletions.
There are no files selected for viewing
59 changes: 47 additions & 12 deletions
59
src/main/java/org/jabref/logic/formatter/bibtexfields/CleanupUrlFormatter.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 |
---|---|---|
@@ -1,53 +1,88 @@ | ||
package org.jabref.logic.formatter.bibtexfields; | ||
|
||
import java.net.URLDecoder; | ||
import java.nio.charset.Charset; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.Objects; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
import org.jabref.logic.cleanup.Formatter; | ||
import org.jabref.logic.l10n.Localization; | ||
|
||
/** | ||
* Cleanup URL link | ||
* Cleanup URL link. | ||
* <p> | ||
* Expose string representations URL links clean up logic. | ||
* </p> | ||
*/ | ||
public class CleanupUrlFormatter extends Formatter { | ||
|
||
// This regexp find "url=" or "to=" parameter in full link and get text after them | ||
/**This regexp find "url=" or "to=" parameter in full link and get text after them*/ | ||
private static final Pattern PATTERN_URL = Pattern.compile("(?:url|to)=([^&]*)"); | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public String getName() { | ||
return Localization.lang("Cleanup URL link"); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public String getKey() { | ||
return "cleanup_url"; | ||
} | ||
|
||
/** | ||
* Escape and decodes a String from the application/x-www-form-urlencoded MIME format. | ||
* <p> | ||
* Method will also try to find a URL placed after "url=" or "to=". | ||
* <p> | ||
* The conversion process is the same as executed by {@link URLDecoder} to try to | ||
* take guarantees against code injections. | ||
* <p> | ||
* The plus sign is replaced by its correspondent code (%2b) to avoid the character | ||
* to be replaced by a space during the decoding execution. | ||
* | ||
* @param url should not be null | ||
* @return the decoded URL as a String representation | ||
* | ||
* @see URLDecoder#decode(String, Charset) | ||
*/ | ||
@Override | ||
public String format(String value) { | ||
String decodedLink = value; | ||
String toDecode = value; | ||
|
||
Matcher matcher = PATTERN_URL.matcher(value); | ||
public String format(String url) { | ||
var toDecode = Objects | ||
.requireNonNull(url, "Null url") | ||
.replaceAll("\\+", "%2b"); | ||
Matcher matcher = PATTERN_URL.matcher(toDecode); | ||
if (matcher.find()) { | ||
toDecode = matcher.group(1); | ||
return URLDecoder.decode(matcher.group(1), StandardCharsets.UTF_8); | ||
} | ||
return URLDecoder.decode(toDecode, StandardCharsets.UTF_8); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public String getDescription() { | ||
return Localization.lang("Cleanup URL link by removing special symbols and extracting simple link"); | ||
return Localization | ||
.lang("Cleanup URL link by removing special symbols and extracting simple link"); | ||
} | ||
|
||
/** | ||
* {@inheritDoc} | ||
*/ | ||
@Override | ||
public String getExampleInput() { | ||
return "https://www.google.de/url?sa=t&rct=j&q=&esrc=s&source=web&cd=11&cad=" + | ||
"rja&uact=8&ved=0ahUKEwjg3ZrB_ZPXAhVGuhoKHYdOBOg4ChAWCCYwAA&url=" + | ||
"http%3A%2F%2Fwww.focus.de%2Fgesundheit%2Fratgeber%2Fherz%2Ftest%2" + | ||
"Flebenserwartung-werden-sie-100-jahre-alt_aid_363828.html" + "&usg=AOvVaw1G6m2jf-pTHYkXceii4hXU"; | ||
"rja&uact=8&ved=0ahUKEwjg3ZrB_ZPXAhVGuhoKHYdOBOg4ChAWCCYwAA&url=" + | ||
"http%3A%2F%2Fwww.focus.de%2Fgesundheit%2Fratgeber%2Fherz%2Ftest%2" + | ||
"Flebenserwartung-werden-sie-100-jahre-alt_aid_363828.html" + | ||
"&usg=AOvVaw1G6m2jf-pTHYkXceii4hXU"; | ||
} | ||
} |
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