KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > taglibs > gnat > lsTag


1 /*
2  * Copyright 1999,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc 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 JavaDoc item = null;
32     private String JavaDoc[] _files = null;
33     private int _i = 0;
34         
35     public void setDir(String JavaDoc dir) {
36         this.dir = dir;
37     }
38     
39     public void setItem(String JavaDoc item) {
40         this.item = item;
41     }
42     
43     /**
44      * Gets the file listing and kicks off the loop through all the file names
45      * in a directory.
46      *
47      * @return SKIP_BODY if no items are found, EVAL_BODY_TAG if items exist.
48      */

49     public final int doStartTag() throws JspException
50     {
51         if (!dir.equals(""))
52         { // Get directory using absolute file name (add capability for relative files?)
53
_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 JavaDoc 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     /**
83      * Takes control of the loop from doStartTag(), It accesses the
84      * variables set up there and sets the logic for the rest of the loop.
85      *
86      * @return EVAL_BODY_TAG if there is another list item, or SKIP_BODY if there are no more.
87      */

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     /**
101      * Method called at end of Tag to write out the response.
102      * @return EVAL_PAGE
103      */

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     /**
118     * Returns a String from a given array and index. Called from doStartTag and
119     * and doAfterBody.
120     */

121     private String JavaDoc getNext(String JavaDoc array[], int index)
122     {
123         String JavaDoc s = array[index];
124         return s;
125     }
126
127     /**
128      * Remove the id variable after ls tag is finished.
129      */

130     public final void release()
131     {
132         if( id != null )
133         pageContext.removeAttribute( id, PageContext.PAGE_SCOPE );
134         _i = 0;
135     }
136
137     /* Put tag attribute accessors down here, out of the way, since they're more for
138        JavaBean completeness than programmer use.
139      */

140     public String JavaDoc getDir() { return dir; }
141     public String JavaDoc getItem() { return item; }
142 }
143
Popular Tags