use of freemarker.template.TemplateNodeModel in project freemarker by apache.
the class Environment method recurse.
void recurse(TemplateNodeModel node, TemplateSequenceModel namespaces) throws TemplateException, IOException {
if (node == null) {
node = this.getCurrentVisitorNode();
if (node == null) {
throw new _TemplateModelException("The target node of recursion is missing or null.");
}
}
TemplateSequenceModel children = node.getChildNodes();
if (children == null) {
return;
}
int size = children.size();
for (int i = 0; i < size; i++) {
TemplateNodeModel child = (TemplateNodeModel) children.get(i);
if (child != null) {
invokeNodeHandlerFor(child, namespaces);
}
}
}
use of freemarker.template.TemplateNodeModel in project freemarker by apache.
the class Environment method invokeNodeHandlerFor.
/**
* Used for {@code #visit} and {@code #recurse}.
*/
void invokeNodeHandlerFor(TemplateNodeModel node, TemplateSequenceModel namespaces) throws TemplateException, IOException {
if (nodeNamespaces == null) {
SimpleSequence ss = new SimpleSequence(1);
ss.add(currentNamespace);
nodeNamespaces = ss;
}
int prevNodeNamespaceIndex = this.nodeNamespaceIndex;
String prevNodeName = this.currentNodeName;
String prevNodeNS = this.currentNodeNS;
TemplateSequenceModel prevNodeNamespaces = nodeNamespaces;
TemplateNodeModel prevVisitorNode = currentVisitorNode;
currentVisitorNode = node;
if (namespaces != null) {
this.nodeNamespaces = namespaces;
}
try {
TemplateModel macroOrTransform = getNodeProcessor(node);
if (macroOrTransform instanceof Macro) {
invoke((Macro) macroOrTransform, null, null, null, null);
} else if (macroOrTransform instanceof TemplateTransformModel) {
visitAndTransform(null, (TemplateTransformModel) macroOrTransform, null);
} else {
String nodeType = node.getNodeType();
if (nodeType != null) {
// If the node's type is 'text', we just output it.
if ((nodeType.equals("text") && node instanceof TemplateScalarModel)) {
out.write(((TemplateScalarModel) node).getAsString());
} else if (nodeType.equals("document")) {
recurse(node, namespaces);
} else // we just ignore it.
if (!nodeType.equals("pi") && !nodeType.equals("comment") && !nodeType.equals("document_type")) {
throw new _MiscTemplateException(this, noNodeHandlerDefinedDescription(node, node.getNodeNamespace(), nodeType));
}
} else {
throw new _MiscTemplateException(this, noNodeHandlerDefinedDescription(node, node.getNodeNamespace(), "default"));
}
}
} finally {
this.currentVisitorNode = prevVisitorNode;
this.nodeNamespaceIndex = prevNodeNamespaceIndex;
this.currentNodeName = prevNodeName;
this.currentNodeNS = prevNodeNS;
this.nodeNamespaces = prevNodeNamespaces;
}
}
use of freemarker.template.TemplateNodeModel in project freemarker by apache.
the class VisitNode method accept.
@Override
TemplateElement[] accept(Environment env) throws IOException, TemplateException {
TemplateModel node = targetNode.eval(env);
if (!(node instanceof TemplateNodeModel)) {
throw new NonNodeException(targetNode, node, env);
}
TemplateModel nss = namespaces == null ? null : namespaces.eval(env);
if (namespaces instanceof StringLiteral) {
nss = env.importLib(((TemplateScalarModel) nss).getAsString(), null);
} else if (namespaces instanceof ListLiteral) {
nss = ((ListLiteral) namespaces).evaluateStringsToNamespaces(env);
}
if (nss != null) {
if (nss instanceof Environment.Namespace) {
SimpleSequence ss = new SimpleSequence(1);
ss.add(nss);
nss = ss;
} else if (!(nss instanceof TemplateSequenceModel)) {
if (namespaces != null) {
throw new NonSequenceException(namespaces, nss, env);
} else {
// Should not occur
throw new _MiscTemplateException(env, "Expecting a sequence of namespaces after \"using\"");
}
}
}
env.invokeNodeHandlerFor((TemplateNodeModel) node, (TemplateSequenceModel) nss);
return null;
}
use of freemarker.template.TemplateNodeModel in project freemarker by apache.
the class RecurseNode method accept.
@Override
TemplateElement[] accept(Environment env) throws IOException, TemplateException {
TemplateModel node = targetNode == null ? null : targetNode.eval(env);
if (node != null && !(node instanceof TemplateNodeModel)) {
throw new NonNodeException(targetNode, node, "node", env);
}
TemplateModel nss = namespaces == null ? null : namespaces.eval(env);
if (namespaces instanceof StringLiteral) {
nss = env.importLib(((TemplateScalarModel) nss).getAsString(), null);
} else if (namespaces instanceof ListLiteral) {
nss = ((ListLiteral) namespaces).evaluateStringsToNamespaces(env);
}
if (nss != null) {
if (nss instanceof TemplateHashModel) {
SimpleSequence ss = new SimpleSequence(1);
ss.add(nss);
nss = ss;
} else if (!(nss instanceof TemplateSequenceModel)) {
if (namespaces != null) {
throw new NonSequenceException(namespaces, nss, env);
} else {
// Should not occur
throw new _MiscTemplateException(env, "Expecting a sequence of namespaces after \"using\"");
}
}
}
env.recurse((TemplateNodeModel) node, (TemplateSequenceModel) nss);
return null;
}
use of freemarker.template.TemplateNodeModel 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());
}
}
Aggregations