/** * This method inserts a cite marker in the text for the given BibtexEntry, * and may refresh the bibliography. * @param entries The entries to cite. * @param database The database the entry belongs to. * @param style The bibliography style we are using. * @param inParenthesis Indicates whether it is an in-text citation or a citation in parenthesis. * This is not relevant if numbered citations are used. * @param withText Indicates whether this should be a normal citation (true) or an empty * (invisible) citation (false). * @param sync Indicates whether the reference list should be refreshed. * @throws Exception */ public void insertEntry(BibtexEntry[] entries, BibtexDatabase database, List<BibtexDatabase> allBases, OOBibStyle style, boolean inParenthesis, boolean withText, String pageInfo, boolean sync) throws Exception { try { XTextViewCursor xViewCursor = xViewCursorSupplier.getViewCursor(); if (entries.length > 1) { if (style.getBooleanCitProperty("MultiCiteChronological")) Arrays.sort(entries, yearComparator); else Arrays.sort(entries, entryComparator); } StringBuilder sb = new StringBuilder(); for (int i = 0; i < entries.length; i++) { BibtexEntry entry = entries[i]; if (i > 0) sb.append(","); sb.append(entry.getCiteKey()); } String keyString = sb.toString(); // Insert bookmark: String bName = getUniqueReferenceMarkName(keyString, withText ? (inParenthesis ? AUTHORYEAR_PAR : AUTHORYEAR_INTEXT) : INVISIBLE_CIT); //XTextContent content = insertBookMark(bName, xViewCursor); // If we should store metadata for page info, do that now: if (pageInfo != null) { System.out.println("Storing page info: "+pageInfo); setCustomProperty(bName, pageInfo); } String citeText = style.isNumberEntries() ? "-" : style.getCitationMarker(entries, database, inParenthesis, null, null); //System.out.println(text+" / "+xViewCursor.getText()); xViewCursor.getText().insertString(xViewCursor, " ", false); if (style.isFormatCitations()) { XPropertySet xCursorProps = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, xViewCursor); String charStyle = style.getCitationCharacterFormat(); try { xCursorProps.setPropertyValue("CharStyleName", charStyle); } catch (Throwable ex) { // Setting the character format failed, so we throw an exception that // will result in an error message for the user. Before that, // delete the space we inserted: xViewCursor.goLeft((short)1,true); xViewCursor.setString(""); throw new UndefinedCharacterFormatException(charStyle); } } xViewCursor.goLeft((short)1,false); insertReferenceMark(bName, citeText, xViewCursor, withText, style); //xViewCursor.collapseToEnd(); xViewCursor.collapseToEnd(); xViewCursor.goRight((short)1,false); XTextRange position = xViewCursor.getEnd(); if (sync) { // To account for numbering and for uniqiefiers, we must refresh the cite markers: updateSortedReferenceMarks(); refreshCiteMarkers(allBases, style); // Insert it at the current position: rebuildBibTextSection(allBases, style); } // Go back to the relevant position: try { xViewCursor.gotoRange(position, false); } catch (Exception ex) { System.out.println("Catch"); ex.printStackTrace(); } } catch (DisposedException ex) { // We need to catch this one here because the OpenOfficePanel class is // loaded before connection, and therefore cannot directly reference // or catch a DisposedException (which is in a OO jar file). throw new ConnectionLostException(ex.getMessage()); } }
public void combineCiteMarkers(List<BibtexDatabase> databases, OOBibStyle style) throws Exception { XReferenceMarksSupplier supplier = (XReferenceMarksSupplier) UnoRuntime.queryInterface( XReferenceMarksSupplier.class, xCurrentComponent); XNameAccess nameAccess = supplier.getReferenceMarks(); // TODO: doesn't work for citations in footnotes/tables String[] names = getSortedReferenceMarks(nameAccess); final XTextRangeCompare compare = (XTextRangeCompare) UnoRuntime.queryInterface (XTextRangeCompare.class, text); int piv = 0; boolean madeModifications = false; while (piv < names.length-1) { XTextRange r1 = ((XTextContent) UnoRuntime.queryInterface (XTextContent.class, nameAccess.getByName(names[piv]))).getAnchor().getEnd(); XTextRange r2 = ((XTextContent) UnoRuntime.queryInterface (XTextContent.class, nameAccess.getByName(names[piv+1]))).getAnchor().getStart(); if (r1.getText() != r2.getText()) { piv++; continue; } XTextCursor mxDocCursor = r1.getText().createTextCursorByRange(r1); mxDocCursor.goRight((short)1, true); boolean couldExpand = true; while (couldExpand && (compare.compareRegionEnds(mxDocCursor, r2) > 0)) { couldExpand = mxDocCursor.goRight((short)1, true); } String text = mxDocCursor.getString(); // Check if the string contains no line breaks and only whitespace: if ((text.indexOf('\n') == -1) && (text.trim().length() == 0)) { // If we are supposed to set character format for citations, test this before // making any changes. This way we can throw an exception before any reference // marks are removed, preventing damage to the user's document: if (style.isFormatCitations()) { XPropertySet xCursorProps = (XPropertySet) UnoRuntime.queryInterface( XPropertySet.class, mxDocCursor); String charStyle = style.getCitationCharacterFormat(); try { xCursorProps.setPropertyValue("CharStyleName", charStyle); } catch (Throwable ex) { // Setting the character format failed, so we throw an exception that // will result in an error message for the user: throw new UndefinedCharacterFormatException(charStyle); } } List<String> keys = parseRefMarkName(names[piv]); keys.addAll(parseRefMarkName(names[piv+1])); removeReferenceMark(names[piv]); removeReferenceMark(names[piv+1]); ArrayList<BibtexEntry> entries = new ArrayList<BibtexEntry>(); for (String key : keys) { bases: for (BibtexDatabase database : databases) { BibtexEntry entry = database.getEntryByKey(key); if (entry != null) { entries.add(OOUtil.createAdaptedEntry(entry)); break bases; } } } Collections.sort(entries, new FieldComparator("year")); StringBuilder sb = new StringBuilder(); int i=0; for (BibtexEntry entry : entries) { if (i > 0) sb.append(","); sb.append(entry.getCiteKey()); i++; } String keyString = sb.toString(); boolean inParenthesis = true; // Insert bookmark: String bName = getUniqueReferenceMarkName(keyString, inParenthesis ? AUTHORYEAR_PAR : AUTHORYEAR_INTEXT); insertReferenceMark(bName, "tmp", mxDocCursor, true, style); names[piv+1] = bName; madeModifications = true; } piv++; } if (madeModifications) { updateSortedReferenceMarks(); refreshCiteMarkers(databases, style); } }
Clone fragments detected by clone detection tool
File path: /jabref-2.10/src/java/net/sf/jabref/oo/OOBibBase.java File path: /jabref-2.10/src/java/net/sf/jabref/oo/OOBibBase.java
Method name: void insertEntry(BibtexEntry[], BibtexDatabase, List, OOBibStyle, boolean, boolean, String, boolean) Method name: void combineCiteMarkers(List, OOBibStyle)
Number of AST nodes: 5 Number of AST nodes: 5
1
/**
1
public void combineCiteMarkers(List<BibtexDatabase> databases, OOBibStyle style) throws Exception {
2
     * This method inserts a cite marker in the text for the given BibtexEntry,
2
        XReferenceMarksSupplier supplier = (XReferenceMarksSupplier) UnoRuntime.queryInterface(
3
     * and may refresh the bibliography.
3
                XReferenceMarksSupplier.class, xCurrentComponent);
4
     * @param entries The entries to cite.
4
        XNameAccess nameAccess = supplier.getReferenceMarks();
5
     * @param database The database the entry belongs to.
5
        // TODO: doesn't work for citations in footnotes/tables
6
     * @param style The bibliography style we are using.
6
        String[] names = getSortedReferenceMarks(nameAccess);
7
     * @param inParenthesis Indicates whether it is an in-text citation or a citation in parenthesis.
7
8
     *   This is not relevant if numbered citations are used.
8
9
     * @param withText Indicates whether this should be a normal citation (true) or an empty
9
        final XTextRangeCompare compare = (XTextRangeCompare) UnoRuntime.queryInterface
10
     *   (invisible) citation (false).
10
                (XTextRangeCompare.class, text);
11
     * @param sync Indicates whether the reference list should be refreshed.
11
12
     * @throws Exception
12
        int piv = 0;
13
     */
13
        boolean madeModifications = false;
14
    public void insertEntry(BibtexEntry[] entries, BibtexDatabase database,
14
        while (piv < names.length-1) {
15
                            List<BibtexDatabase> allBases, OOBibStyle style,
15
            XTextRange r1 = ((XTextContent) UnoRuntime.queryInterface
16
                            boolean inParenthesis, boolean withText, String pageInfo,
16
                            (XTextContent.class, nameAccess.getByName(names[piv]))).getAnchor().getEnd();
17
                            boolean sync) throws Exception {
17
            XTextRange r2 = ((XTextContent) UnoRuntime.queryInterface
18
18
                            (XTextContent.class,
19
        try {
19
                                    nameAccess.getByName(names[piv+1]))).getAnchor().getStart();
20
20
            if (r1.getText() != r2.getText()) {
21
            XTextViewCursor xViewCursor = xViewCursorSupplier.getViewCursor();
21
                piv++;
22
22
                continue;
23
            if (entries.length > 1) {
23
            }
24
                if (style.getBooleanCitProperty("MultiCiteChronological"))
24
            XTextCursor mxDocCursor = r1.getText().createTextCursorByRange(r1);
25
                    Arrays.sort(entries, yearComparator);
25
            mxDocCursor.goRight((short)1, true);
26
                else
26
            boolean couldExpand = true;
27
                    Arrays.sort(entries, entryComparator);
27
            while (couldExpand && (compare.compareRegionEnds(mxDocCursor, r2) > 0)) {
28
            }
28
                couldExpand = mxDocCursor.goRight((short)1, true);
29
29
            }
30
            StringBuilder sb = new StringBuilder();
30
            String text = mxDocCursor.getString();
31
            for (int i = 0; i < entries.length; i++) {
31
            // Check if the string contains no line breaks and only whitespace:
32
                BibtexEntry entry = entries[i];
32
            if ((text.indexOf('\n') == -1) && (text.trim().length() == 0)) {
33
                if (i > 0)
33
34
                    sb.append(",");
34
                // If we are supposed to set character format for citations, test this before
35
                sb.append(entry.getCiteKey());
35
                // making any changes. This way we can throw an exception before any reference
36
            }
36
                // marks are removed, preventing damage to the user's document:
37
            String keyString = sb.toString();
37
                if (style.isFormatCitations()) {
38
            // Insert bookmark:
38
                    XPropertySet xCursorProps = (XPropertySet) UnoRuntime.queryInterface(
39
            String bName = getUniqueReferenceMarkName(keyString,
39
                                        XPropertySet.class, mxDocCursor);
40
                    withText ? (inParenthesis ? AUTHORYEAR_PAR : AUTHORYEAR_INTEXT) : INVISIBLE_CIT);
40
                    String charStyle = style.getCitationCharacterFormat();
41
            //XTextContent content = insertBookMark(bName, xViewCursor);
41
                    try {
42
42
                        xCursorProps.setPropertyValue("CharStyleName", charStyle);
43
            // If we should store metadata for page info, do that now:
43
                    } catch (Throwable ex) {
44
            if (pageInfo != null) {
44
                        // Setting the character format failed, so we throw an exception that
45
                System.out.println("Storing page info: "+pageInfo);
45
                        // will result in an error message for the user:
46
                setCustomProperty(bName, pageInfo);
46
                        throw new UndefinedCharacterFormatException(charStyle);
47
            }
47
                    }
48
48
                }
49
49
50
            String citeText = style.isNumberEntries() ? "-" : style.getCitationMarker(entries, database, inParenthesis, null, null);
50
                List<String> keys = parseRefMarkName(names[piv]);
51
51
                keys.addAll(parseRefMarkName(names[piv+1]));
52
            //System.out.println(text+" / "+xViewCursor.getText());
52
                removeReferenceMark(names[piv]);
53
            xViewCursor.getText().insertString(xViewCursor, " ", false);
53
                removeReferenceMark(names[piv+1]);
54
            if (style.isFormatCitations()) {
54
                ArrayList<BibtexEntry> entries = new ArrayList<BibtexEntry>();
55
                XPropertySet xCursorProps = (XPropertySet) UnoRuntime.queryInterface(
55
                for (String key : keys) {
56
                                    XPropertySet.class, xViewCursor);
56
                    bases: for (BibtexDatabase database : databases) {
57
                String charStyle = style.getCitationCharacterFormat();
57
                        BibtexEntry entry = database.getEntryByKey(key);
58
                try {
58
                        if (entry != null) {
59
                    xCursorProps.setPropertyValue("CharStyleName", charStyle);
59
                            entries.add(OOUtil.createAdaptedEntry(entry));
60
                } catch (Throwable ex) {
60
                            break bases;
61
                    // Setting the character format failed, so we throw an exception that
61
                        }
62
                    // will result in an error message for the user. Before that,
62
                    }
63
                    // delete the space we inserted:
63
                }
64
                    xViewCursor.goLeft((short)1,true);
64
                Collections.sort(entries, new FieldComparator("year"));
65
                    xViewCursor.setString("");
65
                StringBuilder sb = new StringBuilder();
66
                    throw new UndefinedCharacterFormatException(charStyle);
66
                int i=0;
67
                }
67
                for (BibtexEntry entry : entries) {
68
            }
68
                    if (i > 0)
69
            xViewCursor.goLeft((short)1,false);
69
                        sb.append(",");
70
            insertReferenceMark(bName, citeText, xViewCursor, withText, style);
70
                    sb.append(entry.getCiteKey());
71
            //xViewCursor.collapseToEnd();
71
                    i++;
72
72
                }
73
            xViewCursor.collapseToEnd();
73
                String keyString = sb.toString();
74
            xViewCursor.goRight((short)1,false);
74
                boolean inParenthesis = true;
75
75
                // Insert bookmark:
76
            XTextRange position = xViewCursor.getEnd();
76
                String bName = getUniqueReferenceMarkName(keyString,
77
77
                        inParenthesis ? AUTHORYEAR_PAR : AUTHORYEAR_INTEXT);
78
            if (sync) {
78
                insertReferenceMark(bName, "tmp", mxDocCursor, true, style);
79
                // To account for numbering and for uniqiefiers, we must refresh the cite markers:
79
                names[piv+1] = bName;
80
                updateSortedReferenceMarks();
80
                madeModifications = true;
81
                refreshCiteMarkers(allBases, style);
81
            }
82
82
            piv++;
83
                // Insert it at the current position:
83
        }
84
                rebuildBibTextSection(allBases, style);
84
        if (madeModifications) {
85
            }
85
            updateSortedReferenceMarks();
86
86
            refreshCiteMarkers(databases, style);
87
            // Go back to the relevant position:
87
        }
88
            try {
88
89
                xViewCursor.gotoRange(position, false);
89
90
            } catch (Exception ex) {
90
    }
91
                System.out.println("Catch");
92
                ex.printStackTrace();
93
            }
94
        } catch (DisposedException ex) {
95
            // We need to catch this one here because the OpenOfficePanel class is
96
            // loaded before connection, and therefore cannot directly reference
97
            // or catch a DisposedException (which is in a OO jar file).
98
            throw new ConnectionLostException(ex.getMessage());
99
        }
100
    }
  1. {Non-refactorable}
    Mapping Summary
    Number of mapped statements5
    Number of unmapped statements in the first code fragment0
    Number of unmapped statements in the second code fragment1
    Time elapsed for statement mapping (ms)0.0
    Similarity Score0.857
    Clone typeType 2
    Mapped Statements
    ID Statement ID Statement
    8
    for (int i = 0; i < entries.length; i++)
    8
    for (int i = 0; i < entries.length; i++)
    39
    for (BibtexEntry entry : entries)
    Differences
    Expression1Expression2Difference
    net.sf.jabref.BibtexEntry[]java.util.ArrayListVARIABLE_TYPE_MISMATCH
    net.sf.jabref.BibtexEntry[]java.util.ArrayListVARIABLE_TYPE_MISMATCH
    Preondition Violations
    Type net.sf.jabref.BibtexEntry[] of variable entries does not match with type java.util.ArrayList<net.sf.jabref.BibtexEntry> of variable entries
    • Make classes net.sf.jabref.BibtexEntry[] and java.util.ArrayList extend a common superclass
    Type net.sf.jabref.BibtexEntry[] of variable entries does not match with type java.util.ArrayList<net.sf.jabref.BibtexEntry> of variable entries
    • Make classes net.sf.jabref.BibtexEntry[] and java.util.ArrayList extend a common superclass
    39
    for (BibtexEntry entry : entries)
    9
    BibtexEntry entry = entries[i];
    9
    BibtexEntry entry = entries[i];
    Differences
    Expression1Expression2Difference
    net.sf.jabref.BibtexEntry[]java.util.ArrayListVARIABLE_TYPE_MISMATCH
    Preondition Violations
    Type net.sf.jabref.BibtexEntry[] of variable entries does not match with type java.util.ArrayList<net.sf.jabref.BibtexEntry> of variable entries
    • Make classes net.sf.jabref.BibtexEntry[] and java.util.ArrayList extend a common superclass
                                                                    
    10
    if (i > 0)
    40
    if (i > 0)
    11
    sb.append(",");
    41
    sb.append(",");
    12
    sb.append(entry.getCiteKey());
    42
    sb.append(entry.getCiteKey());
                    
    43
    i++;
    Preondition Violations
    Unmatched statement i++; cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted
    43
    i++;
    Precondition Violations (4)
    Row Violation
    1Type net.sf.jabref.BibtexEntry[] of variable entries does not match with type java.util.ArrayList<net.sf.jabref.BibtexEntry> of variable entries
    2Type net.sf.jabref.BibtexEntry[] of variable entries does not match with type java.util.ArrayList<net.sf.jabref.BibtexEntry> of variable entries
    3Type net.sf.jabref.BibtexEntry[] of variable entries does not match with type java.util.ArrayList<net.sf.jabref.BibtexEntry> of variable entries
    4Unmatched statement i++; cannot be moved before or after the extracted code, because it has dependencies to/from statements that will be extracted