01: /*
02: * @(#)transformXSL.java 1.2 04/12/06
03: *
04: * Copyright (c) 2004 Sun Microsystems, Inc. All Rights Reserved.
05: *
06: * See the file "LICENSE.txt" for information on usage and redistribution
07: * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
08: */
09: package org.pnuts.xml;
10:
11: import pnuts.lang.PnutsFunction;
12: import pnuts.lang.Context;
13: import pnuts.lang.PnutsException;
14: import javax.xml.transform.*;
15: import javax.xml.transform.dom.*;
16: import javax.xml.transform.stream.*;
17: import org.w3c.dom.Node;
18:
19: public class transformXSL extends PnutsFunction {
20: public transformXSL() {
21: super ("transformXSL");
22: }
23:
24: public boolean defined(int nargs) {
25: return nargs == 2 || nargs == 3;
26: }
27:
28: protected Object exec(Object[] args, Context context) {
29: int nargs = args.length;
30: Result result;
31: Node node = null;
32: try {
33: if (nargs == 2) {
34: node = Util.getDocumentBuilder(null, context)
35: .newDocument();
36: result = new DOMResult(node);
37: } else if (nargs == 3) {
38: result = Util.getResult(args[2], context);
39: } else {
40: undefined(args, context);
41: return null;
42: }
43: Util.getTransformerXSL(args[1], context).transform(
44: Util.streamSource(args[0], context), result);
45: return node;
46: } catch (Exception e) {
47: throw new PnutsException(e, context);
48: }
49: }
50:
51: public String toString() {
52: return "function transformXSL(xml, xsl {, output })";
53: }
54: }
|