1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs.tasks;
18
19 import org.apache.commons.vfs.FileContent;
20 import org.apache.commons.vfs.FileObject;
21 import org.apache.tools.ant.BuildException;
22
23 import java.io.BufferedReader;
24 import java.io.InputStream;
25 import java.io.InputStreamReader;
26 import java.util.Date;
27
28 /***
29 * An Ant task that writes the details of a file to Ant's log.
30 *
31 * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
32 * @version $Revision: 480428 $ $Date: 2006-11-29 07:15:24 +0100 (Mi, 29 Nov 2006) $
33 */
34 public class ShowFileTask
35 extends VfsTask
36 {
37 private String url;
38 private boolean showContent;
39 private boolean recursive;
40 private static final String INDENT = " ";
41
42 /***
43 * The URL of the file to display.
44 */
45 public void setFile(final String url)
46 {
47 this.url = url;
48 }
49
50 /***
51 * Shows the content. Assumes the content is text, encoded using the
52 * platform's default encoding.
53 */
54 public void setShowContent(final boolean showContent)
55 {
56 this.showContent = showContent;
57 }
58
59 /***
60 * Recursively shows the decendents of the file.
61 */
62 public void setRecursive(final boolean recursive)
63 {
64 this.recursive = recursive;
65 }
66
67 /***
68 * Executes the task.
69 */
70 public void execute() throws BuildException
71 {
72 try
73 {
74 final FileObject file = resolveFile(url);
75 log("Details of " + file.getName().getURI());
76 showFile(file, INDENT);
77 }
78 catch (final Exception e)
79 {
80 throw new BuildException(e);
81 }
82 }
83
84 /***
85 * Logs the details of a file.
86 */
87 private void showFile(final FileObject file, final String prefix) throws Exception
88 {
89
90 StringBuffer msg = new StringBuffer(prefix);
91 msg.append(file.getName().getBaseName());
92 if (file.exists())
93 {
94 msg.append(" (");
95 msg.append(file.getType().getName());
96 msg.append(")");
97 }
98 else
99 {
100 msg.append(" (unknown)");
101 }
102 log(msg.toString());
103
104 if (file.exists())
105 {
106 final String newPrefix = prefix + INDENT;
107 if (file.getType().hasContent())
108 {
109 final FileContent content = file.getContent();
110 log(newPrefix + "Content-Length: " + content.getSize());
111 log(newPrefix + "Last-Modified" + new Date(content.getLastModifiedTime()));
112 if (showContent)
113 {
114 log(newPrefix + "Content:");
115 logContent(file, newPrefix);
116 }
117 }
118 if (file.getType().hasChildren())
119 {
120 final FileObject[] children = file.getChildren();
121 for (int i = 0; i < children.length; i++)
122 {
123 FileObject child = children[i];
124 if (recursive)
125 {
126 showFile(child, newPrefix);
127 }
128 else
129 {
130 log(newPrefix + child.getName().getBaseName());
131 }
132 }
133 }
134 }
135 }
136
137 /***
138 * Writes the content of the file to Ant log.
139 */
140 private void logContent(final FileObject file, final String prefix)
141 throws Exception
142 {
143 final InputStream instr = file.getContent().getInputStream();
144 try
145 {
146 final BufferedReader reader = new BufferedReader(new InputStreamReader(instr));
147 while (true)
148 {
149 final String line = reader.readLine();
150 if (line == null)
151 {
152 break;
153 }
154 log(prefix + line);
155 }
156 }
157 finally
158 {
159 instr.close();
160 }
161 }
162 }