KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > info > magnolia > cms > taglibs > util > FileSrc


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-2005 obinary Ltd. (http://www.obinary.com) All rights reserved.
11  *
12  */

13 package info.magnolia.cms.taglibs.util;
14
15 import info.magnolia.cms.beans.config.Server;
16 import info.magnolia.cms.core.Content;
17 import info.magnolia.cms.core.NodeData;
18 import info.magnolia.cms.util.Resource;
19
20 import javax.jcr.RepositoryException;
21 import javax.servlet.http.HttpServletRequest JavaDoc;
22 import javax.servlet.jsp.JspWriter JavaDoc;
23 import javax.servlet.jsp.PageContext JavaDoc;
24 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
25
26 import org.apache.commons.lang.StringUtils;
27 import org.apache.log4j.Logger;
28
29
30 /**
31  * @author Marcel Salathe
32  * @version $Revision $ ($Author $)
33  * @deprecated
34  */

35 public class FileSrc extends TagSupport JavaDoc {
36
37     /**
38      * Stable serialVersionUID.
39      */

40     private static final long serialVersionUID = 222L;
41
42     /**
43      * Logger.
44      */

45     private static Logger log = Logger.getLogger(FileSrc.class);
46
47     private transient NodeData nodeData;
48
49     private transient Content contentNode;
50
51     private transient Content actpage;
52
53     private String JavaDoc nodeDataName = StringUtils.EMPTY;
54
55     private String JavaDoc contentNodeName = StringUtils.EMPTY;
56
57     private String JavaDoc fileNameOnly = StringUtils.EMPTY;
58
59     private HttpServletRequest JavaDoc request;
60
61     private String JavaDoc fileExtension;
62
63     private String JavaDoc fileName;
64
65     private String JavaDoc fileExtendedName;
66
67     private String JavaDoc slash = StringUtils.EMPTY;
68
69     /**
70      * @deprecated
71      */

72     public void setAtomName(String JavaDoc name) {
73         this.setNodeDataName(name);
74     }
75
76     /**
77      * @deprecated
78      * @param nodeDataName
79      */

80     public void setNodeDataName(String JavaDoc nodeDataName) {
81         this.nodeDataName = nodeDataName;
82     }
83
84     /**
85      * @deprecated
86      */

87     public void setContainerName(String JavaDoc name) {
88         this.setContentNodeName(name);
89     }
90
91     /**
92      * @deprecated
93      * @param contentNodeName
94      */

95     public void setContentNodeName(String JavaDoc contentNodeName) {
96         this.contentNodeName = contentNodeName;
97     }
98
99     /**
100      * @deprecated
101      * @param value
102      */

103     public void setFileNameOnly(String JavaDoc value) {
104         this.fileNameOnly = "true"; //$NON-NLS-1$
105
}
106
107     /**
108      * @see javax.servlet.jsp.tagext.Tag#doStartTag()
109      */

110     public int doStartTag() {
111         this.request = (HttpServletRequest JavaDoc) pageContext.getRequest();
112         this.actpage = Resource.getCurrentActivePage(request);
113         if (StringUtils.isNotEmpty(this.contentNodeName)) {
114             try {
115                 this.contentNode = this.actpage.getContent(this.contentNodeName);
116             }
117             catch (RepositoryException re) {
118                 writeSrc(StringUtils.EMPTY);
119             }
120             if (this.contentNode == null) {
121                 writeSrc(StringUtils.EMPTY);
122                 return SKIP_BODY;
123             }
124         }
125         else {
126             this.contentNode = Resource.getLocalContentNode(request);
127             if (this.contentNode == null) {
128                 this.contentNode = Resource.getGlobalContentNode(request);
129             }
130             if (this.contentNode != null) {
131                 this.contentNodeName = this.contentNode.getName();
132             }
133             else {
134                 writeSrc(StringUtils.EMPTY);
135                 return SKIP_BODY;
136             }
137         }
138         if (StringUtils.isEmpty(this.nodeDataName)) {
139             writeSrc(StringUtils.EMPTY);
140             return SKIP_BODY;
141         }
142         try {
143             this.nodeData = this.contentNode.getNodeData(this.contentNodeName);
144         }
145         catch (Exception JavaDoc e) {
146             writeSrc(StringUtils.EMPTY);
147             return SKIP_BODY;
148         }
149         if (this.nodeData == null) {
150             writeSrc(StringUtils.EMPTY);
151             return SKIP_BODY;
152         }
153         setFileProperties();
154
155         String JavaDoc contentNodeCollectionName = (String JavaDoc) pageContext.getAttribute("contentNodeCollectionName", //$NON-NLS-1$
156
PageContext.REQUEST_SCOPE);
157         if (this.fileNameOnly.equals("true")) { //$NON-NLS-1$
158
try {
159                 writeSrc(this.fileExtendedName);
160             }
161             catch (Exception JavaDoc e) {
162                 log.debug(e.getMessage());
163             }
164         }
165         else {
166             if (contentNodeCollectionName == null) {
167                 // we are not in a loop
168
try {
169                     writeSrc(this.contentNode.getHandle() + "/" //$NON-NLS-1$
170
+ this.nodeDataName + this.slash + this.fileExtendedName);
171                 }
172                 catch (Exception JavaDoc e) {
173                     log.debug(e.getMessage());
174                 }
175             }
176             else {
177                 try {
178                     writeSrc(Resource.getLocalContentNode(request).getHandle() + "/" //$NON-NLS-1$
179
+ this.nodeDataName + this.slash + this.fileExtendedName);
180                 }
181                 catch (Exception JavaDoc e) {
182                     log.debug(e.getMessage());
183                 }
184             }
185         }
186         return EVAL_PAGE;
187     }
188
189     private void writeSrc(String JavaDoc src) {
190         JspWriter JavaDoc out = pageContext.getOut();
191         try {
192             out.print(src);
193         }
194         catch (Exception JavaDoc e) {
195             log.debug("Exception caught: " + e.getMessage(), e); //$NON-NLS-1$
196
}
197     }
198
199     /**
200      * @deprecated
201      */

202     private void setFileProperties() {
203         this.fileExtension = Server.getDefaultExtension();
204         Content properties = null;
205         String JavaDoc contentNodeCollectionName = (String JavaDoc) pageContext.getAttribute("contentNodeCollectionName", //$NON-NLS-1$
206
PageContext.REQUEST_SCOPE);
207         if (contentNodeCollectionName == null) {
208             // we are not in a loop
209
try {
210                 properties = Resource.getGlobalContentNode(this.request).getContent(this.nodeDataName + "_properties"); //$NON-NLS-1$
211
}
212             catch (Exception JavaDoc e) {
213                 log.debug(e.getMessage());
214             }
215         }
216         else {
217             try {
218                 properties = Resource.getLocalContentNode(this.request).getContent(this.nodeDataName + "_properties"); //$NON-NLS-1$
219
}
220             catch (Exception JavaDoc e) {
221                 log.debug("Exception caught: " + e.getMessage(), e); //$NON-NLS-1$
222
}
223         }
224         if (properties != null) {
225             this.fileName = properties.getNodeData("fileName").getString(); //$NON-NLS-1$
226
this.fileExtension = properties.getNodeData("extension").getString(); //$NON-NLS-1$
227
if (StringUtils.isEmpty(this.fileName)) {
228                 this.fileExtendedName = "." + this.fileExtension; //$NON-NLS-1$
229
}
230             else {
231                 this.slash = "/"; //$NON-NLS-1$
232
this.fileExtendedName = this.fileName;
233                 int posLastDot = this.fileName.lastIndexOf("."); //$NON-NLS-1$
234
int posExt = this.fileName.lastIndexOf("." + this.fileExtension); //$NON-NLS-1$
235
if (posExt == -1 || (posExt != -1 && posExt != posLastDot)) {
236                     // magnolia v 1.0: fileName saved with extension
237
this.fileExtendedName += "." + this.fileExtension; //$NON-NLS-1$
238
}
239             }
240         }
241     }
242 }
243
Popular Tags