Skip to content

Commit

Permalink
Add new json parsing method to extract all bib files and their corres…
Browse files Browse the repository at this point in the history
…ponding doc id
  • Loading branch information
Siedlerchr committed Jun 8, 2017
1 parent 5eb0234 commit 6fe01c4
Show file tree
Hide file tree
Showing 4 changed files with 116 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import org.jabref.logic.importer.ImportFormatPreferences;
import org.jabref.model.database.BibDatabaseContext;
import org.jabref.model.sharelatex.ShareLatexProject;

import com.google.gson.JsonArray;
import com.google.gson.JsonElement;

Expand Down
110 changes: 105 additions & 5 deletions src/main/java/org/jabref/logic/sharelatex/ShareLatexParser.java
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) {
Expand All @@ -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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,6 @@ public void processMessageFromServer(String message, Session session)
//We got changes at our doc
//leave doc and rejoin doc


session.getBasicRemote().sendText("5:6+::{\"name\":\"leaveDoc\",\"args\":[\"" + documentId + "\"]}");
Thread.sleep(200);
session.getBasicRemote().sendText("5:7+::{\"name\":\"joinDoc\",\"args\":[\"" + documentId + "\"]}");
Expand Down
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;

Expand All @@ -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
Expand All @@ -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));

}
Expand All @@ -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);

}
}

0 comments on commit 6fe01c4

Please sign in to comment.