KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > taglibs > Out


1 /**
2  *
3  * Magnolia and its source-code is licensed under the LGPL.
4  * You may copy, adapt, and redistribute this file for commercial or non-commercial use.
5  * When copying, adapting, or redistributing this document in keeping with the guidelines above,
6  * you are required to provide proper attribution to obinary.
7  * If you reproduce or distribute the document without making any substantive modifications to its content,
8  * please use the following attribution line:
9  *
10  * Copyright 1993-2006 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.taglibs;
14
15 import info.magnolia.cms.core.Content;
16 import info.magnolia.cms.core.NodeData;
17 import info.magnolia.cms.gui.misc.FileProperties;
18 import info.magnolia.cms.util.LinkUtil;
19 import info.magnolia.cms.util.Resource;
20
21 import java.io.IOException JavaDoc;
22 import java.util.Date JavaDoc;
23 import java.util.Locale JavaDoc;
24
25 import javax.jcr.PropertyType;
26 import javax.servlet.http.HttpServletRequest JavaDoc;
27 import javax.servlet.jsp.JspWriter JavaDoc;
28 import javax.servlet.jsp.PageContext JavaDoc;
29
30 import org.apache.commons.lang.StringUtils;
31 import org.apache.commons.lang.exception.NestableRuntimeException;
32 import org.apache.commons.lang.time.DateFormatUtils;
33
34
35 /**
36  * @author Sameer Charles
37  * @author Fabrizio Giustina
38  * @version $Revision: 7707 $ ($Author: philipp $)
39  */

40 public class Out extends BaseContentTag {
41
42     /**
43      * Stable serialVersionUID.
44      */

45     private static final long serialVersionUID = 222L;
46
47     private static final String JavaDoc DEFAULT_LINEBREAK = "<br />"; //$NON-NLS-1$
48

49     private static final String JavaDoc DEFAULT_DATEPATTERN = "yyyy-MM-dd"; //$NON-NLS-1$
50

51     private String JavaDoc fileProperty = StringUtils.EMPTY;
52
53     private String JavaDoc datePattern = DEFAULT_DATEPATTERN; // according to ISO 8601
54

55     private String JavaDoc dateLanguage;
56
57     private String JavaDoc lineBreak = DEFAULT_LINEBREAK;
58
59     /**
60      * If set, the result of the evaluation will be set to a variable named from this attribute (and in the scope
61      * defined using the "scope" attribute, defaulting to PAGE) instead of being written directly to the page.
62      */

63     private String JavaDoc var;
64
65     /**
66      * Scope for the attribute named from the "var" parameter. Setting this attribute doesn't have any effect if "var"
67      * is not set.
68      */

69     private int scope = PageContext.PAGE_SCOPE;
70
71     /**
72      * Setter for <code>var</code>.
73      * @param var The var to set.
74      */

75     public void setVar(String JavaDoc var) {
76         this.var = var;
77     }
78
79     /**
80      * Setter for <code>scope</code>.
81      * @param scope The scope to set.
82      */

83     public void setScope(String JavaDoc scope) {
84         if ("request".equalsIgnoreCase(scope)) { //$NON-NLS-1$
85
this.scope = PageContext.REQUEST_SCOPE;
86         }
87         else if ("session".equalsIgnoreCase(scope)) { //$NON-NLS-1$
88
this.scope = PageContext.SESSION_SCOPE;
89         }
90         else if ("application".equalsIgnoreCase(scope)) { //$NON-NLS-1$
91
this.scope = PageContext.APPLICATION_SCOPE;
92         }
93         else {
94             // default
95
this.scope = PageContext.PAGE_SCOPE;
96         }
97     }
98
99     /**
100      * <p>
101      * set which information of a file to retrieve
102      * </p>
103      * <p>
104      * does only apply for nodeDatas of type=Binary
105      * </p>
106      * <p>
107      * supported values (sample value):
108      * </p>
109      * <ul>
110      * <li><b>path (default): </b> path inlcuding the filename (/dev/mainColumnParagraphs/0/image/Alien.png)</li>
111      * <li><b>name </b>: name and extension (Alien.png)</li>
112      * <li><b>extension: </b> extension as is (Png)</li>
113      * <li><b>extensionLowerCase: </b> extension lower case (png)</li>
114      * <li><b>extensionUpperCase: </b> extension upper case (PNG)</li>
115      * <li><b>nameWithoutExtension: </b> (Alien)</li>
116      * <li><b>handle: </b> /dev/mainColumnParagraphs/0/image</li>
117      * <li><b>icon: </b>the default icon for the type of document</li>
118      * <li><b>pathWithoutName: </b> (/dev/mainColumnParagraphs/0/image.png)</li>
119      * <li><b>size: </b> size in bytes (2827)</li>
120      * <li><b>sizeString: </b> size in bytes, KB or MB - max. 3 digits before comma - with unit (2.7 KB)</li>
121      * <li><b>contentType: </b> (image/png)</li>
122      * <li><b>width: </b>image width in pixels (images only)</li>
123      * <li><b>height: </b>image height in pixels (images only)</li>
124      * </ul>
125      * </p>
126      * @param property
127      */

128     public void setFileProperty(String JavaDoc property) {
129         this.fileProperty = property;
130     }
131
132     /**
133      * <p/> set which date format shall be delivered
134      * </p>
135      * <p/> does only apply for nodeDatas of type=Date
136      * </p>
137      * <p/> language according to java.text.SimpleDateFormat:
138      * <ul>
139      * <li><b>G </b> Era designator Text AD
140      * <li><b>y </b> Year Year 1996; 96
141      * <li><b>M </b> Month in year Month July; Jul; 07
142      * <li><b>w </b> Week in year Number 27
143      * <li><b>W </b> Week in month Number 2
144      * <li><b>D </b> Day in year Number 189
145      * <li><b>d </b> Day in month Number 10
146      * <li><b>F </b> Day of week in month Number 2
147      * <li><b>E </b> Day in week Text Tuesday; Tue
148      * <li><b>a </b> Am/pm marker Text PM
149      * <li><b>H </b> Hour in day (0-23) Number 0
150      * <li><b>k </b> Hour in day (1-24) Number 24
151      * <li><b>K </b> Hour in am/pm (0-11) Number 0
152      * <li><b>h </b> Hour in am/pm (1-12) Number 12
153      * <li><b>m </b> Minute in hour Number 30
154      * <li><b>s </b> Second in minute Number 55
155      * <li><b>S </b> Millisecond Number 978
156      * <li><b>z </b> Time zone General time zone Pacific Standard Time; PST; GMT-08:00
157      * <li><b>Z </b> Time zone RFC 822 time zone -0800
158      * </ul>
159      * </p>
160      * @param pattern , default is yyyy-MM-dd
161      */

162     public void setDatePattern(String JavaDoc pattern) {
163         this.datePattern = pattern;
164     }
165
166     /**
167      * Set which date format shall be delivered. Does only apply for nodeDatas of type=Date. Language according to
168      * <code>java.util.Locale</code>.
169      * @param language
170      */

171     public void setDateLanguage(String JavaDoc language) {
172         this.dateLanguage = language;
173     }
174
175     /**
176      * Set the lineBreak String.
177      * @param lineBreak
178      */

179     public void setLineBreak(String JavaDoc lineBreak) {
180         this.lineBreak = lineBreak;
181     }
182
183     protected String JavaDoc getFilePropertyValue(Content contentNode) {
184         FileProperties props = new FileProperties(contentNode, this.nodeDataName);
185         String JavaDoc value = props.getProperty(this.fileProperty);
186         return value;
187     }
188
189     /**
190      * @see javax.servlet.jsp.tagext.Tag#doEndTag()
191      */

192     public int doEndTag() {
193         // don't reset any value set using a tag attribute here, or it will break any container that does tag pooling!
194

195         Content contentNode = getFirtMatchingNode();
196         if (contentNode == null) {
197             return EVAL_PAGE;
198         }
199
200         NodeData nodeData = contentNode.getNodeData(this.nodeDataName);
201
202         if (!nodeData.isExist()) {
203             return EVAL_PAGE;
204         }
205
206         String JavaDoc value = null;
207         int type = nodeData.getType();
208
209         switch (type) {
210             case PropertyType.DATE:
211
212                 Date JavaDoc date = nodeData.getDate().getTime();
213                 if (date != null) {
214                     if (this.dateLanguage == null) {
215                         value = DateFormatUtils.format(date, this.datePattern);
216                     }
217                     else {
218                         value = DateFormatUtils.format(date, this.datePattern, new Locale JavaDoc(this.dateLanguage));
219                     }
220                 }
221                 break;
222
223             case PropertyType.BINARY:
224                 value = this.getFilePropertyValue(contentNode);
225                 break;
226
227             default:
228                 value = StringUtils.isEmpty(this.lineBreak) ? nodeData.getString() : nodeData.getString(this.lineBreak);
229                 // replace internal links
230
value = LinkUtil.convertUUIDsToRelativeLinks(value, Resource
231                     .getActivePage((HttpServletRequest JavaDoc) pageContext.getRequest())); // static actpage
232
break;
233         }
234
235         if (var != null) {
236             // set result as a variable
237
pageContext.setAttribute(var, value, scope);
238         }
239         else if (value != null) {
240             JspWriter JavaDoc out = pageContext.getOut();
241             try {
242                 out.print(value);
243             }
244             catch (IOException JavaDoc e) {
245                 // should never happen
246
throw new NestableRuntimeException(e);
247             }
248         }
249
250         return EVAL_PAGE;
251     }
252
253     /**
254      * @see javax.servlet.jsp.tagext.TagSupport#release()
255      */

256     public void release() {
257         super.release();
258
259         this.fileProperty = StringUtils.EMPTY;
260         this.datePattern = DEFAULT_DATEPATTERN;
261         this.dateLanguage = null;
262         this.lineBreak = DEFAULT_LINEBREAK;
263         this.var = null;
264         this.scope = PageContext.PAGE_SCOPE;
265     }
266
267 }
Popular Tags