Skip to content

Commit 7728417

Browse files
committed
Add unversioned files
Signed-off-by: Dominik Voigt <[email protected]>
1 parent 554f2f6 commit 7728417

File tree

6 files changed

+283
-0
lines changed

6 files changed

+283
-0
lines changed
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
# Use Jackson to parse study.yml
2+
3+
## Context and Problem Statement
4+
5+
The study definition file is formulated as a YAML document.
6+
To accessed the definition within JabRef this document has to be parsed.
7+
What parser should be used to parse YAML files?
8+
9+
## Considered Options
10+
11+
* Jackson
12+
* SnakeYAML Engine
13+
* yamlbeans
14+
* eo-yaml
15+
* Own parser
16+
17+
## Decision Outcome
18+
19+
Chosen option: Jackson, because as it is a dedicated library for parsing YAML. yamlbeans also seem to be viable. They all offer similar functionality
20+
21+
## Pros and Cons of the Options
22+
23+
### Jackson
24+
25+
* Good, because established YAML parser library
26+
* Good, because supports YAML 1.2
27+
* Good, because it can parse LocalDate
28+
29+
### SnakeYAML Engine
30+
31+
* Good, because established YAML parser library
32+
* Good, because supports YAML 1.2
33+
* Bad, because cannot parse YAML into Java DTOs
34+
35+
### yamlbeans
36+
37+
* Good, because established YAML parser library
38+
* Good, because [nice getting started page](https://github.com/EsotericSoftware/yamlbeans)
39+
* Bad, because objects need to be annotated in the yaml file to be parsed into Java objects
40+
41+
### eo-yaml
42+
43+
* Good, because established YAML parser library
44+
* Good, because supports YAML 1.2
45+
* Bad, because cannot parse YAML into Java DTOs?
46+
47+
### Own parser
48+
49+
* Good, because easily customizable
50+
* Bad, because high effort
51+
* Bad, because has to be tested extensively
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package org.jabref.logic.crawler;
2+
3+
import java.io.FileInputStream;
4+
import java.io.IOException;
5+
import java.io.InputStream;
6+
import java.nio.file.Path;
7+
8+
import org.jabref.model.study.Study;
9+
10+
import com.fasterxml.jackson.databind.ObjectMapper;
11+
import com.fasterxml.jackson.databind.SerializationFeature;
12+
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
13+
import com.fasterxml.jackson.dataformat.yaml.YAMLGenerator;
14+
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
15+
16+
public class StudyYAMLParser {
17+
18+
/**
19+
* Parses the given yaml study definition file into a study instance
20+
*/
21+
public Study parseStudyYAMLFile(Path studyYAMLFile) throws IOException {
22+
ObjectMapper yamlMapper = new ObjectMapper(new YAMLFactory());
23+
yamlMapper.registerModule(new JavaTimeModule());
24+
try (InputStream fileInputStream = new FileInputStream(studyYAMLFile.toFile())) {
25+
return yamlMapper.readValue(fileInputStream, Study.class);
26+
}
27+
}
28+
29+
/**
30+
* Writes the given study instance into a yaml file to the given path
31+
*/
32+
public void writeStudyYAMLFile(Study study, Path studyYAMLFile) throws IOException {
33+
ObjectMapper yamlMapper = new ObjectMapper(new YAMLFactory().disable(YAMLGenerator.Feature.WRITE_DOC_START_MARKER).enable(YAMLGenerator.Feature.MINIMIZE_QUOTES));
34+
yamlMapper.registerModule(new JavaTimeModule());
35+
yamlMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
36+
yamlMapper.writeValue(studyYAMLFile.toFile(), study);
37+
}
38+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package org.jabref.model.study;
2+
3+
public class LibraryEntry {
4+
private String name;
5+
private boolean enabled;
6+
7+
public LibraryEntry(String name, boolean enabled) {
8+
this.name = name;
9+
this.enabled = enabled;
10+
}
11+
12+
/**
13+
* Used for Jackson deserialization
14+
*/
15+
public LibraryEntry() {
16+
// Per default fetcher is activated
17+
this.enabled = true;
18+
}
19+
20+
public String getName() {
21+
return name;
22+
}
23+
24+
public void setName(String name) {
25+
this.name = name;
26+
}
27+
28+
public boolean isEnabled() {
29+
return enabled;
30+
}
31+
32+
public void setEnabled(boolean enabled) {
33+
this.enabled = enabled;
34+
}
35+
36+
@Override
37+
public boolean equals(Object o) {
38+
if (this == o) {
39+
return true;
40+
}
41+
if (o == null || getClass() != o.getClass()) {
42+
return false;
43+
}
44+
45+
LibraryEntry that = (LibraryEntry) o;
46+
47+
if (isEnabled() != that.isEnabled()) {
48+
return false;
49+
}
50+
return getName() != null ? getName().equals(that.getName()) : that.getName() == null;
51+
}
52+
53+
@Override
54+
public int hashCode() {
55+
int result = getName() != null ? getName().hashCode() : 0;
56+
result = 31 * result + (isEnabled() ? 1 : 0);
57+
return result;
58+
}
59+
60+
@Override
61+
public String toString() {
62+
return "LibraryEntry{" +
63+
"name='" + name + '\'' +
64+
", enabled=" + enabled +
65+
'}';
66+
}
67+
}
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package org.jabref.model.study;
2+
3+
public class QueryEntry {
4+
private String query;
5+
6+
public QueryEntry(String query) {
7+
this.query = query;
8+
}
9+
10+
/**
11+
* Used for Jackson deserialization
12+
*/
13+
public QueryEntry() {
14+
15+
}
16+
17+
public String getQuery() {
18+
return query;
19+
}
20+
21+
public void setQuery(String query) {
22+
this.query = query;
23+
}
24+
25+
@Override
26+
public boolean equals(Object o) {
27+
if (this == o) {
28+
return true;
29+
}
30+
if (o == null || getClass() != o.getClass()) {
31+
return false;
32+
}
33+
34+
QueryEntry that = (QueryEntry) o;
35+
36+
return getQuery() != null ? getQuery().equals(that.getQuery()) : that.getQuery() == null;
37+
}
38+
39+
@Override
40+
public int hashCode() {
41+
return getQuery() != null ? getQuery().hashCode() : 0;
42+
}
43+
44+
@Override
45+
public String toString() {
46+
return "QueryEntry{" +
47+
"query='" + query + '\'' +
48+
'}';
49+
}
50+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package org.jabref.logic.crawler;
2+
3+
import java.net.URL;
4+
import java.nio.file.Path;
5+
import java.time.LocalDate;
6+
import java.util.List;
7+
8+
import org.jabref.logic.util.io.FileUtil;
9+
import org.jabref.model.study.LibraryEntry;
10+
import org.jabref.model.study.QueryEntry;
11+
import org.jabref.model.study.Study;
12+
13+
import org.junit.jupiter.api.BeforeAll;
14+
import org.junit.jupiter.api.BeforeEach;
15+
import org.junit.jupiter.api.Test;
16+
import org.junit.jupiter.api.io.TempDir;
17+
18+
import static org.junit.jupiter.api.Assertions.assertEquals;
19+
20+
class StudyYAMLParserTest {
21+
@TempDir
22+
static Path testDirectory;
23+
Study expectedStudy;
24+
25+
@BeforeAll
26+
static void copyStudyYAMLIntoTestDirectory() throws Exception {
27+
Path destination = testDirectory.resolve("study.yml");
28+
URL studyDefinition = StudyYAMLParser.class.getResource("study.yml");
29+
FileUtil.copyFile(Path.of(studyDefinition.toURI()), destination, false);
30+
}
31+
32+
@BeforeEach
33+
void setupStudy() {
34+
List<String> authors = List.of("Jab Ref");
35+
String studyName = "TestStudyName";
36+
List<String> researchQuestions = List.of("Question1", "Question2");
37+
List<QueryEntry> queryEntries = List.of(new QueryEntry("Quantum"), new QueryEntry("Cloud Computing"), new QueryEntry("\"Software Engineering\""));
38+
List<LibraryEntry> libraryEntries = List.of(new LibraryEntry("Springer", true), new LibraryEntry("ArXiv", true));
39+
40+
expectedStudy = new Study(authors, studyName, researchQuestions, queryEntries, libraryEntries);
41+
expectedStudy.setLastSearchDate(LocalDate.parse("2020-11-26"));
42+
}
43+
44+
@Test
45+
public void parseStudyFileSuccessfully() throws Exception {
46+
Study study = new StudyYAMLParser().parseStudyYAMLFile(testDirectory.resolve("study.yml"));
47+
48+
assertEquals(expectedStudy, study);
49+
}
50+
51+
@Test
52+
public void writeStudyFileSuccessfully() throws Exception {
53+
new StudyYAMLParser().writeStudyYAMLFile(expectedStudy, testDirectory.resolve("study.yml"));
54+
55+
Study study = new StudyYAMLParser().parseStudyYAMLFile(testDirectory.resolve("study.yml"));
56+
57+
assertEquals(expectedStudy, study);
58+
}
59+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
authors:
2+
- Jab Ref
3+
studyName: TestStudyName
4+
lastSearchDate: 2020-11-26
5+
researchQuestions:
6+
- Question1
7+
- Question2
8+
queries:
9+
- query: Quantum
10+
- query: Cloud Computing
11+
- query: '"Software Engineering"'
12+
libraries:
13+
- name: Springer
14+
enabled: true
15+
- name: ArXiv
16+
enabled: true
17+
- name: IEEEXplore
18+
enabled: false

0 commit comments

Comments
 (0)