View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * An Ant task that writes the details of a file to Ant's log.
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       * The URL of the file to display.
41       *
42       * @param url The url of the file.
43       */
44      public void setFile(final String url) {
45          this.url = url;
46      }
47  
48      /**
49       * Shows the content. Assumes the content is text, encoded using the platform's default encoding.
50       *
51       * @param showContent true if the content should be shown.
52       */
53      public void setShowContent(final boolean showContent) {
54          this.showContent = showContent;
55      }
56  
57      /**
58       * Recursively shows the descendants of the file.
59       *
60       * @param recursive true if descendants should be shown.
61       */
62      public void setRecursive(final boolean recursive) {
63          this.recursive = recursive;
64      }
65  
66      /**
67       * Executes the task.
68       *
69       * @throws BuildException if any exception is thrown.
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       * Logs the details of a file.
85       */
86      private void showFile(final FileObject file, final String prefix) throws Exception {
87          // Write details
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      * Writes the content of the file to Ant log.
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 }