WordTemplateBuilder implements TemplateBuilder<E> { byte[] createBasicTemplate() { try { Docx4JWordTemplate basicTemplate = new Docx4JWordTemplate(); Map<String, Class<?>> dynamicElements = modelService .listModelElements(inputType, modelProperties); for (String key : dynamicElements.keySet()) { PlainTextContent plainTextContent = basicTemplate .addPlainTextContent(); plainTextContent.setExpression(key); plainTextContent.setTitle(key); plainTextContent.setPlaceHolderContent(key); } ByteArrayOutputStream out = new ByteArrayOutputStream(); basicTemplate.writeAsOpenXML(out); return out.toByteArray(); } catch (Exception e) { throw new WteException("Can not create BasicTemplate: " + e.getMessage(), e); } } WordTemplateBuilder(TemplateContextFactory contextFactory,
			WteModelService service, Class<E> inputType); @Override TemplateBuilder<E> setProperties(Map<String, String> modelProperties); @Override TemplateBuilder<E> setDocumentName(String documentName); @Override TemplateBuilder<E> setAuthor(User user); @Override TemplateBuilder<E> setLanguage(String language); @Override TemplateBuilder<E> setMappingData(Map<String, MappingDetail> mappingData); @Override TemplateBuilder<E> setTemplateFile(Path aTemplateFile); Template<E> build();  }
@Test public void createBasicTemplate() throws Exception { Map<String, Class<?>> elements = new HashMap<String, Class<?>>(); elements.put("string", String.class); WteModelService service = mock(WteModelService.class); Map<String, String> properties = anyMapOf(String.class, String.class); Class<?> inputType = any(Class.class); when(service.listModelElements(inputType, properties)).thenReturn( elements); TemplateContextFactory contextFactory = mock(TemplateContextFactory.class); WordTemplateBuilder<String> builder = new WordTemplateBuilder<String>( contextFactory, service, String.class); byte[] templateContent = builder.createBasicTemplate(); File templateFile = File.createTempFile("generated", "docx"); try { FileUtils.writeByteArrayToFile(templateFile, templateContent); ZipFile zipFile = new ZipFile(templateFile); ZipEntry entry = zipFile.getEntry("word/document.xml"); InputStream documentIn = zipFile.getInputStream(entry); String doucmentXml = IOUtils.toString(documentIn); doucmentXml = doucmentXml.replaceAll("\\n\\s+", ""); String expected = "<w:sdt>" + "<w:sdtPr>" + "<w:tag w:val=\"string\"/>" + "<w:text/>" + "<w:showingPlcHdr/>" + "<w15:appearance w15:val=\"tags\"/>" + "<w:alias w:val=\"string\"/>" + "</w:sdtPr>" + "<w:sdtContent>" + "<w:p><w:r>" + "<w:rPr><w:rStyle w:val=\"PlaceholderText\"/></w:rPr>" + "<w:t>string</w:t>" + "</w:r></w:p>" + "</w:sdtContent>" + "</w:sdt>"; assertTrue(doucmentXml.contains(expected)); } finally { templateFile.delete(); } }