1 52 53 package freemarker.ext.dom; 54 55 import org.w3c.dom.*; 56 import freemarker.template.*; 57 import freemarker.core.Environment; 58 import freemarker.template.utility.StringUtil; 59 60 64 65 class DocumentModel extends NodeModel implements TemplateHashModel { 66 67 private ElementModel rootElement; 68 69 DocumentModel(Document doc) { 70 super(doc); 71 } 72 73 public String getNodeName() { 74 return "@document"; 75 } 76 77 public TemplateModel get(String key) throws TemplateModelException { 78 if (key.equals("*")) { 79 return getRootElement(); 80 } 81 else if (key.equals("**")) { 82 NodeList nl = ((Document)node).getElementsByTagName("*"); 83 return new NodeListModel(nl, this); 84 } 85 else if (StringUtil.isXMLID(key)) { 86 ElementModel em = (ElementModel) NodeModel.wrap(((Document) node).getDocumentElement()); 87 if (em.matchesName(key, Environment.getCurrentEnvironment())) { 88 return em; 89 } else { 90 return new NodeListModel(this); 91 } 92 } 93 return super.get(key); 94 } 95 96 ElementModel getRootElement() { 97 if (rootElement == null) { 98 rootElement = (ElementModel) wrap(((Document) node).getDocumentElement()); 99 } 100 return rootElement; 101 } 102 103 public boolean isEmpty() { 104 return false; 105 } 106 } | Popular Tags |