Search in sources :

Example 1 with XTextRangeCompare

use of com.sun.star.text.XTextRangeCompare in project jabref by JabRef.

the class OOBibBase method combineCiteMarkers.

public void combineCiteMarkers(List<BibDatabase> databases, OOBibStyle style) throws IOException, WrappedTargetException, NoSuchElementException, IllegalArgumentException, UndefinedCharacterFormatException, UnknownPropertyException, PropertyVetoException, CreationException, BibEntryNotFoundException {
    XNameAccess nameAccess = getReferenceMarks();
    // TODO: doesn't work for citations in footnotes/tables
    List<String> names = getSortedReferenceMarks(nameAccess);
    final XTextRangeCompare compare = UnoRuntime.queryInterface(XTextRangeCompare.class, text);
    int piv = 0;
    boolean madeModifications = false;
    while (piv < (names.size() - 1)) {
        XTextRange range1 = UnoRuntime.queryInterface(XTextContent.class, nameAccess.getByName(names.get(piv))).getAnchor().getEnd();
        XTextRange range2 = UnoRuntime.queryInterface(XTextContent.class, nameAccess.getByName(names.get(piv + 1))).getAnchor().getStart();
        if (range1.getText() != range2.getText()) {
            piv++;
            continue;
        }
        XTextCursor mxDocCursor = range1.getText().createTextCursorByRange(range1);
        mxDocCursor.goRight((short) 1, true);
        boolean couldExpand = true;
        while (couldExpand && (compare.compareRegionEnds(mxDocCursor, range2) > 0)) {
            couldExpand = mxDocCursor.goRight((short) 1, true);
        }
        String cursorText = mxDocCursor.getString();
        // Check if the string contains no line breaks and only whitespace:
        if ((cursorText.indexOf('\n') == -1) && cursorText.trim().isEmpty()) {
            // marks are removed, preventing damage to the user's document:
            if (style.isFormatCitations()) {
                XPropertySet xCursorProps = UnoRuntime.queryInterface(XPropertySet.class, mxDocCursor);
                String charStyle = style.getCitationCharacterFormat();
                try {
                    xCursorProps.setPropertyValue(CHAR_STYLE_NAME, charStyle);
                } catch (UnknownPropertyException | PropertyVetoException | IllegalArgumentException | WrappedTargetException ex) {
                    // will result in an error message for the user:
                    throw new UndefinedCharacterFormatException(charStyle);
                }
            }
            List<String> keys = parseRefMarkName(names.get(piv));
            keys.addAll(parseRefMarkName(names.get(piv + 1)));
            removeReferenceMark(names.get(piv));
            removeReferenceMark(names.get(piv + 1));
            List<BibEntry> entries = new ArrayList<>();
            for (String key : keys) {
                for (BibDatabase database : databases) {
                    Optional<BibEntry> entry = database.getEntryByKey(key);
                    if (entry.isPresent()) {
                        entries.add(entry.get());
                        break;
                    }
                }
            }
            Collections.sort(entries, new FieldComparator(FieldName.YEAR));
            String keyString = String.join(",", entries.stream().map(entry -> entry.getCiteKeyOptional().orElse("")).collect(Collectors.toList()));
            // Insert bookmark:
            String bName = getUniqueReferenceMarkName(keyString, OOBibBase.AUTHORYEAR_PAR);
            insertReferenceMark(bName, "tmp", mxDocCursor, true, style);
            names.set(piv + 1, bName);
            madeModifications = true;
        }
        piv++;
    }
    if (madeModifications) {
        updateSortedReferenceMarks();
        refreshCiteMarkers(databases, style);
    }
}
Also used : XTextRange(com.sun.star.text.XTextRange) BibEntry(org.jabref.model.entry.BibEntry) WrappedTargetException(com.sun.star.lang.WrappedTargetException) ArrayList(java.util.ArrayList) XNameAccess(com.sun.star.container.XNameAccess) XTextCursor(com.sun.star.text.XTextCursor) UnknownPropertyException(com.sun.star.beans.UnknownPropertyException) XTextRangeCompare(com.sun.star.text.XTextRangeCompare) Point(com.sun.star.awt.Point) XPropertySet(com.sun.star.beans.XPropertySet) PropertyVetoException(com.sun.star.beans.PropertyVetoException) FieldComparator(org.jabref.logic.bibtex.comparator.FieldComparator) BibDatabase(org.jabref.model.database.BibDatabase) IllegalArgumentException(com.sun.star.lang.IllegalArgumentException)

Aggregations

Point (com.sun.star.awt.Point)1 PropertyVetoException (com.sun.star.beans.PropertyVetoException)1 UnknownPropertyException (com.sun.star.beans.UnknownPropertyException)1 XPropertySet (com.sun.star.beans.XPropertySet)1 XNameAccess (com.sun.star.container.XNameAccess)1 IllegalArgumentException (com.sun.star.lang.IllegalArgumentException)1 WrappedTargetException (com.sun.star.lang.WrappedTargetException)1 XTextCursor (com.sun.star.text.XTextCursor)1 XTextRange (com.sun.star.text.XTextRange)1 XTextRangeCompare (com.sun.star.text.XTextRangeCompare)1 ArrayList (java.util.ArrayList)1 FieldComparator (org.jabref.logic.bibtex.comparator.FieldComparator)1 BibDatabase (org.jabref.model.database.BibDatabase)1 BibEntry (org.jabref.model.entry.BibEntry)1