[JAVA-9345] Split apache-poi module

This commit is contained in:
Haroon Khan
2022-01-06 22:57:49 +00:00
parent 4bf18421c9
commit f3d1642ae3
11 changed files with 42 additions and 22 deletions

View File

@@ -0,0 +1,239 @@
package com.baeldung.poi.powerpoint;
import org.apache.poi.sl.usermodel.AutoNumberingScheme;
import org.apache.poi.sl.usermodel.PictureData;
import org.apache.poi.sl.usermodel.TableCell;
import org.apache.poi.sl.usermodel.TextParagraph;
import org.apache.poi.util.IOUtils;
import org.apache.poi.xslf.usermodel.SlideLayout;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFAutoShape;
import org.apache.poi.xslf.usermodel.XSLFHyperlink;
import org.apache.poi.xslf.usermodel.XSLFPictureData;
import org.apache.poi.xslf.usermodel.XSLFPictureShape;
import org.apache.poi.xslf.usermodel.XSLFShape;
import org.apache.poi.xslf.usermodel.XSLFSlide;
import org.apache.poi.xslf.usermodel.XSLFSlideLayout;
import org.apache.poi.xslf.usermodel.XSLFSlideMaster;
import org.apache.poi.xslf.usermodel.XSLFTable;
import org.apache.poi.xslf.usermodel.XSLFTableCell;
import org.apache.poi.xslf.usermodel.XSLFTableRow;
import org.apache.poi.xslf.usermodel.XSLFTextParagraph;
import org.apache.poi.xslf.usermodel.XSLFTextRun;
import org.apache.poi.xslf.usermodel.XSLFTextShape;
import java.awt.*;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* Helper class for the PowerPoint presentation creation
*/
public class PowerPointHelper {
/**
* Read an existing presentation
*
* @param fileLocation
* File location of the presentation
* @return instance of {@link XMLSlideShow}
* @throws IOException
*/
public XMLSlideShow readingExistingSlideShow(String fileLocation) throws IOException {
return new XMLSlideShow(new FileInputStream(fileLocation));
}
/**
* Create a sample presentation
*
* @param fileLocation
* File location of the presentation
* @throws IOException
*/
public void createPresentation(String fileLocation) throws IOException {
// Create presentation
XMLSlideShow ppt = new XMLSlideShow();
XSLFSlideMaster defaultMaster = ppt.getSlideMasters().get(0);
// Retriving the slide layout
XSLFSlideLayout layout = defaultMaster.getLayout(SlideLayout.TITLE_ONLY);
// Creating the 1st slide
XSLFSlide slide1 = ppt.createSlide(layout);
XSLFTextShape title = slide1.getPlaceholder(0);
// Clearing text to remove the predefined one in the template
title.clearText();
XSLFTextParagraph p = title.addNewTextParagraph();
XSLFTextRun r1 = p.addNewTextRun();
r1.setText("Baeldung");
r1.setFontColor(new Color(78, 147, 89));
r1.setFontSize(48.);
// Add Image
ClassLoader classLoader = getClass().getClassLoader();
byte[] pictureData = IOUtils.toByteArray(new FileInputStream(classLoader.getResource("logo-leaf.png").getFile()));
XSLFPictureData pd = ppt.addPicture(pictureData, PictureData.PictureType.PNG);
XSLFPictureShape picture = slide1.createPicture(pd);
picture.setAnchor(new Rectangle(320, 230, 100, 92));
// Creating 2nd slide
layout = defaultMaster.getLayout(SlideLayout.TITLE_AND_CONTENT);
XSLFSlide slide2 = ppt.createSlide(layout);
// setting the tile
title = slide2.getPlaceholder(0);
title.clearText();
XSLFTextRun r = title.addNewTextParagraph().addNewTextRun();
r.setText("Baeldung");
// Adding the link
XSLFHyperlink link = r.createHyperlink();
link.setAddress("http://www.baeldung.com");
// setting the content
XSLFTextShape content = slide2.getPlaceholder(1);
content.clearText(); // unset any existing text
content.addNewTextParagraph().addNewTextRun().setText("First paragraph");
content.addNewTextParagraph().addNewTextRun().setText("Second paragraph");
content.addNewTextParagraph().addNewTextRun().setText("Third paragraph");
// Creating 3rd slide - List
layout = defaultMaster.getLayout(SlideLayout.TITLE_AND_CONTENT);
XSLFSlide slide3 = ppt.createSlide(layout);
title = slide3.getPlaceholder(0);
title.clearText();
r = title.addNewTextParagraph().addNewTextRun();
r.setText("Lists");
content = slide3.getPlaceholder(1);
content.clearText();
XSLFTextParagraph p1 = content.addNewTextParagraph();
p1.setIndentLevel(0);
p1.setBullet(true);
r1 = p1.addNewTextRun();
r1.setText("Bullet");
// the next three paragraphs form an auto-numbered list
XSLFTextParagraph p2 = content.addNewTextParagraph();
p2.setBulletAutoNumber(AutoNumberingScheme.alphaLcParenRight, 1);
p2.setIndentLevel(1);
XSLFTextRun r2 = p2.addNewTextRun();
r2.setText("Numbered List Item - 1");
// Creating 4th slide
XSLFSlide slide4 = ppt.createSlide();
createTable(slide4);
// Save presentation
FileOutputStream out = new FileOutputStream(fileLocation);
ppt.write(out);
out.close();
// Closing presentation
ppt.close();
}
/**
* Delete a slide from the presentation
*
* @param ppt
* The presentation
* @param slideNumber
* The number of the slide to be deleted (0-based)
*/
public void deleteSlide(XMLSlideShow ppt, int slideNumber) {
ppt.removeSlide(slideNumber);
}
/**
* Re-order the slides inside a presentation
*
* @param ppt
* The presentation
* @param slideNumber
* The number of the slide to move
* @param newSlideNumber
* The new position of the slide (0-base)
*/
public void reorderSlide(XMLSlideShow ppt, int slideNumber, int newSlideNumber) {
List<XSLFSlide> slides = ppt.getSlides();
XSLFSlide secondSlide = slides.get(slideNumber);
ppt.setSlideOrder(secondSlide, newSlideNumber);
}
/**
* Retrieve the placeholder inside a slide
*
* @param slide
* The slide
* @return List of placeholder inside a slide
*/
public List<XSLFShape> retrieveTemplatePlaceholders(XSLFSlide slide) {
List<XSLFShape> placeholders = new ArrayList<>();
for (XSLFShape shape : slide.getShapes()) {
if (shape instanceof XSLFAutoShape) {
placeholders.add(shape);
}
}
return placeholders;
}
/**
* Create a table
*
* @param slide
* Slide
*/
private void createTable(XSLFSlide slide) {
XSLFTable tbl = slide.createTable();
tbl.setAnchor(new Rectangle(50, 50, 450, 300));
int numColumns = 3;
int numRows = 5;
// header
XSLFTableRow headerRow = tbl.addRow();
headerRow.setHeight(50);
for (int i = 0; i < numColumns; i++) {
XSLFTableCell th = headerRow.addCell();
XSLFTextParagraph p = th.addNewTextParagraph();
p.setTextAlign(TextParagraph.TextAlign.CENTER);
XSLFTextRun r = p.addNewTextRun();
r.setText("Header " + (i + 1));
r.setBold(true);
r.setFontColor(Color.white);
th.setFillColor(new Color(79, 129, 189));
th.setBorderWidth(TableCell.BorderEdge.bottom, 2.0);
th.setBorderColor(TableCell.BorderEdge.bottom, Color.white);
// all columns are equally sized
tbl.setColumnWidth(i, 150);
}
// data
for (int rownum = 0; rownum < numRows; rownum++) {
XSLFTableRow tr = tbl.addRow();
tr.setHeight(50);
for (int i = 0; i < numColumns; i++) {
XSLFTableCell cell = tr.addCell();
XSLFTextParagraph p = cell.addNewTextParagraph();
XSLFTextRun r = p.addNewTextRun();
r.setText("Cell " + (i * rownum + 1));
if (rownum % 2 == 0) {
cell.setFillColor(new Color(208, 216, 232));
} else {
cell.setFillColor(new Color(233, 247, 244));
}
}
}
}
}

View File

@@ -0,0 +1,95 @@
package com.baeldung.poi.word;
import org.apache.poi.util.Units;
import org.apache.poi.xwpf.usermodel.ParagraphAlignment;
import org.apache.poi.xwpf.usermodel.UnderlinePatterns;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class WordDocument {
public static String logo = "logo-leaf.png";
public static String paragraph1 = "poi-word-para1.txt";
public static String paragraph2 = "poi-word-para2.txt";
public static String paragraph3 = "poi-word-para3.txt";
public static String output = "rest-with-spring.docx";
public void handleSimpleDoc() throws Exception {
XWPFDocument document = new XWPFDocument();
XWPFParagraph title = document.createParagraph();
title.setAlignment(ParagraphAlignment.CENTER);
XWPFRun titleRun = title.createRun();
titleRun.setText("Build Your REST API with Spring");
titleRun.setColor("009933");
titleRun.setBold(true);
titleRun.setFontFamily("Courier");
titleRun.setFontSize(20);
XWPFParagraph subTitle = document.createParagraph();
subTitle.setAlignment(ParagraphAlignment.CENTER);
XWPFRun subTitleRun = subTitle.createRun();
subTitleRun.setText("from HTTP fundamentals to API Mastery");
subTitleRun.setColor("00CC44");
subTitleRun.setFontFamily("Courier");
subTitleRun.setFontSize(16);
subTitleRun.setTextPosition(20);
subTitleRun.setUnderline(UnderlinePatterns.DOT_DOT_DASH);
XWPFParagraph image = document.createParagraph();
image.setAlignment(ParagraphAlignment.CENTER);
XWPFRun imageRun = image.createRun();
imageRun.setTextPosition(20);
Path imagePath = Paths.get(ClassLoader.getSystemResource(logo).toURI());
imageRun.addPicture(Files.newInputStream(imagePath), XWPFDocument.PICTURE_TYPE_PNG, imagePath.getFileName().toString(), Units.toEMU(50), Units.toEMU(50));
XWPFParagraph sectionTitle = document.createParagraph();
XWPFRun sectionTRun = sectionTitle.createRun();
sectionTRun.setText("What makes a good API?");
sectionTRun.setColor("00CC44");
sectionTRun.setBold(true);
sectionTRun.setFontFamily("Courier");
XWPFParagraph para1 = document.createParagraph();
para1.setAlignment(ParagraphAlignment.BOTH);
String string1 = convertTextFileToString(paragraph1);
XWPFRun para1Run = para1.createRun();
para1Run.setText(string1);
XWPFParagraph para2 = document.createParagraph();
para2.setAlignment(ParagraphAlignment.RIGHT);
String string2 = convertTextFileToString(paragraph2);
XWPFRun para2Run = para2.createRun();
para2Run.setText(string2);
para2Run.setItalic(true);
XWPFParagraph para3 = document.createParagraph();
para3.setAlignment(ParagraphAlignment.LEFT);
String string3 = convertTextFileToString(paragraph3);
XWPFRun para3Run = para3.createRun();
para3Run.setText(string3);
FileOutputStream out = new FileOutputStream(output);
document.write(out);
out.close();
document.close();
}
public String convertTextFileToString(String fileName) {
try (Stream<String> stream = Files.lines(Paths.get(ClassLoader.getSystemResource(fileName).toURI()))) {
return stream.collect(Collectors.joining(" "));
} catch (IOException | URISyntaxException e) {
e.printStackTrace();
}
return null;
}
}

Binary file not shown.

After

Width:  |  Height:  |  Size: 782 B

View File

@@ -0,0 +1 @@
I published the first REST with Spring tutorials over four years ago, and after 300,000 readers and over a thousand personal questions over email, I am finally realizing that there is a huge education gap in the ecosystem.

View File

@@ -0,0 +1,2 @@
I had to learn what makes a good API is the hard way, and I couldnt find anything out there to help me speed things up.
It took me 3 years, more than 10 production grade APIs built from scratch and a decent number of mistakes to get to a mature understanding of how an API should really be done well.

View File

@@ -0,0 +1,3 @@
Sure, there were tutorials and random code samples out there, but nothing coherent.
Nothing start to finish to act as a guide through the challenges of building a mature API with Spring.
The educational gap between beginner and the API professional remains vast.

View File

@@ -0,0 +1,82 @@
package com.baeldung.poi.powerpoint;
import org.apache.poi.xslf.usermodel.XMLSlideShow;
import org.apache.poi.xslf.usermodel.XSLFShape;
import org.apache.poi.xslf.usermodel.XSLFSlide;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
import java.io.File;
import java.util.List;
public class PowerPointIntegrationTest {
private PowerPointHelper pph;
private String fileLocation;
private static final String FILE_NAME = "presentation.pptx";
@Rule
public TemporaryFolder tempFolder = new TemporaryFolder();
@Before
public void setUp() throws Exception {
File currDir = tempFolder.newFolder();
String path = currDir.getAbsolutePath();
fileLocation = path.substring(0, path.length() - 1) + FILE_NAME;
pph = new PowerPointHelper();
pph.createPresentation(fileLocation);
}
@Test
public void whenReadingAPresentation_thenOK() throws Exception {
XMLSlideShow xmlSlideShow = pph.readingExistingSlideShow(fileLocation);
Assert.assertNotNull(xmlSlideShow);
Assert.assertEquals(4, xmlSlideShow.getSlides().size());
}
@Test
public void whenRetrievingThePlaceholdersForEachSlide_thenOK() throws Exception {
XMLSlideShow xmlSlideShow = pph.readingExistingSlideShow(fileLocation);
List<XSLFShape> onlyTitleSlidePlaceholders = pph.retrieveTemplatePlaceholders(xmlSlideShow.getSlides().get(0));
List<XSLFShape> titleAndBodySlidePlaceholders = pph.retrieveTemplatePlaceholders(xmlSlideShow.getSlides().get(1));
List<XSLFShape> emptySlidePlaceholdes = pph.retrieveTemplatePlaceholders(xmlSlideShow.getSlides().get(3));
Assert.assertEquals(1, onlyTitleSlidePlaceholders.size());
Assert.assertEquals(2, titleAndBodySlidePlaceholders.size());
Assert.assertEquals(0, emptySlidePlaceholdes.size());
}
@Test
public void whenSortingSlides_thenOK() throws Exception {
XMLSlideShow xmlSlideShow = pph.readingExistingSlideShow(fileLocation);
XSLFSlide slide4 = xmlSlideShow.getSlides().get(3);
pph.reorderSlide(xmlSlideShow, 3, 1);
Assert.assertEquals(slide4, xmlSlideShow.getSlides().get(1));
}
@Test
public void givenPresentation_whenDeletingASlide_thenOK() throws Exception {
XMLSlideShow xmlSlideShow = pph.readingExistingSlideShow(fileLocation);
pph.deleteSlide(xmlSlideShow, 3);
Assert.assertEquals(3, xmlSlideShow.getSlides().size());
}
@After
public void tearDown() throws Exception {
File testFile = new File(fileLocation);
if (testFile.exists()) {
testFile.delete();
}
pph = null;
}
}

View File

@@ -0,0 +1,47 @@
package com.baeldung.poi.word;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.junit.BeforeClass;
import org.junit.Test;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.List;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
public class WordIntegrationTest {
static WordDocument wordDocument;
@BeforeClass
public static void generateMSWordFile() throws Exception {
WordIntegrationTest.wordDocument = new WordDocument();
wordDocument.handleSimpleDoc();
}
@Test
public void whenParsingOutputDocument_thenCorrect() throws Exception {
Path msWordPath = Paths.get(WordDocument.output);
XWPFDocument document = new XWPFDocument(Files.newInputStream(msWordPath));
List<XWPFParagraph> paragraphs = document.getParagraphs();
document.close();
XWPFParagraph title = paragraphs.get(0);
XWPFRun titleRun = title.getRuns().get(0);
assertEquals("Build Your REST API with Spring", title.getText());
assertEquals("009933", titleRun.getColor());
assertTrue(titleRun.isBold());
assertEquals("Courier", titleRun.getFontFamily());
assertEquals(20, titleRun.getFontSize());
assertEquals("from HTTP fundamentals to API Mastery", paragraphs.get(1).getText());
assertEquals("What makes a good API?", paragraphs.get(3).getText());
assertEquals(wordDocument.convertTextFileToString(WordDocument.paragraph1), paragraphs.get(4).getText());
assertEquals(wordDocument.convertTextFileToString(WordDocument.paragraph2), paragraphs.get(5).getText());
assertEquals(wordDocument.convertTextFileToString(WordDocument.paragraph3), paragraphs.get(6).getText());
}
}