]> source.dussan.org Git - poi.git/commitdiff
Bug 56075 - Add Change Tracking support to XWPF
authorAndreas Beeker <kiwiwings@apache.org>
Thu, 13 Mar 2014 00:16:56 +0000 (00:16 +0000)
committerAndreas Beeker <kiwiwings@apache.org>
Thu, 13 Mar 2014 00:16:56 +0000 (00:16 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1577010 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/TestChangeTracking.java [new file with mode: 0644]
test-data/document/bug56075-changeTracking_off.docx [new file with mode: 0644]
test-data/document/bug56075-changeTracking_on.docx [new file with mode: 0644]

index 65952cbae9b49ca717f27349b12d0bbac5f7c2a9..5badf3a6719e458970cd721fbb97c05f0c71d867 100644 (file)
@@ -1157,6 +1157,24 @@ public class XWPFDocument extends POIXMLDocument implements Document, IBody {
         settings.setUpdateFields();
     }
     
+    /**
+      * Check if revision tracking is turned on.
+      * 
+      * @return <code>true</code> if revision tracking is turned on
+      */
+     public boolean isTrackRevisions() {
+         return settings.isTrackRevisions();
+     }
+    
+     /**
+      * Enable or disable revision tracking.
+      * 
+      * @param <code>true</code> to  turn on revision tracking, <code>false</code> to turn off revision tracking
+      */
+     public void setTrackRevisions(boolean enable) {
+         settings.setTrackRevisions(enable);
+     }
+
     /**
      * inserts an existing XWPFTable to the arrays bodyElements and tables
      * @param pos
index dab349a274d1593dc743746425e59be224efa281..43041bb0c64ede56285f31369189fbb12835bec5 100644 (file)
@@ -326,6 +326,32 @@ public class XWPFSettings extends POIXMLDocumentPart {
         return ctSettings.isSetUpdateFields() && ctSettings.getUpdateFields().getVal() == STOnOff.TRUE;
     }
 
+    /**
+     * Check if revision tracking is turned on.
+     * 
+     * @return <code>true</code> if revision tracking is turned on
+     */
+    public boolean isTrackRevisions() {
+        return ctSettings.isSetTrackRevisions();
+    }
+
+    /**
+     * Enable or disable revision tracking.
+     * 
+     * @param <code>true</code> to  turn on revision tracking, <code>false</code> to turn off revision tracking
+     */
+    public void setTrackRevisions(boolean enable) {
+        if(enable) {
+            if(!ctSettings.isSetTrackRevisions()) {
+                ctSettings.addNewTrackRevisions();
+            }
+        } else {
+            if(ctSettings.isSetTrackRevisions()) {
+                ctSettings.unsetTrackRevisions();
+            }
+        }
+    }
+
     @Override
     protected void commit() throws IOException {
         if (ctSettings == null) {
diff --git a/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestChangeTracking.java b/src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestChangeTracking.java
new file mode 100644 (file)
index 0000000..d3e4758
--- /dev/null
@@ -0,0 +1,71 @@
+/* ====================================================================\r
+   Licensed to the Apache Software Foundation (ASF) under one or more\r
+   contributor license agreements.  See the NOTICE file distributed with\r
+   this work for additional information regarding copyright ownership.\r
+   The ASF licenses this file to You under the Apache License, Version 2.0\r
+   (the "License"); you may not use this file except in compliance with\r
+   the License.  You may obtain a copy of the License at\r
+\r
+       http://www.apache.org/licenses/LICENSE-2.0\r
+\r
+   Unless required by applicable law or agreed to in writing, software\r
+   distributed under the License is distributed on an "AS IS" BASIS,\r
+   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+   See the License for the specific language governing permissions and\r
+   limitations under the License.\r
+==================================================================== */\r
+package org.apache.poi.xwpf.usermodel;\r
+\r
+import static org.junit.Assert.assertFalse;\r
+import static org.junit.Assert.assertTrue;\r
+\r
+import java.io.ByteArrayInputStream;\r
+import java.io.ByteArrayOutputStream;\r
+\r
+import org.apache.poi.xwpf.XWPFTestDataSamples;\r
+import org.junit.Test;\r
+\r
+public class TestChangeTracking {\r
+\r
+    @Test\r
+    public void detection() throws Exception {\r
+\r
+        XWPFDocument documentWithoutChangeTracking = XWPFTestDataSamples.openSampleDocument("bug56075-changeTracking_off.docx");\r
+        assertFalse(documentWithoutChangeTracking.isTrackRevisions());\r
+\r
+        XWPFDocument documentWithChangeTracking = XWPFTestDataSamples.openSampleDocument("bug56075-changeTracking_on.docx");\r
+        assertTrue(documentWithChangeTracking.isTrackRevisions());\r
+\r
+    }\r
+\r
+    @Test\r
+    public void activateChangeTracking() throws Exception {\r
+        XWPFDocument document = XWPFTestDataSamples.openSampleDocument("bug56075-changeTracking_off.docx");\r
+        assertFalse(document.isTrackRevisions());\r
+\r
+        document.setTrackRevisions(true);\r
+        \r
+        assertTrue(document.isTrackRevisions());\r
+    }\r
+\r
+    @Test\r
+    public void integration() throws Exception {\r
+        XWPFDocument doc = new XWPFDocument();\r
+\r
+        XWPFParagraph p1 = doc.createParagraph();\r
+\r
+        XWPFRun r1 = p1.createRun();\r
+        r1.setText("Lorem ipsum dolor sit amet.");\r
+        doc.setTrackRevisions(true);\r
+\r
+        ByteArrayOutputStream out = new ByteArrayOutputStream();\r
+        doc.write(out);\r
+\r
+        ByteArrayInputStream inputStream = new ByteArrayInputStream(out.toByteArray());\r
+        XWPFDocument document = new XWPFDocument(inputStream);\r
+        inputStream.close();\r
+\r
+        assertTrue(document.isTrackRevisions());\r
+    }\r
+\r
+}\r
diff --git a/test-data/document/bug56075-changeTracking_off.docx b/test-data/document/bug56075-changeTracking_off.docx
new file mode 100644 (file)
index 0000000..a92202d
Binary files /dev/null and b/test-data/document/bug56075-changeTracking_off.docx differ
diff --git a/test-data/document/bug56075-changeTracking_on.docx b/test-data/document/bug56075-changeTracking_on.docx
new file mode 100644 (file)
index 0000000..401af50
Binary files /dev/null and b/test-data/document/bug56075-changeTracking_on.docx differ