KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > fr > improve > struts > taglib > layout > ResourceTag


1 /*
2  * Created on 19 oct. 2004
3  *
4  * TODO To change the template for this generated file go to
5  * Window - Preferences - Java - Code Style - Code Templates
6  */

7 package fr.improve.struts.taglib.layout;
8
9 import javax.servlet.jsp.JspException JavaDoc;
10
11 import fr.improve.struts.taglib.layout.skin.Skin;
12 import fr.improve.struts.taglib.layout.util.LayoutUtils;
13 import fr.improve.struts.taglib.layout.util.TagUtils;
14
15 /**
16  * Generate an absolute path to a struts-layout resource.
17  *
18  * @author JN Ribette
19  */

20 public class ResourceTag extends LayoutTagSupport {
21     protected static final int IMG = 1;
22     protected static final int CFG = 2;
23     protected static final int CSS = 3;
24     protected int type;
25     protected String JavaDoc name;
26     
27     public int doStartLayoutTag() throws JspException JavaDoc {
28         // Get skin
29
Skin lc_skin = LayoutUtils.getSkin(pageContext.getSession());
30         StringBuffer JavaDoc lc_buffer = new StringBuffer JavaDoc();
31         
32         switch (type) {
33             case IMG:
34                 // Get image directory.
35
String JavaDoc lc_imgSrc = lc_skin.getImageDirectory(pageContext.getRequest());
36                 // Generate HTML code.
37
lc_buffer.append(lc_imgSrc);
38                 if (!lc_imgSrc.endsWith("/") && !name.startsWith("/")) {
39                     lc_buffer.append('/');
40                 }
41                 lc_buffer.append(name);
42                 break;
43             case CFG:
44                 // Get config directory.
45
String JavaDoc lc_cfgDir = lc_skin.getConfigDirectory(pageContext.getRequest());
46                 // Generate HTML code.
47
lc_buffer.append(lc_cfgDir);
48                 if (!lc_cfgDir.endsWith("/") && !name.startsWith("/")) {
49                     lc_buffer.append('/');
50                 }
51                 lc_buffer.append(name);
52                 break;
53             case CSS:
54                 // Get css directory.
55
String JavaDoc lc_cssDir = lc_skin.getCssDirectory(pageContext.getRequest());
56                 // Generate HTML code.
57
lc_buffer.append(lc_cssDir);
58                 if (!lc_cssDir.endsWith("/") && !name.startsWith("/")) {
59                     lc_buffer.append('/');
60                 }
61                 lc_buffer.append(name);
62                 break;
63         }
64         
65         TagUtils.write(pageContext, lc_buffer.toString());
66         return SKIP_BODY;
67     }
68     public void release() {
69         super.release();
70         type = IMG;
71         name = null;
72     }
73     
74     public void setType(String JavaDoc in_type) throws JspException JavaDoc {
75         if ("img".equals(in_type)) {
76             type = IMG;
77         } else if ("cfg".equals(in_type)) {
78             type = CFG;
79         } else if ("css".equals(in_type)) {
80             type = CSS;
81         } else {
82             throw new JspException JavaDoc("Type " + in_type + " is not supported.");
83         }
84     }
85     
86     public void setName(String JavaDoc in_name) {
87         name = in_name;
88     }
89 }
90
Popular Tags