1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195
|
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.poi.xwpf.usermodel.IBodyElement;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.apache.poi.xwpf.usermodel.XWPFParagraph;
import org.apache.poi.xwpf.usermodel.XWPFRun;
import org.apache.poi.xwpf.usermodel.XWPFTable;
import org.apache.poi.xwpf.usermodel.XWPFTableCell;
import org.apache.poi.xwpf.usermodel.XWPFTableRow;
public class WordPatcher {
//private final Pattern PATTERN = Pattern.compile("\\$\\{([^:]*?)\\}");
private final Pattern PATTERN = Pattern.compile("\\$\\{(.*?)\\}");
private DataProvider dataProvider;
public WordPatcher(DataProvider dataProvider) {
this.dataProvider=dataProvider;
}
public void patch(InputStream inputStream, OutputStream outputStream) throws IOException {
@SuppressWarnings("resource")
XWPFDocument document = new XWPFDocument(inputStream);
for(IBodyElement element : (Iterable<IBodyElement>)()->document.getBodyElementsIterator()) {
patch(element);
}
document.write(outputStream);
}
private void patch(IBodyElement element) {
switch(element.getElementType()) {
case PARAGRAPH:
patch((XWPFParagraph)element);
break;
case TABLE:
patch((XWPFTable)element);
break;
case CONTENTCONTROL:
default:
}
}
private void patch(XWPFTable element) {
for(XWPFTableRow row : element.getRows()) {
patch(row);
}
}
private void patch(XWPFTableRow row) {
for(XWPFTableCell cell : row.getTableCells()) {
patch(cell);
}
}
private void patch(XWPFTableCell cell) {
for(IBodyElement element : cell.getBodyElements()) {
patch(element);
}
}
private void patch(XWPFParagraph element) {
List<XWPFRun> runs = element.getRuns();
patchRuns(element, runs);
/*for(XWPFRun run : element.getRuns()) {
patch(run);
}*/
}
private void patchRuns(XWPFParagraph parent, List<XWPFRun> runs) {
for(int i=0; i<runs.size(); ) {
i = patch(runs, i);
}
}
private int patch(List<XWPFRun> runs, int i) {
XWPFRun run = runs.get(i);
patch(run); // effectue le remplacement dans le run, si le motif est inclus dans le run
// cherche si on a pas le motif qui couvre plusieurs runs, et si oui, on transfert vers un run unique, puis on effectue le remplacement sur ce run
String text = run.getText(0);
if ( text!=null ) {
if ( text.contains("${") ) {
int endRun = findEndRun(runs, i+1);
if ( endRun!=-1 ) {
i = compactAndPatch(runs, i, endRun);
}
}
else if ( text.contains("$") ) {
String nextRunText = nextRunText(runs, i);
if ( nextRunText!=null ) {
if ( text.startsWith("{") ) {
int endRun = findEndRun(runs, i+1);
if ( endRun!=-1 ) {
i = compactAndPatch(runs, i, endRun);
}
}
}
}
}
return i+1;
}
private int compactAndPatch(List<XWPFRun> runs, int startRunIndex, int endRunIndex) {
XWPFRun startRun = runs.get(startRunIndex);
XWPFRun endRun = runs.get(endRunIndex);
int startPos = startRun.getText(0).indexOf("$");
int endPos = endRun.getText(0).indexOf("}");
StringBuilder builder = new StringBuilder(startRun.getText(0).substring(startPos));
for(int i = startRunIndex+1; i<endRunIndex; i++) {
builder.append(runs.get(i).getText(0));
runs.get(i).setText("",0);
}
builder.append(endRun.getText(0).substring(0, endPos+1));
patch(startRun.getText(0).substring(0,startPos)+builder, startRun);
if ( endPos==endRun.getText(0).length()-1 ) {
endRun.setText("",0);
}
else {
endRun.setText(endRun.getText(0).substring(endPos+1),0);
}
return startRunIndex;
}
private int findEndRun(List<XWPFRun> runs, int start) {
for(int i=start; i<runs.size(); i++) {
if ( runs.get(i).getText(0).contains("}") ) {
return i;
}
}
return -1;
}
private String nextRunText(List<XWPFRun> runs, int index) {
if (index<runs.size()) {
return runs.get(index).getText(0);
}
return null;
}
private void patch(XWPFRun run) {
String text = run.getText(0);
patch(text, run);
}
private void patch(String text, XWPFRun run) {
if ( text!=null ) {
StringBuffer sb = new StringBuffer();
// TODO : voir s'il n'y a pas un décalage d'offset à gérer
Matcher matcher = PATTERN.matcher(text);
while( matcher.find() ) {
Replacement replacement = getReplacement(matcher.start(), matcher.end(), matcher.group(1));
replacement.replace(run, matcher, sb);
}
matcher.appendTail(sb);
run.setText(sb.toString(),0);
}
}
private Replacement getReplacement(int start, int end, String description) {
if( description.startsWith("image:") ) {
Image image = Image.getImage(dataProvider, start, description.split(":"));
return new ImageReplacement(image);
}
else if ( dataProvider.isImage(description) ) {
Image image = Image.getImage(dataProvider, start, description);
return new ImageReplacement(image);
}
Replacement replacement = new TextReplacement(dataProvider.getValue(description));
if ( dataProvider.isDynamic() ) {
if ( dataProvider.isImage(description) ) {
Image image = Image.getImage(dataProvider, start, description);
return new ImageReplacement(image);
}
}
return replacement;
}
} |
Partager