Skip to content

Commit fbbb325

Browse files
add new formatters and cayw resource tests
1 parent 225bba6 commit fbbb325

File tree

13 files changed

+341
-31
lines changed

13 files changed

+341
-31
lines changed

jabsrv/src/main/java/org/jabref/http/server/cayw/CAYWQueryParams.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ public class CAYWQueryParams {
2222
private String clipboard;
2323

2424
@QueryParam("command")
25-
@DefaultValue("autocite")
2625
private String command;
2726

2827
@QueryParam("minimize")
@@ -46,8 +45,8 @@ public class CAYWQueryParams {
4645
@QueryParam("application")
4746
private String application;
4847

49-
public String getCommand() {
50-
return command;
48+
public Optional<String> getCommand() {
49+
return Optional.ofNullable(command);
5150
}
5251

5352
public boolean isClipboard() {

jabsrv/src/main/java/org/jabref/http/server/cayw/CAYWResource.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ public Response getCitation(
7474
if (queryParams.isProbe()) {
7575
return Response.ok("ready").build();
7676
}
77-
77+
7878
BibDatabaseContext databaseContext = getBibDatabaseContext(queryParams);
7979

8080
// Selected parameter handling
@@ -127,7 +127,7 @@ public Response getCitation(
127127

128128
// Push to Application parameter handling
129129
if (queryParams.getApplication().isPresent()) {
130-
CitationCommandString citationCmd = new CitationCommandString("\\".concat(queryParams.getCommand()).concat("{"), ",", "}");
130+
CitationCommandString citationCmd = new CitationCommandString("\\".concat(queryParams.getCommand().orElse("autocite")).concat("{"), ",", "}");
131131
PushToApplications.getApplication(queryParams.getApplication().get(), LOGGER::info, preferences.getPushToApplicationPreferences().withCitationCommand(citationCmd))
132132
.ifPresent(application -> application.pushEntries(searchResults.stream().map(CAYWEntry::bibEntry).toList()));
133133
}

jabsrv/src/main/java/org/jabref/http/server/cayw/format/BibLatexFormatter.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package org.jabref.http.server.cayw.format;
22

33
import java.util.List;
4+
import java.util.Optional;
45
import java.util.stream.Collectors;
56

67
import org.jabref.http.server.cayw.CAYWQueryParams;
@@ -13,9 +14,11 @@
1314
@Service
1415
public class BibLatexFormatter implements CAYWFormatter {
1516

16-
@Override
17-
public String getFormatName() {
18-
return "biblatex";
17+
private final String defaultCommand;
18+
19+
public BibLatexFormatter(String defaultCommand) {
20+
super();
21+
this.defaultCommand = defaultCommand;
1922
}
2023

2124
@Override
@@ -25,15 +28,16 @@ public MediaType getMediaType() {
2528

2629
@Override
2730
public String format(CAYWQueryParams queryParams, List<CAYWEntry> caywEntries) {
28-
String command = queryParams.getCommand();
31+
String command = queryParams.getCommand().orElse(defaultCommand);
2932

3033
List<BibEntry> bibEntries = caywEntries.stream()
3134
.map(CAYWEntry::bibEntry)
3235
.toList();
3336

3437
return "\\%s{%s}".formatted(command,
3538
bibEntries.stream()
36-
.map(entry -> entry.getCitationKey().orElse(""))
39+
.map(BibEntry::getCitationKey)
40+
.flatMap(Optional::stream)
3741
.collect(Collectors.joining(",")));
3842
}
3943
}

jabsrv/src/main/java/org/jabref/http/server/cayw/format/CAYWFormatter.java

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,6 @@
99

1010
public interface CAYWFormatter {
1111

12-
String getFormatName();
13-
1412
MediaType getMediaType();
1513

1614
String format(CAYWQueryParams caywQueryParams, List<CAYWEntry> caywEntries);
Lines changed: 24 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.jabref.http.server.cayw.format;
22

3-
import java.util.HashMap;
4-
import java.util.Map;
3+
import java.util.Locale;
54

65
import org.jabref.http.server.cayw.CAYWQueryParams;
76

@@ -10,20 +9,32 @@
109
@Service
1110
public class FormatterService {
1211

13-
private static final String DEFAULT_FORMATTER = "biblatex";
14-
private final Map<String, CAYWFormatter> formatters;
15-
1612
public FormatterService() {
17-
this.formatters = new HashMap<>();
18-
registerFormatter(new SimpleJsonFormatter());
19-
registerFormatter(new BibLatexFormatter());
20-
}
21-
22-
public void registerFormatter(CAYWFormatter formatter) {
23-
formatters.putIfAbsent(formatter.getFormatName(), formatter);
2413
}
2514

2615
public CAYWFormatter getFormatter(CAYWQueryParams queryParams) {
27-
return formatters.getOrDefault(queryParams.getFormat().toLowerCase(), formatters.get(DEFAULT_FORMATTER));
16+
String format = queryParams.getFormat().toLowerCase(Locale.ROOT);
17+
18+
switch (format) {
19+
case "natbib":
20+
case "latex":
21+
case "cite":
22+
return new NatbibFormatter("cite");
23+
case "citep":
24+
return new NatbibFormatter("citep");
25+
case "citet":
26+
return new NatbibFormatter("citet");
27+
case "mmd":
28+
return new MMDFormatter();
29+
case "pandoc":
30+
return new PandocFormatter();
31+
case "simple-json":
32+
return new SimpleJsonFormatter();
33+
case "typst":
34+
return new TypstFormatter();
35+
case "biblatex":
36+
default:
37+
return new BibLatexFormatter("autocite");
38+
}
2839
}
2940
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.jabref.http.server.cayw.format;
2+
3+
import java.util.List;
4+
import java.util.Optional;
5+
import java.util.stream.Collectors;
6+
7+
import org.jabref.http.server.cayw.CAYWQueryParams;
8+
import org.jabref.http.server.cayw.gui.CAYWEntry;
9+
import org.jabref.model.entry.BibEntry;
10+
11+
import jakarta.ws.rs.core.MediaType;
12+
13+
public class MMDFormatter implements CAYWFormatter {
14+
15+
@Override
16+
public MediaType getMediaType() {
17+
return MediaType.TEXT_PLAIN_TYPE;
18+
}
19+
20+
@Override
21+
public String format(CAYWQueryParams queryParams, List<CAYWEntry> caywEntries) {
22+
List<BibEntry> bibEntries = caywEntries.stream()
23+
.map(CAYWEntry::bibEntry)
24+
.toList();
25+
26+
return bibEntries.stream()
27+
.map(entry -> entry.getCitationKey().map("[#%s][]"::formatted))
28+
.flatMap(Optional::stream)
29+
.collect(Collectors.joining(""));
30+
}
31+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package org.jabref.http.server.cayw.format;
2+
3+
import java.util.List;
4+
import java.util.Optional;
5+
import java.util.stream.Collectors;
6+
7+
import org.jabref.http.server.cayw.CAYWQueryParams;
8+
import org.jabref.http.server.cayw.gui.CAYWEntry;
9+
import org.jabref.model.entry.BibEntry;
10+
11+
import jakarta.ws.rs.core.MediaType;
12+
import org.jvnet.hk2.annotations.Service;
13+
14+
@Service
15+
public class NatbibFormatter implements CAYWFormatter {
16+
17+
private final String defaultCommand;
18+
19+
public NatbibFormatter(String defaultCommand) {
20+
this.defaultCommand = defaultCommand;
21+
}
22+
23+
@Override
24+
public MediaType getMediaType() {
25+
return MediaType.TEXT_PLAIN_TYPE;
26+
}
27+
28+
@Override
29+
public String format(CAYWQueryParams queryParams, List<CAYWEntry> caywEntries) {
30+
String command = queryParams.getCommand().orElse(defaultCommand);
31+
32+
List<BibEntry> bibEntries = caywEntries.stream()
33+
.map(CAYWEntry::bibEntry)
34+
.toList();
35+
36+
return "\\%s{%s}".formatted(command,
37+
bibEntries.stream()
38+
.map(BibEntry::getCitationKey)
39+
.flatMap(Optional::stream)
40+
.collect(Collectors.joining(",")));
41+
}
42+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.jabref.http.server.cayw.format;
2+
3+
import java.util.List;
4+
import java.util.Optional;
5+
import java.util.stream.Collectors;
6+
7+
import org.jabref.http.server.cayw.CAYWQueryParams;
8+
import org.jabref.http.server.cayw.gui.CAYWEntry;
9+
import org.jabref.model.entry.BibEntry;
10+
11+
import jakarta.ws.rs.core.MediaType;
12+
13+
public class PandocFormatter implements CAYWFormatter {
14+
15+
@Override
16+
public MediaType getMediaType() {
17+
return MediaType.TEXT_PLAIN_TYPE;
18+
}
19+
20+
@Override
21+
public String format(CAYWQueryParams queryParams, List<CAYWEntry> caywEntries) {
22+
List<BibEntry> bibEntries = caywEntries.stream()
23+
.map(CAYWEntry::bibEntry)
24+
.toList();
25+
26+
return "[%s]".formatted(bibEntries.stream()
27+
.map(entry -> entry.getCitationKey().map("@%s"::formatted))
28+
.flatMap(Optional::stream)
29+
.collect(Collectors.joining("; ")));
30+
}
31+
}

jabsrv/src/main/java/org/jabref/http/server/cayw/format/SimpleJsonFormatter.java

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,6 @@ public SimpleJsonFormatter() {
2020
this.gson = new GsonFactory().provide();
2121
}
2222

23-
@Override
24-
public String getFormatName() {
25-
return "simple-json";
26-
}
27-
2823
@Override
2924
public MediaType getMediaType() {
3025
return MediaType.APPLICATION_JSON_TYPE;
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package org.jabref.http.server.cayw.format;
2+
3+
import java.util.List;
4+
import java.util.Optional;
5+
import java.util.stream.Collectors;
6+
7+
import org.jabref.http.server.cayw.CAYWQueryParams;
8+
import org.jabref.http.server.cayw.gui.CAYWEntry;
9+
import org.jabref.model.entry.BibEntry;
10+
11+
import jakarta.ws.rs.core.MediaType;
12+
13+
public class TypstFormatter implements CAYWFormatter {
14+
15+
@Override
16+
public MediaType getMediaType() {
17+
return MediaType.TEXT_PLAIN_TYPE;
18+
}
19+
20+
@Override
21+
public String format(CAYWQueryParams queryParams, List<CAYWEntry> caywEntries) {
22+
List<BibEntry> bibEntries = caywEntries.stream()
23+
.map(CAYWEntry::bibEntry)
24+
.toList();
25+
26+
return bibEntries.stream()
27+
.map(entry -> entry.getCitationKey().map("@%s"::formatted))
28+
.flatMap(Optional::stream)
29+
.collect(Collectors.joining(" "));
30+
}
31+
}

0 commit comments

Comments
 (0)