Skip to content

Commit 582fbec

Browse files
author
Igor Steinmacher
committed
Merge pull request #12 from IngvarJackal/master
BugFix for #959 "StringIndexOutOfBoundsException with invalid Preview text"
2 parents fc8c503 + 064f708 commit 582fbec

File tree

4 files changed

+44
-18
lines changed

4 files changed

+44
-18
lines changed

CHANGELOG

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- Support FindFullText with ACS DOIs (pull request #9)
1111
- Fixes groups and adds optional 2.9.2 save ordering (pull request #10)
1212
- Fixes bug 880 "PubMed Import broken" (pull request #11 by vegeziel)
13+
- Fixes bug #959 "StringIndexOutOfBoundsException with invalid Preview text" (pull request #12 by IngvarJackal)
1314
2.10
1415
- Made IEEEXploreFetcher author parsing work again.
1516
- Added a few more characters in the HTML/Unicode to LaTeX conversion.

src/main/java/net/sf/jabref/PreviewPanel.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,8 @@ public void setEntry(BibtexEntry newEntry) {
364364
try {
365365
readLayout();
366366
update();
367+
} catch (StringIndexOutOfBoundsException ex) {
368+
throw ex;
367369
} catch (Exception ex) {
368370
ex.printStackTrace();
369371
}

src/main/java/net/sf/jabref/PreviewPrefsTab.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -163,20 +163,30 @@ public void actionPerformed(ActionEvent e) {
163163
test1.addActionListener(new ActionListener() {
164164
public void actionPerformed(ActionEvent e) {
165165
getTestEntry();
166-
PreviewPanel testPanel = new PreviewPanel(null, entry, null , new MetaData(), layout1.getText());
167-
testPanel.setPreferredSize(new Dimension(800, 350));
168-
JOptionPane.showMessageDialog(null, testPanel, Globals.lang("Preview"),
169-
JOptionPane.PLAIN_MESSAGE);
166+
try {
167+
PreviewPanel testPanel = new PreviewPanel(null, entry, null , new MetaData(), layout1.getText());
168+
testPanel.setPreferredSize(new Dimension(800, 350));
169+
JOptionPane.showMessageDialog(null, testPanel, Globals.lang("Preview"),
170+
JOptionPane.PLAIN_MESSAGE);
171+
} catch (StringIndexOutOfBoundsException ex) {
172+
ex.printStackTrace();
173+
JOptionPane.showMessageDialog(null, "Parsing error: illegal backslash expression.\n" + ex.getMessage() + "\nLook at stderr for details.", "Parsing error", JOptionPane.WARNING_MESSAGE);
174+
}
170175
}
171176
});
172177

173178
test2.addActionListener(new ActionListener() {
174179
public void actionPerformed(ActionEvent e) {
175180
getTestEntry();
176-
PreviewPanel testPanel = new PreviewPanel(null, entry, null, new MetaData(), layout2.getText());
177-
testPanel.setPreferredSize(new Dimension(800, 350));
178-
JOptionPane.showMessageDialog(null, new JScrollPane(testPanel),
179-
Globals.lang("Preview"), JOptionPane.PLAIN_MESSAGE);
181+
try {
182+
PreviewPanel testPanel = new PreviewPanel(null, entry, null, new MetaData(), layout2.getText());
183+
testPanel.setPreferredSize(new Dimension(800, 350));
184+
JOptionPane.showMessageDialog(null, new JScrollPane(testPanel),
185+
Globals.lang("Preview"), JOptionPane.PLAIN_MESSAGE);
186+
} catch (StringIndexOutOfBoundsException ex) {
187+
ex.printStackTrace();
188+
JOptionPane.showMessageDialog(null, "Parsing error: illegal backslash expression.\n" + ex.getMessage() + "\nLook at stderr for details.", "Parsing error", JOptionPane.WARNING_MESSAGE);
189+
}
180190
}
181191
});
182192
}

src/main/java/net/sf/jabref/export/layout/LayoutHelper.java

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import java.io.Reader;
2121
import java.util.Vector;
2222

23+
import javax.swing.JOptionPane;
24+
2325

2426
/**
2527
* Helper class to get a Layout object.
@@ -62,8 +64,8 @@ public LayoutHelper(Reader in)
6264
}
6365

6466
public Layout getLayoutFromText(String classPrefix) throws Exception
65-
{
66-
parse();
67+
{
68+
parse();
6769

6870
StringInt si;
6971

@@ -76,7 +78,7 @@ public Layout getLayoutFromText(String classPrefix) throws Exception
7678
si.s = si.s.trim().toLowerCase();
7779
}
7880
}
79-
81+
8082
Layout layout = new Layout(parsedEntries, classPrefix);
8183

8284
return layout;
@@ -288,7 +290,7 @@ else if (c == '"') {
288290
return null;
289291
}
290292

291-
private Object parse() throws IOException {
293+
private Object parse() throws IOException, StringIndexOutOfBoundsException {
292294
skipWhitespace();
293295

294296
int c;
@@ -344,10 +346,11 @@ private Object parse() throws IOException {
344346
/**
345347
*
346348
*/
347-
private void parseField() throws IOException
349+
private void parseField() throws IOException, StringIndexOutOfBoundsException
348350
{
349351
int c;
350352
StringBuffer buffer = null;
353+
char firstLetter = ' ';
351354
String name;
352355

353356
while (!_eof)
@@ -365,11 +368,21 @@ private void parseField() throws IOException
365368

366369
//System.out.println("\n#" + (char) c);
367370
name = buffer != null ? buffer.toString() : "";
368-
371+
372+
try {
373+
firstLetter = name.charAt(0);
374+
} catch (StringIndexOutOfBoundsException ex) {
375+
StringBuilder lastFive = new StringBuilder(10);
376+
for (StringInt entry : parsedEntries.subList(Math.max(0, parsedEntries.size()-6), parsedEntries.size()-1)) {
377+
lastFive.append(entry.s);
378+
}
379+
throw new StringIndexOutOfBoundsException("Backslash parsing error near " + "\'" + lastFive.toString().replace("\n", " ") + "\'");
380+
}
381+
369382
//System.out.println("NAME:" + name);
370383
buffer = null;
371-
372-
if (name.charAt(0) == 'b')
384+
385+
if (firstLetter == 'b')
373386
{
374387
if (name.equalsIgnoreCase("begin"))
375388
{
@@ -385,7 +398,7 @@ else if (name.equalsIgnoreCase("begingroup"))
385398
return;
386399
}
387400
}
388-
else if (name.charAt(0) == 'f')
401+
else if (firstLetter == 'f')
389402
{
390403
if (name.equalsIgnoreCase("format"))
391404
{
@@ -422,7 +435,7 @@ else if (name.equalsIgnoreCase("filepath"))
422435
return;
423436
}
424437
}
425-
else if (name.charAt(0) == 'e')
438+
else if (firstLetter == 'e')
426439
{
427440
if (name.equalsIgnoreCase("end"))
428441
{

0 commit comments

Comments
 (0)