CitationsVisualizer { public static String getJsonAnnotations(Document teiDoc, List<String> resolvedBibRefUrl) throws IOException, XPathException { StringWriter refW = new StringWriter(); JsonGenerator jsonRef = jFactory.createGenerator(refW); jsonRef.writeStartObject(); List<Page> pages = teiDoc.getPages(); int pageNumber = 1; jsonRef.writeArrayFieldStart("pages"); for(Page page : pages) { jsonRef.writeStartObject(); jsonRef.writeNumberField("page_height", page.getHeight()); jsonRef.writeNumberField("page_width", page.getWidth()); jsonRef.writeEndObject(); pageNumber++; } jsonRef.writeEndArray(); StringWriter markW = new StringWriter(); JsonGenerator jsonMark = jFactory.createGenerator(markW); jsonMark.writeStartArray(); int totalMarkers1 = 0; int totalMarkers2 = 0; int totalBib = 0; jsonRef.writeArrayFieldStart("refBibs"); String tei = teiDoc.getTei(); Multimap<String, DataSetContext> contexts = DataSetContextExtractor.getCitationReferences(tei); int bibIndex = 0; for (BibDataSet cit : teiDoc.getBibDataSets()) { String teiId = cit.getResBib().getTeiId(); totalBib++; jsonRef.writeStartObject(); jsonRef.writeStringField("id", teiId); if ( (resolvedBibRefUrl != null) && (resolvedBibRefUrl.size()>bibIndex) && (resolvedBibRefUrl.get(bibIndex) != null) ) { jsonRef.writeStringField("url", resolvedBibRefUrl.get(bibIndex)); } else { BiblioItem biblio = cit.getResBib(); String theUrl = null; if (!StringUtils.isEmpty(biblio.getOAURL())) { theUrl = biblio.getOAURL(); } else if (!StringUtils.isEmpty(biblio.getDOI())) { theUrl = "https: } else if (!StringUtils.isEmpty(biblio.getArXivId())) { theUrl = "https: } else if (!StringUtils.isEmpty(biblio.getWeb())) { theUrl = biblio.getWeb(); } if (theUrl != null) jsonRef.writeStringField("url", theUrl); } jsonRef.writeArrayFieldStart("pos"); if (cit.getResBib().getCoordinates() != null) { for (BoundingBox b : cit.getResBib().getCoordinates()) { jsonRef.writeStartObject(); b.writeJsonProps(jsonRef); jsonRef.writeEndObject(); } } jsonRef.writeEndArray(); jsonRef.writeEndObject(); for (DataSetContext c : contexts.get(teiId)) { String mrect = c.getDocumentCoords(); if ((mrect != null) && (mrect.trim().length()>0)) { for (String coords : mrect.split(";")) { if ((coords == null) || (coords.length() == 0)) continue; jsonMark.writeStartObject(); jsonMark.writeStringField("id", teiId); BoundingBox b2 = BoundingBox.fromString(coords); b2.writeJsonProps(jsonMark); jsonMark.writeEndObject(); totalMarkers1++; } } } bibIndex++; } jsonRef.writeEndArray(); for (DataSetContext c : contexts.get("")) { String mrect = c.getDocumentCoords(); if ((mrect != null) && (mrect.trim().length()>0)) { for (String coords : mrect.split(";")) { if (coords.trim().length() == 0) continue; BoundingBox b = BoundingBox.fromString(coords); jsonMark.writeStartObject(); b.writeJsonProps(jsonMark); jsonMark.writeEndObject(); totalMarkers2++; } } } jsonMark.writeEndArray(); jsonMark.close(); LOGGER.debug("totalBib: " + totalBib); LOGGER.debug("totalBibMarkers1: " + totalMarkers1); LOGGER.debug("totalBibMarkers2: " + totalMarkers2); jsonRef.writeFieldName("refMarkers"); jsonRef.writeRawValue(markW.toString()); markW = new StringWriter(); jsonMark = jFactory.createGenerator(markW); jsonMark.writeStartArray(); totalMarkers1 = 0; totalMarkers2 = 0; int totalFormulas = 0; jsonRef.writeArrayFieldStart("formulas"); contexts = DataSetContextExtractor.getFormulaReferences(tei); for (Equation formula : teiDoc.getEquations()) { String teiId = formula.getTeiId(); totalFormulas++; jsonRef.writeStartObject(); jsonRef.writeStringField("id", teiId); jsonRef.writeArrayFieldStart("pos"); if (formula.getCoordinates() != null) { for (BoundingBox b : formula.getCoordinates()) { jsonRef.writeStartObject(); b.writeJsonProps(jsonRef); jsonRef.writeEndObject(); } } jsonRef.writeEndArray(); jsonRef.writeEndObject(); for (DataSetContext c : contexts.get(teiId)) { String mrect = c.getDocumentCoords(); if ((mrect != null) && (mrect.trim().length()>0)) { for (String coords : mrect.split(";")) { if ((coords == null) || (coords.length() == 0)) continue; jsonMark.writeStartObject(); jsonMark.writeStringField("id", teiId); BoundingBox b2 = BoundingBox.fromString(coords); b2.writeJsonProps(jsonMark); jsonMark.writeEndObject(); totalMarkers1++; } } } } jsonRef.writeEndArray(); for (DataSetContext c : contexts.get("")) { String mrect = c.getDocumentCoords(); if ((mrect != null) && (mrect.trim().length()>0)) { for (String coords : mrect.split(";")) { if (coords.trim().length() == 0) continue; BoundingBox b = BoundingBox.fromString(coords); jsonMark.writeStartObject(); b.writeJsonProps(jsonMark); jsonMark.writeEndObject(); totalMarkers2++; } } } jsonMark.writeEndArray(); jsonMark.close(); jsonRef.writeFieldName("formulaMarkers"); jsonRef.writeRawValue(markW.toString()); LOGGER.debug("totalFormulas: " + totalBib); LOGGER.debug("totalFormulaMarkers1: " + totalMarkers1); LOGGER.debug("totalFormulaMarkers2: " + totalMarkers2); jsonRef.writeEndObject(); jsonRef.close(); return refW.toString(); }  static PDDocument annotatePdfWithCitations(PDDocument document, Document teiDoc,
            List<String> resolvedBibRefUrl); static String getJsonAnnotations(Document teiDoc, List<String> resolvedBibRefUrl);  }
@Test public void testJSONAnnotationStructure() throws Exception { Engine engine = GrobidFactory.getInstance().getEngine(); File inputTmpFile = getInputDocument("/test/test_Grobid_1_05452615.pdf"); Document tei = engine.fullTextToTEIDoc(inputTmpFile, GrobidAnalysisConfig.defaultInstance()); String refURL = "http: List<String> refURLs = Arrays.asList(refURL); String json = CitationsVisualizer.getJsonAnnotations(tei, refURLs); JsonNode root = mapper.readTree(json); assertTrue(root.has("pages")); assertTrue(root.has("refBibs")); assertTrue(root.has("refMarkers")); JsonNode pages = root.get("pages"); assertTrue(pages.isArray()); assertEquals(tei.getPages().size(), pages.size()); assertTrue(pages.size() > 0); JsonNode firstPage = pages.get(0); assertTrue(firstPage.has("page_height")); assertTrue(firstPage.has("page_width")); JsonNode bibs = root.get("refBibs"); JsonNode firstBib = bibs.get(0); assertTrue(firstBib.has("id")); assertTrue(firstBib.has("url")); assertEquals(refURL, firstBib.get("url").asText()); assertTrue(firstBib.has("pos")); JsonNode fbPos = firstBib.get("pos"); assertTrue(fbPos.isArray()); assertTrue(fbPos.size() > 0); for (JsonNode bbox : fbPos) { assertTrue(bbox.isObject()); assertTrue(bbox.has("p")); assertTrue(bbox.has("x")); assertTrue(bbox.has("y")); assertTrue(bbox.has("w")); assertTrue(bbox.has("h")); } }
@Test public void testJSONAnnotationEscaping() throws Exception { Engine engine = GrobidFactory.getInstance().getEngine(); File inputTmpFile = getInputDocument("/test/test_Grobid_1_05452615.pdf"); Document tei = engine.fullTextToTEIDoc(inputTmpFile, GrobidAnalysisConfig.defaultInstance()); String refURL = "http: List<String> refURLs = Arrays.asList(refURL); String json = CitationsVisualizer.getJsonAnnotations(tei, refURLs); JsonNode root = mapper.readTree(json); }