From d2e7cd51c3a00b52859e53210aea5b726b40d2bf Mon Sep 17 00:00:00 2001 From: Andreas Beeker Date: Mon, 11 May 2015 22:07:40 +0000 Subject: [PATCH] Commit changes in common_sl - need to update trunk ... git-svn-id: https://svn.apache.org/repos/asf/poi/branches/common_sl@1678832 13f79535-47bb-0310-9956-ffa450edef68 --- .../apache/poi/hslf/examples/Hyperlinks.java | 62 +++-- .../org/apache/poi/hslf/examples/PPT2PNG.java | 27 +-- .../apache/poi/hslf/examples/SoundFinder.java | 30 +-- .../apache/poi/hslf/examples/TableDemo.java | 23 +- .../poi/xslf/usermodel/PieChartDemo.java | 2 +- .../apache/poi/xslf/usermodel/Tutorial1.java | 2 +- .../poi/xslf/usermodel/tutorial/Step2.java | 2 +- .../poi/hslf/usermodel/HSLFHyperlink.java | 81 +++---- .../apache/poi/hslf/usermodel/HSLFSlide.java | 39 ++- .../poi/hslf/usermodel/HSLFSlideShow.java | 2 +- .../poi/hslf/usermodel/HSLFTextParagraph.java | 61 +++-- .../poi/hslf/usermodel/HSLFTextRun.java | 4 + .../poi/hslf/usermodel/HSLFTextShape.java | 2 +- .../apache/poi/hslf/model/TestHyperlink.java | 26 +- .../hslf/record/TestDocumentEncryption.java | 5 +- .../apache/poi/hslf/record/TestSlideAtom.java | 4 +- .../hslf/record/TestStyleTextPropAtom.java | 42 ++-- .../poi/hslf/usermodel/TestAddingSlides.java | 73 +++--- .../apache/poi/hslf/usermodel/TestBugs.java | 228 ++++++++---------- 19 files changed, 346 insertions(+), 369 deletions(-) diff --git a/src/examples/src/org/apache/poi/hslf/examples/Hyperlinks.java b/src/examples/src/org/apache/poi/hslf/examples/Hyperlinks.java index b9af6d2d56..9e88082508 100644 --- a/src/examples/src/org/apache/poi/hslf/examples/Hyperlinks.java +++ b/src/examples/src/org/apache/poi/hslf/examples/Hyperlinks.java @@ -17,9 +17,10 @@ package org.apache.poi.hslf.examples; -import org.apache.poi.hslf.usermodel.*; - import java.io.FileInputStream; +import java.util.List; + +import org.apache.poi.hslf.usermodel.*; /** * Demonstrates how to read hyperlinks from a presentation @@ -34,44 +35,37 @@ public final class Hyperlinks { HSLFSlideShow ppt = new HSLFSlideShow(is); is.close(); - HSLFSlide[] slide = ppt.getSlides(); - for (int j = 0; j < slide.length; j++) { - System.out.println("slide " + slide[j].getSlideNumber()); + for (HSLFSlide slide : ppt.getSlides()) { + System.out.println("\nslide " + slide.getSlideNumber()); + + // read hyperlinks from the slide's text runs + System.out.println("- reading hyperlinks from the text runs"); + for (List txtParas : slide.getTextParagraphs()) { + List links = HSLFHyperlink.find(txtParas); + String text = HSLFTextParagraph.getRawText(txtParas); - //read hyperlinks from the slide's text runs - System.out.println("reading hyperlinks from the text runs"); - HSLFTextParagraph[] txt = slide[j].getTextParagraphs(); - for (int k = 0; k < txt.length; k++) { - String text = txt[k].getRawText(); - HSLFHyperlink[] links = txt[k].getHyperlinks(); - if(links != null) for (int l = 0; l < links.length; l++) { - HSLFHyperlink link = links[l]; - String title = link.getTitle(); - String address = link.getAddress(); - System.out.println(" " + title); - System.out.println(" " + address); - String substring = text.substring(link.getStartIndex(), link.getEndIndex()-1);//in ppt end index is inclusive - System.out.println(" " + substring); + for (HSLFHyperlink link : links) { + System.out.println(toStr(link, text)); } } - //in PowerPoint you can assign a hyperlink to a shape without text, - //for example to a Line object. The code below demonstrates how to - //read such hyperlinks - System.out.println(" reading hyperlinks from the slide's shapes"); - HSLFShape[] sh = slide[j].getShapes(); - for (int k = 0; k < sh.length; k++) { - HSLFHyperlink link = sh[k].getHyperlink(); - if(link != null) { - String title = link.getTitle(); - String address = link.getAddress(); - System.out.println(" " + title); - System.out.println(" " + address); - } + // in PowerPoint you can assign a hyperlink to a shape without text, + // for example to a Line object. The code below demonstrates how to + // read such hyperlinks + System.out.println("- reading hyperlinks from the slide's shapes"); + for (HSLFShape sh : slide.getShapes()) { + HSLFHyperlink link = HSLFHyperlink.find(sh); + if (link == null) continue; + System.out.println(toStr(link, null)); } } - } - } + + static String toStr(HSLFHyperlink link, String rawText) { + //in ppt end index is inclusive + String formatStr = "title: %1$s, address: %2$s" + (rawText == null ? "" : ", start: %3$s, end: %4$s, substring: %5$s"); + String substring = (rawText == null) ? "" : rawText.substring(link.getStartIndex(), link.getEndIndex()-1); + return String.format(formatStr, link.getTitle(), link.getAddress(), link.getStartIndex(), link.getEndIndex(), substring); + } } diff --git a/src/examples/src/org/apache/poi/hslf/examples/PPT2PNG.java b/src/examples/src/org/apache/poi/hslf/examples/PPT2PNG.java index 784d912c0e..8981c64c3c 100644 --- a/src/examples/src/org/apache/poi/hslf/examples/PPT2PNG.java +++ b/src/examples/src/org/apache/poi/hslf/examples/PPT2PNG.java @@ -17,16 +17,16 @@ package org.apache.poi.hslf.examples; -import org.apache.poi.hslf.usermodel.*; -import org.apache.poi.hslf.model.*; +import java.awt.*; +import java.awt.geom.Rectangle2D; +import java.awt.image.BufferedImage; +import java.io.FileInputStream; +import java.io.FileOutputStream; import javax.imageio.ImageIO; -import java.io.FileOutputStream; -import java.io.FileInputStream; -import java.awt.*; -import java.awt.image.BufferedImage; -import java.awt.geom.Rectangle2D; +import org.apache.poi.hslf.usermodel.HSLFSlide; +import org.apache.poi.hslf.usermodel.HSLFSlideShow; /** * Demonstrates how you can use HSLF to convert each slide into a PNG image @@ -70,12 +70,11 @@ public final class PPT2PNG { int width = (int)(pgsize.width*scale); int height = (int)(pgsize.height*scale); - HSLFSlide[] slide = ppt.getSlides(); - for (int i = 0; i < slide.length; i++) { - if (slidenum != -1 && slidenum != (i+1)) continue; + for (HSLFSlide slide : ppt.getSlides()) { + if (slidenum != -1 && slidenum != slide.getSlideNumber()) continue; - String title = slide[i].getTitle(); - System.out.println("Rendering slide "+slide[i].getSlideNumber() + (title == null ? "" : ": " + title)); + String title = slide.getTitle(); + System.out.println("Rendering slide "+slide.getSlideNumber() + (title == null ? "" : ": " + title)); BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D graphics = img.createGraphics(); @@ -89,9 +88,9 @@ public final class PPT2PNG { graphics.scale((double)width/pgsize.width, (double)height/pgsize.height); - slide[i].draw(graphics); + slide.draw(graphics); - String fname = file.replaceAll("\\.ppt", "-" + (i+1) + ".png"); + String fname = file.replaceAll("\\.ppt", "-" + slide.getSlideNumber() + ".png"); FileOutputStream out = new FileOutputStream(fname); ImageIO.write(img, "png", out); out.close(); diff --git a/src/examples/src/org/apache/poi/hslf/examples/SoundFinder.java b/src/examples/src/org/apache/poi/hslf/examples/SoundFinder.java index 7ebbea9f17..3cdb101be5 100644 --- a/src/examples/src/org/apache/poi/hslf/examples/SoundFinder.java +++ b/src/examples/src/org/apache/poi/hslf/examples/SoundFinder.java @@ -15,17 +15,14 @@ limitations under the License. ==================================================================== */ package org.apache.poi.hslf.examples; -import org.apache.poi.ddf.*; -import org.apache.poi.hslf.model.*; -import org.apache.poi.hslf.record.InteractiveInfo; -import org.apache.poi.hslf.record.InteractiveInfoAtom; -import org.apache.poi.hslf.record.Record; -import org.apache.poi.hslf.usermodel.*; - import java.io.FileInputStream; import java.util.Iterator; import java.util.List; +import org.apache.poi.ddf.*; +import org.apache.poi.hslf.record.*; +import org.apache.poi.hslf.usermodel.*; + /** * For each slide iterate over shapes and found associated sound data. * @@ -36,16 +33,15 @@ public class SoundFinder { HSLFSlideShow ppt = new HSLFSlideShow(new FileInputStream(args[0])); HSLFSoundData[] sounds = ppt.getSoundData(); - HSLFSlide[] slide = ppt.getSlides(); - for (int i = 0; i < slide.length; i++) { - HSLFShape[] shape = slide[i].getShapes(); - for (int j = 0; j < shape.length; j++) { - int soundRef = getSoundReference(shape[j]); - if(soundRef != -1) { - System.out.println("Slide["+i+"], shape["+j+"], soundRef: "+soundRef); - System.out.println(" " + sounds[soundRef].getSoundName()); - System.out.println(" " + sounds[soundRef].getSoundType()); - } + for (HSLFSlide slide : ppt.getSlides()) { + for (HSLFShape shape : slide.getShapes()) { + int soundRef = getSoundReference(shape); + if(soundRef == -1) continue; + + + System.out.println("Slide["+slide.getSlideNumber()+"], shape["+shape.getShapeId()+"], soundRef: "+soundRef); + System.out.println(" " + sounds[soundRef].getSoundName()); + System.out.println(" " + sounds[soundRef].getSoundType()); } } } diff --git a/src/examples/src/org/apache/poi/hslf/examples/TableDemo.java b/src/examples/src/org/apache/poi/hslf/examples/TableDemo.java index 1d03e1b6f1..bb2e813865 100644 --- a/src/examples/src/org/apache/poi/hslf/examples/TableDemo.java +++ b/src/examples/src/org/apache/poi/hslf/examples/TableDemo.java @@ -19,6 +19,8 @@ package org.apache.poi.hslf.examples; import org.apache.poi.hslf.usermodel.*; import org.apache.poi.hslf.model.*; +import org.apache.poi.sl.usermodel.TextParagraph.TextAlign; +import org.apache.poi.sl.usermodel.VerticalAlignment; import java.awt.*; import java.io.FileOutputStream; @@ -51,8 +53,7 @@ public final class TableDemo { for (int i = 0; i < txt1.length; i++) { for (int j = 0; j < txt1[i].length; j++) { TableCell cell = table1.getCell(i, j); - cell.setText(txt1[i][j]); - HSLFTextRun rt = cell.getTextParagraphs().getTextRuns()[0]; + HSLFTextRun rt = cell.getTextParagraphs().get(0).getTextRuns().get(0); rt.setFontName("Arial"); rt.setFontSize(10); if(i == 0){ @@ -60,8 +61,9 @@ public final class TableDemo { } else { rt.setBold(true); } - cell.setVerticalAlignment(HSLFTextBox.AnchorMiddle); - cell.setHorizontalAlignment(HSLFTextBox.AlignCenter); + cell.setVerticalAlignment(VerticalAlignment.MIDDLE); + cell.setHorizontalCentered(true); + cell.setText(txt1[i][j]); } } @@ -90,8 +92,7 @@ public final class TableDemo { for (int i = 0; i < txt2.length; i++) { for (int j = 0; j < txt2[i].length; j++) { TableCell cell = table2.getCell(i, j); - cell.setText(txt2[i][j]); - HSLFTextRun rt = cell.getTextParagraphs().getTextRuns()[0]; + HSLFTextRun rt = cell.getTextParagraphs().get(0).getTextRuns().get(0); rt.setFontSize(10); rt.setFontName("Arial"); if(i == 0){ @@ -99,13 +100,15 @@ public final class TableDemo { rt.setFontColor(Color.white); rt.setBold(true); rt.setFontSize(14); - cell.setHorizontalAlignment(HSLFTextBox.AlignCenter); + cell.setHorizontalCentered(true); } else { - rt.setBullet(true); + rt.getTextParagraph().setBullet(true); rt.setFontSize(12); - cell.setHorizontalAlignment(HSLFTextBox.AlignLeft); + rt.getTextParagraph().setAlignment(TextAlign.LEFT); + cell.setHorizontalCentered(false); } - cell.setVerticalAlignment(HSLFTextBox.AnchorMiddle); + cell.setVerticalAlignment(VerticalAlignment.MIDDLE); + cell.setText(txt2[i][j]); } } table2.setColumnWidth(0, 300); diff --git a/src/examples/src/org/apache/poi/xslf/usermodel/PieChartDemo.java b/src/examples/src/org/apache/poi/xslf/usermodel/PieChartDemo.java index de90c52d20..e399d3d0ed 100644 --- a/src/examples/src/org/apache/poi/xslf/usermodel/PieChartDemo.java +++ b/src/examples/src/org/apache/poi/xslf/usermodel/PieChartDemo.java @@ -67,7 +67,7 @@ public class PieChartDemo { String chartTitle = modelReader.readLine(); // first line is chart title XMLSlideShow pptx = new XMLSlideShow(new FileInputStream(args[0])); - XSLFSlide slide = pptx.getSlides()[0]; + XSLFSlide slide = pptx.getSlides().get(0); // find chart in the slide XSLFChart chart = null; diff --git a/src/examples/src/org/apache/poi/xslf/usermodel/Tutorial1.java b/src/examples/src/org/apache/poi/xslf/usermodel/Tutorial1.java index 93d437b913..458b987320 100644 --- a/src/examples/src/org/apache/poi/xslf/usermodel/Tutorial1.java +++ b/src/examples/src/org/apache/poi/xslf/usermodel/Tutorial1.java @@ -37,7 +37,7 @@ public class Tutorial1 { /*XSLFSlide blankSlide =*/ ppt.createSlide(); - XSLFSlideMaster master = ppt.getSlideMasters()[0]; + XSLFSlideMaster master = ppt.getSlideMasters().get(0); XSLFSlideLayout layout1 = master.getLayout(SlideLayout.TITLE); XSLFSlide slide1 = ppt.createSlide(layout1) ; diff --git a/src/examples/src/org/apache/poi/xslf/usermodel/tutorial/Step2.java b/src/examples/src/org/apache/poi/xslf/usermodel/tutorial/Step2.java index b006eb86a1..cd01d60c8c 100644 --- a/src/examples/src/org/apache/poi/xslf/usermodel/tutorial/Step2.java +++ b/src/examples/src/org/apache/poi/xslf/usermodel/tutorial/Step2.java @@ -49,7 +49,7 @@ public class Step2 { // blank slide /*XSLFSlide blankSlide =*/ ppt.createSlide(); - XSLFSlideMaster defaultMaster = ppt.getSlideMasters()[0]; + XSLFSlideMaster defaultMaster = ppt.getSlideMasters().get(0); // title slide XSLFSlideLayout titleLayout = defaultMaster.getLayout(SlideLayout.TITLE); diff --git a/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFHyperlink.java b/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFHyperlink.java index baead9e31c..02608b74a3 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFHyperlink.java +++ b/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFHyperlink.java @@ -146,50 +146,31 @@ public final class HSLFHyperlink { * @param shape TextRun to lookup hyperlinks in * @return found hyperlinks or null if not found */ - public static HSLFHyperlink[] find(HSLFTextShape shape){ - List lst = new ArrayList(); - HSLFSlideShow ppt = shape.getSheet().getSlideShow(); - //document-level container which stores info about all links in a presentation - ExObjList exobj = ppt.getDocumentRecord().getExObjList(); - if (exobj == null) { - return null; - } - - Record[] records = shape.getClientRecords(); - find(records, exobj, lst); - - HSLFHyperlink[] links = null; - if (lst.size() > 0){ - links = new HSLFHyperlink[lst.size()]; - lst.toArray(links); - } - return links; + public static List find(HSLFTextShape shape){ + return find(shape.getTextParagraphs()); } /** * Find hyperlinks in a text paragraph * - * @param paragraph TextParagraph to lookup hyperlinks in - * @return found hyperlinks or null if not found + * @param paragraphs List of TextParagraph to lookup hyperlinks + * @return found hyperlinks */ - public static HSLFHyperlink[] find(HSLFTextParagraph paragraph){ + public static List find(List paragraphs){ List lst = new ArrayList(); - HSLFSlideShow ppt = paragraph.getSheet().getSlideShow(); + if (paragraphs == null || paragraphs.isEmpty()) return lst; + + HSLFTextParagraph firstPara = paragraphs.get(0); + + HSLFSlideShow ppt = firstPara.getSheet().getSlideShow(); //document-level container which stores info about all links in a presentation ExObjList exobj = ppt.getDocumentRecord().getExObjList(); - if (exobj == null) { - return null; - } + if (exobj == null) return lst; - Record[] records = paragraph.getRecords(); + Record[] records = firstPara.getRecords(); find(records, exobj, lst); - HSLFHyperlink[] links = null; - if (lst.size() > 0){ - links = new HSLFHyperlink[lst.size()]; - lst.toArray(links); - } - return links; + return lst; } /** @@ -224,24 +205,24 @@ public final class HSLFHyperlink { if (records == null) return; for (int i = 0; i < records.length; i++) { //see if we have InteractiveInfo in the textrun's records - if( records[i] instanceof InteractiveInfo){ - InteractiveInfo hldr = (InteractiveInfo)records[i]; - InteractiveInfoAtom info = hldr.getInteractiveInfoAtom(); - int id = info.getHyperlinkID(); - ExHyperlink linkRecord = exobj.get(id); - if (linkRecord != null){ - HSLFHyperlink link = new HSLFHyperlink(); - link.title = linkRecord.getLinkTitle(); - link.address = linkRecord.getLinkURL(); - link.type = info.getAction(); - - if (++i < records.length && records[i] instanceof TxInteractiveInfoAtom){ - TxInteractiveInfoAtom txinfo = (TxInteractiveInfoAtom)records[i]; - link.startIndex = txinfo.getStartIndex(); - link.endIndex = txinfo.getEndIndex(); - } - out.add(link); - } + if(!(records[i] instanceof InteractiveInfo)) continue; + + InteractiveInfo hldr = (InteractiveInfo)records[i]; + InteractiveInfoAtom info = hldr.getInteractiveInfoAtom(); + int id = info.getHyperlinkID(); + ExHyperlink linkRecord = exobj.get(id); + if (linkRecord == null) continue; + + HSLFHyperlink link = new HSLFHyperlink(); + link.title = linkRecord.getLinkTitle(); + link.address = linkRecord.getLinkURL(); + link.type = info.getAction(); + out.add(link); + + if (i+1 < records.length && records[i+1] instanceof TxInteractiveInfoAtom){ + TxInteractiveInfoAtom txinfo = (TxInteractiveInfoAtom)records[++i]; + link.startIndex = txinfo.getStartIndex(); + link.endIndex = txinfo.getEndIndex(); } } } diff --git a/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFSlide.java b/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFSlide.java index 2fbd5c201f..048655fa64 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFSlide.java +++ b/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFSlide.java @@ -62,8 +62,8 @@ public final class HSLFSlide extends HSLFSheet implements Slide 0) { - List> llhtp = HSLFTextParagraph.findTextParagraphs(_atomSet.getSlideRecords()); - _paragraphs.addAll(llhtp); + // Grab text from SlideListWithTexts entries + _paragraphs.addAll(HSLFTextParagraph.findTextParagraphs(_atomSet.getSlideRecords())); if (_paragraphs.isEmpty()) { throw new RuntimeException("No text records found for slide"); } @@ -71,23 +71,14 @@ public final class HSLFSlide extends HSLFSheet implements Slide ltp : _paragraphs) { - for (HSLFTextParagraph tp : ltp) { - tp.supplySheet(this); - } - } + // Grab text from slide's PPDrawing + _paragraphs.addAll(HSLFTextParagraph.findTextParagraphs(getPPDrawing())); - // Grab the TextRuns from the PPDrawing - List> llOtherRuns = HSLFTextParagraph.findTextParagraphs(getPPDrawing()); - for (List otherRuns : llOtherRuns) { - // Grab text from slide's PPDrawing - for(HSLFTextParagraph tp : otherRuns) { - tp.supplySheet(this); - tp.setIndex(-1); // runs found in PPDrawing are not linked with SlideListWithTexts - } - } - _paragraphs.addAll(llOtherRuns); + for(List ltp : _paragraphs) { + for (HSLFTextParagraph tp : ltp) { + tp.supplySheet(this); + } + } } /** @@ -449,19 +440,19 @@ public final class HSLFSlide extends HSLFSheet implements Slide records = new ArrayList(); - List sa = Arrays.asList(sas); + List sa = new ArrayList(Arrays.asList(sas)); HSLFSlide removedSlide = _slides.remove(index); _notes.remove(removedSlide.getNotes()); diff --git a/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFTextParagraph.java b/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFTextParagraph.java index 7d76efe8da..b9c02dc7f3 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFTextParagraph.java +++ b/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFTextParagraph.java @@ -668,11 +668,33 @@ public final class HSLFTextParagraph implements TextParagraph { prop.setSubValue(value,index); } + /** + * Check and add linebreaks to text runs leading other paragraphs + * + * @param paragraphs + */ + protected static void fixLineEndings(List paragraphs) { + HSLFTextRun lastRun = null; + for (HSLFTextParagraph p : paragraphs) { + if (lastRun != null && !lastRun.getRawText().endsWith("\r")) { + lastRun.setText(lastRun.getRawText()+"\r"); + } + List ltr = p.getTextRuns(); + if (ltr.isEmpty()) { + throw new RuntimeException("paragraph without textruns found"); + } + lastRun = ltr.get(ltr.size()-1); + assert(lastRun.getRawText() != null); + } + } + /** * Saves the modified paragraphs/textrun to the records. * Also updates the styles to the correct text length. */ protected static void storeText(List paragraphs) { + fixLineEndings(paragraphs); + String rawText = toInternalString(getRawText(paragraphs)); // Will it fit in a 8 bit atom? @@ -738,16 +760,16 @@ public final class HSLFTextParagraph implements TextParagraph { styleAtom.clearStyles(); - TextPropCollection lastPTPC = null, lastRTPC = null; + TextPropCollection lastPTPC = null, lastRTPC = null, ptpc = null, rtpc = null; for (HSLFTextParagraph para : paragraphs) { - TextPropCollection ptpc = para.getParagraphStyle(); + ptpc = para.getParagraphStyle(); ptpc.updateTextSize(0); if (!ptpc.equals(lastPTPC)) { lastPTPC = styleAtom.addParagraphTextPropCollection(0); lastPTPC.copy(ptpc); } for (HSLFTextRun tr : para.getTextRuns()) { - TextPropCollection rtpc = tr.getCharacterStyle(); + rtpc = tr.getCharacterStyle(); rtpc.updateTextSize(0); if (!rtpc.equals(lastRTPC)) { lastRTPC = styleAtom.addCharacterTextPropCollection(0); @@ -761,7 +783,9 @@ public final class HSLFTextParagraph implements TextParagraph { } } - assert(lastPTPC != null && lastRTPC != null); + assert(lastPTPC != null && lastRTPC != null && ptpc != null && rtpc != null); + ptpc.updateTextSize(ptpc.getCharactersCovered()+1); + rtpc.updateTextSize(rtpc.getCharactersCovered()+1); lastPTPC.updateTextSize(lastPTPC.getCharactersCovered()+1); lastRTPC.updateTextSize(lastRTPC.getCharactersCovered()+1); @@ -817,6 +841,8 @@ public final class HSLFTextParagraph implements TextParagraph { } htr.setText(rawText); } + + storeText(paragraphs); return htr; } @@ -909,16 +935,7 @@ public final class HSLFTextParagraph implements TextParagraph { public static List> findTextParagraphs(PPDrawing ppdrawing) { List> runsV = new ArrayList>(); for (EscherTextboxWrapper wrapper : ppdrawing.getTextboxWrappers()) { - // propagate parents to parent-aware records - RecordContainer.handleParentAwareRecords(wrapper); - int shapeId = wrapper.getShapeId(); - List> rv = findTextParagraphs(wrapper); - for (List htpList : rv) { - for (HSLFTextParagraph htp : htpList) { - htp.setShapeId(shapeId); - } - } - runsV.addAll(rv); + runsV.addAll(findTextParagraphs(wrapper)); } return runsV; } @@ -940,8 +957,17 @@ public final class HSLFTextParagraph implements TextParagraph { * * @param wrapper an EscherTextboxWrapper */ - protected static List> findTextParagraphs(final EscherTextboxWrapper wrapper) { - return findTextParagraphs(wrapper.getChildRecords(), wrapper.getStyleTextProp9Atom()); + protected static List> findTextParagraphs(EscherTextboxWrapper wrapper) { + // propagate parents to parent-aware records + RecordContainer.handleParentAwareRecords(wrapper); + int shapeId = wrapper.getShapeId(); + List> rv = findTextParagraphs(wrapper.getChildRecords(), wrapper.getStyleTextProp9Atom()); + for (List htpList : rv) { + for (HSLFTextParagraph htp : htpList) { + htp.setShapeId(shapeId); + } + } + return rv; } /** @@ -999,7 +1025,8 @@ public final class HSLFTextParagraph implements TextParagraph { } assert(header != null); - if (header.getIndex() == -1) { + if (header.getParentRecord() instanceof SlideListWithText) { + // runs found in PPDrawing are not linked with SlideListWithTexts header.setIndex(slwtIndex); } diff --git a/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFTextRun.java b/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFTextRun.java index e94c844649..d06715b173 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFTextRun.java +++ b/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFTextRun.java @@ -383,6 +383,10 @@ public final class HSLFTextRun implements TextRun { prop.setSubValue(value, index); } + public HSLFTextParagraph getTextParagraph() { + return parentParagraph; + } + public TextCap getTextCap() { return TextCap.NONE; } diff --git a/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFTextShape.java b/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFTextShape.java index d431f0bac4..e05ae79a60 100644 --- a/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFTextShape.java +++ b/src/scratchpad/src/org/apache/poi/hslf/usermodel/HSLFTextShape.java @@ -750,7 +750,7 @@ public abstract class HSLFTextShape extends HSLFSimpleShape implements TextShape * @return the array of all hyperlinks in this text run or null * if not found. */ - public HSLFHyperlink[] getHyperlinks() { + public List getHyperlinks() { return HSLFHyperlink.find(this); } diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/model/TestHyperlink.java b/src/scratchpad/testcases/org/apache/poi/hslf/model/TestHyperlink.java index 5438ac8af9..5429b96fbc 100644 --- a/src/scratchpad/testcases/org/apache/poi/hslf/model/TestHyperlink.java +++ b/src/scratchpad/testcases/org/apache/poi/hslf/model/TestHyperlink.java @@ -53,17 +53,17 @@ public final class TestHyperlink { "In addition, its notes has one link"; assertEquals(expected, rawText); - HSLFHyperlink[] links = HSLFHyperlink.find(para.get(1)); + List links = HSLFHyperlink.find(para); assertNotNull(links); - assertEquals(2, links.length); + assertEquals(2, links.size()); - assertEquals("http://jakarta.apache.org/poi/", links[0].getTitle()); - assertEquals("http://jakarta.apache.org/poi/", links[0].getAddress()); - assertEquals("http://jakarta.apache.org/poi/", rawText.substring(links[0].getStartIndex(), links[0].getEndIndex()-1)); + assertEquals("http://jakarta.apache.org/poi/", links.get(0).getTitle()); + assertEquals("http://jakarta.apache.org/poi/", links.get(0).getAddress()); + assertEquals("http://jakarta.apache.org/poi/", rawText.substring(links.get(0).getStartIndex(), links.get(0).getEndIndex()-1)); - assertEquals("http://slashdot.org/", links[1].getTitle()); - assertEquals("http://slashdot.org/", links[1].getAddress()); - assertEquals("http://slashdot.org/", rawText.substring(links[1].getStartIndex(), links[1].getEndIndex()-1)); + assertEquals("http://slashdot.org/", links.get(1).getTitle()); + assertEquals("http://slashdot.org/", links.get(1).getAddress()); + assertEquals("http://slashdot.org/", rawText.substring(links.get(1).getStartIndex(), links.get(1).getEndIndex()-1)); slide = ppt.getSlides().get(1); para = slide.getTextParagraphs().get(1); @@ -73,12 +73,12 @@ public final class TestHyperlink { "Jakarta HSSF"; assertEquals(expected, rawText); - links = HSLFHyperlink.find(para.get(1)); + links = HSLFHyperlink.find(para); assertNotNull(links); - assertEquals(1, links.length); + assertEquals(1, links.size()); - assertEquals("http://jakarta.apache.org/poi/hssf/", links[0].getTitle()); - assertEquals("http://jakarta.apache.org/poi/hssf/", links[0].getAddress()); - assertEquals("Jakarta HSSF", rawText.substring(links[0].getStartIndex(), links[0].getEndIndex()-1)); + assertEquals("http://jakarta.apache.org/poi/hssf/", links.get(0).getTitle()); + assertEquals("http://jakarta.apache.org/poi/hssf/", links.get(0).getAddress()); + assertEquals("Jakarta HSSF", rawText.substring(links.get(0).getStartIndex(), links.get(0).getEndIndex()-1)); } } diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/record/TestDocumentEncryption.java b/src/scratchpad/testcases/org/apache/poi/hslf/record/TestDocumentEncryption.java index 4f23e4998b..7176c504e7 100644 --- a/src/scratchpad/testcases/org/apache/poi/hslf/record/TestDocumentEncryption.java +++ b/src/scratchpad/testcases/org/apache/poi/hslf/record/TestDocumentEncryption.java @@ -143,8 +143,9 @@ public class TestDocumentEncryption { HSLFSlideShowImpl hss = new HSLFSlideShowImpl(fs); HSLFSlideShow ss = new HSLFSlideShow(hss); - HSLFSlide slide = ss.getSlides()[0]; - assertEquals("Dominic Salemno", slide.getTextParagraphs()[0].getRawText()); + HSLFSlide slide = ss.getSlides().get(0); + String rawText = HSLFTextParagraph.getRawText(slide.getTextParagraphs().get(0)); + assertEquals("Dominic Salemno", rawText); String picCmp[][] = { {"0","nKsDTKqxTCR8LFkVVWlP9GSTvZ0="}, diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/record/TestSlideAtom.java b/src/scratchpad/testcases/org/apache/poi/hslf/record/TestSlideAtom.java index 4dad61a6cb..447a7646b1 100644 --- a/src/scratchpad/testcases/org/apache/poi/hslf/record/TestSlideAtom.java +++ b/src/scratchpad/testcases/org/apache/poi/hslf/record/TestSlideAtom.java @@ -85,8 +85,8 @@ public final class TestSlideAtom extends TestCase { ss.write(bos); ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray()); ss = new HSLFSlideShow(bis); - slide1 = ss.getSlides()[0]; - slide2 = ss.getSlides()[1]; + slide1 = ss.getSlides().get(0); + slide2 = ss.getSlides().get(1); assertFalse(slide1.getHidden()); assertTrue(slide2.getHidden()); } diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/record/TestStyleTextPropAtom.java b/src/scratchpad/testcases/org/apache/poi/hslf/record/TestStyleTextPropAtom.java index 665c86e691..e0e2de515a 100644 --- a/src/scratchpad/testcases/org/apache/poi/hslf/record/TestStyleTextPropAtom.java +++ b/src/scratchpad/testcases/org/apache/poi/hslf/record/TestStyleTextPropAtom.java @@ -21,13 +21,11 @@ import static org.junit.Assert.assertArrayEquals; import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.util.LinkedList; +import java.util.List; import junit.framework.TestCase; -import org.apache.poi.hslf.model.textproperties.CharFlagsTextProp; -import org.apache.poi.hslf.model.textproperties.TextProp; -import org.apache.poi.hslf.model.textproperties.TextPropCollection; +import org.apache.poi.hslf.model.textproperties.*; import org.apache.poi.util.HexDump; /** @@ -188,7 +186,7 @@ public final class TestStyleTextPropAtom extends TestCase { stpb.setParentTextSize(data_b_text_len); // 54 chars, 21 + 17 + 16 - LinkedList a_ch_l = stpa.getCharacterStyles(); + List a_ch_l = stpa.getCharacterStyles(); TextPropCollection a_ch_1 = a_ch_l.get(0); TextPropCollection a_ch_2 = a_ch_l.get(1); TextPropCollection a_ch_3 = a_ch_l.get(2); @@ -197,7 +195,7 @@ public final class TestStyleTextPropAtom extends TestCase { assertEquals(16, a_ch_3.getCharactersCovered()); // 179 chars, 30 + 28 + 25 - LinkedList b_ch_l = stpb.getCharacterStyles(); + List b_ch_l = stpb.getCharacterStyles(); TextPropCollection b_ch_1 = b_ch_l.get(0); TextPropCollection b_ch_2 = b_ch_l.get(1); TextPropCollection b_ch_3 = b_ch_l.get(2); @@ -213,7 +211,7 @@ public final class TestStyleTextPropAtom extends TestCase { StyleTextPropAtom stpb = new StyleTextPropAtom(data_b,0,data_b.length); stpb.setParentTextSize(data_b_text_len); - LinkedList b_ch_l = stpb.getCharacterStyles(); + List b_ch_l = stpb.getCharacterStyles(); TextPropCollection b_ch_1 = b_ch_l.get(0); TextPropCollection b_ch_2 = b_ch_l.get(1); TextPropCollection b_ch_3 = b_ch_l.get(2); @@ -260,7 +258,7 @@ public final class TestStyleTextPropAtom extends TestCase { StyleTextPropAtom stpb = new StyleTextPropAtom(data_b,0,data_b.length); stpb.setParentTextSize(data_b_text_len); - LinkedList b_p_l = stpb.getParagraphStyles(); + List b_p_l = stpb.getParagraphStyles(); TextPropCollection b_p_1 = b_p_l.get(0); TextPropCollection b_p_2 = b_p_l.get(1); TextPropCollection b_p_3 = b_p_l.get(2); @@ -304,7 +302,7 @@ public final class TestStyleTextPropAtom extends TestCase { StyleTextPropAtom stpb = new StyleTextPropAtom(data_b,0,data_b.length); stpb.setParentTextSize(data_b_text_len); - LinkedList b_ch_l = stpb.getCharacterStyles(); + List b_ch_l = stpb.getCharacterStyles(); TextPropCollection b_ch_1 = b_ch_l.get(0); TextPropCollection b_ch_2 = b_ch_l.get(1); TextPropCollection b_ch_3 = b_ch_l.get(2); @@ -375,13 +373,13 @@ public final class TestStyleTextPropAtom extends TestCase { StyleTextPropAtom stpb = new StyleTextPropAtom(data_b,0,data_b.length); stpb.setParentTextSize(data_b_text_len); - LinkedList b_p_l = stpb.getParagraphStyles(); + List b_p_l = stpb.getParagraphStyles(); TextPropCollection b_p_1 = b_p_l.get(0); TextPropCollection b_p_2 = b_p_l.get(1); TextPropCollection b_p_3 = b_p_l.get(2); TextPropCollection b_p_4 = b_p_l.get(3); - LinkedList b_ch_l = stpb.getCharacterStyles(); + List b_ch_l = stpb.getCharacterStyles(); TextPropCollection b_ch_1 = b_ch_l.get(0); TextPropCollection b_ch_2 = b_ch_l.get(1); TextPropCollection b_ch_3 = b_ch_l.get(2); @@ -431,7 +429,7 @@ public final class TestStyleTextPropAtom extends TestCase { // Don't need to touch the paragraph styles // Add two more character styles - LinkedList cs = stpa.getCharacterStyles(); + List cs = stpa.getCharacterStyles(); // First char style is boring, and 21 long TextPropCollection tpca = cs.get(0); @@ -468,7 +466,7 @@ public final class TestStyleTextPropAtom extends TestCase { // Need 4 paragraph styles - LinkedList ps = stpa.getParagraphStyles(); + List ps = stpa.getParagraphStyles(); // First is 30 long, left aligned, normal spacing TextPropCollection tppa = ps.get(0); @@ -503,7 +501,7 @@ public final class TestStyleTextPropAtom extends TestCase { // Now do 4 character styles - LinkedList cs = stpa.getCharacterStyles(); + List cs = stpa.getCharacterStyles(); // First is 30 long, bold and font size TextPropCollection tpca = cs.get(0); @@ -568,16 +566,16 @@ public final class TestStyleTextPropAtom extends TestCase { // Compare in detail to b StyleTextPropAtom stpb = new StyleTextPropAtom(data_b,0,data_b.length); stpb.setParentTextSize(data_b_text_len); - LinkedList psb = stpb.getParagraphStyles(); - LinkedList csb = stpb.getCharacterStyles(); + List psb = stpb.getParagraphStyles(); + List csb = stpb.getCharacterStyles(); assertEquals(psb.size(), ps.size()); assertEquals(csb.size(), cs.size()); // Ensure Paragraph Character styles match for(int z=0; z<2; z++) { - LinkedList lla = cs; - LinkedList llb = csb; + List lla = cs; + List llb = csb; int upto = 5; if(z == 1) { lla = ps; @@ -605,8 +603,8 @@ public final class TestStyleTextPropAtom extends TestCase { ByteArrayOutputStream ba = new ByteArrayOutputStream(); ByteArrayOutputStream bb = new ByteArrayOutputStream(); - ca.writeOut(ba); - cb.writeOut(bb); + ca.writeOut(ba, StyleTextPropAtom.characterTextPropTypes); + cb.writeOut(bb, StyleTextPropAtom.characterTextPropTypes); byte[] cab = ba.toByteArray(); byte[] cbb = bb.toByteArray(); @@ -695,12 +693,12 @@ public final class TestStyleTextPropAtom extends TestCase { StyleTextPropAtom atom = new StyleTextPropAtom(data_d, 0, data_d.length); atom.setParentTextSize(data_d_text_len); - TextPropCollection prprops = atom.getParagraphStyles().getFirst(); + TextPropCollection prprops = atom.getParagraphStyles().get(0); assertEquals(data_d_text_len+1, prprops.getCharactersCovered()); assertEquals(1, prprops.getTextPropList().size()); //1 property found assertEquals(1, prprops.findByName("alignment").getValue()); - TextPropCollection chprops = atom.getCharacterStyles().getFirst(); + TextPropCollection chprops = atom.getCharacterStyles().get(0); assertEquals(data_d_text_len+1, chprops.getCharactersCovered()); assertEquals(5, chprops.getTextPropList().size()); //5 properties found assertEquals(1, chprops.findByName("char_flags").getValue()); diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestAddingSlides.java b/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestAddingSlides.java index 5778beac2c..c0634f05fc 100644 --- a/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestAddingSlides.java +++ b/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestAddingSlides.java @@ -20,16 +20,12 @@ package org.apache.poi.hslf.usermodel; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; +import java.util.List; 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.*; import org.apache.poi.POIDataSamples; +import org.apache.poi.hslf.record.*; /** * Tests that SlideShow adds additional sheets properly @@ -70,7 +66,7 @@ public final class TestAddingSlides extends TestCase { */ public void testAddSlideToEmpty() throws Exception { // Doesn't have any slides - assertEquals(0, ss_empty.getSlides().length); + assertEquals(0, ss_empty.getSlides().size()); // Should only have a master SLWT assertEquals(1, ss_empty.getDocumentRecord().getSlideListWithTexts().length); @@ -88,7 +84,7 @@ public final class TestAddingSlides extends TestCase { // Add one HSLFSlide slide = ss_empty.createSlide(); - assertEquals(1, ss_empty.getSlides().length); + assertEquals(1, ss_empty.getSlides().size()); assertEquals(256, slide._getSheetNumber()); assertEquals(3, slide._getSheetRefId()); assertEquals(1, slide.getSlideNumber()); @@ -103,13 +99,13 @@ public final class TestAddingSlides extends TestCase { HSLFSlideShow ss_read = new HSLFSlideShow(hss_read); // Check it now has a slide - assertEquals(1, ss_read.getSlides().length); + assertEquals(1, ss_read.getSlides().size()); // Check it now has two SLWTs assertEquals(2, ss_empty.getDocumentRecord().getSlideListWithTexts().length); // And check it's as expected - slide = ss_read.getSlides()[0]; + slide = ss_read.getSlides().get(0); assertEquals(256, slide._getSheetNumber()); assertEquals(3, slide._getSheetRefId()); assertEquals(1, slide.getSlideNumber()); @@ -120,8 +116,8 @@ public final class TestAddingSlides extends TestCase { */ public void testAddSlideToExisting() throws Exception { // Has one slide - assertEquals(1, ss_one.getSlides().length); - HSLFSlide s1 = ss_one.getSlides()[0]; + assertEquals(1, ss_one.getSlides().size()); + HSLFSlide s1 = ss_one.getSlides().get(0); // Should have two SLTWs assertEquals(2, ss_one.getDocumentRecord().getSlideListWithTexts().length); @@ -133,7 +129,7 @@ public final class TestAddingSlides extends TestCase { // Add a second one HSLFSlide s2 = ss_one.createSlide(); - assertEquals(2, ss_one.getSlides().length); + assertEquals(2, ss_one.getSlides().size()); assertEquals(257, s2._getSheetNumber()); assertEquals(4, s2._getSheetRefId()); assertEquals(2, s2.getSlideNumber()); @@ -147,14 +143,14 @@ public final class TestAddingSlides extends TestCase { HSLFSlideShow ss_read = new HSLFSlideShow(hss_read); // Check it now has two slides - assertEquals(2, ss_read.getSlides().length); + assertEquals(2, ss_read.getSlides().size()); // Should still have two SLTWs assertEquals(2, ss_read.getDocumentRecord().getSlideListWithTexts().length); // And check it's as expected - s1 = ss_read.getSlides()[0]; - s2 = ss_read.getSlides()[1]; + s1 = ss_read.getSlides().get(0); + s2 = ss_read.getSlides().get(1); assertEquals(256, s1._getSheetNumber()); assertEquals(3, s1._getSheetRefId()); assertEquals(1, s1.getSlideNumber()); @@ -167,7 +163,8 @@ public final class TestAddingSlides extends TestCase { * Test adding a slide to an existing slideshow, * with two slides already */ - public void testAddSlideToExisting2() throws Exception { + @SuppressWarnings("unused") + public void testAddSlideToExisting2() throws Exception { //grab UserEditAtom UserEditAtom usredit = null; Record[] _records = hss_two.getRecords(); @@ -180,9 +177,9 @@ public final class TestAddingSlides extends TestCase { assertNotNull(usredit); // Has two slides - assertEquals(2, ss_two.getSlides().length); - HSLFSlide s1 = ss_two.getSlides()[0]; - HSLFSlide s2 = ss_two.getSlides()[1]; + assertEquals(2, ss_two.getSlides().size()); + HSLFSlide s1 = ss_two.getSlides().get(0); + HSLFSlide s2 = ss_two.getSlides().get(1); // Check slide 1 is as expected assertEquals(256, s1._getSheetNumber()); @@ -195,7 +192,7 @@ public final class TestAddingSlides extends TestCase { // Add a third one HSLFSlide s3 = ss_two.createSlide(); - assertEquals(3, ss_two.getSlides().length); + assertEquals(3, ss_two.getSlides().size()); assertEquals(258, s3._getSheetNumber()); assertEquals(8, s3._getSheetRefId()); // lots of notes before us assertEquals(3, s3.getSlideNumber()); @@ -210,12 +207,12 @@ public final class TestAddingSlides extends TestCase { HSLFSlideShow ss_read = new HSLFSlideShow(hss_read); // Check it now has three slides - assertEquals(3, ss_read.getSlides().length); + assertEquals(3, ss_read.getSlides().size()); // And check it's as expected - s1 = ss_read.getSlides()[0]; - s2 = ss_read.getSlides()[1]; - s3 = ss_read.getSlides()[2]; + s1 = ss_read.getSlides().get(0); + s2 = ss_read.getSlides().get(1); + s3 = ss_read.getSlides().get(2); assertEquals(256, s1._getSheetNumber()); assertEquals(4, s1._getSheetRefId()); assertEquals(1, s1.getSlideNumber()); @@ -235,8 +232,8 @@ public final class TestAddingSlides extends TestCase { HSLFSlide slide1 = ppt.createSlide(); HSLFSlide slide2 = ppt.createSlide(); - HSLFSlide[] s1 = ppt.getSlides(); - assertEquals(2, s1.length); + List s1 = ppt.getSlides(); + assertEquals(2, s1.size()); try { ppt.removeSlide(-1); fail("expected exception"); @@ -254,10 +251,10 @@ public final class TestAddingSlides extends TestCase { assertEquals(1, slide1.getSlideNumber()); HSLFSlide removedSlide = ppt.removeSlide(0); - HSLFSlide[] s2 = ppt.getSlides(); - assertEquals(1, s2.length); + List s2 = ppt.getSlides(); + assertEquals(1, s2.size()); assertSame(slide1, removedSlide); - assertSame(slide2, s2[0]); + assertSame(slide2, s2.get(0)); assertEquals(0, slide2.getSlideNumber()); @@ -266,29 +263,29 @@ public final class TestAddingSlides extends TestCase { ppt = new HSLFSlideShow(new ByteArrayInputStream(out.toByteArray())); - HSLFSlide[] s3 = ppt.getSlides(); - assertEquals(1, s3.length); + List s3 = ppt.getSlides(); + assertEquals(1, s3.size()); } public void test47261() throws Exception { POIDataSamples slTests = POIDataSamples.getSlideShowInstance(); HSLFSlideShow ppt = new HSLFSlideShow(slTests.openResourceAsStream("47261.ppt")); - HSLFSlide[] slides = ppt.getSlides(); + List slides = ppt.getSlides(); Document doc = ppt.getDocumentRecord(); assertNotNull(doc.getSlideSlideListWithText()); - assertEquals(14, ppt.getSlides().length); - int notesId = slides[0].getSlideRecord().getSlideAtom().getNotesID(); + assertEquals(14, ppt.getSlides().size()); + int notesId = slides.get(0).getSlideRecord().getSlideAtom().getNotesID(); assertTrue(notesId > 0); assertNotNull(doc.getNotesSlideListWithText()); assertEquals(14, doc.getNotesSlideListWithText().getSlideAtomsSets().length); //remove all slides, corresponding notes should be removed too - for (int i = 0; i < slides.length; i++) { + for (int i = 0; i < slides.size(); i++) { ppt.removeSlide(0); } - assertEquals(0, ppt.getSlides().length); - assertEquals(0, ppt.getNotes().length); + assertEquals(0, ppt.getSlides().size()); + assertEquals(0, ppt.getNotes().size()); assertNull(doc.getSlideSlideListWithText()); assertNull(doc.getNotesSlideListWithText()); diff --git a/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestBugs.java b/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestBugs.java index 1dcd58ca37..12e3ff6c30 100644 --- a/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestBugs.java +++ b/src/scratchpad/testcases/org/apache/poi/hslf/usermodel/TestBugs.java @@ -17,7 +17,7 @@ package org.apache.poi.hslf.usermodel; -import static org.junit.Assert.assertEquals; +import static org.junit.Assert.*; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; @@ -73,7 +73,7 @@ public final class TestBugs { HSLFSlideShow ppt = new HSLFSlideShow(hslf); assertTrue("No Exceptions while reading file", true); - assertEquals(1, ppt.getSlides().length); + assertEquals(1, ppt.getSlides().size()); HSLFPictureData[] pict = ppt.getPictureData(); assertEquals(2, pict.length); @@ -91,23 +91,23 @@ public final class TestBugs { HSLFSlideShow ppt = new HSLFSlideShow(hslf); assertTrue("No Exceptions while reading file", true); - assertEquals(2, ppt.getSlides().length); + assertEquals(2, ppt.getSlides().size()); - HSLFTextParagraph txrun; + List txrun; HSLFNotes notes; - notes = ppt.getSlides()[0].getNotesSheet(); + notes = ppt.getSlides().get(0).getNotes(); assertNotNull(notes); - txrun = notes.getTextParagraphs()[0]; - assertEquals("Notes-1", txrun.getRawText()); - assertEquals(false, txrun.getTextRuns()[0].isBold()); + txrun = notes.getTextParagraphs().get(0); + assertEquals("Notes-1", HSLFTextParagraph.getRawText(txrun)); + assertEquals(false, txrun.get(0).getTextRuns().get(0).isBold()); //notes for the second slide are in bold - notes = ppt.getSlides()[1].getNotesSheet(); + notes = ppt.getSlides().get(1).getNotes(); assertNotNull(notes); - txrun = notes.getTextParagraphs()[0]; - assertEquals("Notes-2", txrun.getRawText()); - assertEquals(true, txrun.getTextRuns()[0].isBold()); + txrun = notes.getTextParagraphs().get(0); + assertEquals("Notes-2", HSLFTextParagraph.getRawText(txrun)); + assertEquals(true, txrun.get(0).getTextRuns().get(0).isBold()); } @@ -128,13 +128,12 @@ public final class TestBugs { notesMap.put(Integer.valueOf(7), "Although multiply and square root are easier"); notesMap.put(Integer.valueOf(8), "The bus Z is split into Z_H and Z_L"); - HSLFSlide[] slide = ppt.getSlides(); - for (int i = 0; i < slide.length; i++) { - Integer slideNumber = Integer.valueOf(slide[i].getSlideNumber()); - HSLFNotes notes = slide[i].getNotesSheet(); + for (HSLFSlide slide : ppt.getSlides()) { + Integer slideNumber = Integer.valueOf(slide.getSlideNumber()); + HSLFNotes notes = slide.getNotes(); if (notesMap.containsKey(slideNumber)){ assertNotNull(notes); - String text = notes.getTextParagraphs()[0].getRawText(); + String text = HSLFTextParagraph.getRawText(notes.getTextParagraphs().get(0)); String startingPhrase = notesMap.get(slideNumber); assertTrue("Notes for slide " + slideNumber + " must start with " + startingPhrase , text.startsWith(startingPhrase)); @@ -150,14 +149,12 @@ public final class TestBugs { HSLFSlideShowImpl hslf = new HSLFSlideShowImpl(_slTests.openResourceAsStream("42485.ppt")); HSLFSlideShow ppt = new HSLFSlideShow(hslf); - HSLFShape[] shape = ppt.getSlides()[0].getShapes(); - for (int i = 0; i < shape.length; i++) { - if(shape[i] instanceof HSLFGroupShape){ - HSLFGroupShape group = (HSLFGroupShape)shape[i]; - HSLFShape[] sh = group.getShapes(); - for (int j = 0; j < sh.length; j++) { - if( sh[j] instanceof HSLFTextBox){ - HSLFTextBox txt = (HSLFTextBox)sh[j]; + for (HSLFShape shape : ppt.getSlides().get(0).getShapes()) { + if(shape instanceof HSLFGroupShape){ + HSLFGroupShape group = (HSLFGroupShape)shape; + for (HSLFShape sh : group.getShapes()) { + if(sh instanceof HSLFTextBox){ + HSLFTextBox txt = (HSLFTextBox)sh; assertNotNull(txt.getTextParagraphs()); } } @@ -173,14 +170,12 @@ public final class TestBugs { HSLFSlideShowImpl hslf = new HSLFSlideShowImpl(_slTests.openResourceAsStream("42485.ppt")); HSLFSlideShow ppt = new HSLFSlideShow(hslf); - HSLFShape[] shape = ppt.getSlides()[0].getShapes(); - for (int i = 0; i < shape.length; i++) { - if(shape[i] instanceof HSLFGroupShape){ - HSLFGroupShape group = (HSLFGroupShape)shape[i]; + for (HSLFShape shape : ppt.getSlides().get(0).getShapes()) { + if(shape instanceof HSLFGroupShape){ + HSLFGroupShape group = (HSLFGroupShape)shape; assertNotNull(group.getAnchor()); - HSLFShape[] sh = group.getShapes(); - for (int j = 0; j < sh.length; j++) { - assertNotNull(sh[j].getAnchor()); + for (HSLFShape sh : group.getShapes()) { + assertNotNull(sh.getAnchor()); } } } @@ -197,28 +192,28 @@ public final class TestBugs { HSLFSlideShow ppt = new HSLFSlideShow(hslf); assertTrue("No Exceptions while reading file", true); - assertEquals(1, ppt.getSlidesMasters().length); - assertEquals(1, ppt.getTitleMasters().length); - HSLFSlide[] slide = ppt.getSlides(); - for (int i = 0; i < slide.length; i++) { - HSLFMasterSheet master = slide[i].getMasterSheet(); - if (i == 0) assertTrue(master instanceof HSLFTitleMaster); //the first slide follows TitleMaster - else assertTrue(master instanceof HSLFSlideMaster); + assertEquals(1, ppt.getSlideMasters().size()); + assertEquals(1, ppt.getTitleMasters().size()); + boolean isFirst = true; + for (HSLFSlide slide : ppt.getSlides()) { + HSLFMasterSheet master = slide.getMasterSheet(); + // the first slide follows TitleMaster + assertTrue(isFirst ? master instanceof HSLFTitleMaster : master instanceof HSLFSlideMaster); + isFirst = false; } } /** * Bug 42486: Failure parsing a seemingly valid PPT */ + @SuppressWarnings("unused") @Test public void bug42486 () throws Exception { HSLFSlideShowImpl hslf = new HSLFSlideShowImpl(_slTests.openResourceAsStream("42486.ppt")); HSLFSlideShow ppt = new HSLFSlideShow(hslf); - HSLFSlide[] slide = ppt.getSlides(); - for (int i = 0; i < slide.length; i++) { - @SuppressWarnings("unused") - HSLFShape[] shape = slide[i].getShapes(); + for (HSLFSlide slide : ppt.getSlides()) { + List shape = slide.getShapes(); } assertTrue("No Exceptions while reading file", true); @@ -233,16 +228,13 @@ public final class TestBugs { HSLFSlideShow ppt = new HSLFSlideShow(hslf); //walk down the tree and see if there were no errors while reading - HSLFSlide[] slide = ppt.getSlides(); - for (int i = 0; i < slide.length; i++) { - HSLFShape[] shape = slide[i].getShapes(); - for (int j = 0; j < shape.length; j++) { - assertNotNull(shape[j].getShapeName()); - if (shape[j] instanceof HSLFGroupShape){ - HSLFGroupShape group = (HSLFGroupShape)shape[j]; - HSLFShape[] comps = group.getShapes(); - for (int k = 0; k < comps.length; k++) { - assertNotNull(comps[k].getShapeName()); + for (HSLFSlide slide : ppt.getSlides()) { + for (HSLFShape shape : slide.getShapes()) { + assertNotNull(shape.getShapeName()); + if (shape instanceof HSLFGroupShape){ + HSLFGroupShape group = (HSLFGroupShape)shape; + for (HSLFShape comps : group.getShapes()) { + assertNotNull(comps.getShapeName()); } } } @@ -255,6 +247,7 @@ public final class TestBugs { /** * Bug 42520: NPE in Picture.getPictureData() */ + @SuppressWarnings("unused") @Test public void bug42520 () throws Exception { HSLFSlideShowImpl hslf = new HSLFSlideShowImpl(_slTests.openResourceAsStream("42520.ppt")); @@ -262,22 +255,17 @@ public final class TestBugs { HSLFSlideShow ppt = new HSLFSlideShow(hslf); //test case from the bug report - HSLFGroupShape shapeGroup = (HSLFGroupShape)ppt.getSlides()[11].getShapes()[10]; - HSLFPictureShape picture = (HSLFPictureShape)shapeGroup.getShapes()[0]; + HSLFGroupShape shapeGroup = (HSLFGroupShape)ppt.getSlides().get(11).getShapes().get(10); + HSLFPictureShape picture = (HSLFPictureShape)shapeGroup.getShapes().get(0); picture.getPictureData(); //walk down the tree and see if there were no errors while reading - HSLFSlide[] slide = ppt.getSlides(); - for (int i = 0; i < slide.length; i++) { - HSLFShape[] shape = slide[i].getShapes(); - for (int j = 0; j < shape.length; j++) { - if (shape[j] instanceof HSLFGroupShape){ - HSLFGroupShape group = (HSLFGroupShape)shape[j]; - HSLFShape[] comps = group.getShapes(); - for (int k = 0; k < comps.length; k++) { - HSLFShape comp = comps[k]; + for (HSLFSlide slide : ppt.getSlides()) { + for (HSLFShape shape : slide.getShapes()) { + if (shape instanceof HSLFGroupShape){ + HSLFGroupShape group = (HSLFGroupShape)shape; + for (HSLFShape comp : group.getShapes()) { if (comp instanceof HSLFPictureShape){ - @SuppressWarnings("unused") HSLFPictureData pict = ((HSLFPictureShape)comp).getPictureData(); } } @@ -299,10 +287,10 @@ public final class TestBugs { assertTrue("No Exceptions while reading file", true); - HSLFSlide[] slide = ppt.getSlides(); - assertEquals(1, slide.length); - HSLFTextParagraph[] runs = slide[0].getTextParagraphs(); - assertEquals(4, runs.length); + List slide = ppt.getSlides(); + assertEquals(1, slide.size()); + List> paras = slide.get(0).getTextParagraphs(); + assertEquals(4, paras.size()); Set txt = new HashSet(); txt.add("\u201CHAPPY BIRTHDAY SCOTT\u201D"); @@ -310,8 +298,8 @@ public final class TestBugs { txt.add("PS Nobody is allowed to hassle Scott TODAY\u2026"); txt.add("Drinks will be in the Boardroom at 5pm today to celebrate Scott\u2019s B\u2019Day\u2026 See you all there!"); - for (int i = 0; i < runs.length; i++) { - String text = runs[i].getRawText(); + for (List para : paras) { + String text = HSLFTextParagraph.getRawText(para); assertTrue(text, txt.contains(text)); } @@ -322,39 +310,37 @@ public final class TestBugs { * ( also fixed followup: getTextRuns() returns no text ) */ @Test - public void bug43781 () throws Exception { + public void bug43781() throws Exception { HSLFSlideShow ppt = new HSLFSlideShow(_slTests.openResourceAsStream("43781.ppt")); assertTrue("No Exceptions while reading file", true); // Check the first slide - HSLFSlide slide = ppt.getSlides()[0]; - HSLFTextParagraph[] slTr = slide.getTextParagraphs(); + HSLFSlide slide = ppt.getSlides().get(0); + List> slTr = slide.getTextParagraphs(); - // Has two text runs, one from slide text, one from drawing - assertEquals(2, slTr.length); - assertEquals(false, slTr[0].isDrawingBased()); - assertEquals(true, slTr[1].isDrawingBased()); - assertEquals("First run", slTr[0].getRawText()); - assertEquals("Second run", slTr[1].getRawText()); + // Has 3 text paragraphs, two from slide text (empty title / filled body), one from drawing + assertEquals(3, slTr.size()); + assertFalse(slTr.get(0).get(0).isDrawingBased()); + assertFalse(slTr.get(1).get(0).isDrawingBased()); + assertTrue(slTr.get(2).get(0).isDrawingBased()); + assertEquals("", HSLFTextParagraph.getRawText(slTr.get(0))); + assertEquals("First run", HSLFTextParagraph.getRawText(slTr.get(1))); + assertEquals("Second run", HSLFTextParagraph.getRawText(slTr.get(2))); // Check the shape based text runs List lst = new ArrayList(); - HSLFShape[] shape = slide.getShapes(); - for (int i = 0; i < shape.length; i++) { - if( shape[i] instanceof HSLFTextShape){ - HSLFTextParagraph textRun = ((HSLFTextShape)shape[i]).getTextParagraphs(); - if(textRun != null) { - lst.add(textRun); - } + for (HSLFShape shape : slide.getShapes()) { + if (shape instanceof HSLFTextShape){ + List textRun = ((HSLFTextShape)shape).getTextParagraphs(); + lst.addAll(textRun); } } - // There should be only one shape based one found - assertEquals(1, lst.size()); - // And it should be the second one - assertEquals("Second run", lst.get(0).getRawText()); + // There are two shapes in the ppt + assertEquals(2, lst.size()); + assertEquals("First runSecond run", HSLFTextParagraph.getRawText(lst)); } /** @@ -364,7 +350,7 @@ public final class TestBugs { public void bug44296 () throws Exception { HSLFSlideShow ppt = new HSLFSlideShow(_slTests.openResourceAsStream("44296.ppt")); - HSLFSlide slide = ppt.getSlides()[0]; + HSLFSlide slide = ppt.getSlides().get(0); HSLFBackground b = slide.getBackground(); HSLFFill f = b.getFill(); @@ -397,16 +383,16 @@ public final class TestBugs { public void bug41071() throws Exception { HSLFSlideShow ppt = new HSLFSlideShow(_slTests.openResourceAsStream("41071.ppt")); - HSLFSlide slide = ppt.getSlides()[0]; - HSLFShape[] sh = slide.getShapes(); - assertEquals(1, sh.length); - assertTrue(sh[0] instanceof HSLFTextShape); - HSLFTextShape tx = (HSLFTextShape)sh[0]; - assertEquals("Fundera, planera och involvera.", tx.getTextParagraphs().getRawText()); + HSLFSlide slide = ppt.getSlides().get(0); + List sh = slide.getShapes(); + assertEquals(1, sh.size()); + assertTrue(sh.get(0) instanceof HSLFTextShape); + HSLFTextShape tx = (HSLFTextShape)sh.get(0); + assertEquals("Fundera, planera och involvera.", HSLFTextParagraph.getRawText(tx.getTextParagraphs())); - HSLFTextParagraph[] run = slide.getTextParagraphs(); - assertEquals(1, run.length); - assertEquals("Fundera, planera och involvera.", run[0].getRawText()); + List> run = slide.getTextParagraphs(); + assertEquals(3, run.size()); + assertEquals("Fundera, planera och involvera.", HSLFTextParagraph.getRawText(run.get(2))); } /** @@ -429,10 +415,10 @@ public final class TestBugs { public void bug49648() throws Exception { HSLFSlideShow ppt = new HSLFSlideShow(_slTests.openResourceAsStream("49648.ppt")); for(HSLFSlide slide : ppt.getSlides()) { - for(HSLFTextParagraph run : slide.getTextParagraphs()) { - String text = run.getRawText(); + for(List run : slide.getTextParagraphs()) { + String text = HSLFTextParagraph.getRawText(run); text.replace("{txtTot}", "With \u0123\u1234\u5678 unicode"); - run.setRawText(text); + HSLFTextParagraph.setText(run, text); } } } @@ -487,9 +473,9 @@ public final class TestBugs { str = str.replace("$$DATE$$", new Date().toString()); tb.setText(str); - HSLFTextParagraph tr = tb.getTextParagraphs(); - assertEquals(str.length()+1,tr.getStyleTextPropAtom().getParagraphStyles().getFirst().getCharactersCovered()); - assertEquals(str.length()+1,tr.getStyleTextPropAtom().getCharacterStyles().getFirst().getCharactersCovered()); + List tr = tb.getTextParagraphs(); + assertEquals(str.length()+1,tr.get(0).getParagraphStyle().getCharactersCovered()); + assertEquals(str.length()+1,tr.get(0).getTextRuns().get(0).getCharacterStyle().getCharactersCovered()); } } } @@ -500,7 +486,7 @@ public final class TestBugs { HSLFSlideShowImpl ss = new HSLFSlideShowImpl(file.getAbsolutePath()); HSLFSlideShow _show = new HSLFSlideShow(ss); - HSLFSlide[] _slides = _show.getSlides(); + List _slides = _show.getSlides(); /* Iterate over slides and extract text */ for( HSLFSlide slide : _slides ) { @@ -516,8 +502,8 @@ public final class TestBugs { HSLFSlideShowImpl ss = new HSLFSlideShowImpl(file.getAbsolutePath()); HSLFSlideShow _show = new HSLFSlideShow(ss); - HSLFSlide[] _slides = _show.getSlides(); - assertEquals(13, _slides.length); + List _slides = _show.getSlides(); + assertEquals(13, _slides.size()); // Check the number of TextHeaderAtoms on Slide 1 Document dr = _show.getDocumentRecord(); @@ -538,8 +524,8 @@ public final class TestBugs { // Check the number of text runs based on the slide (not textbox) // Will have skipped the empty one int str = 0; - for (HSLFTextParagraph tr : _slides[0].getTextParagraphs()) { - if (! tr.isDrawingBased()) str++; + for (List tr : _slides.get(0).getTextParagraphs()) { + if (! tr.get(0).isDrawingBased()) str++; } assertEquals(1, str); } @@ -549,11 +535,11 @@ public final class TestBugs { InputStream inputStream = new FileInputStream(_slTests.getFile("37625.ppt")); try { HSLFSlideShow slideShow = new HSLFSlideShow(inputStream); - assertEquals(29, slideShow.getSlides().length); + assertEquals(29, slideShow.getSlides().size()); HSLFSlideShow slideBack = HSLFTestDataSamples.writeOutAndReadBack(slideShow); assertNotNull(slideBack); - assertEquals(29, slideBack.getSlides().length); + assertEquals(29, slideBack.getSlides().size()); } finally { inputStream.close(); } @@ -564,11 +550,11 @@ public final class TestBugs { InputStream inputStream = new FileInputStream(_slTests.getFile("57272_corrupted_usereditatom.ppt")); try { HSLFSlideShow slideShow = new HSLFSlideShow(inputStream); - assertEquals(6, slideShow.getSlides().length); + assertEquals(6, slideShow.getSlides().size()); HSLFSlideShow slideBack = HSLFTestDataSamples.writeOutAndReadBack(slideShow); assertNotNull(slideBack); - assertEquals(6, slideBack.getSlides().length); + assertEquals(6, slideBack.getSlides().size()); } finally { inputStream.close(); } @@ -579,9 +565,9 @@ public final class TestBugs { InputStream inputStream = new FileInputStream(_slTests.getFile("49541_symbol_map.ppt")); try { HSLFSlideShow slideShow = new HSLFSlideShow(inputStream); - HSLFSlide slide = slideShow.getSlides()[0]; - HSLFGroupShape sg = (HSLFGroupShape)slide.getShapes()[0]; - HSLFTextBox tb = (HSLFTextBox)sg.getShapes()[0]; + HSLFSlide slide = slideShow.getSlides().get(0); + HSLFGroupShape sg = (HSLFGroupShape)slide.getShapes().get(0); + HSLFTextBox tb = (HSLFTextBox)sg.getShapes().get(0); String text = StringUtil.mapMsCodepointString(tb.getText()); assertEquals("\u226575 years", text); } finally { @@ -608,7 +594,7 @@ public final class TestBugs { InputStream inputStream = new FileInputStream(_slTests.getFile("bug56240.ppt")); try { HSLFSlideShow slideShow = new HSLFSlideShow(inputStream); - int slideCnt = slideShow.getSlides().length; + int slideCnt = slideShow.getSlides().size(); assertEquals(105, slideCnt); ByteArrayOutputStream bos = new ByteArrayOutputStream(); slideShow.write(bos); @@ -623,7 +609,7 @@ public final class TestBugs { InputStream inputStream = new FileInputStream(_slTests.getFile("bug46441.ppt")); try { HSLFSlideShow slideShow = new HSLFSlideShow(inputStream); - HSLFAutoShape as = (HSLFAutoShape)slideShow.getSlides()[0].getShapes()[0]; + HSLFAutoShape as = (HSLFAutoShape)slideShow.getSlides().get(0).getShapes().get(0); EscherOptRecord opt = as.getEscherOptRecord(); EscherArrayProperty ep = HSLFShape.getEscherProperty(opt, EscherProperties.FILL__SHADECOLORS); double exp[][] = { -- 2.39.5