Skip to content

Commit

Permalink
Merge pull request #874 from Siedlerchr/ooCitation
Browse files Browse the repository at this point in the history
Fixing OpenOffice Citation with curly braces and manual connection browsing
  • Loading branch information
oscargus committed Mar 2, 2016
2 parents f3eb02a + b38d1ce commit a824074
Show file tree
Hide file tree
Showing 6 changed files with 216 additions and 240 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,8 +57,9 @@ to [sourceforge feature requests](https://sourceforge.net/p/jabref/features/) by
- Fixed [#803](https://github.com/JabRef/jabref/issues/803): Fixed dynamically group, free-form search
- Fixed [#743](https://github.com/JabRef/jabref/issues/743): Logger not configured when JAR is started
- Fixed [#822](https://github.com/JabRef/jabref/issues/822):OSX - Exception when adding the icon to the dock

- Fixed [#685](https://github.com/JabRef/jabref/issues/685): Fixed MySQL exporting for more than one entry
- Fixed [#815](https://github.com/JabRef/jabref/issues/815): Curly Braces no longer ignored in OpenOffice/LibreOffice citation
- Fixed [#855](https://github.com/JabRef/jabref/issues/856): Fixed OpenOffice Manual connect - Clicking on browse does now work correctly

### Removed
- Fixed [#627](https://github.com/JabRef/jabref/issues/627): The pdf field is removed from the export formats, use the file field
Expand Down
27 changes: 25 additions & 2 deletions src/main/java/net/sf/jabref/logic/util/strings/StringUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -411,12 +411,35 @@ public static String unifyLineBreaksToConfiguredLineBreaks(String s) {
return LINE_BREAKS.matcher(s).replaceAll(Globals.NEWLINE);
}

/**
* Checks if the given String has exactly one pair of surrounding curly braces <br>
* Strings with escaped characters in curly braces at the beginning and end are respected, too
* @param toCheck The string to check
* @return True, if the check was succesful. False otherwise.
*/
public static boolean isInCurlyBrackets(String toCheck) {
int count = 0;
int brackets = 0;
if ((toCheck == null) || toCheck.isEmpty()) {
return false; // In case of null or empty string
return false;
} else {
return (toCheck.charAt(0) == '{') && (toCheck.charAt(toCheck.length() - 1) == '}');
if ((toCheck.charAt(0) == '{') && (toCheck.charAt(toCheck.length() - 1) == '}')) {
for (char c : toCheck.toCharArray()) {
if (c == '{') {
if (brackets == 0) {
count++;
}
brackets++;
} else if (c == '}') {
brackets--;
}
}

return count == 1;
}
return false;
}

}

public static boolean isInSquareBrackets(String toCheck) {
Expand Down
61 changes: 29 additions & 32 deletions src/main/java/net/sf/jabref/openoffice/OOBibStyle.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import net.sf.jabref.logic.layout.Layout;
import net.sf.jabref.logic.layout.LayoutFormatter;
import net.sf.jabref.logic.layout.LayoutHelper;
import net.sf.jabref.logic.util.strings.StringUtil;

import java.io.*;
import java.nio.charset.Charset;
Expand Down Expand Up @@ -117,7 +118,6 @@ class OOBibStyle implements Comparable<OOBibStyle> {
private static final String AUTHOR_LAST_SEPARATOR = "AuthorLastSeparator";
private static final String AUTHOR_SEPARATOR = "AuthorSeparator";


private final JournalAbbreviationRepository repository;
private static final Pattern QUOTED = Pattern.compile("\".*\"");

Expand Down Expand Up @@ -472,7 +472,6 @@ public String getNumCitationMarker(List<Integer> number, int minGroupingCount, b
return sb.toString();
}


/**
* Format the marker for the in-text citation according to this bib style. Uniquefier letters are added as
* provided by the uniquefiers argument. If successive entries within the citation are uniquefied from each other,
Expand All @@ -488,7 +487,7 @@ public String getNumCitationMarker(List<Integer> number, int minGroupingCount, b
* @return The formatted citation.
*/
public String getCitationMarker(List<BibEntry> entries, Map<BibEntry, BibDatabase> database, boolean inParenthesis,
String[] uniquefiers, int[] unlimAuthors) {
String[] uniquefiers, int[] unlimAuthors) {
// Look for groups of uniquefied entries that should be combined in the output.
// E.g. (Olsen, 2005a, b) should be output instead of (Olsen, 2005a; Olsen, 2005b).
int piv = -1;
Expand All @@ -504,8 +503,7 @@ public String getCitationMarker(List<BibEntry> entries, Map<BibEntry, BibDatabas
if (piv == -1) {
piv = i;
tmpMarker = getAuthorYearParenthesisMarker(Collections.singletonList(entries.get(i)), tmpMap,
authorField,
(String) citProperties.get(YEAR_FIELD), maxAuthors,
authorField, (String) citProperties.get(YEAR_FIELD), maxAuthors,
(String) citProperties.get(AUTHOR_SEPARATOR),
(String) citProperties.get(AUTHOR_LAST_SEPARATOR),
(String) citProperties.get(ET_AL_STRING), (String) citProperties.get(YEAR_SEPARATOR),
Expand All @@ -514,14 +512,14 @@ public String getCitationMarker(List<BibEntry> entries, Map<BibEntry, BibDatabas
} else {
// See if this entry can go into a group with the previous one:
String thisMarker = getAuthorYearParenthesisMarker(Collections.singletonList(entries.get(i)),
tmpMap,
authorField, (String) citProperties.get(YEAR_FIELD), maxAuthors,
tmpMap, authorField, (String) citProperties.get(YEAR_FIELD), maxAuthors,
(String) citProperties.get(AUTHOR_SEPARATOR),
(String) citProperties.get(AUTHOR_LAST_SEPARATOR),
(String) citProperties.get(ET_AL_STRING), (String) citProperties.get(YEAR_SEPARATOR),
(String) citProperties.get(BRACKET_BEFORE), (String) citProperties.get(BRACKET_AFTER),
(String) citProperties.get(CITATION_SEPARATOR), null, unlimAuthors);


String author = getCitationMarkerField(entries.get(i), database.get(entries.get(i)),
authorField);
AuthorList al = AuthorList.getAuthorList(author);
Expand Down Expand Up @@ -560,36 +558,25 @@ public String getCitationMarker(List<BibEntry> entries, Map<BibEntry, BibDatabas
}

if (inParenthesis) {
return getAuthorYearParenthesisMarker(entries, database,
(String) citProperties.get(AUTHOR_FIELD),
(String) citProperties.get(YEAR_FIELD),
(Integer) citProperties.get(MAX_AUTHORS),
(String) citProperties.get(AUTHOR_SEPARATOR),
(String) citProperties.get(AUTHOR_LAST_SEPARATOR),
(String) citProperties.get(ET_AL_STRING),
(String) citProperties.get(YEAR_SEPARATOR),
(String) citProperties.get(BRACKET_BEFORE),
(String) citProperties.get(BRACKET_AFTER),
(String) citProperties.get(CITATION_SEPARATOR),
uniquefiers, unlimAuthors);
return getAuthorYearParenthesisMarker(entries, database, (String) citProperties.get(AUTHOR_FIELD),
(String) citProperties.get(YEAR_FIELD), (Integer) citProperties.get(MAX_AUTHORS),
(String) citProperties.get(AUTHOR_SEPARATOR), (String) citProperties.get(AUTHOR_LAST_SEPARATOR),
(String) citProperties.get(ET_AL_STRING), (String) citProperties.get(YEAR_SEPARATOR),
(String) citProperties.get(BRACKET_BEFORE), (String) citProperties.get(BRACKET_AFTER),
(String) citProperties.get(CITATION_SEPARATOR), uniquefiers, unlimAuthors);
} else {
String authorLastSeparator = (String) citProperties.get(AUTHOR_LAST_SEPARATOR);
String alsInText = (String) citProperties.get(AUTHOR_LAST_SEPARATOR_IN_TEXT);
if (alsInText != null) {
authorLastSeparator = alsInText;
}
return getAuthorYearInTextMarker(entries, database,
(String) citProperties.get(AUTHOR_FIELD),
(String) citProperties.get(YEAR_FIELD),
(Integer) citProperties.get(MAX_AUTHORS),
(String) citProperties.get(AUTHOR_SEPARATOR),
authorLastSeparator,
(String) citProperties.get(ET_AL_STRING),
(String) citProperties.get(IN_TEXT_YEAR_SEPARATOR),
(String) citProperties.get(BRACKET_BEFORE),
(String) citProperties.get(BRACKET_AFTER),
(String) citProperties.get(CITATION_SEPARATOR),
uniquefiers, unlimAuthors);

return getAuthorYearInTextMarker(entries, database, (String) citProperties.get(AUTHOR_FIELD),
(String) citProperties.get(YEAR_FIELD), (Integer) citProperties.get(MAX_AUTHORS),
(String) citProperties.get(AUTHOR_SEPARATOR), authorLastSeparator,
(String) citProperties.get(ET_AL_STRING), (String) citProperties.get(IN_TEXT_YEAR_SEPARATOR),
(String) citProperties.get(BRACKET_BEFORE), (String) citProperties.get(BRACKET_AFTER),
(String) citProperties.get(CITATION_SEPARATOR), uniquefiers, unlimAuthors);
}
}

Expand Down Expand Up @@ -732,20 +719,30 @@ private String getAuthorYearInTextMarker(List<BibEntry> entries, Map<BibEntry, B
* @return The resolved field content, or an empty string if the field(s) were empty.
*/
private String getCitationMarkerField(BibEntry entry, BibDatabase database, String field) {
String authorField = (String) citProperties.get(AUTHOR_FIELD);
String[] fields = field.split("/");
for (String s : fields) {
String content = BibDatabase.getResolvedField(s, entry, database);

if ((content != null) && !content.trim().isEmpty()) {
if (fieldFormatter != null) {
content = fieldFormatter.format(content);

if (field.equals(authorField) && StringUtil.isInCurlyBrackets(content)) {
content = fieldFormatter.format(content);
content = "{" + content + "}";
return content;
}
return fieldFormatter.format(content);
}
return content;

}
}
// No luck? Return an empty string:
return "";
}


/**
* Look up the nth author and return the proper last name for citation markers.
*
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/net/sf/jabref/openoffice/OOPreFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ public String format(String field) {
.replace("\\$", "&dollar;") // Replace \$ with &dollar;
.replaceAll("\\$([^\\$]*)\\$", "\\{$1\\}"); // Replace $...$ with {...} to simplify conversion



StringBuilder sb = new StringBuilder();
StringBuilder currentCommand = null;

Expand Down Expand Up @@ -66,7 +68,8 @@ public String format(String field) {
incommand = true;
currentCommand = new StringBuilder();
} else if (!incommand && ((c == '{') || (c == '}'))) {
// Swallow the brace.
//Swallow braces, necessary for replacing encoded characters

} else if (Character.isLetter(c) || (c == '%')
|| Globals.SPECIAL_COMMAND_CHARS.contains(String.valueOf(c))) {
escaped = false;
Expand Down
Loading

0 comments on commit a824074

Please sign in to comment.