Refactor Docx examples (#2716)

This commit is contained in:
Grzegorz Piwowarek 2017-10-08 14:07:30 +02:00 committed by GitHub
parent 26508f3104
commit 491649a866
3 changed files with 33 additions and 35 deletions

View File

@ -1,12 +1,5 @@
package com.baeldung.docx;
import java.io.File;
import java.nio.file.Files;
import java.util.List;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import org.docx4j.dml.wordprocessingDrawing.Inline;
import org.docx4j.jaxb.Context;
import org.docx4j.model.table.TblFactory;
@ -26,13 +19,20 @@ import org.docx4j.wml.Tc;
import org.docx4j.wml.Text;
import org.docx4j.wml.Tr;
public class Docx4jExample {
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import java.io.File;
import java.nio.file.Files;
import java.util.List;
public void createDocumentPackage(String outputPath, String imagePath) throws Exception {
class Docx4jExample {
void createDocumentPackage(String outputPath, String imagePath) throws Exception {
WordprocessingMLPackage wordPackage = WordprocessingMLPackage.createPackage();
MainDocumentPart mainDocumentPart = wordPackage.getMainDocumentPart();
mainDocumentPart.addStyledParagraphOfText("Title", "Hello World!");
mainDocumentPart.addParagraphOfText("Welcome To Baeldung!");
ObjectFactory factory = Context.getWmlObjectFactory();
P p = factory.createP();
R r = factory.createR();
@ -53,13 +53,16 @@ public class Docx4jExample {
File image = new File(imagePath);
byte[] fileContent = Files.readAllBytes(image.toPath());
BinaryPartAbstractImage imagePart = BinaryPartAbstractImage.createImagePart(wordPackage, fileContent);
Inline inline = imagePart.createImageInline("Baeldung Image", "Alt Text", 1, 2, false);
BinaryPartAbstractImage imagePart = BinaryPartAbstractImage
.createImagePart(wordPackage, fileContent);
Inline inline = imagePart.createImageInline(
"Baeldung Image", "Alt Text", 1, 2, false);
P Imageparagraph = addImageToParagraph(inline);
mainDocumentPart.getContent().add(Imageparagraph);
int writableWidthTwips = wordPackage.getDocumentModel().getSections().get(0).getPageDimensions()
.getWritableWidthTwips();
int writableWidthTwips = wordPackage.getDocumentModel()
.getSections().get(0).getPageDimensions()
.getWritableWidthTwips();
int columnNumber = 3;
Tbl tbl = TblFactory.createTable(3, 3, writableWidthTwips / columnNumber);
List<Object> rows = tbl.getContent();
@ -77,7 +80,7 @@ public class Docx4jExample {
wordPackage.save(exportFile);
}
public boolean isTextExist(String testText) throws Docx4JException, JAXBException {
boolean isTextExist(String testText) throws Docx4JException, JAXBException {
File doc = new File("helloWorld.docx");
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(doc);
MainDocumentPart mainDocumentPart = wordMLPackage.getMainDocumentPart();

View File

@ -52,7 +52,7 @@ public class MyJiraClient {
myJiraClient.restClient.close();
}
public String createIssue(String projectKey, Long issueType, String issueSummary) {
private String createIssue(String projectKey, Long issueType, String issueSummary) {
IssueRestClient issueClient = restClient.getIssueClient();
@ -65,30 +65,30 @@ public class MyJiraClient {
return restClient.getIssueClient().getIssue(issueKey).claim();
}
public void voteForAnIssue(Issue issue) {
private void voteForAnIssue(Issue issue) {
restClient.getIssueClient().vote(issue.getVotesUri()).claim();
}
public int getTotalVotesCount(String issueKey) {
private int getTotalVotesCount(String issueKey) {
BasicVotes votes = getIssue(issueKey).getVotes();
return votes == null ? 0 : votes.getVotes();
}
public void addComment(Issue issue, String commentBody) {
private void addComment(Issue issue, String commentBody) {
restClient.getIssueClient().addComment(issue.getCommentsUri(), Comment.valueOf(commentBody));
}
public List<Comment> getAllComments(String issueKey) {
private List<Comment> getAllComments(String issueKey) {
return StreamSupport.stream(getIssue(issueKey).getComments().spliterator(), false)
.collect(Collectors.toList());
}
public void updateIssueDescription(String issueKey, String newDescription) {
private void updateIssueDescription(String issueKey, String newDescription) {
IssueInput input = new IssueInputBuilder().setDescription(newDescription).build();
restClient.getIssueClient().updateIssue(issueKey, input).claim();
}
public void deleteIssue(String issueKey, boolean deleteSubtasks) {
private void deleteIssue(String issueKey, boolean deleteSubtasks) {
restClient.getIssueClient().deleteIssue(issueKey, deleteSubtasks).claim();
}

View File

@ -1,24 +1,19 @@
package com.baeldung.docx;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
public class Docx4jReadAndWriteTest {
String imagePath = "src/main/resources/image.jpg";
String outputPath = "helloWorld.docx";
private static final String imagePath = "src/main/resources/image.jpg";
private static final String outputPath = "helloWorld.docx";
@Test
public void givenWordPackage_whenTextExist_thenReturnTrue() {
public void givenWordPackage_whenTextExist_thenReturnTrue() throws Exception {
Docx4jExample docx4j = new Docx4jExample();
try {
docx4j.createDocumentPackage(outputPath, imagePath);
assertTrue(docx4j.isTextExist("Hello World!"));
assertTrue(!docx4j.isTextExist("InexistantText"));
} catch (Exception e) {
fail();
}
docx4j.createDocumentPackage(outputPath, imagePath);
assertTrue(docx4j.isTextExist("Hello World!"));
assertTrue(!docx4j.isTextExist("InexistantText"));
}
}