KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > dlog4j > tags > ListFilesTag


1 /*
2  * This program is free software; you can redistribute it and/or modify
3  * it under the terms of the GNU General Public License as published by
4  * the Free Software Foundation; either version 2 of the License, or
5  * (at your option) any later version.
6  *
7  * This program is distributed in the hope that it will be useful,
8  * but WITHOUT ANY WARRANTY; without even the implied warranty of
9  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10  * GNU Library General Public License for more details.
11  *
12  * You should have received a copy of the GNU General Public License
13  * along with this program; if not, write to the Free Software
14  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
15  */

16 package dlog4j.tags;
17
18 import java.io.File JavaDoc;
19 import java.util.ArrayList JavaDoc;
20 import java.util.List JavaDoc;
21
22 import javax.servlet.jsp.JspException JavaDoc;
23 import javax.servlet.jsp.tagext.TagSupport JavaDoc;
24
25 import org.apache.commons.lang.StringUtils;
26
27 /**
28  * 罗列出指定目录的所有文件
29  * @author liudong
30  */

31 public class ListFilesTag extends TagSupport JavaDoc {
32     
33     public final static String JavaDoc WEB_PATH = "{webapp}";
34     String JavaDoc path;
35     String JavaDoc extend;
36     boolean exclude = false;
37     String JavaDoc pathId;
38
39     public int doStartTag() throws JspException JavaDoc {
40         String JavaDoc dir = StringUtils.replace(path,
41                                          WEB_PATH,
42                                          pageContext.getServletContext().getRealPath(""));
43         pageContext.setAttribute(pathId, dir);
44         File JavaDoc f = new File JavaDoc(dir);
45         if(f.exists() && f.isDirectory()){
46             String JavaDoc[] fns = f.list();
47             List JavaDoc lfns = new ArrayList JavaDoc();
48             for(int i=0;i<fns.length;i++){
49                 if(extend!=null){
50                      if(exclude && fns[i].endsWith(extend))
51                         continue;
52                      else
53                      if(!exclude && !fns[i].endsWith(extend))
54                         continue;
55                 }
56                 lfns.add(fns[i]);
57             }
58             pageContext.setAttribute(id, lfns);
59         }
60         else
61             pageContext.getServletContext().log("目录["+path+"]不存在");
62         return SKIP_BODY;
63     }
64     
65     public void release() {
66         path = null;
67         extend = null;
68         exclude = false;
69     }
70     public boolean isExclude() {
71         return exclude;
72     }
73     public void setExclude(boolean exclude) {
74         this.exclude = exclude;
75     }
76     public String JavaDoc getExtend() {
77         return extend;
78     }
79     public void setExtend(String JavaDoc extend) {
80         this.extend = extend;
81     }
82     public String JavaDoc getPath() {
83         return path;
84     }
85     public void setPath(String JavaDoc path) {
86         this.path = path;
87     }
88     public String JavaDoc getPathId() {
89         return pathId;
90     }
91     public void setPathId(String JavaDoc pathId) {
92         this.pathId = pathId;
93     }
94 }
95
Popular Tags