public ContentStructureCitationPositions findReferences( ContentStructure structure, List<BibEntry> citations) { List<Integer> indexes = new ArrayList<Integer>(); Map<Integer, Integer> paragraphIndex = new HashMap<Integer, Integer>(); Map<Integer, DocumentSection> sectionIndex = new HashMap<Integer, DocumentSection>(); List<DocumentSection> sections = toSectionList(structure); StringBuilder sb = new StringBuilder(); int length = 0; int index = 0; for (DocumentSection section : sections) { for (int i = 0; i < section.getParagraphs().size(); i++) { indexes.add(length); paragraphIndex.put(index, i); sectionIndex.put(index, section); sb.append(section.getParagraphs().get(i)); length += section.getParagraphs().get(i).length(); index++; } } String fullText = sb.toString(); CitationPositionFinder finder = new CitationPositionFinder(); List<List<CitationPosition>> positions = finder.findReferences(fullText, citations); ContentStructureCitationPositions contentPositions = new ContentStructureCitationPositions(); for (int i = 0; i < positions.size(); i++) { List<CitationPosition> citationPositions = positions.get(i); for (CitationPosition pos : citationPositions) { int start = findIndex(pos.getStartRefPosition(), indexes); int end = findIndex(pos.getEndRefPosition(), indexes); if (start != end) { continue; } CitationPosition shifted = new CitationPosition(); shifted.setStartRefPosition(pos.getStartRefPosition() - indexes.get(start)); shifted.setEndRefPosition(pos.getEndRefPosition() - indexes.get(start)); contentPositions.addPosition( sectionIndex.get(start), paragraphIndex.get(start), i, shifted); } } return contentPositions; }
private void addSections(List<DocumentSection> sectionList, DocumentSection section) { sectionList.add(section); for (DocumentSection subsection : section.getSubsections()) { addSections(sectionList, subsection); } }