-
-
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.
Add new json parsing method to extract all bib files and their corres…
…ponding doc id
- Loading branch information
1 parent
5eb0234
commit 6fe01c4
Showing
4 changed files
with
116 additions
and
11 deletions.
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 |
---|---|---|
@@ -1,21 +1,110 @@ | ||
package org.jabref.logic.sharelatex; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
|
||
import org.jabref.logic.importer.ImportFormatPreferences; | ||
import org.jabref.logic.importer.ParseException; | ||
import org.jabref.logic.importer.fileformat.BibtexParser; | ||
import org.jabref.model.entry.BibEntry; | ||
|
||
import com.github.wnameless.json.flattener.JsonFlattener; | ||
import com.google.gson.JsonArray; | ||
import com.google.gson.JsonElement; | ||
import com.google.gson.JsonObject; | ||
import com.google.gson.JsonParser; | ||
|
||
public class ShareLatexParser { | ||
|
||
/** | ||
* json Data source containing all details and docs for one project | ||
[ | ||
null, | ||
{ | ||
"_id": "5909edaff31ff96200ef58dd", | ||
"name": "Test", | ||
"rootDoc_id": "5909edaff31ff96200ef58de", | ||
"rootFolder": [ | ||
{ | ||
"_id": "5909edaff31ff96200ef58dc", | ||
"name": "rootFolder", | ||
"folders": [], | ||
"fileRefs": [ | ||
{ | ||
"_id": "5909edb0f31ff96200ef58e0", | ||
"name": "universe.jpg" | ||
}, | ||
{ | ||
"_id": "59118cae98ba55690073c2a0", | ||
"name": "all2.ris" | ||
} | ||
], | ||
"docs": [ | ||
{ | ||
"_id": "5909edaff31ff96200ef58de", | ||
"name": "main.tex" | ||
}, | ||
{ | ||
"_id": "5909edb0f31ff96200ef58df", | ||
"name": "references.bib" | ||
}, | ||
{ | ||
"_id": "5911801698ba55690073c29c", | ||
"name": "aaaaaaaaaaaaaa.bib" | ||
}, | ||
{ | ||
"_id": "59368d551bd5906b0082f53a", | ||
"name": "aaaaaaaaaaaaaa (copy 1).bib" | ||
} | ||
] | ||
} | ||
], | ||
"publicAccesLevel": "private", | ||
"dropboxEnabled": false, | ||
"compiler": "pdflatex", | ||
"description": "", | ||
"spellCheckLanguage": "en", | ||
"deletedByExternalDataSource": false, | ||
"deletedDocs": [], | ||
"members": [ | ||
{ | ||
"_id": "5912e195a303b468002eaad0", | ||
"first_name": "jim", | ||
"last_name": "", | ||
"email": "[email protected]", | ||
"privileges": "readAndWrite", | ||
"signUpDate": "2017-05-10T09:47:01.325Z" | ||
} | ||
], | ||
"invites": [], | ||
"owner": { | ||
"_id": "5909ed80761dc10a01f7abc0", | ||
"first_name": "joe", | ||
"last_name": "", | ||
"email": "[email protected]", | ||
"privileges": "owner", | ||
"signUpDate": "2017-05-03T14:47:28.665Z" | ||
}, | ||
"features": { | ||
"trackChanges": true, | ||
"references": true, | ||
"templates": true, | ||
"compileGroup": "standard", | ||
"compileTimeout": 180, | ||
"github": false, | ||
"dropbox": true, | ||
"versioning": true, | ||
"collaborators": -1, | ||
"trackChangesVisible": false | ||
} | ||
}, | ||
"owner", | ||
2 | ||
] | ||
*/ | ||
|
||
private final JsonParser parser = new JsonParser(); | ||
|
||
public JsonArray parseFirstPartOfJson(String documentToParse) { | ||
|
@@ -40,16 +129,27 @@ public List<BibEntry> parseBibEntryFromJsonArray(JsonArray arr, ImportFormatPref | |
|
||
} | ||
|
||
public Map<String, Object> getDatabaseWithId(String json) { | ||
public Map<String, String> getDatabaseWithId(String json) { | ||
Map<String, String> bibFileWithId = new HashMap<>(); | ||
|
||
JsonObject obj = parseFirstPartOfJson(json).get(1).getAsJsonObject(); | ||
JsonArray arr = obj.get("rootFolder").getAsJsonArray(); | ||
|
||
Map<String, Object> flattenJson = JsonFlattener.flattenAsMap(obj.toString()); | ||
Optional<JsonArray> docs = arr.get(0).getAsJsonObject().entrySet().stream() | ||
.filter(entry -> entry.getKey().equals("docs")).map(v -> v.getValue().getAsJsonArray()).findFirst(); | ||
|
||
docs.ifPresent(jsonArray -> { | ||
for (JsonElement doc : jsonArray) { | ||
String name = doc.getAsJsonObject().get("name").getAsString(); | ||
String id = doc.getAsJsonObject().get("_id").getAsString(); | ||
|
||
System.out.println("As Json flat: " + JsonFlattener.flatten(obj.toString())); | ||
if (name.endsWith(".bib")) { | ||
bibFileWithId.put(name, id); | ||
} | ||
|
||
} | ||
}); | ||
|
||
return flattenJson; | ||
return bibFileWithId; | ||
} | ||
} |
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 |
---|---|---|
@@ -1,5 +1,6 @@ | ||
package org.jabref.logic.sharelatex; | ||
|
||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
|
@@ -13,9 +14,9 @@ | |
import org.mockito.Answers; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.junit.Assert.assertFalse; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
public class ShareLatexParserTest { | ||
|
||
@Test | ||
|
@@ -29,7 +30,6 @@ public void testParseDocIdFromProject() { | |
JsonParser jsonParser = new JsonParser(); | ||
JsonArray obj = jsonParser.parse(withoutFirstSix).getAsJsonArray(); | ||
|
||
//TODO: Write new method that extracs the database name and the id of the database | ||
assertEquals(obj, parser.parseFirstPartOfJson(document)); | ||
|
||
} | ||
|
@@ -45,17 +45,22 @@ public void testParseBibEntries() throws ParseException { | |
List<BibEntry> entries = parser.parseBibEntryFromJsonArray(parser.parseFirstPartOfJson(bibTexString), | ||
prefs); | ||
System.out.println(entries); | ||
assertFalse(entries.isEmpty()); | ||
// assertFalse(entries.isEmpty()); | ||
} | ||
|
||
@Test | ||
public void testgetDatabaseWithId() { | ||
String document = "6:::1+[null,{\"_id\":\"5909edaff31ff96200ef58dd\",\"name\":\"Test\",\"rootDoc_id\":\"5909edaff31ff96200ef58de\",\"rootFolder\":[{\"_id\":\"5909edaff31ff96200ef58dc\",\"name\":\"rootFolder\",\"folders\":[],\"fileRefs\":[{\"_id\":\"5909edb0f31ff96200ef58e0\",\"name\":\"universe.jpg\"},{\"_id\":\"59118cae98ba55690073c2a0\",\"name\":\"all2.ris\"}],\"docs\":[{\"_id\":\"5909edaff31ff96200ef58de\",\"name\":\"main.tex\"},{\"_id\":\"5909edb0f31ff96200ef58df\",\"name\":\"references.bib\"},{\"_id\":\"5911801698ba55690073c29c\",\"name\":\"aaaaaaaaaaaaaa.bib\"},{\"_id\":\"59368d551bd5906b0082f53a\",\"name\":\"aaaaaaaaaaaaaa (copy 1).bib\"}]}],\"publicAccesLevel\":\"private\",\"dropboxEnabled\":false,\"compiler\":\"pdflatex\",\"description\":\"\",\"spellCheckLanguage\":\"en\",\"deletedByExternalDataSource\":false,\"deletedDocs\":[],\"members\":[{\"_id\":\"5912e195a303b468002eaad0\",\"first_name\":\"jim\",\"last_name\":\"\",\"email\":\"[email protected]\",\"privileges\":\"readAndWrite\",\"signUpDate\":\"2017-05-10T09:47:01.325Z\"}],\"invites\":[],\"owner\":{\"_id\":\"5909ed80761dc10a01f7abc0\",\"first_name\":\"joe\",\"last_name\":\"\",\"email\":\"[email protected]\",\"privileges\":\"owner\",\"signUpDate\":\"2017-05-03T14:47:28.665Z\"},\"features\":{\"trackChanges\":true,\"references\":true,\"templates\":true,\"compileGroup\":\"standard\",\"compileTimeout\":180,\"github\":false,\"dropbox\":true,\"versioning\":true,\"collaborators\":-1,\"trackChangesVisible\":false}},\"owner\",2]"; | ||
|
||
ShareLatexParser parser = new ShareLatexParser(); | ||
Map<String, Object> flatMap = parser.getDatabaseWithId(document); | ||
Map<String, String> actual = parser.getDatabaseWithId(document); | ||
|
||
Map<String, String> expected = new HashMap<>(); | ||
expected.put("references.bib", "5909edb0f31ff96200ef58df"); | ||
expected.put("aaaaaaaaaaaaaa.bib", "5911801698ba55690073c29c"); | ||
expected.put("aaaaaaaaaaaaaa (copy 1).bib", "59368d551bd5906b0082f53a"); | ||
|
||
flatMap.entrySet().forEach(entry -> System.out.println(entry.getKey() + " " + entry.getValue())); | ||
assertEquals(expected, actual); | ||
|
||
} | ||
} |