Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Oobranch e : add backend #7790

Merged
merged 74 commits into from
Aug 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
74 commits
Select commit Hold shift + click to select a range
a50cc79
step0 : start model/openoffice, logic/openoffice/style
antalk2 May 27, 2021
d93bd56
correction: import order
antalk2 May 27, 2021
c9b72e9
add general utilities
antalk2 May 27, 2021
381a549
add UNO utilities, move CreationException, NoDocumentException
antalk2 May 27, 2021
f94c1f6
Xlint:unchecked model/openoffice/util
antalk2 May 28, 2021
f9a8176
add ootext
antalk2 May 28, 2021
6d013f1
add rangesort
antalk2 May 28, 2021
0434579
add compareStartsUnsafe, compareStartsThenEndsUnsafe
antalk2 Jun 3, 2021
3c45a8a
add Tuple3
antalk2 Jun 3, 2021
32f7a43
add ootext
antalk2 May 28, 2021
6bae6b4
add rangesort
antalk2 May 28, 2021
314902c
delNamesArray size correction
antalk2 Jun 3, 2021
dfe18bc
rangeSort update
antalk2 Jun 3, 2021
c9f4e57
Merge remote-tracking branch 'origin/oobranch-C' into oobranch-C
antalk2 Jun 3, 2021
6e4c7c2
cleanup
antalk2 Jun 3, 2021
9388b2a
style additions
antalk2 Jun 3, 2021
d2a53c7
add backend
antalk2 Jun 3, 2021
88cab9c
checkstyle on tests
antalk2 Jun 3, 2021
b7aaf2a
add missing message
antalk2 Jun 3, 2021
0fa9a4c
add backend
antalk2 Jun 3, 2021
946242f
Merge remote-tracking branch 'origin/oobranch-E' into oobranch-E
antalk2 Jun 3, 2021
a26aace
apply oobranch-D-update.patch
antalk2 Jun 19, 2021
047fa64
apply oobranch-E-update.patch
antalk2 Jun 19, 2021
5a27d9c
Merge remote-tracking branch 'upstream/main' into oobranch-E
antalk2 Jun 19, 2021
c80d94d
deleted src/main/java/org/jabref/model/openoffice/rangesort/RangeS…
antalk2 Jun 19, 2021
8daa056
use StringUtil.isNullOrEmpty
antalk2 Jun 22, 2021
87d08de
no natural sort for ComparableMark
antalk2 Jun 22, 2021
7b908b4
in response to review
antalk2 Jul 7, 2021
cf5992e
use {@code }, PMD suggestions
antalk2 Jul 9, 2021
dc69a49
Merge remote-tracking branch 'upstream/main' into oobranch-E
antalk2 Jul 13, 2021
d9e07a1
update logic/style from improve-reversibility-rebased-03
antalk2 Jul 13, 2021
d05b94c
update model/style from improve-reversibility-rebased-03
antalk2 Jul 13, 2021
964a10e
replaced single-character names in OOBibStyle.java (in changed part)
antalk2 Jul 18, 2021
71c5acc
some longer names in OOBibStyleGetCitationMarker.java
antalk2 Jul 18, 2021
75b0ca8
drop normalizePageInfos, use 'preferred' and 'fallback' in getAuthorL…
antalk2 Jul 18, 2021
3da5dc1
checkstyle
antalk2 Jul 18, 2021
139c74d
use putIfAbsent
antalk2 Jul 18, 2021
e1d4474
use "{}" with LOGGER
antalk2 Jul 18, 2021
5a89a7e
use Objects.hash and Objects.equals in CitationLookupResult
antalk2 Jul 18, 2021
675989c
simplified CitedKey.getBibEntry
antalk2 Jul 18, 2021
e43a101
more use of "{}" in LOGGER
antalk2 Jul 18, 2021
ccf6de1
more use of "{}" in LOGGER
antalk2 Jul 18, 2021
f7b4776
Citation.lookup: use streams
antalk2 Jul 24, 2021
1fa5aef
Citation.lookup: Optional::get before findFirst
antalk2 Jul 24, 2021
8d46bce
Merge remote-tracking branch 'upstream/main' into oobranch-E
antalk2 Aug 2, 2021
3f55553
putIfAbsent returns null for new entry
antalk2 Aug 2, 2021
29a57ab
What is 52 in Backend52
antalk2 Aug 3, 2021
b80a174
apply 2021-08-20-a/oobranch-E-update.patch
antalk2 Aug 20, 2021
f517627
putIfAbsent returns null for new entry
antalk2 Aug 2, 2021
cc2d534
What is 52 in Backend52
antalk2 Aug 3, 2021
62ce321
using orElseThrow
antalk2 Aug 18, 2021
ceacd4b
using StringBuilder
antalk2 Aug 18, 2021
e6e2e91
refMarkName renamed to markName
antalk2 Aug 18, 2021
86b35a6
import StringBuilder is not needed
antalk2 Aug 19, 2021
d7da551
orElseThrow correction (now without message)
antalk2 Aug 19, 2021
6da3e08
update
antalk2 Aug 20, 2021
b0f754d
orElseThrow correction
antalk2 Aug 19, 2021
072f44b
renamed cg to group
antalk2 Aug 19, 2021
4496555
drop message not understandable by end user
antalk2 Aug 19, 2021
47584f3
reorganized reference mark name generation
antalk2 Aug 19, 2021
4db355a
renamed nCitations to numberOfCitations and totalCitations
antalk2 Aug 19, 2021
3e1bd0a
format
antalk2 Aug 19, 2021
33294f2
insert dummy case JabRef60: branch
antalk2 Aug 19, 2021
706d909
indent case-label in model/style
antalk2 Aug 19, 2021
4d0e8bd
indent case-label in logic/backend
antalk2 Aug 19, 2021
e36f168
indent case-label in model/ootext
antalk2 Aug 19, 2021
9071d94
rname cgPageInfos tro pageInfos
antalk2 Aug 19, 2021
1e469e0
renamed cgPageInfo to singlePageInfo
antalk2 Aug 19, 2021
8649239
replace Collectors.toSet() with new HashSet<>()
antalk2 Aug 19, 2021
66112e4
use method reference
antalk2 Aug 19, 2021
8e451dc
drop two comments
antalk2 Aug 19, 2021
531141d
use String.join
antalk2 Aug 19, 2021
0b72e35
remove nr/nrm prefixes from NamedRange and NamedRangeManager methods
antalk2 Aug 19, 2021
10724cc
align dots
antalk2 Aug 20, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
447 changes: 447 additions & 0 deletions src/main/java/org/jabref/logic/openoffice/backend/Backend52.java

Large diffs are not rendered by default.

137 changes: 137 additions & 0 deletions src/main/java/org/jabref/logic/openoffice/backend/Codec52.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,137 @@
package org.jabref.logic.openoffice.backend;

import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;

import org.jabref.model.openoffice.style.CitationType;

/**
* How and what is encoded in reference mark names under JabRef 5.2.
*
* - pageInfo does not appear here. It is not encoded in the mark name.
*/
class Codec52 {
private static final String BIB_CITATION = "JR_cite";
private static final Pattern CITE_PATTERN =
// Pattern.compile(BIB_CITATION + "(\\d*)_(\\d*)_(.*)");
// citationType is always "1" "2" or "3"
Pattern.compile(BIB_CITATION + "(\\d*)_([123])_(.*)");

private Codec52() {
/**/
}

/**
* This is what we get back from parsing a refMarkName.
*/
public static class ParsedMarkName {
/** "", "0", "1" ... */
public final String i;
/** in-text-citation type */
public final CitationType citationType;
/** Citation keys embedded in the reference mark. */
public final List<String> citationKeys;

ParsedMarkName(String i, CitationType citationType, List<String> citationKeys) {
Objects.requireNonNull(i);
Objects.requireNonNull(citationKeys);
this.i = i;
this.citationType = citationType;
this.citationKeys = citationKeys;
}
}

/**
* Integer representation was written into the document in JabRef52, keep it for compatibility.
*/
private static CitationType citationTypeFromInt(int i) {
return switch (i) {
case 1 -> CitationType.AUTHORYEAR_PAR;
case 2 -> CitationType.AUTHORYEAR_INTEXT;
case 3 -> CitationType.INVISIBLE_CIT;
default -> throw new IllegalArgumentException("Invalid CitationType code");
};
}

private static int citationTypeToInt(CitationType i) {
return switch (i) {
case AUTHORYEAR_PAR -> 1;
case AUTHORYEAR_INTEXT -> 2;
case INVISIBLE_CIT -> 3;
default -> throw new IllegalArgumentException("Invalid CitationType");
};
}

/**
* Produce a reference mark name for JabRef for the given citationType and list citation keys that
* does not yet appear among the reference marks of the document.
*
* @param usedNames Reference mark names already in use.
* @param citationKeys Identifies the cited sources.
* @param citationType Encodes the effect of withText and inParenthesis options.
*
* The first occurrence of citationKeys gets no serial number, the second gets 0, the third 1 ...
*
* Or the first unused in this series, after removals.
*/
public static String getUniqueMarkName(Set<String> usedNames,
List<String> citationKeys,
CitationType citationType) {

String citationKeysPart = String.join(",", citationKeys);

int i = 0;
int citTypeCode = citationTypeToInt(citationType);
String name = BIB_CITATION + '_' + citTypeCode + '_' + citationKeysPart;
while (usedNames.contains(name)) {
name = BIB_CITATION + i + '_' + citTypeCode + '_' + citationKeysPart;
i++;
}
return name;
}

/**
* Parse a JabRef (reference) mark name.
*
* @return Optional.empty() on failure.
*
*/
public static Optional<ParsedMarkName> parseMarkName(String refMarkName) {

Matcher citeMatcher = CITE_PATTERN.matcher(refMarkName);
if (!citeMatcher.find()) {
return Optional.empty();
}

List<String> keys = Arrays.asList(citeMatcher.group(3).split(","));
String i = citeMatcher.group(1);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i is normally used only in loops, but for the moment it doesn't matter. You can fix this in a later PR.

int citTypeCode = Integer.parseInt(citeMatcher.group(2));
CitationType citationType = citationTypeFromInt(citTypeCode);
return (Optional.of(new Codec52.ParsedMarkName(i, citationType, keys)));
}

/**
* @return true if name matches the pattern used for JabRef
* reference mark names.
*/
public static boolean isJabRefReferenceMarkName(String name) {
return (CITE_PATTERN.matcher(name).find());
}

/**
* Filter a list of reference mark names by `isJabRefReferenceMarkName`
*
* @param names The list to be filtered.
*/
public static List<String> filterIsJabRefReferenceMarkName(List<String> names) {
return (names.stream()
.filter(Codec52::isJabRefReferenceMarkName)
.collect(Collectors.toList()));
}
}
81 changes: 81 additions & 0 deletions src/main/java/org/jabref/logic/openoffice/backend/GetContext.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package org.jabref.logic.openoffice.backend;

import com.sun.star.text.XTextCursor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Utility methods for processing OO Writer documents.
*/
public class GetContext {

private static final Logger LOGGER = LoggerFactory.getLogger(GetContext.class);

private GetContext() {
// Just to hide the public constructor
}

/**
* Get the text belonging to cursor with up to
* charBefore and charAfter characters of context.
*
* The actual context may be smaller than requested.
*
* @param cursor
* @param charBefore Number of characters requested.
* @param charAfter Number of characters requested.
* @param htmlMarkup If true, the text belonging to the reference mark is surrounded by bold
* html tag.
*
*/
public static String getCursorStringWithContext(XTextCursor cursor,
int charBefore,
int charAfter,
boolean htmlMarkup) {

String citPart = cursor.getString();

// extend cursor range left
int flex = 8;
for (int i = 0; i < charBefore; i++) {
try {
cursor.goLeft((short) 1, true);
// If we are close to charBefore and see a space, then cut here. Might avoid cutting
// a word in half.
if ((i >= (charBefore - flex))
&& Character.isWhitespace(cursor.getString().charAt(0))) {
break;
}
} catch (IndexOutOfBoundsException ex) {
LOGGER.warn("Problem going left", ex);
}
}

int lengthWithBefore = cursor.getString().length();
int addedBefore = lengthWithBefore - citPart.length();

cursor.collapseToStart();
for (int i = 0; i < (charAfter + lengthWithBefore); i++) {
try {
cursor.goRight((short) 1, true);
if (i >= ((charAfter + lengthWithBefore) - flex)) {
String strNow = cursor.getString();
if (Character.isWhitespace(strNow.charAt(strNow.length() - 1))) {
break;
}
}
} catch (IndexOutOfBoundsException ex) {
LOGGER.warn("Problem going right", ex);
}
}

String result = cursor.getString();
if (htmlMarkup) {
result = (result.substring(0, addedBefore)
+ "<b>" + citPart + "</b>"
+ result.substring(lengthWithBefore));
}
return result.trim();
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
package org.jabref.logic.openoffice.backend;

import java.util.List;
import java.util.Optional;

import org.jabref.model.openoffice.backend.NamedRange;
import org.jabref.model.openoffice.backend.NamedRangeManager;
import org.jabref.model.openoffice.uno.CreationException;
import org.jabref.model.openoffice.uno.NoDocumentException;
import org.jabref.model.openoffice.uno.UnoReferenceMark;

import com.sun.star.lang.WrappedTargetException;
import com.sun.star.text.XTextCursor;
import com.sun.star.text.XTextDocument;

public class NamedRangeManagerReferenceMark implements NamedRangeManager {

@Override
public NamedRange createNamedRange(XTextDocument doc,
String refMarkName,
XTextCursor position,
boolean insertSpaceAfter,
boolean withoutBrackets)
throws
CreationException {
return NamedRangeReferenceMark.create(doc, refMarkName, position, insertSpaceAfter, withoutBrackets);
}

@Override
public List<String> getUsedNames(XTextDocument doc)
throws
NoDocumentException {
return UnoReferenceMark.getListOfNames(doc);
}

@Override
public Optional<NamedRange> getNamedRangeFromDocument(XTextDocument doc, String refMarkName)
throws
NoDocumentException,
WrappedTargetException {
return (NamedRangeReferenceMark
.getFromDocument(doc, refMarkName)
.map(x -> x));
}
}

Loading