1 52 53 package freemarker.ext.xml; 54 55 import java.io.StringWriter ; 56 import java.util.Iterator ; 57 import java.util.List ; 58 59 import freemarker.template.TemplateModelException; 60 61 import org.dom4j.Attribute; 62 import org.dom4j.Branch; 63 import org.dom4j.Document; 64 import org.dom4j.DocumentType; 65 import org.dom4j.Element; 66 import org.dom4j.Node; 67 import org.dom4j.ProcessingInstruction; 68 import org.dom4j.tree.DefaultAttribute; 69 import org.jaxen.Context; 70 import org.jaxen.NamespaceContext; 71 import org.jaxen.dom4j.Dom4jXPath; 72 73 77 class Dom4jNavigator extends Navigator { 78 79 Dom4jNavigator() { 80 } 81 82 void getAsString(Object node, StringWriter sw) { 83 sw.getBuffer().append(((Node)node).asXML()); 84 } 85 86 void getChildren(Object node, String localName, String namespaceUri, List result) { 87 if(node instanceof Element) { 88 Element e = (Element)node; 89 if(localName == null) { 90 result.addAll(e.elements()); 91 } 92 else { 93 result.addAll(e.elements(e.getQName().getDocumentFactory().createQName(localName, "", namespaceUri))); 94 } 95 } 96 else if(node instanceof Document) { 97 Element root = ((Document)node).getRootElement(); 98 if(localName == null || (equal(root.getName(), localName) && equal(root.getNamespaceURI(), namespaceUri))) { 99 result.add(root); 100 } 101 } 102 } 103 104 void getAttributes(Object node, String localName, String namespaceUri, List result) { 105 if(node instanceof Element) { 106 Element e = (Element)node; 107 if(localName == null) { 108 result.addAll(e.attributes()); 109 } 110 else { 111 Attribute attr = e.attribute(e.getQName().getDocumentFactory().createQName(localName, "", namespaceUri)); 112 if(attr != null) { 113 result.add(attr); 114 } 115 } 116 } 117 else if (node instanceof ProcessingInstruction) { 118 ProcessingInstruction pi = (ProcessingInstruction)node; 119 if ("target".equals(localName)) { 120 result.add(new DefaultAttribute("target", pi.getTarget())); 121 } 122 else if ("data".equals(localName)) { 123 result.add(new DefaultAttribute("data", pi.getText())); 124 } 125 else { 126 result.add(new DefaultAttribute(localName, pi.getValue(localName))); 127 } 128 } else if (node instanceof DocumentType) { 129 DocumentType doctype = (DocumentType)node; 130 if ("publicId".equals(localName)) { 131 result.add(new DefaultAttribute("publicId", doctype.getPublicID())); 132 } 133 else if ("systemId".equals(localName)) { 134 result.add(new DefaultAttribute("systemId", doctype.getSystemID())); 135 } 136 else if ("elementName".equals(localName)) { 137 result.add(new DefaultAttribute("elementName", doctype.getElementName())); 138 } 139 } 140 } 141 142 void getDescendants(Object node, List result) { 143 if(node instanceof Branch) { 144 getDescendants((Branch)node, result); 145 } 146 } 147 148 private void getDescendants(Branch node, List result) { 149 List content = node.content(); 150 for (Iterator iter = content.iterator(); iter.hasNext();) { 151 Node subnode = (Node) iter.next(); 152 if(subnode instanceof Element) { 153 result.add(subnode); 154 getDescendants(subnode, result); 155 } 156 } 157 } 158 159 Object getParent(Object node) { 160 return ((Node)node).getParent(); 161 } 162 163 Object getDocument(Object node) { 164 return ((Node)node).getDocument(); 165 } 166 167 Object getDocumentType(Object node) { 168 return 169 node instanceof Document 170 ? ((Document)node).getDocType() 171 : null; 172 } 173 174 void getContent(Object node, List result) { 175 if(node instanceof Branch) { 176 result.addAll(((Branch)node).content()); 177 } 178 } 179 180 String getText(Object node) { 181 return ((Node)node).getText(); 182 } 183 184 String getLocalName(Object node) { 185 return ((Node)node).getName(); 186 } 187 188 String getNamespacePrefix(Object node) { 189 if(node instanceof Element) { 190 return ((Element)node).getNamespacePrefix(); 191 } 192 if(node instanceof Attribute) { 193 return ((Attribute)node).getNamespacePrefix(); 194 } 195 return null; 196 } 197 198 String getNamespaceUri(Object node) { 199 if(node instanceof Element) { 200 return ((Element)node).getNamespaceURI(); 201 } 202 if(node instanceof Attribute) { 203 return ((Attribute)node).getNamespaceURI(); 204 } 205 return null; 206 } 207 208 String getType(Object node) { 209 switch(((Node)node).getNodeType()) { 210 case Node.ATTRIBUTE_NODE: { 211 return "attribute"; 212 } 213 case Node.CDATA_SECTION_NODE: { 214 return "cdata"; 215 } 216 case Node.COMMENT_NODE: { 217 return "comment"; 218 } 219 case Node.DOCUMENT_NODE: { 220 return "document"; 221 } 222 case Node.DOCUMENT_TYPE_NODE: { 223 return "documentType"; 224 } 225 case Node.ELEMENT_NODE: { 226 return "element"; 227 } 228 case Node.ENTITY_REFERENCE_NODE: { 229 return "entityReference"; 230 } 231 case Node.NAMESPACE_NODE: { 232 return "namespace"; 233 } 234 case Node.PROCESSING_INSTRUCTION_NODE: { 235 return "processingInstruction"; 236 } 237 case Node.TEXT_NODE: { 238 return "text"; 239 } 240 } 241 return "unknown"; 242 } 243 244 XPathEx createXPathEx(String xpathString) throws TemplateModelException 245 { 246 try { 247 return new Dom4jXPathEx(xpathString); 248 } 249 catch(Exception e) { 250 throw new TemplateModelException(e); 251 } 252 } 253 254 private static final class Dom4jXPathEx 255 extends 256 Dom4jXPath 257 implements 258 XPathEx 259 { 260 Dom4jXPathEx(String path) 261 throws 262 Exception 263 { 264 super(path); 265 } 266 267 public List selectNodes(Object object, NamespaceContext namespaces) 268 throws 269 TemplateModelException 270 { 271 Context context = getContext(object); 272 context.getContextSupport().setNamespaceContext(namespaces); 273 try { 274 return selectNodesForContext(context); 275 } 276 catch(Exception e) { 277 throw new TemplateModelException(e); 278 } 279 } 280 } 281 } 282 | Popular Tags |