1 package net.sf.cantina.tags;
2
3 import org.apache.log4j.Logger;
4
5 import java.util.Locale;
6
7 import net.sf.cantina.util.Localizer;
8 import net.sf.cantina.util.BeanUtils;
9 import net.sf.cantina.Document;
10 import net.sf.cantina.DataSource;
11 import net.sf.cantina.exceptions.ObjectNotFoundException;
12
13 import javax.servlet.jsp.JspException;
14 import javax.servlet.jsp.PageContext;
15 import javax.servlet.jsp.tagext.TagSupport;
16
17 /***
18 * @author Stephane JAIS
19 */
20 public class DefineTag
21 extends TagSupport
22 {
23 private static final Logger logger = Logger.getLogger(DefineTag.class);
24 private String documentId;
25 private Locale locale;
26 private String id;
27 private String property;
28
29 public String getProperty()
30 {
31 return property;
32 }
33
34 public void setProperty(String property)
35 {
36 this.property = property;
37 }
38
39 public String getId()
40 {
41 return id;
42 }
43
44 public void setId(String id)
45 {
46 this.id = id;
47 }
48
49
50 public String getScope()
51 {
52 return scope;
53 }
54
55 public void setScope(String scope)
56 {
57 this.scope = scope;
58 }
59
60 private String scope;
61
62 public String getDocumentId()
63 {
64 return documentId;
65 }
66
67 public void setDocumentId(String documentId)
68 {
69 this.documentId = documentId;
70 }
71
72 public String getLocale()
73 {
74 return locale.toString();
75 }
76
77 public void setLocale(String localeString)
78 {
79 this.locale = Localizer.parseLocaleString(localeString);
80 }
81
82 public int doStartTag()
83 throws JspException
84 {
85 Locale locale;
86 if (this.locale == null)
87 locale = pageContext.getResponse().getLocale();
88 else
89 locale = this.locale;
90 try
91 {
92 logger.debug("Loading document ["+documentId+"] in locale ["+locale+"]");
93 Document doc = DataSource.getInstance().loadDocument(documentId);
94 int scopeConstant = scope == null ? PageContext.PAGE_SCOPE : TagUtils.getScope(scope);
95 if (property == null)
96 pageContext.setAttribute(id,doc.getContentAsString(locale),scopeConstant);
97 else
98 pageContext.setAttribute(id,BeanUtils.getProperty(doc,property), scopeConstant);
99 } catch (ObjectNotFoundException e )
100 {
101 logger.warn("Document id ["+documentId+"] was not found.");
102 }
103 catch (Exception e)
104 {
105 throw new JspException(e);
106 }
107 return SKIP_BODY;
108 }
109
110 public void release()
111 {
112 documentId = null;
113 locale = null;
114 id = null;
115 scope = null;
116 }
117
118 }