1 52 53 package freemarker.ext.xml; 54 55 import java.io.IOException ; 56 import java.io.StringWriter ; 57 import java.util.Iterator ; 58 import java.util.List ; 59 60 import freemarker.template.TemplateModelException; 61 62 import org.jaxen.Context; 63 import org.jaxen.NamespaceContext; 64 import org.jaxen.jdom.JDOMXPath; 65 import org.jdom.Element; 66 import org.jdom.Attribute; 67 import org.jdom.CDATA; 68 import org.jdom.Comment; 69 import org.jdom.DocType; 70 import org.jdom.Document; 71 import org.jdom.EntityRef; 72 import org.jdom.Namespace; 73 import org.jdom.ProcessingInstruction; 74 import org.jdom.Text; 75 import org.jdom.output.XMLOutputter; 76 77 81 class JdomNavigator extends Navigator { 82 private static final XMLOutputter OUTPUT = new XMLOutputter(); 83 84 JdomNavigator() { 85 } 86 87 void getAsString(Object node, StringWriter sw) 88 throws 89 TemplateModelException 90 { 91 try { 92 if (node instanceof Element) { 93 OUTPUT.output((Element)node, sw); 94 } 95 else if (node instanceof Attribute) { 96 Attribute attribute = (Attribute)node; 97 sw.write(" "); 98 sw.write(attribute.getQualifiedName()); 99 sw.write("=\""); 100 sw.write(OUTPUT.escapeAttributeEntities(attribute.getValue())); 101 sw.write("\""); 102 } 103 else if (node instanceof Text) { 104 OUTPUT.output((Text)node, sw); 105 } 106 else if (node instanceof Document) { 107 OUTPUT.output((Document)node, sw); 108 } 109 else if (node instanceof ProcessingInstruction) { 110 OUTPUT.output((ProcessingInstruction)node, sw); 111 } 112 else if (node instanceof Comment) { 113 OUTPUT.output((Comment)node, sw); 114 } 115 else if (node instanceof CDATA) { 116 OUTPUT.output((CDATA)node, sw); 117 } 118 else if (node instanceof DocType) { 119 OUTPUT.output((DocType)node, sw); 120 } 121 else if (node instanceof EntityRef) { 122 OUTPUT.output((EntityRef)node, sw); 123 } 124 else { 125 throw new TemplateModelException(node.getClass().getName() + " is not a core JDOM class"); 126 } 127 } 128 catch(IOException e) { 129 throw new TemplateModelException(e); 130 } 131 } 132 133 void getChildren(Object node, String localName, String namespaceUri, List result) { 134 if(node instanceof Element) { 135 Element e = (Element)node; 136 if(localName == null) { 137 result.addAll(e.getChildren()); 138 } 139 else { 140 result.addAll(e.getChildren(localName, Namespace.getNamespace("", namespaceUri))); 141 } 142 } 143 else if(node instanceof Document) { 144 Element root = ((Document)node).getRootElement(); 145 if(localName == null || (equal(root.getName(), localName) && equal(root.getNamespaceURI(), namespaceUri))) { 146 result.add(root); 147 } 148 } 149 } 150 151 void getAttributes(Object node, String localName, String namespaceUri, List result) { 152 if(node instanceof Element) { 153 Element e = (Element)node; 154 if(localName == null) { 155 result.addAll(e.getAttributes()); 156 } 157 else { 158 Attribute attr = e.getAttribute(localName, Namespace.getNamespace("", namespaceUri)); 159 if(attr != null) { 160 result.add(attr); 161 } 162 } 163 } else if (node instanceof ProcessingInstruction) { 164 ProcessingInstruction pi = (ProcessingInstruction)node; 165 if ("target".equals(localName)) { 166 result.add(new Attribute("target", pi.getTarget())); 167 } 168 else if ("data".equals(localName)) { 169 result.add(new Attribute("data", pi.getData())); 170 } 171 else { 172 result.add(new Attribute(localName, pi.getValue(localName))); 173 } 174 } else if (node instanceof DocType) { 175 DocType doctype = (DocType)node; 176 if ("publicId".equals(localName)) { 177 result.add(new Attribute("publicId", doctype.getPublicID())); 178 } 179 else if ("systemId".equals(localName)) { 180 result.add(new Attribute("systemId", doctype.getSystemID())); 181 } 182 else if ("elementName".equals(localName)) { 183 result.add(new Attribute("elementName", doctype.getElementName())); 184 } 185 } 186 } 187 188 void getDescendants(Object node, List result) { 189 if(node instanceof Document) { 190 Element root = ((Document)node).getRootElement(); 191 result.add(root); 192 getDescendants(root, result); 193 } 194 else if(node instanceof Element) { 195 getDescendants((Element)node, result); 196 } 197 } 198 199 private void getDescendants(Element node, List result) { 200 for (Iterator iter = node.getChildren().iterator(); iter.hasNext();) { 201 Element subnode = (Element)iter.next(); 202 result.add(subnode); 203 getDescendants(subnode, result); 204 } 205 } 206 207 Object getParent(Object node) { 208 if (node instanceof Element) { 209 return((Element)node).getParent(); 210 } 211 if (node instanceof Attribute) { 212 return((Attribute)node).getParent(); 213 } 214 if (node instanceof Text) { 215 return((Text)node).getParent(); 216 } 217 if (node instanceof ProcessingInstruction) { 218 return((ProcessingInstruction)node).getParent(); 219 } 220 if (node instanceof Comment) { 221 return((Comment)node).getParent(); 222 } 223 if (node instanceof EntityRef) { 224 return((EntityRef)node).getParent(); 225 } 226 return null; 227 } 228 229 Object getDocument(Object node) { 230 if (node instanceof Element) { 231 return ((Element)node).getDocument(); 232 } 233 else if (node instanceof Attribute) { 234 Element parent = ((Attribute)node).getParent(); 235 return parent == null ? null : parent.getDocument(); 236 } 237 else if (node instanceof Text) { 238 Element parent = ((Text)node).getParent(); 239 return parent == null ? null : parent.getDocument(); 240 } 241 else if (node instanceof Document) 242 return (Document)node; 243 else if (node instanceof ProcessingInstruction) { 244 return ((ProcessingInstruction)node).getDocument(); 245 } 246 else if (node instanceof EntityRef) { 247 return ((EntityRef)node).getDocument(); 248 } 249 else if (node instanceof Comment) { 250 return ((Comment)node).getDocument(); 251 } 252 return null; 253 } 254 255 Object getDocumentType(Object node) { 256 return 257 node instanceof Document 258 ? ((Document)node).getDocType() 259 : null; 260 } 261 262 void getContent(Object node, List result) { 263 if (node instanceof Element) 264 result.addAll(((Element)node).getContent()); 265 else if (node instanceof Document) 266 result.addAll(((Document)node).getContent()); 267 } 268 269 String getText(Object node) { 270 if (node instanceof Element) { 271 return ((Element)node).getTextTrim(); 272 } 273 if (node instanceof Attribute) { 274 return ((Attribute)node).getValue(); 275 } 276 if (node instanceof CDATA) { 277 return ((CDATA)node).getText(); 278 } 279 if (node instanceof Comment) { 280 return ((Comment)node).getText(); 281 } 282 if (node instanceof ProcessingInstruction) { 283 return ((ProcessingInstruction)node).getData(); 284 } 285 return null; 286 } 287 288 String getLocalName(Object node) { 289 if (node instanceof Element) { 290 return ((Element)node).getName(); 291 } 292 if (node instanceof Attribute) { 293 return ((Attribute)node).getName(); 294 } 295 if (node instanceof EntityRef) { 296 return ((EntityRef)node).getName(); 297 } 298 if (node instanceof ProcessingInstruction) { 299 return ((ProcessingInstruction)node).getTarget(); 300 } 301 if (node instanceof DocType) { 302 return ((DocType)node).getElementName(); 303 } 304 return null; 305 } 306 307 String getNamespacePrefix(Object node) { 308 if(node instanceof Element) { 309 return ((Element)node).getNamespacePrefix(); 310 } 311 if(node instanceof Attribute) { 312 return ((Attribute)node).getNamespacePrefix(); 313 } 314 return null; 315 } 316 317 String getNamespaceUri(Object node) { 318 if(node instanceof Element) { 319 return ((Element)node).getNamespaceURI(); 320 } 321 if(node instanceof Attribute) { 322 return ((Attribute)node).getNamespaceURI(); 323 } 324 return null; 325 } 326 327 String getType(Object node) { 328 if(node instanceof Attribute) { 329 return "attribute"; 330 } 331 if(node instanceof CDATA) { 332 return "cdata"; 333 } 334 if(node instanceof Comment) { 335 return "comment"; 336 } 337 if(node instanceof Document) { 338 return "document"; 339 } 340 if(node instanceof DocType) { 341 return "documentType"; 342 } 343 if(node instanceof Element) { 344 return "element"; 345 } 346 if(node instanceof EntityRef) { 347 return "entityReference"; 348 } 349 if(node instanceof Namespace) { 350 return "namespace"; 351 } 352 if(node instanceof ProcessingInstruction) { 353 return "processingInstruction"; 354 } 355 if(node instanceof Text) { 356 return "text"; 357 } 358 return "unknown"; 359 } 360 361 XPathEx createXPathEx(String xpathString) throws TemplateModelException 362 { 363 try { 364 return new JDOMXPathEx(xpathString); 365 } 366 catch(Exception e) { 367 throw new TemplateModelException(e); 368 } 369 } 370 371 private static final class JDOMXPathEx 372 extends 373 JDOMXPath 374 implements 375 XPathEx 376 { 377 JDOMXPathEx(String path) 378 throws 379 Exception 380 { 381 super(path); 382 } 383 384 public List selectNodes(Object object, NamespaceContext namespaces) 385 throws 386 TemplateModelException 387 { 388 Context context = getContext(object); 389 context.getContextSupport().setNamespaceContext(namespaces); 390 try { 391 return selectNodesForContext(context); 392 } 393 catch(Exception e) { 394 throw new TemplateModelException(e); 395 } 396 } 397 } 398 } 399 | Popular Tags |