KickJava   Java API By Example, From Geeks To Geeks.

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


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 gzipTag 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 zipfile;
35     private File zf;
36     private String JavaDoc src;
37     private File source;
38     private ServletContext JavaDoc ctx;
39     private FileUtil fut;
40
41     
42     private void setZF(String JavaDoc zipfile){
43
44         zf = fut.resolveFile(null, zipfile);
45     }
46
47     private void setSource(String JavaDoc src) {
48
49         source = fut.resolveFile(null, src);
50     }
51     
52     public void setZipfile(String JavaDoc zipfile) {
53
54         this.zipfile = zipfile;
55     }
56
57     public void setSrc(String JavaDoc src) {
58
59         this.src = src;
60     
61     }
62     
63     public int doStartTag() throws JspException {
64         ctx = pageContext.getServletContext();
65         if (!bdir.equals(""))
66         {
67             baseDir = new File(src);
68             //See notes in gunzipTag.java
69
setZF(zipfile);
70             return SKIP_BODY;
71         }
72         else
73         {
74             baseDir = new File(bdir);
75             setSource(src);
76             //See notes in gunzipTag.java
77
setZF(zipfile);
78             return SKIP_BODY;
79         }
80     }
81     
82     public int doEndTag() throws JspException {
83         if (zf == null)
84         {
85             throw new JspTagException("gzip: zipfile attribute is required");
86         }
87
88         if (source == null)
89         {
90             throw new JspTagException("gzip: src attribute is required");
91         }
92         ctx.log("gzip: Building gzip: " + zf.getAbsolutePath());
93
94         GZIPOutputStream zOut = null;
95         try
96         {
97             zOut = new GZIPOutputStream(new FileOutputStream(zf));
98
99             if (source.isDirectory())
100             {
101                 ctx.log ("gzip: Cannot Gzip a directory!");
102             }
103             else
104             {
105                 zipFile(source, zOut);
106             }
107         }
108         catch (IOException ioe)
109         {
110             String JavaDoc msg = "gzip: Problem gzipping file: " + ioe.getMessage();
111             throw new JspTagException(msg);
112         }
113         finally
114         {
115             if (zOut != null)
116             {
117                 try
118                 {
119                     zOut.close();
120                 }
121                 catch (IOException e)
122                 {
123                     e.printStackTrace();
124                 }
125             }
126         }
127         return EVAL_PAGE;
128     }
129
130     private void zipFile(InputStream in, GZIPOutputStream zOut)
131         throws IOException
132     {
133         byte[] buffer = new byte[8 * 1024];
134         int count = 0;
135         do
136         {
137             zOut.write(buffer, 0, count);
138             count = in.read(buffer, 0, buffer.length);
139         }
140         while (count != -1);
141     }
142
143     private void zipFile(File file, GZIPOutputStream zOut)
144         throws IOException
145     {
146         FileInputStream fIn = new FileInputStream(file);
147         try
148         {
149             zipFile(fIn, zOut);
150         }
151         finally
152         {
153             fIn.close();
154         }
155     }
156
157     public String JavaDoc getZipfile() {
158         return zipfile;
159     }
160
161     public String JavaDoc getSrc() {
162         return src;
163     }
164 }
165
Popular Tags