From: Yegor Kozlov Date: Fri, 10 Feb 2012 08:26:45 +0000 (+0000) Subject: Bugzilla 52576: support changing external file references in HSSFWorkbook X-Git-Tag: REL_3_8_FINAL~56 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=a3a8dcf33576a1bd11e6822fb8d2452c2dc4d41e;p=poi.git Bugzilla 52576: support changing external file references in HSSFWorkbook git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1242701 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index af3bbee4c2..86f2696b39 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,8 @@ + 52576 - support changing external file references in HSSFWorkbook + 49896 - support external references in FormulaRenderer 52527 - avoid exception when matching shared formula records in HSSF 52568 - Added methods to set/get an XWPFRun's text color 52566 - Added methods to set/get vertical alignment and color in XWPFTableCell diff --git a/src/java/org/apache/poi/hssf/model/InternalWorkbook.java b/src/java/org/apache/poi/hssf/model/InternalWorkbook.java index 93d37acc4d..c1ec938cf3 100644 --- a/src/java/org/apache/poi/hssf/model/InternalWorkbook.java +++ b/src/java/org/apache/poi/hssf/model/InternalWorkbook.java @@ -2457,4 +2457,18 @@ public final class InternalWorkbook { } return record; } + + + /** + * Changes an external referenced file to another file. + * A formular in Excel which refers a cell in another file is saved in two parts: + * The referenced file is stored in an reference table. the row/cell information is saved separate. + * This method invokation will only change the reference in the lookup-table itself. + * @param oldUrl The old URL to search for and which is to be replaced + * @param newUrl The URL replacement + * @return true if the oldUrl was found and replaced with newUrl. Otherwise false + */ + public boolean changeExternalReference(String oldUrl, String newUrl) { + return linkTable.changeExternalReference(oldUrl, newUrl); + } } diff --git a/src/java/org/apache/poi/hssf/model/LinkTable.java b/src/java/org/apache/poi/hssf/model/LinkTable.java index 732ab14803..9236a625b5 100644 --- a/src/java/org/apache/poi/hssf/model/LinkTable.java +++ b/src/java/org/apache/poi/hssf/model/LinkTable.java @@ -552,4 +552,27 @@ final class LinkTable { private int findRefIndexFromExtBookIndex(int extBookIndex) { return _externSheetRecord.findRefIndexFromExtBookIndex(extBookIndex); } + + /** + * Changes an external referenced file to another file. + * A formular in Excel which refers a cell in another file is saved in two parts: + * The referenced file is stored in an reference table. the row/cell information is saved separate. + * This method invokation will only change the reference in the lookup-table itself. + * @param oldUrl The old URL to search for and which is to be replaced + * @param newUrl The URL replacement + * @return true if the oldUrl was found and replaced with newUrl. Otherwise false + */ + public boolean changeExternalReference(String oldUrl, String newUrl) { + for(ExternalBookBlock ex : _externalBookBlocks) { + SupBookRecord externalRecord = ex.getExternalBookRecord(); + if (externalRecord.isExternalReferences() + && externalRecord.getURL().equals(oldUrl)) { + + externalRecord.setURL(newUrl); + return true; + } + } + return false; + } + } diff --git a/src/java/org/apache/poi/hssf/record/SupBookRecord.java b/src/java/org/apache/poi/hssf/record/SupBookRecord.java index b8be5deab4..29fa4d91c9 100644 --- a/src/java/org/apache/poi/hssf/record/SupBookRecord.java +++ b/src/java/org/apache/poi/hssf/record/SupBookRecord.java @@ -18,6 +18,8 @@ package org.apache.poi.hssf.record; import org.apache.poi.util.LittleEndianOutput; +import org.apache.poi.util.POILogFactory; +import org.apache.poi.util.POILogger; import org.apache.poi.util.StringUtil; /** @@ -31,6 +33,8 @@ import org.apache.poi.util.StringUtil; */ public final class SupBookRecord extends StandardRecord { + private final static POILogger logger = POILogFactory.getLogger(SupBookRecord.class); + public final static short sid = 0x01AE; private static final short SMALL_RECORD_SIZE = 4; @@ -42,6 +46,15 @@ public final class SupBookRecord extends StandardRecord { private String[] field_3_sheet_names; private boolean _isAddInFunctions; + protected static final char CH_VOLUME = 1; + protected static final char CH_SAME_VOLUME = 2; + protected static final char CH_DOWN_DIR = 3; + protected static final char CH_UP_DIR = 4; + protected static final char CH_LONG_VOLUME = 5; + protected static final char CH_STARTUP_DIR = 6; + protected static final char CH_ALT_STARTUP_DIR = 7; + protected static final char CH_LIB_DIR = 8; + protected static final String PATH_SEPERATOR = System.getProperty("file.separator"); public static SupBookRecord createInternalReferences(short numberOfSheets) { return new SupBookRecord(false, numberOfSheets); @@ -192,21 +205,51 @@ public final class SupBookRecord extends StandardRecord { return encodedUrl; } private static String decodeFileName(String encodedUrl) { - return encodedUrl.substring(1); - // TODO the following special characters may appear in the rest of the string, and need to get interpreted - /* see "MICROSOFT OFFICE EXCEL 97-2007 BINARY FILE FORMAT SPECIFICATION" - chVolume 1 - chSameVolume 2 - chDownDir 3 - chUpDir 4 - chLongVolume 5 - chStartupDir 6 - chAltStartupDir 7 - chLibDir 8 - - */ + /* see "MICROSOFT OFFICE EXCEL 97-2007 BINARY FILE FORMAT SPECIFICATION" */ + StringBuilder sb = new StringBuilder(); + for(int i=1; i