KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > kohsuke > stapler > jelly > IncludeTag


1 package org.kohsuke.stapler.jelly;
2
3 import org.apache.commons.jelly.JellyContext;
4 import org.apache.commons.jelly.JellyTagException;
5 import org.apache.commons.jelly.MissingAttributeException;
6 import org.apache.commons.jelly.Script;
7 import org.apache.commons.jelly.TagSupport;
8 import org.apache.commons.jelly.XMLOutput;
9 import org.kohsuke.stapler.MetaClass;
10
11 /**
12  * Tag that includes views of the object.
13  *
14  * @author Kohsuke Kawaguchi
15  */

16 public class IncludeTag extends TagSupport {
17     private Object JavaDoc it;
18
19     private String JavaDoc page;
20
21     private Object JavaDoc from;
22
23     /**
24      * Specifies the name of the JSP to be included.
25      */

26     public void setPage(String JavaDoc page) {
27         this.page = page;
28     }
29
30     /**
31      * Specifies the object for which JSP will be included.
32      */

33     public void setIt(Object JavaDoc it) {
34         this.it = it;
35     }
36
37     /**
38      * When loading the script, use the classloader from this object
39      * to locate the script.
40      */

41     public void setFrom(Object JavaDoc from) {
42         this.from = from;
43     }
44
45     public void doTag(XMLOutput output) throws MissingAttributeException, JellyTagException {
46         Object JavaDoc it = this.it;
47         if(it==null)
48             it = getContext().getVariable("it");
49
50         MetaClass c = MetaClass.get((from!=null?from:it).getClass());
51         Script script;
52         try {
53             script = c.loadTearOff(JellyClassTearOff.class).findScript(page);
54         } catch (RuntimeException JavaDoc e) {
55             throw e;
56         } catch (Exception JavaDoc e) {
57             throw new JellyTagException("Error loading '"+page+"' for "+it.getClass(),e);
58         }
59
60         if(script==null) {
61             throw new JellyTagException("No page found '"+page+"' for "+it.getClass());
62         }
63
64         JellyContext context = getContext();
65         if(this.it!=null) {
66             context = new JellyContext(context);
67             context.setVariable("it",this.it);
68         }
69
70         ClassLoader JavaDoc old = Thread.currentThread().getContextClassLoader();
71         Thread.currentThread().setContextClassLoader(c.classLoader.loader);
72         try {
73             script.run(context,output);
74         } finally {
75             Thread.currentThread().setContextClassLoader(old);
76         }
77     }
78 }
79
Popular Tags