Search in sources :

Example 1 with NodeListModel

use of freemarker.ext.xml.NodeListModel in project freemarker by apache.

the class FreemarkerXmlTask method process.

/**
 * Process an XML file using FreeMarker
 */
private void process(File baseDir, String xmlFile, File destDir) throws BuildException {
    File outFile = null;
    File inFile = null;
    try {
        // the current input file relative to the baseDir
        inFile = new File(baseDir, xmlFile);
        // the output file relative to basedir
        outFile = new File(destDir, xmlFile.substring(0, xmlFile.lastIndexOf('.')) + extension);
        // only process files that have changed
        if (!incremental || (inFile.lastModified() > outFile.lastModified() || templateFileLastModified > outFile.lastModified() || projectFileLastModified > outFile.lastModified())) {
            ensureDirectoryFor(outFile);
            // -- command line status
            log("Input:  " + xmlFile, Project.MSG_INFO);
            if (projectTemplate == null && projectFile != null) {
                Document doc = builder.parse(projectFile);
                projectTemplate = new NodeListModel(builder.parse(projectFile));
                projectNode = NodeModel.wrap(doc);
            }
            // Build the file DOM
            Document docNode = builder.parse(inFile);
            TemplateModel document = new NodeListModel(docNode);
            TemplateNodeModel docNodeModel = NodeModel.wrap(docNode);
            HashMap root = new HashMap();
            root.put("document", document);
            insertDefaults(root);
            // Process the template and write out
            // the result as the outFile.
            Writer writer = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(outFile), encoding));
            try {
                if (parsedTemplate == null) {
                    throw new BuildException("No template file specified in build script or in XML file");
                }
                if (prepareModel != null) {
                    Map vars = new HashMap();
                    vars.put("model", root);
                    vars.put("doc", docNode);
                    if (projectNode != null) {
                        vars.put("project", ((NodeModel) projectNode).getNode());
                    }
                    prepareModel.execute(vars);
                }
                freemarker.core.Environment env = parsedTemplate.createProcessingEnvironment(root, writer);
                env.setCurrentVisitorNode(docNodeModel);
                if (prepareEnvironment != null) {
                    Map vars = new HashMap();
                    vars.put("env", env);
                    vars.put("doc", docNode);
                    if (projectNode != null) {
                        vars.put("project", ((NodeModel) projectNode).getNode());
                    }
                    prepareEnvironment.execute(vars);
                }
                env.process();
                writer.flush();
            } finally {
                writer.close();
            }
            log("Output: " + outFile, Project.MSG_INFO);
        }
    } catch (SAXParseException spe) {
        Throwable rootCause = spe;
        if (spe.getException() != null)
            rootCause = spe.getException();
        log("XML parsing error in " + inFile.getAbsolutePath(), Project.MSG_ERR);
        log("Line number " + spe.getLineNumber());
        log("Column number " + spe.getColumnNumber());
        throw new BuildException(rootCause, getLocation());
    } catch (Throwable e) {
        if (outFile != null) {
            if (!outFile.delete() && outFile.exists()) {
                log("Failed to delete " + outFile, Project.MSG_WARN);
            }
        }
        e.printStackTrace();
        throw new BuildException(e, getLocation());
    }
}
Also used : HashMap(java.util.HashMap) TemplateNodeModel(freemarker.template.TemplateNodeModel) TemplateModel(freemarker.template.TemplateModel) Document(org.w3c.dom.Document) BufferedWriter(java.io.BufferedWriter) SAXParseException(org.xml.sax.SAXParseException) NodeListModel(freemarker.ext.xml.NodeListModel) FileOutputStream(java.io.FileOutputStream) OutputStreamWriter(java.io.OutputStreamWriter) BuildException(org.apache.tools.ant.BuildException) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) OutputStreamWriter(java.io.OutputStreamWriter) BufferedWriter(java.io.BufferedWriter) Writer(java.io.Writer)

Aggregations

NodeListModel (freemarker.ext.xml.NodeListModel)1 TemplateModel (freemarker.template.TemplateModel)1 TemplateNodeModel (freemarker.template.TemplateNodeModel)1 BufferedWriter (java.io.BufferedWriter)1 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 OutputStreamWriter (java.io.OutputStreamWriter)1 Writer (java.io.Writer)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 BuildException (org.apache.tools.ant.BuildException)1 Document (org.w3c.dom.Document)1 SAXParseException (org.xml.sax.SAXParseException)1