Adding utility method to check any protection is enforced #27

Closed
wants to merge 3 commits into
from
@@ -919,6 +919,22 @@ public void setTable(int pos, XWPFTable table) {
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/>
@@ -120,6 +120,29 @@ public void setZoomPercent(long zoomPercent) {
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/>
@@ -233,8 +233,9 @@ public XWPFVertAlign getVerticalAlignment() {
XWPFVertAlign vAlign = null;
CTTcPr tcpr = ctTc.getTcPr();
if (tcpr != null) {
- CTVerticalJc va = tcpr.getVAlign();
- vAlign = stVertAlignTypeMap.get(va.getVal().intValue());
+ CTVerticalJc va = tcpr.getVAlign();
+ if(va != null)
@centic9

centic9 Feb 15, 2016

Member

Is this change related? Looks like a different change/fix

+ vAlign = stVertAlignTypeMap.get(va.getVal().intValue());
}
return vAlign;
}
@@ -416,4 +416,11 @@ public void testSettings() throws IOException {
doc.close();
}
+
+ @Test
+ public void testEnforcedWith() throws IOException {
+ XWPFDocument docx = XWPFTestDataSamples.openSampleDocument("EnforcedWith.docx");
+ assertTrue(docx.isEnforcedProtection());
+ docx.close();
+ }
}
Binary file not shown.