1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.vfs2.tasks;
18
19 import java.io.BufferedReader;
20 import java.io.InputStream;
21 import java.io.InputStreamReader;
22 import java.nio.charset.Charset;
23 import java.util.Date;
24
25 import org.apache.commons.vfs2.FileContent;
26 import org.apache.commons.vfs2.FileObject;
27 import org.apache.tools.ant.BuildException;
28
29
30
31
32 public class ShowFileTask extends VfsTask {
33
34 private static final String INDENT = " ";
35 private String url;
36 private boolean showContent;
37 private boolean recursive;
38
39
40
41
42
43
44 public void setFile(final String url) {
45 this.url = url;
46 }
47
48
49
50
51
52
53 public void setShowContent(final boolean showContent) {
54 this.showContent = showContent;
55 }
56
57
58
59
60
61
62 public void setRecursive(final boolean recursive) {
63 this.recursive = recursive;
64 }
65
66
67
68
69
70
71 @Override
72 public void execute() throws BuildException {
73 try {
74 try (final FileObject file = resolveFile(url)) {
75 log("Details of " + file.getPublicURIString());
76 showFile(file, INDENT);
77 }
78 } catch (final Exception e) {
79 throw new BuildException(e);
80 }
81 }
82
83
84
85
86 private void showFile(final FileObject file, final String prefix) throws Exception {
87
88 final StringBuilder msg = new StringBuilder(prefix);
89 msg.append(file.getName().getBaseName());
90 if (file.exists()) {
91 msg.append(" (");
92 msg.append(file.getType().getName());
93 msg.append(")");
94 } else {
95 msg.append(" (unknown)");
96 }
97 log(msg.toString());
98
99 if (file.exists()) {
100 final String newPrefix = prefix + INDENT;
101 if (file.getType().hasContent()) {
102 try (final FileContent content = file.getContent()) {
103 log(newPrefix + "Content-Length: " + content.getSize());
104 log(newPrefix + "Last-Modified" + new Date(content.getLastModifiedTime()));
105 }
106 if (showContent) {
107 log(newPrefix + "Content:");
108 logContent(file, newPrefix);
109 }
110 }
111 if (file.getType().hasChildren()) {
112 final FileObject[] children = file.getChildren();
113 for (final FileObject child : children) {
114 if (recursive) {
115 showFile(child, newPrefix);
116 } else {
117 log(newPrefix + child.getName().getBaseName());
118 }
119 }
120 }
121 }
122 }
123
124
125
126
127 private void logContent(final FileObject file, final String prefix) throws Exception {
128 try (final FileContent content = file.getContent();
129 final InputStream instr = content.getInputStream();
130 final BufferedReader reader = new BufferedReader(new InputStreamReader(instr, Charset.defaultCharset()))) {
131 while (true) {
132 final String line = reader.readLine();
133 if (line == null) {
134 break;
135 }
136 log(prefix + line);
137 }
138 }
139 }
140 }