Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: com/RuntimeCollective/webapps/tag/WebfolderSelectTag.java


1   /* $Header: /home/CVS/rjp/src/com/RuntimeCollective/webapps/tag/WebfolderSelectTag.java,v 1.13 2003/09/30 15:13:19 joe Exp $
2    * $Revision: 1.13 $
3    * $Date: 2003/09/30 15:13:19 $
4    *
5    * ====================================================================
6    *
7    * Josephine : http://www.runtime-collective.com/josephine/index.html
8    *
9    * Copyright (C) 2003 Runtime Collective
10   * 
11   * This product includes software developed by the
12   * Apache Software Foundation (http://www.apache.org/).
13   *
14   * This library is free software; you can redistribute it and/or
15   * modify it under the terms of the GNU Lesser General Public
16   * License as published by the Free Software Foundation; either
17   * version 2.1 of the License, or (at your option) any later version.
18   *
19   * This library is distributed in the hope that it will be useful,
20   * but WITHOUT ANY WARRANTY; without even the implied warranty of
21   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22   * Lesser General Public License for more details.
23   *
24   * You should have received a copy of the GNU Lesser General Public
25   * License along with this library; if not, write to the Free Software
26   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
27   *
28   */
29  
30  package com.RuntimeCollective.webapps.tag;
31  
32  import com.RuntimeCollective.webapps.RuntimeDataSource;
33  import com.RuntimeCollective.webapps.RuntimeParameters;
34  import org.apache.struts.util.RequestUtils;
35  
36  
37  import java.util.Vector;
38  import java.util.TreeSet;
39  import java.util.Iterator;
40  import java.util.Enumeration;
41  import java.sql.SQLException;
42  import java.io.IOException;
43  import java.io.PrintStream;
44  
45  import javax.naming.Binding;
46  import javax.naming.Context;
47  import javax.naming.NamingEnumeration;
48  import javax.naming.NamingException;
49  import javax.naming.InitialContext;
50  import javax.naming.directory.DirContext;
51  
52  import javax.servlet.jsp.JspWriter;
53  import javax.servlet.jsp.PageContext;
54  import javax.servlet.jsp.JspException;
55  import javax.servlet.jsp.JspTagException;
56  import javax.servlet.jsp.tagext.Tag;
57  import javax.servlet.jsp.tagext.TagSupport;
58  import javax.servlet.http.HttpSession;
59  import javax.servlet.http.HttpServletRequest;
60  import javax.servlet.ServletContext;
61  
62  import java.io.File;
63  import java.io.FilenameFilter;
64  import com.RuntimeCollective.webapps.bean.User;
65  import com.RuntimeCollective.webapps.bean.WebFolder;
66  import com.RuntimeCollective.webapps.HtmlFilenameFilter;
67  
68  import org.apache.struts.taglib.html.BaseHandlerTag;
69  
70  import org.apache.commons.beanutils.BeanUtils;
71  
72  import org.apache.log4j.Category;
73  
74  /**
75   * A custom JSP tag that creates an input select box containing all
76   * files in the current user's webfolder.<p>
77   *
78   * The filename selected is does NOT include the path to the User's
79   * directory. For this reason <B>the actual Bean which stores the
80   * property</B> should be given in the name property, NOT the
81   * ActionForm bean. This is because it is the form's responsibility to
82   * generate the correct path to the resource.
83   *
84   * <ul>
85   *
86   * <li> <code>property </code> - the property of the form bean that
87   * will be set to the selected filename.
88   *
89   * <li> <code>allowBlank</code> - allows a blank selection to be
90   * made. [Optional] [default: false]
91   *
92   * <li> <code>htmlOnly</code> - only show files that end in .html or
93   * .htm [Optional] [default: false]
94   *
95   * <li> <code>imagesOnly</code> - only show files that end in .gif or
96   * .jpg [Optional] [default: false]
97   *
98   * </ul>
99   *
100  * @version $Id: WebfolderSelectTag.java,v 1.13 2003/09/30 15:13:19 joe Exp $
101  */
102 public class WebfolderSelectTag extends BaseHandlerTag {
103     
104     /**
105      * The property name for the selected file.  Mandatory.
106      */
107     private String ivProperty = "";
108     
109     private boolean ivAllowBlank = false;
110     
111     private boolean ivImagesOnly = false; 
112     
113     private boolean ivHTMLOnly = false; 
114 
115     /**
116      * The name of the bean containing the property.
117      */
118     private String ivName;
119     
120     public String getName() {
121   return (ivName);
122     }
123     
124     public void setName(String name) {
125   ivName = name;
126     }
127     
128     /**
129      * Get the property name.
130      */
131     public String getProperty() {
132   return ivProperty;
133     }
134     
135     /**
136      * Set the property name.
137      */
138     public void setProperty(String property) {
139   ivProperty = property;
140     }
141     
142     
143     public boolean getAllowBlank() {
144   return ivAllowBlank;
145     } 
146     
147     /**
148      * Set allow a blank selection to be made (default false)
149      */ 
150     public void setAllowBlank(boolean flag) {
151   ivAllowBlank = flag;
152     } 
153     
154     
155     public boolean getHtmlOnly() {
156   return ivHTMLOnly;
157     }
158     
159     /**
160      * Set only select HTML files
161      */ 
162     public void setHtmlOnly(boolean flag) {
163   ivHTMLOnly = flag;
164   if(ivHTMLOnly) {
165       ivImagesOnly = false;
166   }
167     } 
168     
169     public boolean getImagesOnly() {
170   return ivImagesOnly;
171     }
172     
173     /**
174      * Set only select gif and jpg files
175      */ 
176     public void setImagesOnly(boolean flag) {
177   ivImagesOnly = flag;
178   if(ivImagesOnly) {
179       ivHTMLOnly = false;
180   }
181     } 
182    
183     // == Tag methods ==================================
184     
185     public int doStartTag() throws JspException { 
186   RuntimeParameters.log(this, "[doStartTag] Starting");
187   
188   // Get the property value from the bean. The bean is stored on the
189   // session under beanName, and the property is property.
190   String beanName = getName();
191   RuntimeParameters.log(this, "[doStartTag] Bean Name :" + beanName);
192   Object bean = pageContext.getSession().getAttribute(beanName);
193   String currentFileName = null;
194   if(bean != null) {
195       RuntimeParameters.log(this, "[doStartTag] Got the form");
196       try {
197     currentFileName = (String) BeanUtils.getSimpleProperty(bean, getProperty());
198       } catch (Exception ex) {
199     RuntimeParameters.log(this, "[doStartTag] Error getting property :" + getProperty());
200       }
201       RuntimeParameters.log(this, "[doStartTag] Current file name : " + currentFileName);
202   }
203 
204   // Build up the select box
205   StringBuffer html = new StringBuffer("<select name=\"" + getProperty() + "\" ");
206   html.append(prepareStyles());
207   html.append(" ");
208   html.append(prepareEventHandlers());
209   html.append(">");
210   
211   // Allow blank?
212   if (ivAllowBlank) {
213       html.append("<option value=\"\"></option>");
214   }
215 
216 
217   TreeSet files = new TreeSet();
218   try {
219       User logonUser = (User) pageContext.getSession().getAttribute(RuntimeParameters.get("logonUserKey"));
220 
221       WebFolder folder = new WebFolder( logonUser );
222       Vector vFiles = folder.getAllFiles(null);
223       // All this tree set stuff is to get the currently select file as
224       // the first select option.
225       String fileName,compareName;
226 
227       boolean append;
228       boolean show;
229       for (int i=0; i<vFiles.size(); i++) {
230     append = true;
231     show = true;
232 
233 
234     fileName = (String) vFiles.get(i);
235     compareName = fileName.toLowerCase();
236     // mjr
237     // can we do this as a temporary solution 
238     // unix hidden files begin with .
239     // mac hidden files begin with ._
240     // windows hidden files end with .dat
241     // NOT YET IMPLEMENTED 
242     if ((compareName.startsWith(".")) || (compareName.endsWith(".dat"))) {
243         RuntimeParameters.logDebug(this, "this file should be hidden :"+fileName);
244         show = false;
245     }
246     
247     if(ivHTMLOnly || ivImagesOnly) {
248         if(ivHTMLOnly) {
249       append = (compareName.endsWith(".html") || compareName.endsWith(".htm"));
250         }
251         if(ivImagesOnly) {
252       append = (compareName.endsWith(".gif") || compareName.endsWith(".jpg"));
253         }
254     }
255     //mjr
256     // IF WE WANT TO IMPLEMENT SWAP FOLLOWING TWO LINES
257     //if((append) && (show)) {
258     if (append) {
259         files.add(fileName);
260     }
261       }
262   } catch (Exception ex) {
263       RuntimeParameters.logWarn(this, "Error getting the webfolder file listings", ex);
264       files = null;
265   }
266   
267   // What do we do if their are no files or an error has occurred
268   // getting the file listings.
269   if(files == null || files.isEmpty()) {
270       // Message not international!
271       if(ivHTMLOnly) {
272     html.append("<option value=\"\"> -- No HTML Found -- </option>");
273       } else if(ivImagesOnly) {
274     html.append("<option value=\"\"> -- No image Found -- </option>");
275       } else {
276     html.append("<option value=\"\"> -- No Files Found -- </option>");
277       }
278   } else {
279       // Lazy evaluation takes care of the null
280       if((currentFileName != null) && files.contains(currentFileName)) {
281     html.append("<option value=\"" + currentFileName + "\">" + currentFileName + "</option>");
282     files.remove(currentFileName);
283       }
284       
285       Iterator i = files.iterator();
286       while(i.hasNext()) {
287     String fileName = (String) i.next();
288     html.append("<option value=\"");
289     html.append(fileName);
290     html.append("\">");
291     html.append(fileName);
292     html.append("</option>");
293       }
294   }
295   
296   html.append("</select>");
297   
298   // And write the html to the jsp page.
299   try {
300       pageContext.getOut().println(html);
301   } catch (IOException e) {
302       throw new JspTagException("I/O Exception " + e.getMessage());
303   }
304   
305   // This tag doesn't have a body. That could be a good way to go
306   // though.
307   return SKIP_BODY;
308     }
309     
310     
311     public void release() {
312   super.release();
313   ivProperty = "";
314     }
315     
316 }