package controller; import java.io.*; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.*; import javax.xml.bind.JAXBElement; import org.docx4j.model.fields.FieldUpdater; import org.docx4j.openpackaging.exceptions.Docx4JException; import org.docx4j.openpackaging.packages.WordprocessingMLPackage; import org.docx4j.wml.ContentAccessor; import org.docx4j.wml.Text; public class Docx4j { private String sourcePath; private static final String destPath = "data/LatestDoc.docx"; private String name; private String address; private String city; private String cap; private String telephone; private String email; public Docx4j(String name, String address, String city, String cap, String telephone, String email) { this.name = name; this.address = address; this.city = city; this.cap = cap; this.telephone = telephone; this.email = email; this.sourcePath = "data/DocumentoCustodiaTemplate.docx"; } private HashMap generateHashMap(){ HashMap hMap = new HashMap(18); hMap.put("${FirstNameLastName}", this.name); hMap.put("${Address}", this.address); hMap.put("${City}", this.city); hMap.put("${CAP}", this.cap); hMap.put("${Telephone}", this.telephone); hMap.put("${Mail}", this.email); return hMap; } private void searchAndReplace(List texts, Map values){ // -- scan all expressions // Will later contain all the expressions used though not used at the moment List els = new ArrayList(); StringBuilder sb = new StringBuilder(); int PASS = 0; int PREPARE = 1; int READ = 2; int mode = PASS; // to nullify List toNullify = new ArrayList(); int[] currentNullifyProps = new int[4]; // Do scan of els and immediately insert value for(int i = 0; i0) for(int i = 0; i=floor && head<=ceil)){ nvalSB.append(c); } if(j>currentNullifyProps[3] && i>=currentNullifyProps[2]){ toNullify.remove(0); if(toNullify.size()==0) { currentNullifyProps = null; continue; } currentNullifyProps = toNullify.get(0); } } textElement.setValue(nvalSB.toString()); } } private static List getAllElementFromObject(Object obj, Class toSearch) { List result = new ArrayList(); if (obj instanceof JAXBElement) obj = ((JAXBElement) obj).getValue(); if (obj.getClass().equals(toSearch)) result.add(obj); else if (obj instanceof ContentAccessor) { List children = ((ContentAccessor) obj).getContent(); for (Object child : children) { result.addAll(getAllElementFromObject(child, toSearch)); } } return result; } private void writeDocxToStream(WordprocessingMLPackage template, String target) throws IOException, Docx4JException { File f = new File(target); template.save(f); } private WordprocessingMLPackage getTemplate(String name) throws Docx4JException, FileNotFoundException { WordprocessingMLPackage template = WordprocessingMLPackage.load(new FileInputStream(new File(name))); return template; } public String fillDoc() throws Docx4JException, IOException { WordprocessingMLPackage template = this.getTemplate(sourcePath); List texts = getAllElementFromObject(template.getMainDocumentPart(), Text.class); HashMap hashMap = this.generateHashMap(); searchAndReplace(texts, hashMap); this.writeDocxToStream(template, destPath); return destPath; } }