1 28 29 package com.caucho.xpath.functions; 30 31 import com.caucho.util.L10N; 32 import com.caucho.xml.XmlPrinter; 33 import com.caucho.xpath.Expr; 34 import com.caucho.xpath.ExprEnvironment; 35 import com.caucho.xpath.XPathException; 36 import com.caucho.xpath.XPathParseException; 37 import com.caucho.xpath.expr.AbstractStringExpr; 38 import com.caucho.xpath.pattern.NodeIterator; 39 40 import org.w3c.dom.Node ; 41 42 import java.io.IOException ; 43 44 47 public class Trace extends AbstractStringExpr { 48 private static final L10N L = new L10N(ResolveURI.class); 49 50 private Expr _expr; 51 private Expr _labelExpr; 52 53 public Trace(Expr expr, Expr labelExpr) 54 throws XPathParseException 55 { 56 _expr = expr; 57 _labelExpr = labelExpr; 58 59 if (expr == null) 60 throw new XPathParseException(L.l("fn:trace(value,[label])")); 61 } 62 63 71 public String evalString(Node node, ExprEnvironment env) 72 throws XPathException 73 { 74 Object value = _expr.evalObject(node, env); 75 76 if (value instanceof NodeIterator) { 77 NodeIterator iter = (NodeIterator) value; 78 79 while (iter.hasNext()) { 80 Node subnode = iter.next(); 81 82 XmlPrinter printer = new XmlPrinter(System.out); 83 84 try { 85 printer.printPrettyXml(subnode); 86 } catch (IOException e) { 87 } 88 } 89 } 90 else if (value instanceof Node ) { 91 Node subnode = (Node ) value; 92 93 XmlPrinter printer = new XmlPrinter(System.out); 94 95 try { 96 printer.printPrettyXml(subnode); 97 } catch (IOException e) { 98 } 99 } 100 else 101 System.out.println(value); 102 103 return ""; 104 } 105 106 public String toString() 107 { 108 return "fn:trace(" + _expr + ")"; 109 } 110 } 111 | Popular Tags |