paragraphs;
@@ -107,7 +107,8 @@ public class XWPFDocument extends POIXMLDocument {
}
// Sort out headers and footers
- headerFooterPolicy = new XWPFHeaderFooterPolicy(this);
+ if (doc.getDocument().getBody().getSectPr() != null)
+ headerFooterPolicy = new XWPFHeaderFooterPolicy(this);
for(POIXMLDocumentPart p : getRelations()){
String relation = p.getPackageRelationship().getRelationshipType();
@@ -117,6 +118,9 @@ public class XWPFDocument extends POIXMLDocument {
comments.add(new XWPFComment(ctcomment));
}
}
+ else if(relation.equals(XWPFRelation.SETTINGS.getRelation())){
+ settings = (XWPFSettings)p;
+ }
}
initHyperlinks();
@@ -191,6 +195,8 @@ public class XWPFDocument extends POIXMLDocument {
ctDocument = CTDocument1.Factory.newInstance();
ctDocument.addNewBody();
+
+ settings = (XWPFSettings) createRelationship(XWPFRelation.SETTINGS, XWPFFactory.getInstance());
POIXMLProperties.ExtendedProperties expProps = getProperties().getExtendedProperties();
expProps.getUnderlyingProperties().setApplication(DOCUMENT_CREATOR);
@@ -390,4 +396,146 @@ public class XWPFDocument extends POIXMLDocument {
}
}
}
+
+ /**
+ * Verifies that the documentProtection tag in settings.xml file
+ * specifies that the protection is enforced (w:enforcement="1")
+ * and that the kind of protection is readOnly (w:edit="readOnly")
+ *
+ * sample snippet from settings.xml
+ *
+ * <w:settings ... >
+ * <w:documentProtection w:edit="readOnly" w:enforcement="1"/>
+ *
+ *
+ * @return true if documentProtection is enforced with option readOnly
+ */
+ public boolean isEnforcedReadonlyProtection() {
+ return settings.isEnforcedWith(STDocProtect.READ_ONLY);
+ }
+
+ /**
+ * Verifies that the documentProtection tag in settings.xml file
+ * specifies that the protection is enforced (w:enforcement="1")
+ * and that the kind of protection is forms (w:edit="forms")
+ *
+ * sample snippet from settings.xml
+ *
+ * <w:settings ... >
+ * <w:documentProtection w:edit="forms" w:enforcement="1"/>
+ *
+ *
+ * @return true if documentProtection is enforced with option forms
+ */
+ public boolean isEnforcedFillingFormsProtection() {
+ return settings.isEnforcedWith(STDocProtect.FORMS);
+ }
+
+ /**
+ * Verifies that the documentProtection tag in settings.xml file
+ * specifies that the protection is enforced (w:enforcement="1")
+ * and that the kind of protection is comments (w:edit="comments")
+ *
+ * sample snippet from settings.xml
+ *
+ * <w:settings ... >
+ * <w:documentProtection w:edit="comments" w:enforcement="1"/>
+ *
+ *
+ * @return true if documentProtection is enforced with option comments
+ */
+ public boolean isEnforcedCommentsProtection() {
+ return settings.isEnforcedWith(STDocProtect.COMMENTS);
+ }
+
+ /**
+ * Verifies that the documentProtection tag in settings.xml file
+ * specifies that the protection is enforced (w:enforcement="1")
+ * and that the kind of protection is trackedChanges (w:edit="trackedChanges")
+ *
+ * sample snippet from settings.xml
+ *
+ * <w:settings ... >
+ * <w:documentProtection w:edit="trackedChanges" w:enforcement="1"/>
+ *
+ *
+ * @return true if documentProtection is enforced with option trackedChanges
+ */
+ public boolean isEnforcedTrackedChangesProtection() {
+ return settings.isEnforcedWith(STDocProtect.TRACKED_CHANGES);
+ }
+
+ /**
+ * Enforces the readOnly protection.
+ * In the documentProtection tag inside settings.xml file,
+ * it sets the value of enforcement to "1" (w:enforcement="1")
+ * and the value of edit to readOnly (w:edit="readOnly")
+ *
+ * sample snippet from settings.xml
+ *
+ * <w:settings ... >
+ * <w:documentProtection w:edit="readOnly" w:enforcement="1"/>
+ *
+ */
+ public void enforceReadonlyProtection() {
+ settings.setEnforcementEditValue(STDocProtect.READ_ONLY);
+ }
+
+ /**
+ * Enforce the Filling Forms protection.
+ * In the documentProtection tag inside settings.xml file,
+ * it sets the value of enforcement to "1" (w:enforcement="1")
+ * and the value of edit to forms (w:edit="forms")
+ *
+ * sample snippet from settings.xml
+ *
+ * <w:settings ... >
+ * <w:documentProtection w:edit="forms" w:enforcement="1"/>
+ *
+ */
+ public void enforceFillingFormsProtection() {
+ settings.setEnforcementEditValue(STDocProtect.FORMS);
+ }
+
+ /**
+ * Enforce the Comments protection.
+ * In the documentProtection tag inside settings.xml file,
+ * it sets the value of enforcement to "1" (w:enforcement="1")
+ * and the value of edit to comments (w:edit="comments")
+ *
+ * sample snippet from settings.xml
+ *
+ * <w:settings ... >
+ * <w:documentProtection w:edit="comments" w:enforcement="1"/>
+ *
+ */
+ public void enforceCommentsProtection() {
+ settings.setEnforcementEditValue(STDocProtect.COMMENTS);
+ }
+
+ /**
+ * Enforce the Tracked Changes protection.
+ * In the documentProtection tag inside settings.xml file,
+ * it sets the value of enforcement to "1" (w:enforcement="1")
+ * and the value of edit to trackedChanges (w:edit="trackedChanges")
+ *
+ * sample snippet from settings.xml
+ *
+ * <w:settings ... >
+ * <w:documentProtection w:edit="trackedChanges" w:enforcement="1"/>
+ *
+ */
+ public void enforceTrackedChangesProtection() {
+ settings.setEnforcementEditValue(STDocProtect.TRACKED_CHANGES);
+ }
+
+ /**
+ * Remove protection enforcement.
+ * In the documentProtection tag inside settings.xml file
+ * it sets the value of enforcement to "0" (w:enforcement="0")
+ */
+ public void removeProtectionEnforcement() {
+ settings.removeEnforcement();
+ }
+
}
diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRelation.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRelation.java
index 13c12c61b..c5accb89f 100644
--- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRelation.java
+++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRelation.java
@@ -68,7 +68,7 @@ public final class XWPFRelation extends POIXMLRelation {
"application/vnd.openxmlformats-officedocument.wordprocessingml.settings+xml",
"http://schemas.openxmlformats.org/officeDocument/2006/relationships/settings",
"/word/settings.xml",
- null
+ XWPFSettings.class
);
public static final XWPFRelation STYLES = new XWPFRelation(
"application/vnd.openxmlformats-officedocument.wordprocessingml.styles+xml",
diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFSettings.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFSettings.java
new file mode 100644
index 000000000..e34d6c145
--- /dev/null
+++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFSettings.java
@@ -0,0 +1,135 @@
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+package org.apache.poi.xwpf.usermodel;
+
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.HashMap;
+import java.util.Map;
+
+import javax.xml.namespace.QName;
+
+import org.apache.poi.POIXMLDocumentPart;
+import org.apache.poi.openxml4j.opc.PackagePart;
+import org.apache.poi.openxml4j.opc.PackageRelationship;
+import org.apache.xmlbeans.XmlOptions;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTDocProtect;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTSettings;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.STDocProtect;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff;
+import org.openxmlformats.schemas.wordprocessingml.x2006.main.SettingsDocument;
+
+public class XWPFSettings extends POIXMLDocumentPart {
+
+ private CTSettings ctSettings;
+
+ public XWPFSettings(PackagePart part, PackageRelationship rel) throws IOException {
+ super(part, rel);
+ readFrom(part.getInputStream());
+ }
+
+ public XWPFSettings() {
+ super();
+ ctSettings = CTSettings.Factory.newInstance();
+ }
+
+
+ /**
+ * Verifies the documentProtection tag inside settings.xml file
+ * if the protection is enforced (w:enforcement="1")
+ * and if the kind of protection equals to passed (STDocProtect.Enum editValue)
+ *
+ *
+ * sample snippet from settings.xml
+ *
+ * <w:settings ... >
+ * <w:documentProtection w:edit="readOnly" w:enforcement="1"/>
+ *
+ *
+ * @return true if documentProtection is enforced with option readOnly
+ */
+ public boolean isEnforcedWith(STDocProtect.Enum editValue) {
+ CTDocProtect ctDocProtect = ctSettings.getDocumentProtection();
+
+ if (ctDocProtect == null) {
+ return false;
+ }
+
+ return ctDocProtect.getEnforcement().equals(STOnOff.X_1) && ctDocProtect.getEdit().equals(editValue);
+ }
+
+ /**
+ * Enforces the protection with the option specified by passed editValue.
+ *
+ * In the documentProtection tag inside settings.xml file
+ * it sets the value of enforcement to "1" (w:enforcement="1")
+ * and the value of edit to the passed editValue (w:edit="[passed editValue]")
+ *
+ * sample snippet from settings.xml
+ *
+ * <w:settings ... >
+ * <w:documentProtection w:edit="[passed editValue]" w:enforcement="1"/>
+ *
+ */
+ public void setEnforcementEditValue(org.openxmlformats.schemas.wordprocessingml.x2006.main.STDocProtect.Enum editValue) {
+ safeGetDocumentProtection().setEnforcement(STOnOff.X_1);
+ safeGetDocumentProtection().setEdit(editValue);
+ }
+
+ /**
+ * Removes protection enforcement.
+ * In the documentProtection tag inside settings.xml file
+ * it sets the value of enforcement to "0" (w:enforcement="0")
+ */
+ public void removeEnforcement() {
+ safeGetDocumentProtection().setEnforcement(STOnOff.X_0);
+ }
+
+ @Override
+ protected void commit() throws IOException {
+
+ XmlOptions xmlOptions = new XmlOptions(DEFAULT_XML_OPTIONS);
+ xmlOptions.setSaveSyntheticDocumentElement(new QName(CTSettings.type.getName().getNamespaceURI(), "settings"));
+ Map map = new HashMap();
+ map.put("http://schemas.openxmlformats.org/wordprocessingml/2006/main", "w");
+ xmlOptions.setSaveSuggestedPrefixes(map);
+
+ PackagePart part = getPackagePart();
+ OutputStream out = part.getOutputStream();
+ ctSettings.save(out, xmlOptions);
+ out.close();
+ }
+
+ private CTDocProtect safeGetDocumentProtection() {
+ CTDocProtect documentProtection = ctSettings.getDocumentProtection();
+ if (documentProtection == null) {
+ documentProtection = CTDocProtect.Factory.newInstance();
+ ctSettings.setDocumentProtection(documentProtection);
+ }
+ return ctSettings.getDocumentProtection();
+ }
+
+ private void readFrom(InputStream inputStream) {
+ try {
+ ctSettings = SettingsDocument.Factory.parse(inputStream).getSettings();
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+}
diff --git a/src/ooxml/testcases/org/apache/poi/xssf/TestSheetProtection.java b/src/ooxml/testcases/org/apache/poi/xssf/TestSheetProtection.java
new file mode 100644
index 000000000..156c0dec4
--- /dev/null
+++ b/src/ooxml/testcases/org/apache/poi/xssf/TestSheetProtection.java
@@ -0,0 +1,231 @@
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+package org.apache.poi.xssf;
+
+import org.apache.poi.xssf.usermodel.XSSFSheet;
+
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+
+import junit.framework.TestCase;
+
+public class TestSheetProtection extends TestCase {
+ private XSSFSheet sheet;
+
+ @Override
+ protected void setUp() throws Exception {
+ XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("sheetProtection_not_protected.xlsx");
+ sheet = workbook.getSheetAt(0);
+ }
+
+ public void testShouldReadWorkbookProtection() throws Exception {
+ assertFalse(sheet.isAutoFilterLocked());
+ assertFalse(sheet.isDeleteColumnsLocked());
+ assertFalse(sheet.isDeleteRowsLocked());
+ assertFalse(sheet.isFormatCellsLocked());
+ assertFalse(sheet.isFormatColumnsLocked());
+ assertFalse(sheet.isFormatRowsLocked());
+ assertFalse(sheet.isInsertColumnsLocked());
+ assertFalse(sheet.isInsertHyperlinksLocked());
+ assertFalse(sheet.isInsertRowsLocked());
+ assertFalse(sheet.isPivotTablesLocked());
+ assertFalse(sheet.isSortLocked());
+ assertFalse(sheet.isObjectsLocked());
+ assertFalse(sheet.isScenariosLocked());
+ assertFalse(sheet.isSelectLockedCellsLocked());
+ assertFalse(sheet.isSelectUnlockedCellsLocked());
+ assertFalse(sheet.isSheetLocked());
+
+ sheet = XSSFTestDataSamples.openSampleWorkbook("sheetProtection_allLocked.xlsx").getSheetAt(0);
+
+ assertTrue(sheet.isAutoFilterLocked());
+ assertTrue(sheet.isDeleteColumnsLocked());
+ assertTrue(sheet.isDeleteRowsLocked());
+ assertTrue(sheet.isFormatCellsLocked());
+ assertTrue(sheet.isFormatColumnsLocked());
+ assertTrue(sheet.isFormatRowsLocked());
+ assertTrue(sheet.isInsertColumnsLocked());
+ assertTrue(sheet.isInsertHyperlinksLocked());
+ assertTrue(sheet.isInsertRowsLocked());
+ assertTrue(sheet.isPivotTablesLocked());
+ assertTrue(sheet.isSortLocked());
+ assertTrue(sheet.isObjectsLocked());
+ assertTrue(sheet.isScenariosLocked());
+ assertTrue(sheet.isSelectLockedCellsLocked());
+ assertTrue(sheet.isSelectUnlockedCellsLocked());
+ assertTrue(sheet.isSheetLocked());
+ }
+
+ public void testWriteAutoFilter() throws Exception {
+ assertFalse(sheet.isAutoFilterLocked());
+ sheet.lockAutoFilter();
+ assertFalse(sheet.isAutoFilterLocked());
+ sheet.enableLocking();
+ assertTrue(sheet.isAutoFilterLocked());
+ }
+
+ public void testWriteDeleteColumns() throws Exception {
+ assertFalse(sheet.isDeleteColumnsLocked());
+ sheet.lockDeleteColumns();
+ assertFalse(sheet.isDeleteColumnsLocked());
+ sheet.enableLocking();
+ assertTrue(sheet.isDeleteColumnsLocked());
+ }
+
+ public void testWriteDeleteRows() throws Exception {
+ assertFalse(sheet.isDeleteRowsLocked());
+ sheet.lockDeleteRows();
+ assertFalse(sheet.isDeleteRowsLocked());
+ sheet.enableLocking();
+ assertTrue(sheet.isDeleteRowsLocked());
+ }
+
+ public void testWriteFormatCells() throws Exception {
+ assertFalse(sheet.isFormatCellsLocked());
+ sheet.lockFormatCells();
+ assertFalse(sheet.isFormatCellsLocked());
+ sheet.enableLocking();
+ assertTrue(sheet.isFormatCellsLocked());
+ }
+
+ public void testWriteFormatColumns() throws Exception {
+ assertFalse(sheet.isFormatColumnsLocked());
+ sheet.lockFormatColumns();
+ assertFalse(sheet.isFormatColumnsLocked());
+ sheet.enableLocking();
+ assertTrue(sheet.isFormatColumnsLocked());
+ }
+
+ public void testWriteFormatRows() throws Exception {
+ assertFalse(sheet.isFormatRowsLocked());
+ sheet.lockFormatRows();
+ assertFalse(sheet.isFormatRowsLocked());
+ sheet.enableLocking();
+ assertTrue(sheet.isFormatRowsLocked());
+ }
+
+ public void testWriteInsertColumns() throws Exception {
+ assertFalse(sheet.isInsertColumnsLocked());
+ sheet.lockInsertColumns();
+ assertFalse(sheet.isInsertColumnsLocked());
+ sheet.enableLocking();
+ assertTrue(sheet.isInsertColumnsLocked());
+ }
+
+ public void testWriteInsertHyperlinks() throws Exception {
+ assertFalse(sheet.isInsertHyperlinksLocked());
+ sheet.lockInsertHyperlinks();
+ assertFalse(sheet.isInsertHyperlinksLocked());
+ sheet.enableLocking();
+ assertTrue(sheet.isInsertHyperlinksLocked());
+ }
+
+ public void testWriteInsertRows() throws Exception {
+ assertFalse(sheet.isInsertRowsLocked());
+ sheet.lockInsertRows();
+ assertFalse(sheet.isInsertRowsLocked());
+ sheet.enableLocking();
+ assertTrue(sheet.isInsertRowsLocked());
+ }
+
+ public void testWritePivotTables() throws Exception {
+ assertFalse(sheet.isPivotTablesLocked());
+ sheet.lockPivotTables();
+ assertFalse(sheet.isPivotTablesLocked());
+ sheet.enableLocking();
+ assertTrue(sheet.isPivotTablesLocked());
+ }
+
+ public void testWriteSort() throws Exception {
+ assertFalse(sheet.isSortLocked());
+ sheet.lockSort();
+ assertFalse(sheet.isSortLocked());
+ sheet.enableLocking();
+ assertTrue(sheet.isSortLocked());
+ }
+
+ public void testWriteObjects() throws Exception {
+ assertFalse(sheet.isObjectsLocked());
+ sheet.lockObjects();
+ assertFalse(sheet.isObjectsLocked());
+ sheet.enableLocking();
+ assertTrue(sheet.isObjectsLocked());
+ }
+
+ public void testWriteScenarios() throws Exception {
+ assertFalse(sheet.isScenariosLocked());
+ sheet.lockScenarios();
+ assertFalse(sheet.isScenariosLocked());
+ sheet.enableLocking();
+ assertTrue(sheet.isScenariosLocked());
+ }
+
+ public void testWriteSelectLockedCells() throws Exception {
+ assertFalse(sheet.isSelectLockedCellsLocked());
+ sheet.lockSelectLockedCells();
+ assertFalse(sheet.isSelectLockedCellsLocked());
+ sheet.enableLocking();
+ assertTrue(sheet.isSelectLockedCellsLocked());
+ }
+
+ public void testWriteSelectUnlockedCells() throws Exception {
+ assertFalse(sheet.isSelectUnlockedCellsLocked());
+ sheet.lockSelectUnlockedCells();
+ assertFalse(sheet.isSelectUnlockedCellsLocked());
+ sheet.enableLocking();
+ assertTrue(sheet.isSelectUnlockedCellsLocked());
+ }
+
+ public void testWriteSelectEnableLocking() throws Exception {
+ sheet = XSSFTestDataSamples.openSampleWorkbook("sheetProtection_allLocked.xlsx").getSheetAt(0);
+
+ assertTrue(sheet.isAutoFilterLocked());
+ assertTrue(sheet.isDeleteColumnsLocked());
+ assertTrue(sheet.isDeleteRowsLocked());
+ assertTrue(sheet.isFormatCellsLocked());
+ assertTrue(sheet.isFormatColumnsLocked());
+ assertTrue(sheet.isFormatRowsLocked());
+ assertTrue(sheet.isInsertColumnsLocked());
+ assertTrue(sheet.isInsertHyperlinksLocked());
+ assertTrue(sheet.isInsertRowsLocked());
+ assertTrue(sheet.isPivotTablesLocked());
+ assertTrue(sheet.isSortLocked());
+ assertTrue(sheet.isObjectsLocked());
+ assertTrue(sheet.isScenariosLocked());
+ assertTrue(sheet.isSelectLockedCellsLocked());
+ assertTrue(sheet.isSelectUnlockedCellsLocked());
+ assertTrue(sheet.isSheetLocked());
+
+ sheet.disableLocking();
+
+ assertFalse(sheet.isAutoFilterLocked());
+ assertFalse(sheet.isDeleteColumnsLocked());
+ assertFalse(sheet.isDeleteRowsLocked());
+ assertFalse(sheet.isFormatCellsLocked());
+ assertFalse(sheet.isFormatColumnsLocked());
+ assertFalse(sheet.isFormatRowsLocked());
+ assertFalse(sheet.isInsertColumnsLocked());
+ assertFalse(sheet.isInsertHyperlinksLocked());
+ assertFalse(sheet.isInsertRowsLocked());
+ assertFalse(sheet.isPivotTablesLocked());
+ assertFalse(sheet.isSortLocked());
+ assertFalse(sheet.isObjectsLocked());
+ assertFalse(sheet.isScenariosLocked());
+ assertFalse(sheet.isSelectLockedCellsLocked());
+ assertFalse(sheet.isSelectUnlockedCellsLocked());
+ assertFalse(sheet.isSheetLocked());
+ }
+}
diff --git a/src/ooxml/testcases/org/apache/poi/xssf/TestWorkbookProtection.java b/src/ooxml/testcases/org/apache/poi/xssf/TestWorkbookProtection.java
new file mode 100644
index 000000000..fc8c159e5
--- /dev/null
+++ b/src/ooxml/testcases/org/apache/poi/xssf/TestWorkbookProtection.java
@@ -0,0 +1,109 @@
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+package org.apache.poi.xssf;
+
+import java.io.File;
+
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+
+import junit.framework.TestCase;
+
+import org.apache.poi.xssf.usermodel.XSSFWorkbook;
+
+public class TestWorkbookProtection extends TestCase {
+
+ public void testShouldReadWorkbookProtection() throws Exception {
+ XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("workbookProtection_not_protected.xlsx");
+ assertFalse(workbook.isStructureLocked());
+ assertFalse(workbook.isWindowsLocked());
+ assertFalse(workbook.isRevisionLocked());
+
+ workbook = XSSFTestDataSamples.openSampleWorkbook("workbookProtection_workbook_structure_protected.xlsx");
+ assertTrue(workbook.isStructureLocked());
+ assertFalse(workbook.isWindowsLocked());
+ assertFalse(workbook.isRevisionLocked());
+
+ workbook = XSSFTestDataSamples.openSampleWorkbook("workbookProtection_workbook_windows_protected.xlsx");
+ assertTrue(workbook.isWindowsLocked());
+ assertFalse(workbook.isStructureLocked());
+ assertFalse(workbook.isRevisionLocked());
+
+ workbook = XSSFTestDataSamples.openSampleWorkbook("workbookProtection_workbook_revision_protected.xlsx");
+ assertTrue(workbook.isRevisionLocked());
+ assertFalse(workbook.isWindowsLocked());
+ assertFalse(workbook.isStructureLocked());
+ }
+
+ public void testShouldWriteStructureLock() throws Exception {
+ XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("workbookProtection_not_protected.xlsx");
+ assertFalse(workbook.isStructureLocked());
+
+ workbook.lockStructure();
+
+ assertTrue(workbook.isStructureLocked());
+
+ workbook.unLockStructure();
+
+ assertFalse(workbook.isStructureLocked());
+ }
+
+ public void testShouldWriteWindowsLock() throws Exception {
+ XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("workbookProtection_not_protected.xlsx");
+ assertFalse(workbook.isWindowsLocked());
+
+ workbook.lockWindows();
+
+ assertTrue(workbook.isWindowsLocked());
+
+ workbook.unLockWindows();
+
+ assertFalse(workbook.isWindowsLocked());
+ }
+
+ public void testShouldWriteRevisionLock() throws Exception {
+ XSSFWorkbook workbook = XSSFTestDataSamples.openSampleWorkbook("workbookProtection_not_protected.xlsx");
+ assertFalse(workbook.isRevisionLocked());
+
+ workbook.lockRevision();
+
+ assertTrue(workbook.isRevisionLocked());
+
+ workbook.unLockRevision();
+
+ assertFalse(workbook.isRevisionLocked());
+ }
+
+ public void testIntegration() throws Exception {
+ XSSFWorkbook wb = new XSSFWorkbook();
+ wb.createSheet("Testing purpose sheet");
+ assertFalse(wb.isRevisionLocked());
+
+ wb.lockRevision();
+
+ File tempFile = File.createTempFile("workbookProtection", ".xlsx");
+ FileOutputStream out = new FileOutputStream(tempFile);
+ wb.write(out);
+ out.close();
+
+ FileInputStream inputStream = new FileInputStream(tempFile);
+ XSSFWorkbook workbook = new XSSFWorkbook(inputStream);
+ inputStream.close();
+
+ assertTrue(workbook.isRevisionLocked());
+ }
+}
diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/TestDocumentProtection.java b/src/ooxml/testcases/org/apache/poi/xwpf/TestDocumentProtection.java
new file mode 100644
index 000000000..10f0e4818
--- /dev/null
+++ b/src/ooxml/testcases/org/apache/poi/xwpf/TestDocumentProtection.java
@@ -0,0 +1,153 @@
+/* ====================================================================
+ Licensed to the Apache Software Foundation (ASF) under one or more
+ contributor license agreements. See the NOTICE file distributed with
+ this work for additional information regarding copyright ownership.
+ The ASF licenses this file to You under the Apache License, Version 2.0
+ (the "License"); you may not use this file except in compliance with
+ the License. You may obtain a copy of the License at
+
+ http://www.apache.org/licenses/LICENSE-2.0
+
+ Unless required by applicable law or agreed to in writing, software
+ distributed under the License is distributed on an "AS IS" BASIS,
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ See the License for the specific language governing permissions and
+ limitations under the License.
+==================================================================== */
+package org.apache.poi.xwpf;
+
+import java.io.ByteArrayInputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+
+import junit.framework.TestCase;
+
+import org.apache.poi.xwpf.usermodel.XWPFDocument;
+import org.apache.poi.xwpf.usermodel.XWPFParagraph;
+import org.apache.poi.xwpf.usermodel.XWPFRun;
+
+public class TestDocumentProtection extends TestCase {
+
+ public void testShouldReadEnforcementProperties() throws Exception {
+
+ XWPFDocument documentWithoutDocumentProtectionTag = XWPFTestDataSamples.openSampleDocument("documentProtection_no_protection.docx");
+ assertFalse(documentWithoutDocumentProtectionTag.isEnforcedReadonlyProtection());
+ assertFalse(documentWithoutDocumentProtectionTag.isEnforcedFillingFormsProtection());
+ assertFalse(documentWithoutDocumentProtectionTag.isEnforcedCommentsProtection());
+ assertFalse(documentWithoutDocumentProtectionTag.isEnforcedTrackedChangesProtection());
+
+ XWPFDocument documentWithoutEnforcement = XWPFTestDataSamples.openSampleDocument("documentProtection_no_protection_tag_existing.docx");
+ assertFalse(documentWithoutEnforcement.isEnforcedReadonlyProtection());
+ assertFalse(documentWithoutEnforcement.isEnforcedFillingFormsProtection());
+ assertFalse(documentWithoutEnforcement.isEnforcedCommentsProtection());
+ assertFalse(documentWithoutEnforcement.isEnforcedTrackedChangesProtection());
+
+ XWPFDocument documentWithReadonlyEnforcement = XWPFTestDataSamples.openSampleDocument("documentProtection_readonly_no_password.docx");
+ assertTrue(documentWithReadonlyEnforcement.isEnforcedReadonlyProtection());
+ assertFalse(documentWithReadonlyEnforcement.isEnforcedFillingFormsProtection());
+ assertFalse(documentWithReadonlyEnforcement.isEnforcedCommentsProtection());
+ assertFalse(documentWithReadonlyEnforcement.isEnforcedTrackedChangesProtection());
+
+ XWPFDocument documentWithFillingFormsEnforcement = XWPFTestDataSamples.openSampleDocument("documentProtection_forms_no_password.docx");
+ assertTrue(documentWithFillingFormsEnforcement.isEnforcedFillingFormsProtection());
+ assertFalse(documentWithFillingFormsEnforcement.isEnforcedReadonlyProtection());
+ assertFalse(documentWithFillingFormsEnforcement.isEnforcedCommentsProtection());
+ assertFalse(documentWithFillingFormsEnforcement.isEnforcedTrackedChangesProtection());
+
+ XWPFDocument documentWithCommentsEnforcement = XWPFTestDataSamples.openSampleDocument("documentProtection_comments_no_password.docx");
+ assertFalse(documentWithCommentsEnforcement.isEnforcedFillingFormsProtection());
+ assertFalse(documentWithCommentsEnforcement.isEnforcedReadonlyProtection());
+ assertTrue(documentWithCommentsEnforcement.isEnforcedCommentsProtection());
+ assertFalse(documentWithCommentsEnforcement.isEnforcedTrackedChangesProtection());
+
+ XWPFDocument documentWithTrackedChangesEnforcement = XWPFTestDataSamples.openSampleDocument("documentProtection_trackedChanges_no_password.docx");
+ assertFalse(documentWithTrackedChangesEnforcement.isEnforcedFillingFormsProtection());
+ assertFalse(documentWithTrackedChangesEnforcement.isEnforcedReadonlyProtection());
+ assertFalse(documentWithTrackedChangesEnforcement.isEnforcedCommentsProtection());
+ assertTrue(documentWithTrackedChangesEnforcement.isEnforcedTrackedChangesProtection());
+
+ }
+
+ public void testShouldEnforceForReadOnly() throws Exception {
+ // XWPFDocument document = createDocumentFromSampleFile("test-data/document/documentProtection_no_protection.docx");
+ XWPFDocument document = XWPFTestDataSamples.openSampleDocument("documentProtection_no_protection.docx");
+ assertFalse(document.isEnforcedReadonlyProtection());
+
+ document.enforceReadonlyProtection();
+
+ assertTrue(document.isEnforcedReadonlyProtection());
+ }
+
+ public void testShouldEnforceForFillingForms() throws Exception {
+ XWPFDocument document = createDocumentFromSampleFile("test-data/document/documentProtection_no_protection.docx");
+ assertFalse(document.isEnforcedFillingFormsProtection());
+
+ document.enforceFillingFormsProtection();
+
+ assertTrue(document.isEnforcedFillingFormsProtection());
+ }
+
+ public void testShouldEnforceForComments() throws Exception {
+ XWPFDocument document = createDocumentFromSampleFile("test-data/document/documentProtection_no_protection.docx");
+ assertFalse(document.isEnforcedCommentsProtection());
+
+ document.enforceCommentsProtection();
+
+ assertTrue(document.isEnforcedCommentsProtection());
+ }
+
+ public void testShouldEnforceForTrackedChanges() throws Exception {
+ XWPFDocument document = createDocumentFromSampleFile("test-data/document/documentProtection_no_protection.docx");
+ assertFalse(document.isEnforcedTrackedChangesProtection());
+
+ document.enforceTrackedChangesProtection();
+
+ assertTrue(document.isEnforcedTrackedChangesProtection());
+ }
+
+ public void testShouldUnsetEnforcement() throws Exception {
+ XWPFDocument document = createDocumentFromSampleFile("test-data/document/documentProtection_readonly_no_password.docx");
+ assertTrue(document.isEnforcedReadonlyProtection());
+
+ document.removeProtectionEnforcement();
+
+ assertFalse(document.isEnforcedReadonlyProtection());
+ }
+
+ public void testIntegration() throws Exception {
+ XWPFDocument doc = new XWPFDocument();
+
+ XWPFParagraph p1 = doc.createParagraph();
+
+ XWPFRun r1 = p1.createRun();
+ r1.setText("Lorem ipsum dolor sit amet.");
+ doc.enforceCommentsProtection();
+
+ File tempFile = File.createTempFile("documentProtectionFile", ".docx");
+ FileOutputStream out = new FileOutputStream(tempFile);
+
+ doc.write(out);
+ out.close();
+
+ FileInputStream inputStream = new FileInputStream(tempFile);
+ XWPFDocument document = new XWPFDocument(inputStream);
+ inputStream.close();
+
+ assertTrue(document.isEnforcedCommentsProtection());
+ }
+
+ private XWPFDocument createDocumentFromSampleFile(String fileName) throws FileNotFoundException, IOException {
+ File file = new File(fileName);
+ FileInputStream in = new FileInputStream(file);
+ byte[] bytes = new byte[(int) file.length()];
+ in.read(bytes);
+
+ ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
+ XWPFDocument document = new XWPFDocument(inputStream);
+ return document;
+ }
+
+}
diff --git a/test-data/document/documentProtection_comments_no_password.docx b/test-data/document/documentProtection_comments_no_password.docx
new file mode 100644
index 000000000..5cc800e36
Binary files /dev/null and b/test-data/document/documentProtection_comments_no_password.docx differ
diff --git a/test-data/document/documentProtection_forms_no_password.docx b/test-data/document/documentProtection_forms_no_password.docx
new file mode 100644
index 000000000..8d31026b4
Binary files /dev/null and b/test-data/document/documentProtection_forms_no_password.docx differ
diff --git a/test-data/document/documentProtection_no_protection.docx b/test-data/document/documentProtection_no_protection.docx
new file mode 100644
index 000000000..fc1089e39
Binary files /dev/null and b/test-data/document/documentProtection_no_protection.docx differ
diff --git a/test-data/document/documentProtection_no_protection_tag_existing.docx b/test-data/document/documentProtection_no_protection_tag_existing.docx
new file mode 100644
index 000000000..2c655b6b1
Binary files /dev/null and b/test-data/document/documentProtection_no_protection_tag_existing.docx differ
diff --git a/test-data/document/documentProtection_readonly_no_password.docx b/test-data/document/documentProtection_readonly_no_password.docx
new file mode 100644
index 000000000..1ec7063ab
Binary files /dev/null and b/test-data/document/documentProtection_readonly_no_password.docx differ
diff --git a/test-data/document/documentProtection_trackedChanges_no_password.docx b/test-data/document/documentProtection_trackedChanges_no_password.docx
new file mode 100644
index 000000000..8531573c0
Binary files /dev/null and b/test-data/document/documentProtection_trackedChanges_no_password.docx differ
diff --git a/test-data/document/protected_sample.docx b/test-data/document/protected_sample.docx
new file mode 100644
index 000000000..e6b5e0bfc
Binary files /dev/null and b/test-data/document/protected_sample.docx differ
diff --git a/test-data/spreadsheet/sheetProtection_allLocked.xlsx b/test-data/spreadsheet/sheetProtection_allLocked.xlsx
new file mode 100644
index 000000000..8afd70aca
Binary files /dev/null and b/test-data/spreadsheet/sheetProtection_allLocked.xlsx differ
diff --git a/test-data/spreadsheet/sheetProtection_not_protected.xlsx b/test-data/spreadsheet/sheetProtection_not_protected.xlsx
new file mode 100644
index 000000000..81a0b5ea7
Binary files /dev/null and b/test-data/spreadsheet/sheetProtection_not_protected.xlsx differ
diff --git a/test-data/spreadsheet/workbookProtection_not_protected.xlsx b/test-data/spreadsheet/workbookProtection_not_protected.xlsx
new file mode 100644
index 000000000..81a0b5ea7
Binary files /dev/null and b/test-data/spreadsheet/workbookProtection_not_protected.xlsx differ
diff --git a/test-data/spreadsheet/workbookProtection_workbook_revision_protected.xlsx b/test-data/spreadsheet/workbookProtection_workbook_revision_protected.xlsx
new file mode 100644
index 000000000..d3d5d9e28
Binary files /dev/null and b/test-data/spreadsheet/workbookProtection_workbook_revision_protected.xlsx differ
diff --git a/test-data/spreadsheet/workbookProtection_workbook_structure_protected.xlsx b/test-data/spreadsheet/workbookProtection_workbook_structure_protected.xlsx
new file mode 100644
index 000000000..88d834470
Binary files /dev/null and b/test-data/spreadsheet/workbookProtection_workbook_structure_protected.xlsx differ
diff --git a/test-data/spreadsheet/workbookProtection_workbook_windows_protected.xlsx b/test-data/spreadsheet/workbookProtection_workbook_windows_protected.xlsx
new file mode 100644
index 000000000..6b3057f22
Binary files /dev/null and b/test-data/spreadsheet/workbookProtection_workbook_windows_protected.xlsx differ
diff --git a/test-data/spreadsheet/workbookProtection_worksheet_protected.xlsx b/test-data/spreadsheet/workbookProtection_worksheet_protected.xlsx
new file mode 100644
index 000000000..f9339f0ae
Binary files /dev/null and b/test-data/spreadsheet/workbookProtection_worksheet_protected.xlsx differ