KickJava   Java API By Example, From Geeks To Geeks.

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


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.ServletContext JavaDoc;
21 import javax.servlet.jsp.*;
22 import javax.servlet.jsp.tagext.*;
23 import java.io.*;
24 import java.util.*;
25
26 public class mkdirTag extends TagSupport
27 {
28     //Note: in Ant's Mkdir task, dir is a File object passed in from a client class
29
private File _f;
30     private String JavaDoc dir = "";
31     private boolean clobber= false;
32     private ResourceBundle gnatRB = ListResourceBundle.getBundle("org.apache.taglibs.gnat.util.GnatTagStrings");
33     private ResourceBundle gnatERB = ListResourceBundle.getBundle("org.apache.taglibs.gnat.util.GnatExceptionStrings");
34     
35     public void setDir(String JavaDoc dir)
36     {
37         this.dir = dir;
38     }
39     
40     public void setClobber(boolean clobber)
41     {
42         this.clobber = clobber;
43     }
44     
45     
46     public int doStartTag() throws JspException
47     {
48         _f = new File(dir);
49         return SKIP_BODY;
50     }
51     
52     public int doEndTag() throws JspException
53     {
54         
55         if (_f == null)
56         {
57             throw new JspTagException( gnatRB.getString("mkdir.tag") + ": " +
58                                        gnatERB.getString("empty.dir.attribute") );
59         }
60         
61         ServletContext JavaDoc ctx = pageContext.getServletContext();
62         if (clobber) //default is clobber == false
63
{
64             if (_f.exists())
65             { // log that we are overwriting the directory . . .
66
ctx.log(gnatRB.getString("mkdir.tag") + ": " +"Overwriting dir " + _f.toString());
67                 try { // . . . and delete it before recreating it..
68
FileUtil.forceDelete(_f);
69                 }
70                 catch(IOException ioe) {
71                     throw new JspTagException( gnatRB.getString("mkdir.tag") + ": " +
72                                                ioe.getMessage() );
73                 }
74             }
75
76             boolean result = _f.mkdirs();
77             if (result == false)
78             {
79                 String JavaDoc msg = gnatRB.getString("mkdir.tag") + ": " +
80                              gnatERB.getString("unknown.mkdir.fail") +
81                              _f.getAbsolutePath();
82                 
83                 throw new JspTagException(msg);
84             }
85             ctx.log( gnatRB.getString("mkdir.tag") + ": " +
86                      gnatRB.getString("mkdir.success") +
87                      _f.getAbsolutePath() );
88         }
89         else if(!clobber) //default is clobber == false
90
{
91             if (_f.exists()) // throw an exception if the file exists
92
{
93                 throw new JspTagException( gnatRB.getString("mkdir.tag") + ": " +
94                                            gnatERB.getString("duplicate.mkdir.fail") +
95                                            _f.getAbsolutePath() );
96             }
97             else
98             {
99             
100                 boolean result = _f.mkdirs();
101                 if (result == false)
102                 {
103                     String JavaDoc msg = gnatRB.getString("mkdir.tag") + ": " +
104                                  gnatERB.getString("unknown.mkdir.fail") +
105                                  _f.getAbsolutePath();
106                 
107                     throw new JspTagException(msg);
108                 }
109                 ctx.log( gnatRB.getString("mkdir.tag") + ": " +
110                          gnatRB.getString("mkdir.success") +
111                          _f.getAbsolutePath() );
112             }
113         }
114
115         return EVAL_PAGE;
116     }
117     
118     public String JavaDoc getDir() { return dir; }
119     public boolean getClobber() { return clobber; }
120 }
121
Popular Tags