KickJava   Java API By Example, From Geeks To Geeks.

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


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.FileUtil;
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.zip.*;
25 import java.util.*;
26
27 public class gunzipTag extends TagSupport
28 {
29     //Need to decide what to do with basedir. Need to use it for full Ant support,
30
//but for JSP taglib, it should be optional. Perhaps a scripting variable.
31
//For now, it's always null until we figure out a good way to use it.
32
private String JavaDoc bdir = "";
33     private File baseDir;
34     private String JavaDoc dest; // the dest attribute
35
private File destF; // the dest file
36
private String JavaDoc src; // the src attribute
37
private File source; // the src file
38
private ServletContext JavaDoc ctx; // for logging
39
private FileUtil fut; // contains some static util methods ripped from Avalon utils
40

41     
42     private void setDestF(String JavaDoc dest){
43
44         destF = fut.resolveFile(null, dest);
45     }
46
47     private void setSource(String JavaDoc src) {
48
49         source = fut.resolveFile(null, src);
50     }
51     
52     public void setDest(String JavaDoc dest) {
53
54         this.dest = dest;
55     }
56
57     public void setSrc(String JavaDoc src) {
58
59         this.src = src;
60     
61     }
62     
63     public int doStartTag() throws JspException
64     {
65         ctx = pageContext.getServletContext();
66         
67         /* Until we decide how to deal with projects in the Ant tags, bdir is
68          * not set. So this if statement is always false, for the time being.
69          */

70         if (!bdir.equals(""))
71         {
72             baseDir = new File(src);
73             //Should fix so that we call different FileUtil methods, depending
74
//whether a basefile name is passed. Could be an optional attribute.
75
setDestF(dest);
76             return SKIP_BODY;
77         }
78         else
79         {
80             baseDir = new File(bdir);
81             setSource(src);
82             //Should fix so that we call different FileUtil methods, depending
83
//whether a basefile name is passed. Could be an optional attribute.
84
setDestF(dest);
85         
86             return SKIP_BODY;
87         }
88     }
89     
90     public int doEndTag() throws JspException
91     {
92         if (source == null)
93         {
94             throw new JspTagException("gunzip: No source file for gunzip specified.");
95         }
96
97         if (!source.exists())
98         {
99             throw new JspTagException("gunzip: Source file "+src+" doesn't exist.");
100         }
101
102         if (source.isDirectory())
103         {
104             throw new JspException("gunzip: Cannot expand a directory. "+src+" is a directory.");
105         }
106
107         if (destF == null)
108         {
109             destF = new File(source.getParent());
110         }
111
112         if (destF.isDirectory())
113         {
114             String JavaDoc sourceName = source.getName();
115             int len = sourceName.length();
116             if (len > 3
117                 && ".gz".equalsIgnoreCase(sourceName.substring(len-3)))
118             {
119                 destF = new File(destF, sourceName.substring(0, len-3));
120             }
121             else
122             {
123                 destF = new File(destF, sourceName);
124             }
125         }
126
127         if (source.lastModified() > destF.lastModified())
128         {
129             ctx.log("gunzip: Expanding "+ source.getAbsolutePath() + " to "
130                         + destF.getAbsolutePath());
131
132             FileOutputStream out = null;
133             GZIPInputStream zIn = null;
134             try
135             {
136                 out = new FileOutputStream(destF);
137                 zIn = new GZIPInputStream(new FileInputStream(source));
138                 byte[] buffer = new byte[8 * 1024];
139                 int count = 0;
140                 do
141                 {
142                     out.write(buffer, 0, count);
143                     count = zIn.read(buffer, 0, buffer.length);
144                 }
145                 while (count != -1);
146             }
147             catch (IOException ioe)
148             {
149                 String JavaDoc msg = "gunzip: Problem expanding gzip " + ioe.getMessage();
150                 throw new JspTagException(msg);
151             }
152             finally
153             {
154                 if (out != null)
155                 {
156                     try
157                     {
158                         out.close();
159                     }
160                     catch (IOException ioex)
161                     {
162                         ioex.printStackTrace();
163                     }
164                 }
165                 if (zIn != null)
166                 {
167                     try
168                     {
169                         zIn.close();
170                     }
171                     catch (IOException ioex)
172                     {
173                         ioex.printStackTrace();
174                     }
175                 }
176             }
177         }
178         return EVAL_PAGE;
179     }
180
181     public String JavaDoc getDest() {
182         return dest;
183     }
184
185     public String JavaDoc getSrc() {
186         return src;
187     }
188 }
189
Popular Tags