Skip to content

Commit

Permalink
Extract logic
Browse files Browse the repository at this point in the history
  • Loading branch information
stefan-kolb committed Jan 22, 2016
1 parent 6f20c3c commit cab451a
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 17 deletions.
88 changes: 75 additions & 13 deletions src/main/java/net/sf/jabref/gui/FileListTableModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Optional;

import javax.swing.JLabel;
Expand Down Expand Up @@ -126,15 +127,75 @@ public void setContentDontGuessTypes(String value) {
setContent(value, false, false);
}

private FileListEntry setContent(String value, boolean firstOnly, boolean deduceUnknownTypes) {
public List<FileListEntry> parseFileField(String value) {
if (value == null) {
value = "";
}
ArrayList<FileListEntry> newList = new ArrayList<>();

ArrayList<FileListEntry> files = new ArrayList<>();
ArrayList<String> entry = new ArrayList<>();

StringBuilder sb = new StringBuilder();
boolean inXmlChar = false;
boolean escaped = false;

for (int i = 0; i < value.length(); i++) {
char c = value.charAt(i);
if (!escaped && (c == '\\')) {
escaped = true;
continue;
}
// Check if we are entering an XML special character construct such
// as "&#44;", because we need to know in order to ignore the semicolon.
else if (!escaped && (c == '&') && !inXmlChar) {
sb.append(c);
if ((value.length() > (i + 1)) && (value.charAt(i + 1) == '#')) {
inXmlChar = true;
}
}
// Check if we are exiting an XML special character construct:
else if (!escaped && inXmlChar && (c == ';')) {
sb.append(c);
inXmlChar = false;
}
else if (!escaped && (c == ':')) {
entry.add(sb.toString());
sb = new StringBuilder();
}
else if (!escaped && (c == ';') && !inXmlChar) {
entry.add(sb.toString());
sb = new StringBuilder();

files.add(decodeEntry(entry, true));
entry.clear();
} else {
sb.append(c);
}
escaped = false;
}
if (sb.length() > 0) {
entry.add(sb.toString());
}

if (!entry.isEmpty()) {
files.add(decodeEntry(entry, true));
}

return files;
}

public FileListEntry setContent(String value, boolean firstOnly, boolean deduceUnknownTypes) {
if (value == null) {
value = "";
}

ArrayList<FileListEntry> files = new ArrayList<>();
ArrayList<String> entry = new ArrayList<>();

StringBuilder sb = new StringBuilder();
ArrayList<String> thisEntry = new ArrayList<>();
boolean inXmlChar = false;
boolean escaped = false;

for (int i = 0; i < value.length(); i++) {
char c = value.charAt(i);
if (!escaped && (c == '\\')) {
Expand All @@ -155,37 +216,38 @@ else if (!escaped && (c == '&') && !inXmlChar) {
inXmlChar = false;
}
else if (!escaped && (c == ':')) {
thisEntry.add(sb.toString());
entry.add(sb.toString());
sb = new StringBuilder();
}
else if (!escaped && (c == ';') && !inXmlChar) {
thisEntry.add(sb.toString());
entry.add(sb.toString());
sb = new StringBuilder();
if (firstOnly) {
return decodeEntry(thisEntry, deduceUnknownTypes);
return decodeEntry(entry, deduceUnknownTypes);
} else {
newList.add(decodeEntry(thisEntry, deduceUnknownTypes));
thisEntry.clear();
files.add(decodeEntry(entry, deduceUnknownTypes));
entry.clear();
}
} else {
sb.append(c);
}
escaped = false;
}
if (sb.length() > 0) {
thisEntry.add(sb.toString());
entry.add(sb.toString());
}
if (!thisEntry.isEmpty()) {

if (!entry.isEmpty()) {
if (firstOnly) {
return decodeEntry(thisEntry, deduceUnknownTypes);
return decodeEntry(entry, deduceUnknownTypes);
} else {
newList.add(decodeEntry(thisEntry, deduceUnknownTypes));
files.add(decodeEntry(entry, deduceUnknownTypes));
}
}

synchronized (list) {
list.clear();
list.addAll(newList);
list.addAll(files);
}
fireTableChanged(new TableModelEvent(this));
return null;
Expand Down
6 changes: 2 additions & 4 deletions src/main/java/net/sf/jabref/importer/DatabaseFileLookup.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ public DatabaseFileLookup(BibDatabase database) {
for (BibEntry entry : database.getEntries()) {
fileCache.addAll(parseFileField(entry));
}

}

/**
Expand Down Expand Up @@ -88,9 +87,8 @@ private List<File> parseFileField(BibEntry entry) {
model.setContent(fileField);

List<File> fileLinks = new ArrayList<>();
for (int i = 0; i < model.getRowCount(); i++) {
FileListEntry flEntry = model.getEntry(i);
String link = flEntry.getLink();
for (FileListEntry e : model.parseFileField(fileField)) {
String link = e.getLink();

if (link == null) {
break;
Expand Down

0 comments on commit cab451a

Please sign in to comment.