01: /*
02: * @(#)PnutsJspTag.java 1.2 04/12/06
03: *
04: * Copyright (c) 1997-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 pnuts.servlet;
10:
11: import javax.servlet.jsp.*;
12: import javax.servlet.jsp.tagext.*;
13: import java.io.*;
14: import pnuts.lang.*;
15: import pnuts.lang.Package;
16: import pnuts.compiler.Compiler;
17:
18: /**
19: * JSP Tag Library for Pnuts
20: *
21: */
22: public class PnutsJspTag extends BodyTagSupport {
23:
24: private Pnuts parsed;
25: private Context context;
26: private static Compiler compiler = new Compiler(null, false, true);
27: public final static String CONTEXT_ATTRIBUTE_NAME = "pnuts.lang.Context";
28:
29: public int doEndTag() throws JspException {
30: try {
31: if (bodyContent == null) {
32: return EVAL_PAGE;
33: }
34: Object ctx_attr = pageContext
35: .getAttribute(CONTEXT_ATTRIBUTE_NAME);
36: if (ctx_attr == null) {
37: context = new Context(new Package());
38: pageContext.setAttribute(CONTEXT_ATTRIBUTE_NAME,
39: context);
40: } else {
41: context = (Context) ctx_attr;
42: }
43: if (parsed == null) {
44: try {
45: BodyContent bodyContent = getBodyContent();
46: parsed = Pnuts.parse(bodyContent.getReader());
47: parsed = compiler.compile(parsed, context);
48: } catch (ParseException pe) {
49: throw new JspException("Script Error: " + pe);
50: } catch (ClassFormatError cfe) {
51: }
52: }
53: context.setWriter(getPreviousOut());
54: parsed.run(context);
55: } catch (java.lang.Exception e) {
56: throw new JspException("IO Error: " + e.getMessage());
57: }
58: return EVAL_PAGE;
59: }
60: }
|