Skip to content

Commit

Permalink
Pass Locale down the PlaceholderData#transform Method
Browse files Browse the repository at this point in the history
  • Loading branch information
alexpartsch committed Mar 17, 2022
1 parent e78a0c1 commit 8874ca0
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 9 deletions.
4 changes: 3 additions & 1 deletion src/main/java/com/docutools/jocument/PlaceholderData.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.docutools.jocument;

import java.util.Locale;
import java.util.stream.Stream;

/**
Expand Down Expand Up @@ -49,9 +50,10 @@ default long count() {
* <p>Only used when {@link this#getType()} returns {@link com.docutools.jocument.PlaceholderType#CUSTOM}.</p>
*
* @param placeholder the placeholder
* @param locale the {@link Locale}
* @param options the {@link GenerationOptions}
*/
default void transform(Object placeholder, GenerationOptions options) {
default void transform(Object placeholder, Locale locale, GenerationOptions options) {
throw new UnsupportedOperationException();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import com.docutools.jocument.GenerationOptions;
import com.docutools.jocument.PlaceholderData;
import com.docutools.jocument.PlaceholderType;
import java.util.Locale;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.poi.xwpf.usermodel.IBodyElement;
Expand All @@ -17,18 +18,17 @@ public PlaceholderType getType() {
}

@Override
public void transform(Object placeholder, GenerationOptions options) {
if (!(placeholder instanceof IBodyElement)) {
public void transform(Object placeholder, Locale locale, GenerationOptions options) {
if (!(placeholder instanceof IBodyElement element)) {
logger.error("{} is not an instance of IBodyElement", placeholder);
throw new IllegalArgumentException("Only IBodyElements accepted.");
}

var element = (IBodyElement) placeholder;
var document = element.getBody().getXWPFDocument();

transform(element, document, options);
transform(element, document, locale, options);
}

protected abstract void transform(IBodyElement placeholder, XWPFDocument document, GenerationOptions options);
protected abstract void transform(IBodyElement placeholder, XWPFDocument document, Locale locale, GenerationOptions options);

}
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,11 @@ private void generate() {

private void transform(IBodyElement element, List<IBodyElement> remaining) {
logger.debug("Trying to transform element {}", element);
Locale locale = WordUtilities.detectMostCommonLocale(element.getBody().getXWPFDocument())
.orElse(LocaleUtil.getUserLocale());
if (isCustomPlaceholder(element)) {
resolver.resolve(WordUtilities.extractPlaceholderName((XWPFParagraph) element))
.ifPresent(placeholderData -> placeholderData.transform(element, options));
.ifPresent(placeholderData -> placeholderData.transform(element, locale, options));
} else if (isLoopStart(element)) {
unrollLoop((XWPFParagraph) element, remaining);
} else if (element instanceof XWPFParagraph xwpfParagraph) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Locale;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.apache.poi.xwpf.usermodel.IBodyElement;
Expand All @@ -30,7 +31,7 @@ public ImagePlaceholderData withMaxWidth(int maxWidth) {
}

@Override
protected void transform(IBodyElement placeholder, XWPFDocument document, GenerationOptions options) {
protected void transform(IBodyElement placeholder, XWPFDocument document, Locale locale, GenerationOptions options) {
Path path = applyOptions(options);
try {
var paragraph = document.insertNewParagraph(WordUtilities.openCursor(placeholder).orElseThrow());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
import com.docutools.jocument.GenerationOptions;
import com.docutools.jocument.impl.word.CustomWordPlaceholderData;
import com.docutools.jocument.impl.word.WordUtilities;
import java.util.Locale;
import org.apache.poi.xwpf.usermodel.IBodyElement;
import org.apache.poi.xwpf.usermodel.XWPFDocument;

public class QuotePlaceholder extends CustomWordPlaceholderData {
@Override
protected void transform(IBodyElement placeholder, XWPFDocument document, GenerationOptions options) {
protected void transform(IBodyElement placeholder, XWPFDocument document, Locale locale, GenerationOptions options) {
var paragraph = document.insertNewParagraph(WordUtilities.openCursor(placeholder).orElseThrow());
paragraph.createRun().setText("Live your life not celebrating victories, but overcoming defeats.");
WordUtilities.removeIfExists(placeholder);
Expand Down

0 comments on commit 8874ca0

Please sign in to comment.