You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ye...@apache.org on 2010/08/18 14:49:06 UTC

svn commit: r986649 - in /poi/trunk/src: documentation/content/xdocs/ java/org/apache/poi/ss/usermodel/ ooxml/java/org/apache/poi/xssf/usermodel/ ooxml/testcases/org/apache/poi/xssf/usermodel/ testcases/org/apache/poi/ss/usermodel/

Author: yegor
Date: Wed Aug 18 12:49:05 2010
New Revision: 986649

URL: http://svn.apache.org/viewvc?rev=986649&view=rev
Log:
support for protecting a XSSF workbook, see Bugzilla #48900

Modified:
    poi/trunk/src/documentation/content/xdocs/status.xml
    poi/trunk/src/java/org/apache/poi/ss/usermodel/Sheet.java
    poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
    poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java
    poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheet.java

Modified: poi/trunk/src/documentation/content/xdocs/status.xml
URL: http://svn.apache.org/viewvc/poi/trunk/src/documentation/content/xdocs/status.xml?rev=986649&r1=986648&r2=986649&view=diff
==============================================================================
--- poi/trunk/src/documentation/content/xdocs/status.xml (original)
+++ poi/trunk/src/documentation/content/xdocs/status.xml Wed Aug 18 12:49:05 2010
@@ -34,6 +34,7 @@
 
     <changes>
         <release version="3.7-beta3" date="2010-??-??">
+           <action dev="POI-DEVELOPERS" type="add">48900 - support for protecting a XSSF workbook</action>
            <action dev="POI-DEVELOPERS" type="fix">49725 - fixed FormulaParser to correctly process defined names with underscore</action>
            <action dev="POI-DEVELOPERS" type="add">48526 - added implementation for RANDBETWEEN()</action>
            <action dev="POI-DEVELOPERS" type="fix">49725 - avoid exception in OperandResolver.parseDouble when input is minus ("-")</action>

Modified: poi/trunk/src/java/org/apache/poi/ss/usermodel/Sheet.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/java/org/apache/poi/ss/usermodel/Sheet.java?rev=986649&r1=986648&r2=986649&view=diff
==============================================================================
--- poi/trunk/src/java/org/apache/poi/ss/usermodel/Sheet.java (original)
+++ poi/trunk/src/java/org/apache/poi/ss/usermodel/Sheet.java Wed Aug 18 12:49:05 2010
@@ -455,7 +455,13 @@ public interface Sheet extends Iterable<
      * @return true => protection enabled; false => protection disabled
      */
     boolean getProtect();
-
+    
+    /**
+     * Sets the protection enabled as well as the password
+     * @param password to set for protection. Pass <code>null</code> to remove protection
+     */
+    public void protectSheet(String password);
+    
     /**
      * Answer whether scenario protection is enabled or disabled
      *

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java?rev=986649&r1=986648&r2=986649&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFSheet.java Wed Aug 18 12:49:05 2010
@@ -32,6 +32,7 @@ import javax.xml.namespace.QName;
 
 import org.apache.poi.POIXMLDocumentPart;
 import org.apache.poi.POIXMLException;
+import org.apache.poi.hssf.record.PasswordRecord;
 import org.apache.poi.hssf.record.formula.FormulaShifter;
 import org.apache.poi.hssf.util.PaneInformation;
 import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
@@ -52,6 +53,7 @@ import org.apache.poi.ss.util.CellRangeA
 import org.apache.poi.ss.util.CellRangeAddressList;
 import org.apache.poi.ss.util.CellReference;
 import org.apache.poi.ss.util.SSCellRange;
+import org.apache.poi.util.HexDump;
 import org.apache.poi.util.Internal;
 import org.apache.poi.util.POILogFactory;
 import org.apache.poi.util.POILogger;
@@ -941,7 +943,41 @@ public class XSSFSheet extends POIXMLDoc
     public boolean getProtect() {
         return worksheet.isSetSheetProtection() && sheetProtectionEnabled();
     }
+ 
+    /**
+     * Enables sheet protection and sets the password for the sheet.
+     * Also sets some attributes on the {@link CTSheetProtection} that correspond to
+     * the default values used by Excel
+     * 
+     * @param password to set for protection. Pass <code>null</code> to remove protection
+     */
+    @Override
+    public void protectSheet(String password) {
+        	
+    	if(password != null) {
+    		CTSheetProtection sheetProtection = worksheet.addNewSheetProtection();
+    		sheetProtection.xsetPassword(stringToExcelPassword(password));
+    		sheetProtection.setSheet(true);
+    		sheetProtection.setScenarios(true);
+    		sheetProtection.setObjects(true);
+    	} else {
+    		worksheet.unsetSheetProtection();
+    	}
+    }
 
+	/**
+	 * Converts a String to a {@link STUnsignedShortHex} value that contains the {@link PasswordRecord#hashPassword(String)}
+	 * value in hexadecimal format
+	 *  
+	 * @param password the password string you wish convert to an {@link STUnsignedShortHex}
+	 * @return {@link STUnsignedShortHex} that contains Excel hashed password in Hex format
+	 */
+	private STUnsignedShortHex stringToExcelPassword(String password) {
+		STUnsignedShortHex hexPassword = STUnsignedShortHex.Factory.newInstance();
+		hexPassword.setStringValue(String.valueOf(HexDump.shortToHex(PasswordRecord.hashPassword(password))).substring(2));
+		return hexPassword;
+	}
+	
     /**
      * Returns the logical row ( 0-based).  If you ask for a row that is not
      * defined you get a null.  This is to say row 4 represents the fifth row on a sheet.

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java?rev=986649&r1=986648&r2=986649&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFSheet.java Wed Aug 18 12:49:05 2010
@@ -24,6 +24,8 @@ import org.apache.poi.xssf.XSSFTestDataS
 import org.apache.poi.xssf.model.CommentsTable;
 import org.apache.poi.xssf.model.StylesTable;
 import org.apache.poi.xssf.usermodel.helpers.ColumnHelper;
+import org.apache.poi.util.HexDump;
+import org.apache.poi.hssf.record.PasswordRecord;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.*;
 
 
@@ -979,4 +981,25 @@ public final class TestXSSFSheet extends
 
         assertEquals("A1:D100", sheet.getCTWorksheet().getAutoFilter().getRef());
     }
+
+    public void testProtectSheet_lowlevel() {
+
+    	XSSFWorkbook wb = new XSSFWorkbook();
+    	XSSFSheet sheet = wb.createSheet();
+        CTSheetProtection pr = sheet.getCTWorksheet().getSheetProtection();
+        assertNull("CTSheetProtection should be null by default", pr);
+        String password = "Test";
+        sheet.protectSheet(password);
+        pr = sheet.getCTWorksheet().getSheetProtection();
+        assertNotNull("CTSheetProtection should be not null", pr);
+        assertTrue("sheet protection should be on", pr.isSetSheet());
+        assertTrue("object protection should be on", pr.isSetObjects());
+        assertTrue("scenario protection should be on", pr.isSetScenarios());
+        String hash = String.valueOf(HexDump.shortToHex(PasswordRecord.hashPassword(password))).substring(2);
+        assertEquals("well known value for top secret hash should be "+ hash, hash, pr.xgetPassword().getStringValue());
+
+        sheet.protectSheet(null);
+        assertNull("protectSheet(null) should unset CTSheetProtection", sheet.getCTWorksheet().getSheetProtection());
+    }
+
 }

Modified: poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheet.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheet.java?rev=986649&r1=986648&r2=986649&view=diff
==============================================================================
--- poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheet.java (original)
+++ poi/trunk/src/testcases/org/apache/poi/ss/usermodel/BaseTestSheet.java Wed Aug 18 12:49:05 2010
@@ -640,4 +640,18 @@ public abstract class BaseTestSheet exte
         sheet.setColumnHidden(2, true);
         assertTrue(sheet.isColumnHidden(2));
     }
+
+    public void testProtectSheet() {
+
+    	Workbook wb = _testDataProvider.createWorkbook();
+    	Sheet sheet = wb.createSheet();
+        assertFalse(sheet.getProtect());
+    	sheet.protectSheet("Test");  
+    	assertTrue(sheet.getProtect());
+    	sheet.protectSheet(null);
+    	assertFalse(sheet.getProtect());
+
+    }
+
+
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org