Skip to content

Commit

Permalink
Fix DBLPFetcherTest -- dblp changed the format (#5582)
Browse files Browse the repository at this point in the history
- Clean URLs in DBLPFetcher
- Style RemoveLatexCommandsFormatter
- Add tests for RemoveLatexCommandsFormatter
- Add support for removing single and multiple whitespaces after a command
- Split tests in RemoveBracketsTest
- Fix casing in CleanupUrlFormatter (to match style and current name of test class CleanUpFormatterTest)
- Fix casing in some ...Url.. method names
- Format CleanupUrlFormatter
- Improve code of ShortenDoiFormatter
- Reformat code and more speaking variable name
- Introduce LayoutFormatterBasedFormatter
  • Loading branch information
koppor authored Nov 10, 2019
1 parent 36d5f4b commit de4a1ae
Show file tree
Hide file tree
Showing 18 changed files with 227 additions and 104 deletions.
9 changes: 4 additions & 5 deletions src/main/java/org/jabref/gui/fieldeditors/UrlEditor.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import org.jabref.gui.DialogService;
import org.jabref.gui.autocompleter.AutoCompleteSuggestionProvider;
import org.jabref.gui.fieldeditors.contextmenu.EditorMenus;
import org.jabref.logic.formatter.bibtexfields.CleanupURLFormatter;
import org.jabref.logic.formatter.bibtexfields.CleanupUrlFormatter;
import org.jabref.logic.formatter.bibtexfields.TrimWhitespaceFormatter;
import org.jabref.logic.integrity.FieldCheckers;
import org.jabref.model.entry.BibEntry;
Expand All @@ -34,12 +34,11 @@ public UrlEditor(Field field, DialogService dialogService, AutoCompleteSuggestio
.load();

textArea.textProperty().bindBidirectional(viewModel.textProperty());
Supplier<List<MenuItem>> contextMenuSupplier = EditorMenus.getCleanupURLMenu(textArea);
Supplier<List<MenuItem>> contextMenuSupplier = EditorMenus.getCleanupUrlMenu(textArea);
textArea.addToContextMenu(contextMenuSupplier);

// init paste handler for URLEditor to format pasted url link in textArea
textArea.setPasteActionHandler(() -> textArea.setText(new CleanupURLFormatter().format(new TrimWhitespaceFormatter().format(textArea.getText()))));

// init paste handler for UrlEditor to format pasted url link in textArea
textArea.setPasteActionHandler(() -> textArea.setText(new CleanupUrlFormatter().format(new TrimWhitespaceFormatter().format(textArea.getText()))));

new EditorValidator(preferences).configureValidation(viewModel.getFieldValidator().getValidationStatus(), textArea);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import org.jabref.gui.actions.ActionFactory;
import org.jabref.gui.actions.StandardActions;
import org.jabref.gui.edit.CopyDoiUrlAction;
import org.jabref.logic.formatter.bibtexfields.CleanupURLFormatter;
import org.jabref.logic.formatter.bibtexfields.CleanupUrlFormatter;
import org.jabref.logic.formatter.bibtexfields.NormalizeNamesFormatter;
import org.jabref.logic.l10n.Localization;

Expand Down Expand Up @@ -91,11 +91,11 @@ public static Supplier<List<MenuItem>> getDOIMenu(TextArea textArea) {
* @param textArea text-area that this menu will be connected to
* @return menu containing items of the default menu and an item to cleanup a URL
*/
public static Supplier<List<MenuItem>> getCleanupURLMenu(TextArea textArea) {
public static Supplier<List<MenuItem>> getCleanupUrlMenu(TextArea textArea) {
return () -> {
CustomMenuItem cleanupURL = new CustomMenuItem(new Label(Localization.lang("Cleanup URL link")));
cleanupURL.setDisable(textArea.textProperty().isEmpty().get());
cleanupURL.setOnAction(event -> textArea.setText(new CleanupURLFormatter().format(textArea.getText())));
cleanupURL.setOnAction(event -> textArea.setText(new CleanupUrlFormatter().format(textArea.getText())));

List<MenuItem> menuItems = new ArrayList<>();
menuItems.add(cleanupURL);
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/jabref/logic/formatter/Formatters.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.util.Objects;
import java.util.Optional;

import org.jabref.logic.formatter.bibtexfields.CleanupURLFormatter;
import org.jabref.logic.formatter.bibtexfields.CleanupUrlFormatter;
import org.jabref.logic.formatter.bibtexfields.ClearFormatter;
import org.jabref.logic.formatter.bibtexfields.EscapeUnderscoresFormatter;
import org.jabref.logic.formatter.bibtexfields.HtmlToLatexFormatter;
Expand Down Expand Up @@ -58,7 +58,7 @@ public static List<Formatter> getCaseChangers() {
public static List<Formatter> getOthers() {
return Arrays.asList(
new ClearFormatter(),
new CleanupURLFormatter(),
new CleanupUrlFormatter(),
new LatexCleanupFormatter(),
new MinifyNameListFormatter(),
new NormalizeDateFormatter(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,9 @@
/**
* Cleanup URL link
*/
public class CleanupURLFormatter extends Formatter {
public class CleanupUrlFormatter extends Formatter {

private static final Log LOGGER = LogFactory.getLog(CleanupURLFormatter.class);
private static final Log LOGGER = LogFactory.getLog(CleanupUrlFormatter.class);
// 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)=([^&]*)");

Expand All @@ -38,15 +38,14 @@ public String format(String value) {

Matcher matcher = PATTERN_URL.matcher(value);
if (matcher.find()) {
toDecode = matcher.group(1);

toDecode = matcher.group(1);
}
try {
decodedLink = URLDecoder.decode(toDecode, StandardCharsets.UTF_8.name());
}
catch (UnsupportedEncodingException e) {
} catch (UnsupportedEncodingException e) {
LOGGER.warn("Used unsupported character encoding", e);
}

return decodedLink;
}

Expand All @@ -61,6 +60,5 @@ public String getExampleInput() {
"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";
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
import org.jabref.logic.l10n.Localization;
import org.jabref.model.cleanup.Formatter;

/**
* Simplifies LaTeX syntax. {@see org.jabref.logic.layout.format.RemoveLatexCommandsFormatter} for a formatter removing LaTeX commands completely.
*/
public class LatexCleanupFormatter extends Formatter {

private static final Pattern REMOVE_REDUNDANT = Pattern
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.jabref.logic.formatter.bibtexfields;

import java.util.Objects;
import java.util.Optional;

import org.jabref.logic.importer.util.ShortDOIService;
import org.jabref.logic.importer.util.ShortDOIServiceException;
Expand Down Expand Up @@ -29,22 +28,15 @@ public String getKey() {
@Override
public String format(String value) {
Objects.requireNonNull(value);

ShortDOIService shortDOIService = new ShortDOIService();

Optional<DOI> doi = Optional.empty();

try {
doi = DOI.parse(value);

if (doi.isPresent()) {
return shortDOIService.getShortDOI(doi.get()).getDOI();
}
} catch (ShortDOIServiceException e) {
LOGGER.error(e.getMessage(), e);
}

return value;
return DOI.parse(value)
.map(doi -> {
try {
return new ShortDOIService().getShortDOI(doi).getDOI();
} catch (ShortDOIServiceException e) {
LOGGER.error(e.getMessage(), e);
return value;
}
}).orElse(value);
}

@Override
Expand Down
18 changes: 12 additions & 6 deletions src/main/java/org/jabref/logic/importer/fetcher/DBLPFetcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.List;
import java.util.Objects;
import java.util.Optional;

Expand All @@ -14,9 +15,13 @@
import org.jabref.logic.importer.Parser;
import org.jabref.logic.importer.SearchBasedParserFetcher;
import org.jabref.logic.importer.fileformat.BibtexParser;
import org.jabref.logic.layout.LayoutFormatterBasedFormatter;
import org.jabref.logic.layout.format.RemoveLatexCommandsFormatter;
import org.jabref.model.cleanup.FieldFormatterCleanup;
import org.jabref.model.cleanup.FieldFormatterCleanups;
import org.jabref.model.entry.BibEntry;
import org.jabref.model.entry.field.InternalField;
import org.jabref.model.entry.field.StandardField;
import org.jabref.model.util.DummyFileUpdateMonitor;

import org.apache.http.client.utils.URIBuilder;
Expand Down Expand Up @@ -57,13 +62,15 @@ public Parser getParser() {
@Override
public void doPostCleanup(BibEntry entry) {
DoiCleanup doiCleaner = new DoiCleanup();

FieldFormatterCleanup clearTimestampFormatter = new FieldFormatterCleanup(InternalField.TIMESTAMP,
new ClearFormatter());

doiCleaner.cleanup(entry);
clearTimestampFormatter.cleanup(entry);

FieldFormatterCleanups cleanups = new FieldFormatterCleanups(true,
List.of(
new FieldFormatterCleanup(InternalField.TIMESTAMP, new ClearFormatter()),
// unescape the the contents of the URL field, e.g., some\_url\_part becomes some_url_part
new FieldFormatterCleanup(StandardField.URL, new LayoutFormatterBasedFormatter(new RemoveLatexCommandsFormatter()))
));
cleanups.applySaveActions(entry);
}

@Override
Expand All @@ -75,5 +82,4 @@ public String getName() {
public Optional<HelpFile> getHelpPage() {
return Optional.of(HelpFile.FETCHER_DBLP);
}

}
3 changes: 0 additions & 3 deletions src/main/java/org/jabref/logic/layout/Layout.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,6 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Main class for formatting DOCUMENT ME!
*/
public class Layout {

private static final Logger LOGGER = LoggerFactory.getLogger(Layout.class);
Expand Down
20 changes: 8 additions & 12 deletions src/main/java/org/jabref/logic/layout/LayoutFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,25 @@

/**
* The LayoutFormatter is used for a Filter design-pattern.
*
* <p>
* Implementing classes have to accept a String and returned a formatted version of it.
*
* <p>
* Example:
*
* "John von Neumann" => "von Neumann, John"
*
* @version 1.2 - Documentation CO
* <p>
* "John von Neumann" => "von Neumann, John"
*/
@FunctionalInterface
public interface LayoutFormatter {

/**
* Failure Mode:
* <p>
* Formatters should be robust in the sense that they always return some
* relevant string.
* Formatters should be robust in the sense that they always return some relevant string.
* <p>
* If the formatter can detect an invalid input it should return the
* original string otherwise it may simply return a wrong output.
* If the formatter can detect an invalid input it should return the original string otherwise it may simply return
* a wrong output.
*
* @param fieldText
* The text to layout.
* @param fieldText The text to layout.
* @return The layouted text.
*/
String format(String fieldText);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package org.jabref.logic.layout;

import org.jabref.model.cleanup.Formatter;

/**
* When having to use a LayoutFormatter as Formatter, this class is helpful. One usecase is {@link org.jabref.model.cleanup.FieldFormatterCleanup}
*/
public class LayoutFormatterBasedFormatter extends Formatter {

private final LayoutFormatter layoutFormatter;

public LayoutFormatterBasedFormatter(LayoutFormatter layoutFormatter) {
this.layoutFormatter = layoutFormatter;
}

@Override
public String getName() {
return layoutFormatter.getClass().getName();
}

@Override
public String getKey() {
return layoutFormatter.getClass().getName();
}

@Override
public String format(String value) {
return layoutFormatter.format(value);
}

@Override
public String getDescription() {
return layoutFormatter.getClass().getName();
}

@Override
public String getExampleInput() {
return layoutFormatter.getClass().getName();
}
}
20 changes: 7 additions & 13 deletions src/main/java/org/jabref/logic/layout/LayoutHelper.java
Original file line number Diff line number Diff line change
Expand Up @@ -67,27 +67,24 @@ public static void setCurrentGroup(String newGroup) {

private void doBracketedField(final int field) throws IOException {
StringBuilder buffer = null;
int c;
int currentCharacter;
boolean start = false;

while (!endOfFile) {
c = read();
currentCharacter = read();

if (c == -1) {
if (currentCharacter == -1) {
endOfFile = true;

if (buffer != null) {
parsedEntries.add(new StringInt(buffer.toString(), field));
}

return;
}

if ((c == '{') || (c == '}')) {
if (c == '}') {
if ((currentCharacter == '{') || (currentCharacter == '}')) {
if (currentCharacter == '}') {
if (buffer != null) {
parsedEntries.add(new StringInt(buffer.toString(), field));

return;
}
} else {
Expand All @@ -98,16 +95,13 @@ private void doBracketedField(final int field) throws IOException {
buffer = new StringBuilder(100);
}

if (start && (c != '}')) {
buffer.append((char) c);
if (start && (currentCharacter != '}')) {
buffer.append((char) currentCharacter);
}
}
}
}

/**
*
*/
private void doBracketedOptionField() throws IOException {
StringBuilder buffer = null;
int c;
Expand Down
Loading

0 comments on commit de4a1ae

Please sign in to comment.