001: /*
002: * @(#)traverseDocument.java 1.4 05/01/22
003: *
004: * Copyright (c) 2004,2005 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * See the file "LICENSE.txt" for information on usage and redistribution
007: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
008: */
009: package org.pnuts.xml;
010:
011: import javax.xml.transform.dom.DOMSource;
012: import javax.xml.transform.sax.SAXResult;
013: import javax.xml.transform.Result;
014: import javax.xml.transform.Transformer;
015: import javax.xml.transform.TransformerException;
016: import org.xml.sax.ContentHandler;
017: import org.w3c.dom.Node;
018: import org.w3c.dom.NodeList;
019: import org.w3c.dom.Element;
020: import pnuts.lang.PnutsFunction;
021: import pnuts.lang.PnutsException;
022: import pnuts.lang.Context;
023: import java.util.*;
024: import org.w3c.dom.Document;
025:
026: public class traverseDocument extends PnutsFunction {
027:
028: public traverseDocument() {
029: super ("traverseDocument");
030: }
031:
032: public boolean defined(int nargs) {
033: return nargs == 2 || nargs == 3;
034: }
035:
036: static Result getTransformResult(Object r, Context context) {
037: if (r instanceof Result) {
038: return (Result) r;
039: } else if (r instanceof Map) {
040: return new SAXResult(Util.contentHandler(r, context));
041: } else if (r instanceof ContentHandler) {
042: return new SAXResult((ContentHandler) r);
043: } else {
044: throw new IllegalArgumentException(String.valueOf(r));
045: }
046: }
047:
048: protected Object exec(Object[] args, Context context) {
049: int nargs = args.length;
050: if (nargs != 2 && nargs != 3) {
051: undefined(args, context);
052: return null;
053: }
054: Object doc = args[0];
055: Node node = (Node) doc;
056: if (!(doc instanceof Node)) {
057: throw new IllegalArgumentException(String.valueOf(doc));
058: } else if (doc instanceof Document) {
059: node = ((Document) doc).getDocumentElement();
060: }
061: Object a1 = args[1];
062: Object a2 = null;
063: if (nargs == 3) {
064: a2 = args[2];
065: }
066: if (a1 instanceof PnutsFunction) {
067: if (doc instanceof Document) {
068: doc = ((Document) doc).getDocumentElement();
069: } else if (!(doc instanceof Element)) {
070: throw new IllegalArgumentException(String.valueOf(doc));
071: }
072: PnutsFunction f1 = (PnutsFunction) a1;
073: f1.call(new Object[] { node }, context);
074: NodeList nodes = node.getChildNodes();
075: int len = nodes.getLength();
076: if (nargs == 3) {
077: for (int i = 0; i < len; i++) {
078: Node n = nodes.item(i);
079: if (n instanceof Element) {
080: exec(new Object[] { n, a1, a2 }, context);
081: }
082: }
083: PnutsFunction f2 = (PnutsFunction) a2;
084: f2.call(new Object[] { node }, context);
085: } else {
086: for (int i = 0; i < len; i++) {
087: Node n = nodes.item(i);
088: if (n instanceof Element) {
089: exec(new Object[] { n, a1 }, context);
090: }
091: }
092: }
093: } else {
094: Transformer trans = Util.getTransformer(null, context);
095: try {
096: trans.transform(new DOMSource(node),
097: getTransformResult(a1, context));
098: } catch (TransformerException e) {
099: throw new PnutsException(e, context);
100: }
101: }
102: return null;
103: }
104:
105: public String toString() {
106: return "function traverseDocument(doc, enterFunc {, exitFunc })";
107: }
108: }
|