Skip to content

Commit

Permalink
Merge pull request JabRef#603 from oscargus/coverityfix
Browse files Browse the repository at this point in the history
Removed Coverity warning in bst and cleaned up all files there
  • Loading branch information
simonharrer committed Jan 4, 2016
2 parents b5685cf + 947f065 commit 217af87
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 137 deletions.
18 changes: 14 additions & 4 deletions src/main/java/net/sf/jabref/bst/BibtexCaseChanger.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class BibtexCaseChanger {
public final class BibtexCaseChanger {

private static final Log LOGGER = LogFactory.getLog(BibtexCaseChanger.class);

Expand Down Expand Up @@ -52,12 +52,12 @@ public enum FORMAT_MODE {
// DIFFERENCE to old CaseChangers.TITLE: last word is NOT capitalized in all cases
//TITLE_UPPERS('T');

private final char asChar;

public char asChar() {
return asChar;
}

private final char asChar;

FORMAT_MODE(char asChar) {
this.asChar = asChar;
}
Expand Down Expand Up @@ -222,6 +222,9 @@ private int convertAccented(char[] c, int pos, String s, StringBuffer sb, FORMAT
sb.append(s);
}
break;
default:
LOGGER.info("convertAccented - Unknown format: " + format);
break;
}
return pos;
}
Expand All @@ -237,6 +240,9 @@ private int convertNonControl(char[] c, int pos, StringBuffer sb, FORMAT_MODE fo
sb.append(Character.toUpperCase(c[pos]));
pos++;
break;
default:
LOGGER.info("convertNonControl - Unknown format: " + format);
break;
}
return pos;
}
Expand All @@ -262,6 +268,10 @@ private int convertCharIfBraceLevelIsZero(char[] c, int i, StringBuffer sb, FORM
break;
case ALL_UPPERS:
sb.append(Character.toUpperCase(c[i]));
break;
default:
LOGGER.info("convertCharIfBraceLevelIsZero - Unknown format: " + format);
break;
}
i++;
return i;
Expand All @@ -277,7 +287,7 @@ private int convertCharIfBraceLevelIsZero(char[] c, int i, StringBuffer sb, FORM
* @param pos the position
* @return the special LaTeX character or null
*/
static Optional<String> findSpecialChar(char[] c, int pos) {
public static Optional<String> findSpecialChar(char[] c, int pos) {
if ((pos + 1) < c.length) {
if ((c[pos] == 'o') && (c[pos + 1] == 'e')) {
return Optional.of("oe");
Expand Down
60 changes: 28 additions & 32 deletions src/main/java/net/sf/jabref/bst/BibtexNameFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

/**
* From Bibtex:
*
*
* "The |built_in| function {\.{format.name\$}} pops the
* top three literals (they are a string, an integer, and a string
* literal, in that order). The last string literal represents a
Expand All @@ -30,9 +30,9 @@
* described in the \BibTeX\ documentation. Finally, this function
* pushes the formatted name. If any of the types is incorrect, it
* complains and pushes the null string."
*
*
* Sounds easy - is a nightmare... X-(
*
*
*/
public class BibtexNameFormatter {

Expand All @@ -47,7 +47,7 @@ public static String formatName(String authorsNameList, int whichName, String fo
}

/**
*
*
* @param author
* @param format
* @param warn may-be-null
Expand All @@ -66,7 +66,6 @@ public static String formatName(Author author, String format, Warn warn) {
while (i < n) {
if (c[i] == '{') {
group++;
int groupStart = sb.length();
i++;
braceLevel++;
StringBuffer level1Chars = new StringBuffer();
Expand All @@ -83,15 +82,15 @@ public static String formatName(Author author, String format, Warn warn) {
i++;
continue;
}
if (braceLevel == 1) {
if (Character.isLetter(c[i])) {
if ("fvlj".indexOf(c[i]) == -1) {
if (warn != null) {
warn.warn("Format String in format.name$ may only contain fvlj on brace level 1 in group " + group + ": " + format);
}
} else {
level1Chars.append(c[i]);
if ((braceLevel == 1) && Character.isLetter(c[i])) {
if ("fvlj".indexOf(c[i]) == -1) {
if (warn != null) {
warn.warn(
"Format String in format.name$ may only contain fvlj on brace level 1 in group "
+ group + ": " + format);
}
} else {
level1Chars.append(c[i]);
}
}
i++;
Expand Down Expand Up @@ -156,6 +155,7 @@ public static String formatName(Author author, String format, Warn warn) {
int bLevel = 1;

String interToken = null;
int groupStart = sb.length();

for (int j = 0; j < d.length; j++) {

Expand All @@ -164,12 +164,10 @@ public static String formatName(Author author, String format, Warn warn) {
if (!abbreviateThatIsSingleLetter) {
j++;
}
if ((j + 1) < d.length) {
if (d[j + 1] == '{') {
StringBuffer interTokenSb = new StringBuffer();
j = BibtexNameFormatter.consumeToMatchingBrace(interTokenSb, d, j + 1);
interToken = interTokenSb.substring(1, interTokenSb.length() - 1);
}
if (((j + 1) < d.length) && (d[j + 1] == '{')) {
StringBuffer interTokenSb = new StringBuffer();
j = BibtexNameFormatter.consumeToMatchingBrace(interTokenSb, d, j + 1);
interToken = interTokenSb.substring(1, interTokenSb.length() - 1);
}

for (int k = 0; k < tokens.length; k++) {
Expand All @@ -195,15 +193,15 @@ public static String formatName(Author author, String format, Warn warn) {
// Output Intertoken String
if (interToken == null) {
if (abbreviateThatIsSingleLetter) {
sb.append(".");
sb.append('.');
}
// No clue what this means (What the hell are tokens anyway???
// if (lex_class[name_sep_char[cur_token]] = sep_char) then
// append_ex_buf_char_and_check (name_sep_char[cur_token])
if ((k == (tokens.length - 2)) || (BibtexNameFormatter.numberOfChars(sb.substring(groupStart, sb.length()), 3) < 3)) {
sb.append("~");
sb.append('~');
} else {
sb.append(" ");
sb.append(' ');
}
} else {
sb.append(interToken);
Expand Down Expand Up @@ -242,7 +240,7 @@ public static String formatName(Author author, String format, Warn warn) {
}
i++;
}
if (braceLevel != 0) {
if ((braceLevel != 0) && (warn != null)) {
warn.warn("Unbalanced brace in format string for nameFormat: " + format);
}

Expand All @@ -251,12 +249,12 @@ public static String formatName(Author author, String format, Warn warn) {

/**
* Including the matching brace.
*
*
* @param sb
* @param c
* @param pos
* @return
*
*
* assert c[pos] == '{'
*/
public static int consumeToMatchingBrace(StringBuffer sb, char[] c, int pos) {
Expand All @@ -282,7 +280,7 @@ public static int consumeToMatchingBrace(StringBuffer sb, char[] c, int pos) {

/**
* Takes care of special characters too
*
*
* @param s
* @return
*/
Expand All @@ -292,12 +290,10 @@ public static String getFirstCharOfString(String s) {
if (Character.isLetter(c[i])) {
return String.valueOf(c[i]);
}
if (c[i] == '{') {
if (((i + 1) < c.length) && (c[i + 1] == '\\')) {
StringBuffer sb = new StringBuffer();
BibtexNameFormatter.consumeToMatchingBrace(sb, c, i);
return sb.toString();
}
if ((c[i] == '{') && ((i + 1) < c.length) && (c[i + 1] == '\\')) {
StringBuffer sb = new StringBuffer();
BibtexNameFormatter.consumeToMatchingBrace(sb, c, i);
return sb.toString();
}
}
return "";
Expand Down
10 changes: 5 additions & 5 deletions src/main/java/net/sf/jabref/bst/ChangeCaseFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

/**
* From the Bibtex manual:
*
*
* Pops the top two (string) literals; it changes the case of the second
* according to the specifications of the first, as follows. (Note: The word
* `letters' in the next sentence refers only to those at brace-level 0, the
Expand All @@ -41,9 +41,9 @@
* note: It ignores case differences in the specification string; for example,
* the strings t and T are equivalent for the purposes of this built-in
* function.)
*
*
* Christopher: I think this should be another grammar! This parser is horrible.
*
*
*/
public class ChangeCaseFunction implements BstFunction {

Expand All @@ -61,13 +61,13 @@ public void execute(BstEntry context) {
if (stack.size() < 2) {
throw new VMException("Not enough operands on stack for operation change.case$");
}
Object o1 = stack.pop();
Object o2 = stack.pop();

Object o1 = stack.pop();
if (!((o1 instanceof String) && (((String) o1).length() == 1))) {
throw new VMException("A format string of length 1 is needed for change.case$");
}

Object o2 = stack.pop();
if (!(o2 instanceof String)) {
throw new VMException("A string is needed as second parameter for change.case$");
}
Expand Down
12 changes: 6 additions & 6 deletions src/main/java/net/sf/jabref/bst/FormatNameFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

/**
* From Bibtex:
*
*
* "The |built_in| function {\.{format.name\$}} pops the
* top three literals (they are a string, an integer, and a string
* literal, in that order). The last string literal represents a
Expand All @@ -34,9 +34,9 @@
* described in the \BibTeX\ documentation. Finally, this function
* pushes the formatted name. If any of the types is incorrect, it
* complains and pushes the null string."
*
*
* All the pain is encapsulated in BibtexNameFormatter. :-)
*
*
*/
public class FormatNameFunction implements BstFunction {

Expand Down Expand Up @@ -68,16 +68,16 @@ public void execute(BstEntry context) {
Integer name = (Integer) o2;
String names = (String) o3;

if (names != null) {
if (names == null) {
stack.push("");
} else {
AuthorList a = AuthorList.getAuthorList(names);
if (name > a.size()) {
throw new VMException("Author Out of Bounds. Number " + name + " invalid for " + names);
}
Author author = a.getAuthor(name - 1);

stack.push(BibtexNameFormatter.formatName(author, format, vm));
} else {
stack.push("");
}
}
}
5 changes: 3 additions & 2 deletions src/main/java/net/sf/jabref/bst/TextPrefixFunction.java
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,15 @@ public void execute(BstEntry context) {
if (stack.size() < 2) {
throw new VMException("Not enough operands on stack for operation text.prefix$");
}
Object o1 = stack.pop();
Object o2 = stack.pop();

Object o1 = stack.pop();
if (!(o1 instanceof Integer)) {
vm.warn("An integer is needed as first parameter to text.prefix$");
stack.push("");
return;
}

Object o2 = stack.pop();
if (!(o2 instanceof String)) {
vm.warn("A string is needed as second parameter to text.prefix$");
stack.push("");
Expand Down
Loading

0 comments on commit 217af87

Please sign in to comment.