Velocity+docx 4 jテンプレートの置換

3692 ワード

http://kodak-zhou.iteye.com/blog/970682http://velocity.apache.org/engine/devel/developer-ガイド.htmlhttp://www.360doc.com/content/11/1203/22/834950_169480722.shtmlhttp://www.java2s.com/Code/Java/Velocity/VelocityMathToolAdd.htm
import java.io.File;
import java.io.StringWriter;
import java.util.Map;
import java.util.Properties;

import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;
import org.docx4j.XmlUtils;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.MainDocumentPart;
import org.docx4j.wml.Document;

public final class DocumentTemplateUtil {
/**
* Method to do the template merge directly in Ms Word and return the document/xml contents
* The format of replaced content is "$colour";
*
* @param sourceFile  the source file of .docx
* @param templateParams
*        the contents need to be merged  like:{( "color", "Red" )}
* @return A StringWriter contents word/document.xml's contents
*/

@SuppressWarnings("rawtypes")
public static StringWriter getMsWordDocumentXMLString(File sourceFile, Map templateParams) throws Exception {

String result = "";
if (sourceFile != null) {
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(sourceFile);
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
org.docx4j.wml.Document wmlDocumentEl = (org.docx4j.wml.Document)documentPart.getJaxbElement();
String xmlContent = org.docx4j.XmlUtils.marshaltoString(wmlDocumentEl, true);
result = xmlContent;
}

// Initialise enigine injecting basic properties
VelocityEngine ve = new VelocityEngine();
Properties properties = new Properties();
properties.setProperty(Velocity.ENCODING_DEFAULT, "UTF-8");
properties.setProperty(Velocity.INPUT_ENCODING, "UTF-8");
properties.setProperty(Velocity.OUTPUT_ENCODING, "UTF-8");
ve.init(properties);

templateParams.put("DateTool", new ComparisonDateTool());
templateParams.put("MathTool", new MathTool());
templateParams.put("NumberTool", new NumberTool());
templateParams.put("EscapeTool", new EscapeTool());
templateParams.put("DisplayTool", new DisplayTool());
templateParams.put("ConversionTool", new ConversionTool());
templateParams.put("LoopTool", new LoopTool()); 

// Template mergering
VelocityContext context = new VelocityContext(templateParams);
StringWriter writer = new StringWriter();
Velocity.init();
Velocity.evaluate( context, writer, "log tag name", result);

return writer;
}

/**
* Method to do the template merge directly in Ms Word and export out to specifc location
* The format of replaced content is "$colour";
*
* @param sourceFile the source file of .docx
* @param templateParams
*        the contents need to be merged  like:{( "colour", "Red" )}
* @param outputPath the location which the file is to be exported to.
*/

@SuppressWarnings("rawtypes")
public static void exportMergedXMLtoDocx(File sourceFile ,String outputPath, Map templateParams) throws Exception {

if (sourceFile != null) {
WordprocessingMLPackage wordMLPackage = WordprocessingMLPackage.load(sourceFile);
MainDocumentPart documentPart = wordMLPackage.getMainDocumentPart();
StringWriter writer = getMsWordDocumentXMLString(sourceFile, templateParams);

Object obj = XmlUtils.unmarshalString(writer.toString());
documentPart.setJaxbElement((Document) obj);
wordMLPackage.addTargetPart(documentPart);
wordMLPackage.save(new java.io.File(outputPath));
writer.flush();
writer.close();
}
}
}