<!-- Don't forget to update status.xml too! -->
<release version="3.1-beta1" date="2008-??-??">
+ <action dev="POI-DEVELOPERS" type="add">44536 - Improved support for detecting read-only recommended files</action>
<action dev="POI-DEVELOPERS" type="fix">43901 - Correctly update the internal last cell number when adding and removing cells (previously sometimes off-by-one)</action>
<action dev="POI-DEVELOPERS" type="fix">28231 - For apparently truncated files, which are somehow still valid, now issue a truncation warning but carry on, rather than giving an exception as before</action>
<action dev="POI-DEVELOPERS" type="fix">44504 - Added initial support for recognising external functions like YEARFRAC and ISEVEN (using NameXPtg), via LinkTable support</action>
<!-- Don't forget to update changes.xml too! -->
<changes>
<release version="3.1-beta1" date="2008-??-??">
+ <action dev="POI-DEVELOPERS" type="add">44536 - Improved support for detecting read-only recommended files</action>
<action dev="POI-DEVELOPERS" type="fix">43901 - Correctly update the internal last cell number when adding and removing cells (previously sometimes off-by-one)</action>
<action dev="POI-DEVELOPERS" type="fix">44504 - Added initial support for recognising external functions like YEARFRAC and ISEVEN (using NameXPtg), via LinkTable support</action>
<action dev="POI-DEVELOPERS" type="fix">44504 - Improvements to FormulaParser - operators, precedence, error literals, quotes in string literals, range checking on IntPtg, formulas with extra un-parsed stuff at the end, improved parse error handling</action>
}
return this.fileShare;
}
+
+ /**
+ * is the workbook protected with a password (not encrypted)?
+ */
+ public boolean isWriteProtected() {
+ if (this.fileShare == null) {
+ return false;
+ }
+ FileSharingRecord frec = getFileSharing();
+ return (frec.getReadOnly() == 1);
+ }
/**
* protect a workbook with a password (not encypted, just sets writeprotect
package org.apache.poi.hssf.record;
import org.apache.poi.util.LittleEndian;
+import org.apache.poi.util.POILogFactory;
+import org.apache.poi.util.POILogger;
import org.apache.poi.util.StringUtil;
/**
*/
public class FileSharingRecord extends Record {
+ private static POILogger logger = POILogFactory.getLogger(FileSharingRecord.class);
+
public final static short sid = 0x5b;
private short field_1_readonly;
private short field_2_password;
field_1_readonly = in.readShort();
field_2_password = in.readShort();
field_3_username_length = in.readByte();
+
+ // Is this really correct? The latest docs
+ // seem to hint there's nothing between the
+ // username length and the username string
field_4_unknown = in.readShort();
- field_5_username = in.readCompressedUnicode(field_3_username_length);
+
+ // Ensure we don't try to read more data than
+ // there actually is
+ if(field_3_username_length > in.remaining()) {
+ logger.log(POILogger.WARN, "FileSharingRecord defined a username of length " + field_3_username_length + ", but only " + in.remaining() + " bytes were left, truncating");
+ field_3_username_length = (byte)in.remaining();
+ }
+ if(field_3_username_length > 0) {
+ field_5_username = in.readCompressedUnicode(field_3_username_length);
+ } else {
+ field_5_username = "";
+ }
}
//this is the world's lamest "security". thanks to Wouter van Vugt for making me
public String readCompressedUnicode(int length) {
if ((length < 0) || ((remaining() < length) && !isContinueNext())) {
- throw new IllegalArgumentException("Illegal length");
+ throw new IllegalArgumentException("Illegal length " + length);
}
StringBuffer buf = new StringBuffer(length);
}
}
+ /**
+ * Is the workbook protected with a password (not encrypted)?
+ */
+ public boolean isWriteProtected() {
+ return this.workbook.isWriteProtected();
+ }
+
/**
* protect a workbook with a password (not encypted, just sets writeprotect
* flags and the password.
assertEquals(1, wb.getNumberOfSheets());
}
+
+ /**
+ * Files with "read only recommended" were giving
+ * grief on the FileSharingRecord
+ */
+ public void test44536() throws Exception {
+ FileInputStream in = new FileInputStream(new File(cwd, "ReadOnlyRecommended.xls"));
+
+ // Used to blow up with an IllegalArgumentException
+ // when creating a FileSharingRecord
+ HSSFWorkbook wb = new HSSFWorkbook(in);
+ in.close();
+
+ // Check read only advised
+ assertEquals(3, wb.getNumberOfSheets());
+ assertTrue(wb.isWriteProtected());
+
+ // But also check that another wb isn't
+ in = new FileInputStream(new File(cwd, "SimpleWithChoose.xls"));
+ wb = new HSSFWorkbook(in);
+ in.close();
+ assertFalse(wb.isWriteProtected());
+ }
}