KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > vfs > tasks > ShowFileTask


1 /*
2  * Copyright 2002-2005 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 package org.apache.commons.vfs.tasks;
17
18 import org.apache.commons.vfs.FileContent;
19 import org.apache.commons.vfs.FileObject;
20 import org.apache.tools.ant.BuildException;
21
22 import java.io.BufferedReader JavaDoc;
23 import java.io.InputStream JavaDoc;
24 import java.io.InputStreamReader JavaDoc;
25 import java.util.Date JavaDoc;
26
27 /**
28  * An Ant task that writes the details of a file to Ant's log.
29  *
30  * @author <a HREF="mailto:adammurdoch@apache.org">Adam Murdoch</a>
31  */

32 public class ShowFileTask
33     extends VfsTask
34 {
35     private String JavaDoc url;
36     private boolean showContent;
37     private boolean recursive;
38     private static final String JavaDoc INDENT = " ";
39
40     /**
41      * The URL of the file to display.
42      */

43     public void setFile(final String JavaDoc url)
44     {
45         this.url = url;
46     }
47
48     /**
49      * Shows the content. Assumes the content is text, encoded using the
50      * platform's default encoding.
51      */

52     public void setShowContent(final boolean showContent)
53     {
54         this.showContent = showContent;
55     }
56
57     /**
58      * Recursively shows the decendents of the file.
59      */

60     public void setRecursive(final boolean recursive)
61     {
62         this.recursive = recursive;
63     }
64
65     /**
66      * Executes the task.
67      */

68     public void execute() throws BuildException
69     {
70         try
71         {
72             final FileObject file = resolveFile(url);
73             log("Details of " + file.getName().getURI());
74             showFile(file, INDENT);
75         }
76         catch (final Exception JavaDoc e)
77         {
78             throw new BuildException(e);
79         }
80     }
81
82     /**
83      * Logs the details of a file.
84      */

85     private void showFile(final FileObject file, final String JavaDoc prefix) throws Exception JavaDoc
86     {
87         // Write details
88
StringBuffer JavaDoc msg = new StringBuffer JavaDoc(prefix);
89         msg.append(file.getName().getBaseName());
90         if (file.exists())
91         {
92             msg.append(" (");
93             msg.append(file.getType().getName());
94             msg.append(")");
95         }
96         else
97         {
98             msg.append(" (unknown)");
99         }
100         log(msg.toString());
101
102         if (file.exists())
103         {
104             final String JavaDoc newPrefix = prefix + INDENT;
105             if (file.getType().hasContent())
106             {
107                 final FileContent content = file.getContent();
108                 log(newPrefix + "Content-Length: " + content.getSize());
109                 log(newPrefix + "Last-Modified" + new Date JavaDoc(content.getLastModifiedTime()));
110                 if (showContent)
111                 {
112                     log(newPrefix + "Content:");
113                     logContent(file, newPrefix);
114                 }
115             }
116             if (file.getType().hasChildren())
117             {
118                 final FileObject[] children = file.getChildren();
119                 for (int i = 0; i < children.length; i++)
120                 {
121                     FileObject child = children[i];
122                     if (recursive)
123                     {
124                         showFile(child, newPrefix);
125                     }
126                     else
127                     {
128                         log(newPrefix + child.getName().getBaseName());
129                     }
130                 }
131             }
132         }
133     }
134
135     /**
136      * Writes the content of the file to Ant log.
137      */

138     private void logContent(final FileObject file, final String JavaDoc prefix)
139         throws Exception JavaDoc
140     {
141         final InputStream JavaDoc instr = file.getContent().getInputStream();
142         try
143         {
144             final BufferedReader JavaDoc reader = new BufferedReader JavaDoc(new InputStreamReader JavaDoc(instr));
145             while (true)
146             {
147                 final String JavaDoc line = reader.readLine();
148                 if (line == null)
149                 {
150                     break;
151                 }
152                 log(prefix + line);
153             }
154         }
155         finally
156         {
157             instr.close();
158         }
159     }
160 }
161
Popular Tags