1 16 17 package org.apache.taglibs.gnat; 18 19 import org.apache.taglibs.gnat.util.*; 20 import javax.servlet.jsp.*; 21 import javax.servlet.jsp.tagext.*; 22 import java.io.*; 23 import java.util.*; 24 25 public class lsTag extends BodyTagSupport 26 { 27 private String dir = ""; 28 private File _f; 29 private ResourceBundle gnatRB = ListResourceBundle.getBundle("org.apache.taglibs.gnat.util.GnatTagStrings"); 30 private ResourceBundle gnatERB = ListResourceBundle.getBundle("org.apache.taglibs.gnat.util.GnatExceptionStrings"); 31 private String item = null; 32 private String [] _files = null; 33 private int _i = 0; 34 35 public void setDir(String dir) { 36 this.dir = dir; 37 } 38 39 public void setItem(String item) { 40 this.item = item; 41 } 42 43 49 public final int doStartTag() throws JspException 50 { 51 if (!dir.equals("")) 52 { _f = FileUtil.resolveFile( null, dir ); 54 55 try 56 { 57 _files = _f.list(); 58 if( _files == null || !( _files.length > 0 ) ) 59 { 60 return SKIP_BODY; 61 } 62 else 63 { 64 item = getNext( _files, _i ); 65 _i++; 66 } 67 68 pageContext.setAttribute( id, this ); 69 } 70 catch(Exception e) 71 { 72 throw new JspTagException(gnatRB.getString("ls.tag") + ": " + e.getMessage()); 73 } 74 return EVAL_BODY_TAG; 75 } 76 else 77 { 78 throw new JspTagException(gnatRB.getString("ls.tag") + ": " + gnatERB.getString("empty.dir.attribute")); 79 } 80 } 81 82 88 public final int doAfterBody() throws JspException 89 { 90 if( _i < _files.length ) { 91 item = getNext( _files, _i ); 92 _i++; 93 return EVAL_BODY_TAG; 94 } 95 else { 96 return SKIP_BODY; 97 } 98 } 99 100 104 public final int doEndTag() throws JspException 105 { 106 try { 107 if( bodyContent != null ) 108 bodyContent.writeOut( bodyContent.getEnclosingWriter() ); 109 } 110 catch(IOException ioe) 111 { 112 throw new JspException( gnatRB.getString("ls.tag") + ": " + ioe.getMessage() ); 113 } 114 return EVAL_PAGE; 115 } 116 117 121 private String getNext(String array[], int index) 122 { 123 String s = array[index]; 124 return s; 125 } 126 127 130 public final void release() 131 { 132 if( id != null ) 133 pageContext.removeAttribute( id, PageContext.PAGE_SCOPE ); 134 _i = 0; 135 } 136 137 140 public String getDir() { return dir; } 141 public String getItem() { return item; } 142 } 143 | Popular Tags |