]> source.dussan.org Git - poi.git/commitdiff
Fixed SlideShow#removeSlide to remove references to Notes, see Bugzilla #47261
authorYegor Kozlov <yegor@apache.org>
Fri, 19 Jun 2009 13:24:41 +0000 (13:24 +0000)
committerYegor Kozlov <yegor@apache.org>
Fri, 19 Jun 2009 13:24:41 +0000 (13:24 +0000)
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@786501 13f79535-47bb-0310-9956-ffa450edef68

src/documentation/content/xdocs/status.xml
src/scratchpad/src/org/apache/poi/hslf/record/Document.java
src/scratchpad/src/org/apache/poi/hslf/record/RecordContainer.java
src/scratchpad/src/org/apache/poi/hslf/usermodel/SlideShow.java
src/scratchpad/testcases/org/apache/poi/hslf/data/47261.ppt [new file with mode: 0755]
src/scratchpad/testcases/org/apache/poi/hslf/record/TestRecordContainer.java
src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestAddingSlides.java

index e81bf101ad868000031d90033c7fe25e02e48de7..764cb7375d01e1682151fa0f052cc174786c609e 100644 (file)
@@ -33,6 +33,7 @@
 
     <changes>
         <release version="3.5-beta7" date="2009-??-??">
+           <action dev="POI-DEVELOPERS" type="fix">47261 - Fixed SlideShow#removeSlide to remove references to Notes</action>
            <action dev="POI-DEVELOPERS" type="fix">47375 - Fixed HSSFHyperlink to correctly set inter-sheet and file links</action>
            <action dev="POI-DEVELOPERS" type="fix">47384 - Fixed ExternalNameRecord to handle unicode names</action>
            <action dev="POI-DEVELOPERS" type="fix">47372 - Fixed locale-sensitive unit tests to pass when running on non-US locale</action>
index 069d877a0a8123e2a20884517e52221e8a3cd6c6..a9510ae55a6c8b921da736b271abf76ec668e837 100644 (file)
@@ -21,6 +21,7 @@ import org.apache.poi.util.POILogger;
 
 import java.io.IOException;
 import java.io.OutputStream;
+import java.util.ArrayList;
 
 /**
  * Master container for Document. There is one of these for every
@@ -189,6 +190,16 @@ public final class Document extends PositionDependentRecordContainer
                slwts = nl;
        }
 
+    public void removeSlideListWithText(SlideListWithText slwt) {
+        ArrayList<SlideListWithText> lst = new ArrayList<SlideListWithText>();
+        for(SlideListWithText s : slwts) {
+            if(s != slwt) lst.add(s);
+            else {
+                removeChild(slwt);
+            }
+        }
+        slwts = lst.toArray(new SlideListWithText[lst.size()]);
+    }
 
        /**
         * We are of type 1000
index 9450e6986e792230dddca50e4bb693e5b40a611c..749e9edcca889067d91399aa5efc1955505d125d 100644 (file)
@@ -24,6 +24,7 @@ import org.apache.poi.hslf.util.MutableByteArrayOutputStream;
 import java.io.IOException;
 import java.io.OutputStream;
 import java.io.ByteArrayOutputStream;
+import java.util.ArrayList;
 
 /**
  * Abstract class which all container records will extend. Providers
@@ -135,7 +136,24 @@ public abstract class RecordContainer extends Record
                return null;
        }
 
-       /* ===============================================================
+    /**
+     * Remove a child record from this record container
+     *
+     * @param ch the child to remove
+     * @return the removed record
+     */
+    public Record removeChild(Record ch) {
+        Record rm = null;
+        ArrayList<Record> lst = new ArrayList<Record>();
+        for(Record r : _children) {
+            if(r != ch) lst.add(r);
+            else rm = r;
+        }
+        _children = lst.toArray(new Record[lst.size()]);
+        return rm;
+    }
+
+    /* ===============================================================
         *                   External Move Methods
         * ===============================================================
         */
index 15650d0ad759b554ca4460e5e6b95699455c7721..cf8d89050f5299185cdd10cbf51ca581c3fe2e56 100644 (file)
@@ -601,10 +601,39 @@ public final class SlideShow {
                 removedSlide = _slides[i];
             }
         }
-        slwt.setSlideAtomsSets( sa.toArray(new SlideAtomsSet[sa.size()]) );
-        slwt.setChildRecord(records.toArray(new Record[records.size()]));
+        if(sa.size() == 0){
+            _documentRecord.removeSlideListWithText(slwt);
+        } else {
+            slwt.setSlideAtomsSets( sa.toArray(new SlideAtomsSet[sa.size()]) );
+            slwt.setChildRecord(records.toArray(new Record[records.size()]));
+        }
         _slides = sl.toArray(new Slide[sl.size()]);
 
+        //if the removed slide had notes - remove references to them too
+        if(removedSlide != null){
+            int notesId = removedSlide.getSlideRecord().getSlideAtom().getNotesID();
+            if(notesId != 0){
+                SlideListWithText nslwt = _documentRecord.getNotesSlideListWithText();
+                records = new ArrayList<Record>();
+                ArrayList<SlideAtomsSet> na = new ArrayList<SlideAtomsSet>();
+                for (SlideAtomsSet ns : nslwt.getSlideAtomsSets()){
+                    if(ns.getSlidePersistAtom().getSlideIdentifier() != notesId) {
+                        na.add(ns);
+                        records.add(ns.getSlidePersistAtom());
+                        if(ns.getSlideRecords() != null) records.addAll(Arrays.asList(ns.getSlideRecords()));
+                    }
+                }
+                if(na.size() == 0){
+                    _documentRecord.removeSlideListWithText(nslwt);
+                } else {
+                    slwt.setSlideAtomsSets( na.toArray(new SlideAtomsSet[na.size()]) );
+                    slwt.setChildRecord(records.toArray(new Record[records.size()]));
+                }
+
+            }
+
+        }
+
         return removedSlide;
     }
 
diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/data/47261.ppt b/src/scratchpad/testcases/org/apache/poi/hslf/data/47261.ppt
new file mode 100755 (executable)
index 0000000..be472de
Binary files /dev/null and b/src/scratchpad/testcases/org/apache/poi/hslf/data/47261.ppt differ
index 89e63872061132d6ab5969a2ef23848f69300fa8..74684e7e73ee768a08a0952d6c5705581aa60e94 100644 (file)
@@ -136,7 +136,14 @@ public final class TestRecordContainer extends TestCase {
                assertEquals(before, ncr[1]);
        }
 
-       protected void setUp() throws Exception {
+    public void testRemove() {
+        Record[] ch = recordContainer.getChildRecords();
+        Record removeRecord = recordContainer.removeChild(ch[0]);
+        assertSame(ch[0], removeRecord);
+        assertEquals(ch.length-1, recordContainer.getChildRecords().length);
+    }
+
+    protected void setUp() throws Exception {
                super.setUp();
 
                // Find a real RecordContainer record
index 52a1da7ce68ce76e09b25eb168e7b02e779c83ba..4ffeb878949212b440825b1f69bcaac6594189c8 100644 (file)
@@ -20,12 +20,15 @@ package org.apache.poi.hslf.usermodel;
 
 import java.io.ByteArrayInputStream;
 import java.io.ByteArrayOutputStream;
+import java.io.FileInputStream;
+import java.io.File;
 
 import junit.framework.TestCase;
 import org.apache.poi.hslf.*;
 import org.apache.poi.hslf.record.Record;
 import org.apache.poi.hslf.record.RecordTypes;
 import org.apache.poi.hslf.record.UserEditAtom;
+import org.apache.poi.hslf.record.Document;
 import org.apache.poi.hslf.model.*;
 
 /**
@@ -269,4 +272,24 @@ public final class TestAddingSlides extends TestCase {
         assertEquals(1, s3.length);
     }
 
+
+    public void test47261() throws Exception {
+        File src = new File(System.getProperty("HSLF.testdata.path"), "47261.ppt");
+        SlideShow ppt = new SlideShow(new FileInputStream(src));
+        Slide[] slides = ppt.getSlides();
+        Document doc = ppt.getDocumentRecord();
+        assertNotNull(doc.getSlideSlideListWithText());
+        assertEquals(1, ppt.getSlides().length);
+        int notesId = slides[0].getSlideRecord().getSlideAtom().getNotesID();
+        assertTrue(notesId > 0);
+        assertNotNull(doc.getNotesSlideListWithText());
+        //the SLWT container for notes has one entry which will deleted
+        assertEquals(1, doc.getNotesSlideListWithText().getSlideAtomsSets().length);
+
+        ppt.removeSlide(0);
+        assertEquals(0, ppt.getSlides().length);
+        assertNull(doc.getSlideSlideListWithText());
+        assertNull(doc.getNotesSlideListWithText());
+
+    }
 }