XSSF external workbook lookup support for in-line file:/// based book "numbers"

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1614724 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nick Burch 2014-07-30 17:53:54 +00:00
parent c62aa2d74b
commit d03c10f257

View File

@ -98,12 +98,45 @@ public final class XSSFEvaluationWorkbook implements FormulaRenderingWorkbook, E
// Look up an External Link Table for this name
List<ExternalLinksTable> tables = _uBook.getExternalLinksTable();
int index = findExternalLinkIndex(bookName, tables);
if (index != -1) return index;
// Is it an absolute file reference?
if (bookName.startsWith("'file:///") && bookName.endsWith("'")) {
String relBookName = bookName.substring(bookName.lastIndexOf('/')+1);
relBookName = relBookName.substring(0, relBookName.length()-1); // Trailing '
// Try with this name
index = findExternalLinkIndex(relBookName, tables);
if (index != -1) return index;
// If we get here, it's got no associated proper links yet
// So, add the missing reference and return
// Note - this is really rather nasty...
ExternalLinksTable fakeLinkTable = new FakeExternalLinksTable(relBookName);
tables.add(fakeLinkTable);
return tables.size(); // 1 based results, 0 = current workbook
}
// Not properly referenced
throw new RuntimeException("Book not linked for filename " + bookName);
}
private int findExternalLinkIndex(String bookName, List<ExternalLinksTable> tables) {
for (int i=0; i<tables.size(); i++) {
if (tables.get(i).getLinkedFileName().equals(bookName)) {
return i;
return i+1; // 1 based results, 0 = current workbook
}
}
throw new RuntimeException("Book not linked for filename " + bookName);
return -1;
}
private static class FakeExternalLinksTable extends ExternalLinksTable {
private final String fileName;
private FakeExternalLinksTable(String fileName) {
this.fileName = fileName;
}
public String getLinkedFileName() {
return fileName;
}
}
public EvaluationName getName(String name, int sheetIndex) {