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.util.Date;
23  
24  import org.apache.commons.vfs2.FileContent;
25  import org.apache.commons.vfs2.FileObject;
26  import org.apache.tools.ant.BuildException;
27  
28  /**
29   * An Ant task that writes the details of a file to Ant's log.
30   */
31  public class ShowFileTask extends VfsTask {
32  
33      private static final String INDENT = "  ";
34      private String url;
35      private boolean showContent;
36      private boolean recursive;
37  
38      /**
39       * The URL of the file to display.
40       *
41       * @param url The url of the file.
42       */
43      public void setFile(final String url) {
44          this.url = url;
45      }
46  
47      /**
48       * Shows the content. Assumes the content is text, encoded using the platform's default encoding.
49       *
50       * @param showContent true if the content should be shown.
51       */
52      public void setShowContent(final boolean showContent) {
53          this.showContent = showContent;
54      }
55  
56      /**
57       * Recursively shows the descendants of the file.
58       *
59       * @param recursive true if descendants should be shown.
60       */
61      public void setRecursive(final boolean recursive) {
62          this.recursive = recursive;
63      }
64  
65      /**
66       * Executes the task.
67       *
68       * @throws BuildException if any exception is thrown.
69       */
70      @Override
71      public void execute() throws BuildException {
72          try {
73              final FileObject file = resolveFile(url);
74              log("Details of " + file.getPublicURIString());
75              showFile(file, INDENT);
76          } catch (final Exception e) {
77              throw new BuildException(e);
78          }
79      }
80  
81      /**
82       * Logs the details of a file.
83       */
84      private void showFile(final FileObject file, final String prefix) throws Exception {
85          // Write details
86          final StringBuilder msg = new StringBuilder(prefix);
87          msg.append(file.getName().getBaseName());
88          if (file.exists()) {
89              msg.append(" (");
90              msg.append(file.getType().getName());
91              msg.append(")");
92          } else {
93              msg.append(" (unknown)");
94          }
95          log(msg.toString());
96  
97          if (file.exists()) {
98              final String newPrefix = prefix + INDENT;
99              if (file.getType().hasContent()) {
100                 final FileContent content = file.getContent();
101                 log(newPrefix + "Content-Length: " + content.getSize());
102                 log(newPrefix + "Last-Modified" + new Date(content.getLastModifiedTime()));
103                 if (showContent) {
104                     log(newPrefix + "Content:");
105                     logContent(file, newPrefix);
106                 }
107             }
108             if (file.getType().hasChildren()) {
109                 final FileObject[] children = file.getChildren();
110                 for (final FileObject child : children) {
111                     if (recursive) {
112                         showFile(child, newPrefix);
113                     } else {
114                         log(newPrefix + child.getName().getBaseName());
115                     }
116                 }
117             }
118         }
119     }
120 
121     /**
122      * Writes the content of the file to Ant log.
123      */
124     private void logContent(final FileObject file, final String prefix) throws Exception {
125         try (
126             final InputStream instr = file.getContent().getInputStream();
127             final BufferedReader reader = new BufferedReader(new InputStreamReader(instr)); )
128         {
129             while (true) {
130                 final String line = reader.readLine();
131                 if (line == null) {
132                     break;
133                 }
134                 log(prefix + line);
135             }
136         }
137     }
138 }