Bug 45776 - Fix corrupt file problem using TextRun.setText

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1554077 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Andreas Beeker 2013-12-29 22:14:05 +00:00
parent ad6a8a3cd1
commit 8c92e5db7a
3 changed files with 60 additions and 12 deletions

View File

@ -17,11 +17,20 @@
package org.apache.poi.hslf.model; package org.apache.poi.hslf.model;
import java.util.ArrayList;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.Vector; import java.util.List;
import org.apache.poi.hslf.model.textproperties.TextPropCollection; import org.apache.poi.hslf.model.textproperties.TextPropCollection;
import org.apache.poi.hslf.record.*; import org.apache.poi.hslf.record.Record;
import org.apache.poi.hslf.record.RecordContainer;
import org.apache.poi.hslf.record.StyleTextProp9Atom;
import org.apache.poi.hslf.record.StyleTextPropAtom;
import org.apache.poi.hslf.record.TextBytesAtom;
import org.apache.poi.hslf.record.TextCharsAtom;
import org.apache.poi.hslf.record.TextHeaderAtom;
import org.apache.poi.hslf.record.TextRulerAtom;
import org.apache.poi.hslf.record.TextSpecInfoAtom;
import org.apache.poi.hslf.usermodel.RichTextRun; import org.apache.poi.hslf.usermodel.RichTextRun;
import org.apache.poi.hslf.usermodel.SlideShow; import org.apache.poi.hslf.usermodel.SlideShow;
import org.apache.poi.util.StringUtil; import org.apache.poi.util.StringUtil;
@ -54,7 +63,7 @@ public final class TextRun
* (there can be misc InteractiveInfo, TxInteractiveInfo and other records) * (there can be misc InteractiveInfo, TxInteractiveInfo and other records)
*/ */
protected Record[] _records; protected Record[] _records;
private StyleTextPropAtom styleTextPropAtom; // private StyleTextPropAtom styleTextPropAtom;
private StyleTextProp9Atom styleTextProp9Atom; private StyleTextProp9Atom styleTextProp9Atom;
/** /**
@ -95,8 +104,8 @@ public final class TextRun
String runRawText = getText(); String runRawText = getText();
// Figure out the rich text runs // Figure out the rich text runs
LinkedList pStyles = new LinkedList(); LinkedList<TextPropCollection> pStyles = new LinkedList<TextPropCollection>();
LinkedList cStyles = new LinkedList(); LinkedList<TextPropCollection> cStyles = new LinkedList<TextPropCollection>();
if(_styleAtom != null) { if(_styleAtom != null) {
// Get the style atom to grok itself // Get the style atom to grok itself
_styleAtom.setParentTextSize(runRawText.length()); _styleAtom.setParentTextSize(runRawText.length());
@ -106,7 +115,7 @@ public final class TextRun
buildRichTextRuns(pStyles, cStyles, runRawText); buildRichTextRuns(pStyles, cStyles, runRawText);
} }
public void buildRichTextRuns(LinkedList pStyles, LinkedList cStyles, String runRawText){ public void buildRichTextRuns(LinkedList<TextPropCollection> pStyles, LinkedList<TextPropCollection> cStyles, String runRawText){
// Handle case of no current style, with a default // Handle case of no current style, with a default
if(pStyles.size() == 0 || cStyles.size() == 0) { if(pStyles.size() == 0 || cStyles.size() == 0) {
@ -115,7 +124,7 @@ public final class TextRun
} else { } else {
// Build up Rich Text Runs, one for each // Build up Rich Text Runs, one for each
// character/paragraph style pair // character/paragraph style pair
Vector rtrs = new Vector(); List<RichTextRun> rtrs = new ArrayList<RichTextRun>();
int pos = 0; int pos = 0;
@ -249,8 +258,7 @@ public final class TextRun
} }
// Build the array // Build the array
_rtRuns = new RichTextRun[rtrs.size()]; _rtRuns = rtrs.toArray(new RichTextRun[rtrs.size()]);
rtrs.copyInto(_rtRuns);
} }
} }
@ -470,13 +478,22 @@ public final class TextRun
// no change, stays with no styling // no change, stays with no styling
// If there is styling: // If there is styling:
// everthing gets the same style that the first block has // everthing gets the same style that the first block has
// Update the lengths +1 for since these will be the only runs
if(_styleAtom != null) { if(_styleAtom != null) {
LinkedList pStyles = _styleAtom.getParagraphStyles(); LinkedList<TextPropCollection> pStyles = _styleAtom.getParagraphStyles();
while(pStyles.size() > 1) { pStyles.removeLast(); } while(pStyles.size() > 1) { pStyles.removeLast(); }
LinkedList cStyles = _styleAtom.getCharacterStyles(); if (!pStyles.isEmpty()) {
while(cStyles.size() > 1) { cStyles.removeLast(); } pStyles.getFirst().updateTextSize( s.length()+1 );
}
LinkedList<TextPropCollection> cStyles = _styleAtom.getCharacterStyles();
while(cStyles.size() > 1) { cStyles.removeLast(); }
if (!cStyles.isEmpty()) {
cStyles.getFirst().updateTextSize( s.length()+1 );
}
_rtRuns[0].setText(s); _rtRuns[0].setText(s);
} else { } else {
// Recreate rich text run with no styling // Recreate rich text run with no styling

View File

@ -19,6 +19,7 @@ package org.apache.poi.hslf.usermodel;
import java.io.InputStream; import java.io.InputStream;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
import java.util.List; import java.util.List;
@ -202,6 +203,7 @@ public final class TestBugs extends TestCase {
SlideShow ppt = new SlideShow(hslf); SlideShow ppt = new SlideShow(hslf);
Slide[] slide = ppt.getSlides(); Slide[] slide = ppt.getSlides();
for (int i = 0; i < slide.length; i++) { for (int i = 0; i < slide.length; i++) {
@SuppressWarnings("unused")
Shape[] shape = slide[i].getShapes(); Shape[] shape = slide[i].getShapes();
} }
assertTrue("No Exceptions while reading file", true); assertTrue("No Exceptions while reading file", true);
@ -259,6 +261,7 @@ public final class TestBugs extends TestCase {
for (int k = 0; k < comps.length; k++) { for (int k = 0; k < comps.length; k++) {
Shape comp = comps[k]; Shape comp = comps[k];
if (comp instanceof Picture){ if (comp instanceof Picture){
@SuppressWarnings("unused")
PictureData pict = ((Picture)comp).getPictureData(); PictureData pict = ((Picture)comp).getPictureData();
} }
} }
@ -435,4 +438,32 @@ public final class TestBugs extends TestCase {
ppt = HSLFTestDataSamples.writeOutAndReadBack(ppt); ppt = HSLFTestDataSamples.writeOutAndReadBack(ppt);
assertTrue("No Exceptions while rewriting file", true); assertTrue("No Exceptions while rewriting file", true);
} }
/**
* Bug 45776: Fix corrupt file problem using TextRun.setText
*/
public void test45776() throws Exception {
InputStream is = _slTests.openResourceAsStream("45776.ppt");
SlideShow ppt = new SlideShow(new HSLFSlideShow(is));
is.close();
// get slides
for (Slide slide : ppt.getSlides()) {
for (Shape shape : slide.getShapes()) {
if (!(shape instanceof TextBox)) continue;
TextBox tb = (TextBox) shape;
// work with TextBox
String str = tb.getText();
if (!str.contains("$$DATE$$")) continue;
str = str.replace("$$DATE$$", new Date().toString());
tb.setText(str);
TextRun tr = tb.getTextRun();
assertEquals(str.length()+1,tr.getStyleTextPropAtom().getParagraphStyles().getFirst().getCharactersCovered());
assertEquals(str.length()+1,tr.getStyleTextPropAtom().getCharacterStyles().getFirst().getCharactersCovered());
}
}
}
} }

Binary file not shown.