Created
October 3, 2017 11:11
-
-
Save aerobium/bf02e443c079c5caec7568e167849dda to your computer and use it in GitHub Desktop.
Replacing a text in Apache POI XWPF
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
iterateThroughParagraphs(doc, fieldsForReport); | |
iterateThroughFooters(doc, fieldsForReport); | |
iterateThroughTables(doc, fieldsForReport); | |
private void iterateThroughParagraphs(XWPFDocument doc, Map<String, String> fieldsForReport) throws NotFoundException { | |
for (XWPFParagraph paragraph : doc.getParagraphs()) { | |
iterateThroughRuns(paragraph, fieldsForReport); | |
} | |
} | |
private void iterateThroughTables(XWPFDocument doc, Map<String, String> fieldsForReport) throws NotFoundException { | |
for (XWPFTable tbl : doc.getTables()) { | |
for (XWPFTableRow row : tbl.getRows()) { | |
for (XWPFTableCell cell : row.getTableCells()) { | |
for (XWPFParagraph paragraph : cell.getParagraphs()) { | |
iterateThroughRuns(paragraph, fieldsForReport); | |
} | |
} | |
} | |
} | |
} | |
private void iterateThroughFooters(XWPFDocument doc, Map<String, String> fieldsForReport) throws NotFoundException { | |
for (XWPFFooter footer : doc.getFooterList()) { | |
for (XWPFParagraph paragraph : footer.getParagraphs()) { | |
iterateThroughRuns(paragraph, fieldsForReport); | |
} | |
} | |
} | |
private void iterateThroughRuns(XWPFParagraph paragraph, Map<String, String> fieldsForReport) throws NotFoundException { | |
List<XWPFRun> runs = paragraph.getRuns(); | |
if (runs != null) { | |
int runsSize = runs.size(); | |
for (int index = 0; index < runsSize; index++) { | |
XWPFRun currentRun = runs.get(index); | |
String text = currentRun.getText(0); | |
if (text != null && text.contains("#")) { | |
if (text.matches("(?i).*#[a-zA-Z0-9]+#.*")) { | |
Matcher m = Pattern.compile("#[a-zA-Z0-9]+#") | |
.matcher(text); | |
while (m.find()) { | |
String variableWithHash = m.group(); | |
String variableWithoutHash = variableWithHash.replace("#", ""); | |
if (fieldsForReport.get(variableWithoutHash) == null) { | |
continue; | |
} | |
String newText = currentRun.getText(0).replace(variableWithHash, fieldsForReport.get(variableWithoutHash)); | |
currentRun.setText(newText, 0); | |
} | |
continue; | |
} | |
if (("#".equals(text) || " #".equals(text)) && index + 1 < runsSize) { | |
replaceVariableBetweenDifferentRuns(index, runs, fieldsForReport); | |
index += 2; | |
} | |
} | |
} | |
} | |
} | |
private void replaceVariableBetweenDifferentRuns(int index, List<XWPFRun> runs, Map<String, String> fieldsForReport) throws NotFoundException { | |
XWPFRun currentRun = runs.get(index); | |
String text = currentRun.getText(0); | |
XWPFRun middleRun = runs.get(index + 1); | |
String middleText = middleRun.getText(0); | |
String newVariableValue = fieldsForReport.get(middleText); | |
if (newVariableValue == null) { | |
throw new NotFoundException("Variable: " + middleText + " is not presented in the data from DB during report prepared."); | |
} | |
XWPFRun lastRun = runs.get(index + 2); | |
String lastText = lastRun.getText(0); | |
if (middleText.matches("[a-zA-Z0-9]+") && "#".equals(lastText)) { | |
currentRun.setText(text.replace("#", ""), 0); | |
middleRun.setText(newVariableValue, 0); | |
lastRun.setText("", 0); | |
} else { | |
throw new NotFoundException("Problems were occurred during variable replacement"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment