KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > bsf > expression


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 package org.apache.taglibs.bsf ;
18
19 import javax.servlet.jsp.* ;
20 import javax.servlet.jsp.tagext.* ;
21 import java.io.IOException JavaDoc ;
22
23 import org.apache.bsf.* ;
24
25 public class expression implements BodyTag
26 {
27     protected Tag parent ;
28     protected BodyContent bodyOut ;
29     protected PageContext pageContext ;
30     protected String JavaDoc language ;
31
32     public void setLanguage(String JavaDoc value) { language = value ; };
33     public void setParent(Tag parent) { this.parent = parent ; };
34     public Tag getParent() { return( this.parent ); };
35     public void doInitBody() throws JspException { };
36     public int doAfterBody() throws JspException { return 0; };
37     public void release() { };
38     public void setBodyContent(BodyContent bodyOut) { this.bodyOut = bodyOut;};
39     public int doStartTag() throws JspException { return(EVAL_BODY_TAG);};
40
41     public void setPageContext(PageContext pageContext) {
42         this.pageContext = pageContext ;
43     };
44
45     private void register( BSFManager mgr, String JavaDoc name, Object JavaDoc bean )
46                      throws Exception JavaDoc {
47     if ( bean == null ) return ;
48         try {
49         mgr.declareBean( name, bean, bean.getClass() );
50     }
51     catch( Exception JavaDoc e ) {
52         throw e ;
53     }
54     };
55
56     public int doEndTag() throws JspException {
57         try {
58             JspWriter out = pageContext.getOut();
59         if ( bodyOut == null ) return( EVAL_PAGE );
60
61             BSFManager mgr = new BSFManager ();
62             BSFEngine BSFengine = mgr.loadScriptingEngine (language);
63
64             try {
65             register( mgr, "request" , pageContext.getRequest() );
66         register( mgr, "response" , pageContext.getResponse() );
67         register( mgr, "pageContext", pageContext );
68         register( mgr, "application", pageContext.getServletContext() );
69         register( mgr, "out" , out );
70         register( mgr, "config" , pageContext.getServletConfig() );
71         register( mgr, "page" , pageContext.getPage() );
72         register( mgr, "exception" , pageContext.getException() );
73         register( mgr, "session" , pageContext.getSession() );
74         } catch( Exception JavaDoc e ) {
75             out.println( e.toString() );
76             e.printStackTrace();
77         }
78
79         Object JavaDoc result ;
80         result = BSFengine.eval ("<unknown>", 0, 0, bodyOut.getString() );
81         if ( result != null )
82             out.println( result.toString() );
83     } catch( Exception JavaDoc e ) {
84         System.out.println( e.toString() );
85         e.printStackTrace();
86         // throw new JspException( e.toString() );
87
}
88         return( EVAL_PAGE );
89     };
90 }
91
Popular Tags