1 52 53 package freemarker.core; 54 55 import java.io.IOException ; 56 import freemarker.template.*; 57 58 59 62 final class VisitNode extends TemplateElement { 63 64 Expression targetNode, namespaces; 65 66 VisitNode(Expression targetNode, Expression namespaces) { 67 this.targetNode = targetNode; 68 this.namespaces = namespaces; 69 } 70 71 void accept(Environment env) throws IOException , TemplateException { 72 TemplateModel node = targetNode.getAsTemplateModel(env); 73 assertNonNull(node, targetNode, env); 74 if (!(node instanceof TemplateNodeModel)) { 75 throw new TemplateException("Expecting an XML node here", env); 76 } 77 TemplateModel nss = namespaces == null ? null : namespaces.getAsTemplateModel(env); 78 if (namespaces instanceof StringLiteral) { 79 nss = env.importLib(((TemplateScalarModel) nss).getAsString(), null); 80 } 81 else if (namespaces instanceof ListLiteral) { 82 nss = ((ListLiteral) namespaces).evaluateStringsToNamespaces(env); 83 } 84 if (nss != null) { 85 if (nss instanceof Environment.Namespace) { 86 SimpleSequence ss = new SimpleSequence(1); 87 ss.add(nss); 88 nss = ss; 89 } 90 else if (!(nss instanceof TemplateSequenceModel)) { 91 throw new TemplateException("Expecting a sequence of namespaces after 'using'", env); 92 } 93 } 94 env.visit((TemplateNodeModel) node, (TemplateSequenceModel) nss); 95 } 96 97 public String getCanonicalForm() { 98 if (namespaces == null) { 99 return "<#visit " + targetNode.getCanonicalForm() + "/>"; 100 } 101 return "<#visit " + targetNode.getCanonicalForm() + " using " + namespaces.getCanonicalForm() + "/>"; 102 } 103 104 public String getDescription() { 105 return "visit instruction"; 106 } 107 } 108 | Popular Tags |