]> source.dussan.org Git - poi.git/commitdiff
GitHub PR 27: Add method to check for any protection in XWPFDocument, closes #27
authorDominik Stadler <centic@apache.org>
Mon, 15 Feb 2016 09:26:51 +0000 (09:26 +0000)
committerDominik Stadler <centic@apache.org>
Mon, 15 Feb 2016 09:26:51 +0000 (09:26 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1730471 13f79535-47bb-0310-9956-ffa450edef68

src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFDocument.java
src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFSettings.java
src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFDocument.java
test-data/document/EnforcedWith.docx [new file with mode: 0644]

index a7cf4dca08aa29dc7e0bf47937b85febeb7f1570..82f545c031a9a48a2be65d7a7323130dcef2a2c0 100644 (file)
@@ -919,6 +919,22 @@ public class XWPFDocument extends POIXMLDocument implements Document, IBody {
         tables.set(pos, table);
         ctDocument.getBody().setTblArray(pos, table.getCTTbl());
     }
+       
+       /**
+     * Verifies that the documentProtection tag in settings.xml file <br/>
+     * specifies that the protection is enforced (w:enforcement="1") <br/>
+     * <br/>
+     * sample snippet from settings.xml
+     * <pre>
+     *     &lt;w:settings  ... &gt;
+     *         &lt;w:documentProtection w:edit=&quot;readOnly&quot; w:enforcement=&quot;1&quot;/&gt;
+     * </pre>
+     *
+     * @return true if documentProtection is enforced with option any
+     */
+    public boolean isEnforcedProtection() {
+        return settings.isEnforcedWith();
+    }
 
     /**
      * Verifies that the documentProtection tag in settings.xml file <br/>
index fa4e6401f9663ca732acc1e2f24d0e70b64033a1..d9d29a3303531cb335732496ba009e77c5877bcb 100644 (file)
@@ -120,6 +120,29 @@ public class XWPFSettings extends POIXMLDocumentPart {
         CTZoom zoom = ctSettings.getZoom();
         zoom.setPercent(BigInteger.valueOf(zoomPercent));
     }
+       
+       /**
+     * Verifies the documentProtection tag inside settings.xml file <br/>
+     * if the protection is enforced (w:enforcement="1") <br/>
+     *  <p/>
+     * <br/>
+     * sample snippet from settings.xml
+     * <pre>
+     *     &lt;w:settings  ... &gt;
+     *         &lt;w:documentProtection w:edit=&quot;readOnly&quot; w:enforcement=&quot;1&quot;/&gt;
+     * </pre>
+     *
+     * @return true if documentProtection is enforced with option any
+     */
+       public boolean isEnforcedWith() {
+        CTDocProtect ctDocProtect = ctSettings.getDocumentProtection();
+
+        if (ctDocProtect == null) {
+            return false;
+        }
+
+        return ctDocProtect.getEnforcement().equals(STOnOff.X_1);
+    }
 
     /**
      * Verifies the documentProtection tag inside settings.xml file <br/>
index 43d8cce0f9c3fc8579926f04d039bf781aeabc25..afa0428b4a2a5e5336d58e0e478c91ba051bad61 100644 (file)
@@ -416,4 +416,11 @@ public final class TestXWPFDocument {
 
         doc.close();
     }
+       
+       @Test
+       public void testEnforcedWith() throws IOException {
+               XWPFDocument docx = XWPFTestDataSamples.openSampleDocument("EnforcedWith.docx");
+               assertTrue(docx.isEnforcedProtection());
+               docx.close();
+       }
 }
diff --git a/test-data/document/EnforcedWith.docx b/test-data/document/EnforcedWith.docx
new file mode 100644 (file)
index 0000000..c50e358
Binary files /dev/null and b/test-data/document/EnforcedWith.docx differ