/** Overrides superclass for better performance. */ @Override public boolean resourceExists(String resourceName) { ITemplateCacheInfoProvider templateCacheInfoProvider = templateEngine.getTemplateCacheInfoProvider(); if (templateCacheInfoProvider == null) { return false; } IEntryInfo cacheInfo = TemplateUtils.getTemplateCacheInfo( templateEngine.getTemplateCacheInfoProvider(), resourceName); return (cacheInfo != null); }
/** * @see * org.apache.velocity.runtime.resource.loader.ResourceLoader#getLastModified(org.apache.velocity.runtime.resource.Resource) */ public long getLastModified(Resource resource) { String resourceName = resource.getName(); IEntryInfo cacheInfo = TemplateUtils.getTemplateCacheInfo( templateEngine.getTemplateCacheInfoProvider(), resourceName); if (cacheInfo != null) { return cacheInfo.getLastModified(); } return 0; }
@Override public InputStream getResourceStream(String source) throws ResourceNotFoundException { IEntryInfo cacheInfo = TemplateUtils.getTemplateCacheInfo(templateEngine.getTemplateCacheInfoProvider(), source); if (cacheInfo != null) { InputStream inputStream = cacheInfo.getInputStream(); if (inputStream != null) { return inputStream; } } throw new ResourceNotFoundException( "Cannot find input stream for the entry with source=" + source); // // // InputStream inputStream = getEntryInputStream(source); // if (inputStream == null) { // throw new ResourceNotFoundException( // "Cannot find input stream for the entry with source=" // + source); // } // return inputStream; }
@Test public void testPreprocessFontsAndMergeTempplate() throws Exception { String xml = " <w:document" + " xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\">" + "<w:body>" + "<w:p>" + "<w:pPr>" + "<w:spacing w:after=\"0\" w:line=\"360\" w:lineRule=\"auto\"/>" + "<w:jc w:val=\"both\"/>" + "<w:rPr>" + "<w:rFonts w:ascii=\"Arial\" w:hAnsi=\"Arial\" w:cs=\"Arial\"/>" + "<w:sz w:val=\"24\"/>" + "</w:rPr>" + "</w:pPr>" + "</w:p>" + "</w:body>" + "</w:document>"; Document document = DOMUtils.load(xml); // 1) Test Fonts preprocessing with Freemarker IDocumentFormatter formatter = new FreemarkerDocumentFormatter(); StringWriter writer = new StringWriter(); DOMFontsPreprocessor.INSTANCE.preprocess( "word/document.xml", document, writer, null, formatter, null); String s = writer.toString(); int index = s.indexOf("<w:document"); if (index != -1) { s = s.substring(index, s.length()); } Assert.assertEquals( "<w:document" + " xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\">" + "[#if ___fontSize??][#assign ___fontSizeTwo=___fontSize * 2][/#if]" + "<w:body>" + "<w:p>" + "<w:pPr>" + "<w:spacing w:after=\"0\" w:line=\"360\" w:lineRule=\"auto\"/>" + "<w:jc w:val=\"both\"/>" + "<w:rPr>" // + "<w:rFonts w:ascii=\"Arial\" w:hAnsi=\"Arial\" w:cs=\"Arial\"/>" + "<w:rFonts w:ascii=\"[#if ___fontName??]${___fontName}[#else]Arial[/#if]\"" + " w:cs=\"[#if ___fontName??]${___fontName}[#else]Arial[/#if]\"" + " w:hAnsi=\"[#if ___fontName??]${___fontName}[#else]Arial[/#if]\"" + "/>" // + "<w:sz w:val=\"24\"/>" + "<w:sz w:val=\"[#if ___fontSize??]${___fontSize}[#else]24[/#if]\"/>" + "</w:rPr>" + "</w:pPr>" + "</w:p>" + "</w:body>" + "</w:document>", s); // 2) Test merge template with Java model ITemplateEngine templateEngine = new FreemarkerTemplateEngine(); IContext context = new XDocFreemarkerContext(); context.put("name", "word"); // Change every font name+size with Magneto + 40 context.put(DOMFontsPreprocessor.FONT_NAME_KEY, "Magneto"); context.put(DOMFontsPreprocessor.FONT_SIZE_KEY, 40); Reader reader = new StringReader(s); StringWriter mergedWriter = new StringWriter(); templateEngine.process("word/document.xml", context, reader, mergedWriter); Assert.assertEquals( "<w:document" + " xmlns:w=\"http://schemas.openxmlformats.org/wordprocessingml/2006/main\">" + "<w:body>" + "<w:p>" + "<w:pPr>" + "<w:spacing w:after=\"0\" w:line=\"360\" w:lineRule=\"auto\"/>" + "<w:jc w:val=\"both\"/>" + "<w:rPr>" // + "<w:rFonts w:ascii=\"Arial\" w:hAnsi=\"Arial\" w:cs=\"Arial\"/>" + "<w:rFonts w:ascii=\"Magneto\"" + " w:cs=\"Magneto\"" + " w:hAnsi=\"Magneto\"" + "/>" // + "<w:sz w:val=\"24\"/>" + "<w:sz w:val=\"40\"/>" + "</w:rPr>" + "</w:pPr>" + "</w:p>" + "</w:body>" + "</w:document>", mergedWriter.toString()); }