mirror of https://github.com/apache/poi.git
Fix for NPE in bug #40036. The TextBox will still be fairly useless though
git-svn-id: https://svn.apache.org/repos/asf/jakarta/poi/trunk@450097 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
a2d54c0fe2
commit
7ef203f71c
|
@ -85,6 +85,12 @@ public class TextBox extends SimpleShape {
|
|||
*/
|
||||
protected EscherTextboxWrapper _txtbox;
|
||||
|
||||
/**
|
||||
* Is the TextBox missing the text records which actually
|
||||
* store the text?
|
||||
*/
|
||||
private boolean _missingTextRecords = false;
|
||||
|
||||
/**
|
||||
* Create a TextBox object and initialize it from the supplied Record container.
|
||||
*
|
||||
|
@ -544,10 +550,20 @@ public class TextBox extends SimpleShape {
|
|||
public void setSheet(Sheet sheet){
|
||||
_sheet = sheet;
|
||||
|
||||
//initialize _txtrun object.
|
||||
//we can't do it in the constructor because the sheet is not assigned yet
|
||||
// Initialize _txtrun object.
|
||||
// (We can't do it in the constructor because the sheet
|
||||
// is not assigned then, it's only built once we have
|
||||
// all the records)
|
||||
if(_txtrun == null) initTextRun();
|
||||
if(_txtrun == null) {
|
||||
// No text records found, skip
|
||||
_missingTextRecords = true;
|
||||
return;
|
||||
} else {
|
||||
_missingTextRecords = false;
|
||||
}
|
||||
|
||||
// Supply the sheet to our child RichTextRuns
|
||||
RichTextRun[] rt = _txtrun.getRichTextRuns();
|
||||
for (int i = 0; i < rt.length; i++) {
|
||||
rt[i].supplySlideShow(_sheet.getSlideShow());
|
||||
|
@ -555,12 +571,13 @@ public class TextBox extends SimpleShape {
|
|||
}
|
||||
|
||||
private void initTextRun(){
|
||||
|
||||
TextHeaderAtom tha = null;
|
||||
TextCharsAtom tca = null;
|
||||
TextBytesAtom tba = null;
|
||||
StyleTextPropAtom sta = null;
|
||||
OutlineTextRefAtom ota = null;
|
||||
|
||||
// Find the interesting child records
|
||||
Record[] child = _txtbox.getChildRecords();
|
||||
for (int i = 0; i < child.length; i++) {
|
||||
if (child[i] instanceof TextHeaderAtom) tha = (TextHeaderAtom)child[i];
|
||||
|
@ -570,8 +587,10 @@ public class TextBox extends SimpleShape {
|
|||
else if (child[i] instanceof TextCharsAtom) tca = (TextCharsAtom)child[i];
|
||||
}
|
||||
|
||||
if (ota != null){
|
||||
//TextHeaderAtom, TextBytesAtom and StyleTextPropAtom are stored outside of EscherContainerRecord
|
||||
// Special handling for cases where there's an OutlineTextRefAtom
|
||||
if (ota != null) {
|
||||
// TextHeaderAtom, TextBytesAtom and StyleTextPropAtom are
|
||||
// stored outside of EscherContainerRecord
|
||||
int idx = ota.getTextIndex();
|
||||
Slide sl = (Slide)getSheet();
|
||||
Record[] rec = sl.getSlideAtomsSet().getSlideRecords();
|
||||
|
@ -591,7 +610,17 @@ public class TextBox extends SimpleShape {
|
|||
}
|
||||
}
|
||||
}
|
||||
if(tba != null) _txtrun = new TextRun(tha,tba,sta);
|
||||
else if (tca != null) _txtrun = new TextRun(tha,tca,sta);
|
||||
|
||||
// If we found the records we needed, create a TextRun
|
||||
if(tba != null) {
|
||||
// Bytes based Text Run
|
||||
_txtrun = new TextRun(tha,tba,sta);
|
||||
} else if (tca != null) {
|
||||
// Characters (unicode) based Text Run
|
||||
_txtrun = new TextRun(tha,tca,sta);
|
||||
} else {
|
||||
// Empty text box
|
||||
System.err.println("Warning - no text records found for TextBox");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Binary file not shown.
|
@ -33,11 +33,15 @@ import java.util.ArrayList;
|
|||
*/
|
||||
public class TestShapes extends TestCase {
|
||||
private SlideShow ppt;
|
||||
private SlideShow pptB;
|
||||
|
||||
protected void setUp() throws Exception {
|
||||
String dirname = System.getProperty("HSLF.testdata.path");
|
||||
String filename = dirname + "/empty.ppt";
|
||||
ppt = new SlideShow(new HSLFSlideShow(filename));
|
||||
|
||||
String filenameB = dirname + "/empty_textbox.ppt";
|
||||
pptB = new SlideShow(new HSLFSlideShow(filenameB));
|
||||
}
|
||||
|
||||
public void testGraphics() throws Exception {
|
||||
|
@ -170,6 +174,19 @@ public class TestShapes extends TestCase {
|
|||
assertEquals(Color.red, txtbox.getFontColor());
|
||||
}
|
||||
|
||||
/**
|
||||
* Test with an empty text box
|
||||
*/
|
||||
public void testEmptyTextBox() throws Exception {
|
||||
assertEquals(2, pptB.getSlides().length);
|
||||
Slide s1 = pptB.getSlides()[0];
|
||||
Slide s2 = pptB.getSlides()[1];
|
||||
|
||||
// Check we can get the shapes count
|
||||
assertEquals(2, s1.getShapes().length);
|
||||
assertEquals(2, s2.getShapes().length);
|
||||
}
|
||||
|
||||
/**
|
||||
* If you iterate over text shapes in a slide and collect them in a set
|
||||
* it must be the same as returned by Slide.getTextRuns().
|
||||
|
|
Loading…
Reference in New Issue