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 | } | | | |