Skip to content

Commit

Permalink
Fixed JabRef#856: Clicking on browse for manual OpenOffice does now work
Browse files Browse the repository at this point in the history
Fixed JabRef#815: Curly braces in OO/LO citation no longer ignored
- Improved the handling of curly braces in StringUtil method
- Added new test cases
- Code cleanup: Replaced ActionListener with Lamdbas
  • Loading branch information
Siedlerchr committed Feb 28, 2016
1 parent 69e4fe2 commit a3fd172
Show file tree
Hide file tree
Showing 6 changed files with 240 additions and 241 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,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 @@ -419,12 +419,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
86 changes: 53 additions & 33 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);

//Have a look at here
String author = getCitationMarkerField(entries.get(i), database.get(entries.get(i)),
authorField);
AuthorList al = AuthorList.getAuthorList(author);
Expand Down Expand Up @@ -560,36 +558,26 @@ 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);

//BracketBeforeAfter sind die definierten Brackets []
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 @@ -652,7 +640,7 @@ private String getAuthorYearParenthesisMarker(List<BibEntry> entries, Map<BibEnt
sb.append(citationSeparator);
}

String author = getCitationMarkerField(entry, database.get(entry), authorField);
String author = getAuthorCitationMarkerField(entry, database.get(entry), authorField);
String authorString = createAuthorList(author, maxAuthors, authorSep, andString, etAlString, yearSep);
sb.append(authorString);
String year = getCitationMarkerField(entry, database.get(entry), yearField);
Expand Down Expand Up @@ -704,7 +692,7 @@ private String getAuthorYearInTextMarker(List<BibEntry> entries, Map<BibEntry, B
if (i > 0) {
sb.append(citationSeparator);
}
String author = getCitationMarkerField(entries.get(i), database.get(entries.get(i)), authorField);
String author = getAuthorCitationMarkerField(entries.get(i), database.get(entries.get(i)), authorField);
String authorString = createAuthorList(author, maxAuthors, authorSep, andString, etAlString, yearSep);
sb.append(authorString);
sb.append(startBrace);
Expand Down Expand Up @@ -735,6 +723,7 @@ private String getCitationMarkerField(BibEntry entry, BibDatabase database, Stri
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);
Expand All @@ -746,6 +735,37 @@ private String getCitationMarkerField(BibEntry entry, BibDatabase database, Stri
return "";
}

/**
* This method is similar to {@link #getCitationMarkerField(BibEntry, BibDatabase, String)} but handles the special case for curly brackets in the author field
* @see OOBibStyle#getCitationMarkerField(BibEntry, BibDatabase, String)
* @param entry
* @param database
* @param field
* @return
*/
private String getAuthorCitationMarkerField(BibEntry entry, BibDatabase database, String 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) {

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

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

Please # to comment.