1 52 53 package freemarker.ext.dom; 54 55 import org.w3c.dom.*; 56 import freemarker.template.*; 57 import freemarker.template.utility.StringUtil; 58 import freemarker.core.Environment; 59 60 class ElementModel extends NodeModel implements TemplateScalarModel { 61 62 public ElementModel(Element element) { 63 super(element); 64 } 65 66 public boolean isEmpty() { 67 return false; 68 } 69 70 81 public TemplateModel get(String key) throws TemplateModelException { 82 if (key.equals("*")) { 83 NodeListModel ns = new NodeListModel(this); 84 TemplateSequenceModel children = getChildNodes(); 85 for (int i=0;i < children.size();i++) { 86 NodeModel child = (NodeModel) children.get(i); 87 if (child.node.getNodeType() == Node.ELEMENT_NODE) { 88 ns.add(child); 89 } 90 } 91 return ns; 92 } 93 if (key.equals("**")) { 94 Element elem = (Element) node; 95 return new NodeListModel(elem.getElementsByTagName("*"), this); 96 } 97 if (key.startsWith("@")) { 98 if (key.equals("@@") || key.equals("@*")) { 99 return new NodeListModel(node.getAttributes(), this); 100 } 101 if (key.equals("@@start_tag")) { 102 NodeOutputter nodeOutputter = new NodeOutputter(node); 103 return new SimpleScalar(nodeOutputter.getOpeningTag((Element) node)); 104 } 105 if (key.equals("@@end_tag")) { 106 NodeOutputter nodeOutputter = new NodeOutputter(node); 107 return new SimpleScalar(nodeOutputter.getClosingTag((Element) node)); 108 } 109 if (key.equals("@@attributes_markup")) { 110 StringBuffer buf = new StringBuffer (); 111 NodeOutputter nu = new NodeOutputter(node); 112 nu.outputContent(node.getAttributes(), buf); 113 return new SimpleScalar(buf.toString().trim()); 114 } 115 if (StringUtil.isXMLID(key.substring(1))) { 116 Attr att = getAttribute(key.substring(1), Environment.getCurrentEnvironment()); 117 if (att == null) { 118 return new NodeListModel(this); 119 } 120 return wrap(att); 121 } 122 } 123 if (StringUtil.isXMLID(key)) { 124 NodeListModel result = ((NodeListModel) getChildNodes()).filterByName(key); 125 if (result.size() == 1) { 126 return result.get(0); 127 } 128 return result; 129 } 130 return super.get(key); 131 } 132 133 public String getAsString() throws TemplateModelException { 134 NodeList nl = node.getChildNodes(); 135 String result = ""; 136 for (int i = 0; i<nl.getLength(); i++) { 137 Node child = nl.item(i); 138 int nodeType = child.getNodeType(); 139 if (nodeType == Node.ELEMENT_NODE) { 140 String msg = "Only elements with no child elements can be processed as text." 141 + "\nThis element with name \"" 142 + node.getNodeName() 143 + "\" has a child element named: " + child.getNodeName(); 144 throw new TemplateModelException(msg); 145 } 146 else if (nodeType == Node.TEXT_NODE || nodeType == Node.CDATA_SECTION_NODE) { 147 result += child.getNodeValue(); 148 } 149 } 150 return result; 151 } 152 153 public String getNodeName() { 154 String result = node.getLocalName(); 155 if (result == null || result.equals("")) { 156 result = node.getNodeName(); 157 } 158 return result; 159 } 160 161 String getQualifiedName() { 162 String nodeName = getNodeName(); 163 String nsURI = getNodeNamespace(); 164 if (nsURI == null || nsURI.length() == 0) { 165 return nodeName; 166 } 167 Environment env = Environment.getCurrentEnvironment(); 168 String defaultNS = env.getDefaultNS(); 169 String prefix; 170 if (defaultNS != null && defaultNS.equals(nsURI)) { 171 prefix = Template.DEFAULT_NAMESPACE_PREFIX; 172 } else { 173 prefix = env.getPrefixForNamespace(nsURI); 174 175 } 176 if (prefix == null) { 177 return null; } 179 if (prefix.length() >0) { 180 prefix += ":"; 181 } 182 return prefix + nodeName; 183 } 184 185 private Attr getAttribute(String qname, Environment env) { 186 Element element = (Element) node; 187 Attr result = element.getAttributeNode(qname); 188 if (result != null) 189 return result; 190 int colonIndex = qname.indexOf(':'); 191 if (colonIndex >0) { 192 String prefix = qname.substring(0, colonIndex); 193 String uri; 194 if (prefix.equals(Template.DEFAULT_NAMESPACE_PREFIX)) { 195 uri = Environment.getCurrentEnvironment().getDefaultNS(); 196 } else { 197 uri = Environment.getCurrentEnvironment().getNamespaceForPrefix(prefix); 198 } 199 String localName = qname.substring(1+colonIndex); 200 if (uri != null) { 201 result = element.getAttributeNodeNS(uri, localName); 202 } 203 } 204 return result; 205 } 206 207 boolean matchesName(String name, Environment env) { 208 return StringUtil.matchesName(name, getNodeName(), getNodeNamespace(), env); 209 } 210 } | Popular Tags |