001: /*
002: * Copyright (c) 1998-2008 Caucho Technology -- all rights reserved
003: *
004: * This file is part of Resin(R) Open Source
005: *
006: * Each copy or derived work must preserve the copyright notice and this
007: * notice unmodified.
008: *
009: * Resin Open Source is free software; you can redistribute it and/or modify
010: * it under the terms of the GNU General Public License as published by
011: * the Free Software Foundation; either version 2 of the License, or
012: * (at your option) any later version.
013: *
014: * Resin Open Source is distributed in the hope that it will be useful,
015: * but WITHOUT ANY WARRANTY; without even the implied warranty of
016: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
017: * of NON-INFRINGEMENT. See the GNU General Public License for more
018: * details.
019: *
020: * You should have received a copy of the GNU General Public License
021: * along with Resin Open Source; if not, write to the
022: * Free SoftwareFoundation, Inc.
023: * 59 Temple Place, Suite 330
024: * Boston, MA 02111-1307 USA
025: *
026: * @author Scott Ferguson
027: */
028:
029: package com.caucho.xpath.functions;
030:
031: import com.caucho.util.L10N;
032: import com.caucho.xml.XmlPrinter;
033: import com.caucho.xpath.Expr;
034: import com.caucho.xpath.ExprEnvironment;
035: import com.caucho.xpath.XPathException;
036: import com.caucho.xpath.XPathParseException;
037: import com.caucho.xpath.expr.AbstractStringExpr;
038: import com.caucho.xpath.pattern.NodeIterator;
039:
040: import org.w3c.dom.Node;
041:
042: import java.io.IOException;
043:
044: /**
045: * Traces an object.
046: */
047: public class Trace extends AbstractStringExpr {
048: private static final L10N L = new L10N(ResolveURI.class);
049:
050: private Expr _expr;
051: private Expr _labelExpr;
052:
053: public Trace(Expr expr, Expr labelExpr) throws XPathParseException {
054: _expr = expr;
055: _labelExpr = labelExpr;
056:
057: if (expr == null)
058: throw new XPathParseException(L
059: .l("fn:trace(value,[label])"));
060: }
061:
062: /**
063: * Evaluates the expression as an string.
064: *
065: * @param node the current node
066: * @param env the variable environment.
067: *
068: * @return the string representation of the expression.
069: */
070: public String evalString(Node node, ExprEnvironment env)
071: throws XPathException {
072: Object value = _expr.evalObject(node, env);
073:
074: if (value instanceof NodeIterator) {
075: NodeIterator iter = (NodeIterator) value;
076:
077: while (iter.hasNext()) {
078: Node subnode = iter.next();
079:
080: XmlPrinter printer = new XmlPrinter(System.out);
081:
082: try {
083: printer.printPrettyXml(subnode);
084: } catch (IOException e) {
085: }
086: }
087: } else if (value instanceof Node) {
088: Node subnode = (Node) value;
089:
090: XmlPrinter printer = new XmlPrinter(System.out);
091:
092: try {
093: printer.printPrettyXml(subnode);
094: } catch (IOException e) {
095: }
096: } else
097: System.out.println(value);
098:
099: return "";
100: }
101:
102: public String toString() {
103: return "fn:trace(" + _expr + ")";
104: }
105: }
|