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              try (final FileObject file = resolveFile(url)) {
74                  log("Details of " + file.getPublicURIString());
75                  showFile(file, INDENT);
76              }
77          } catch (final Exception e) {
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 prefix) throws Exception {
86          // Write details
87          final StringBuilder msg = new StringBuilder(prefix);
88          msg.append(file.getName().getBaseName());
89          if (file.exists()) {
90              msg.append(" (");
91              msg.append(file.getType().getName());
92              msg.append(")");
93          } else {
94              msg.append(" (unknown)");
95          }
96          log(msg.toString());
97  
98          if (file.exists()) {
99              final String newPrefix = prefix + INDENT;
100             if (file.getType().hasContent()) {
101                 try (final FileContent content = file.getContent()) {
102                     log(newPrefix + "Content-Length: " + content.getSize());
103                     log(newPrefix + "Last-Modified" + new Date(content.getLastModifiedTime()));
104                 }
105                 if (showContent) {
106                     log(newPrefix + "Content:");
107                     logContent(file, newPrefix);
108                 }
109             }
110             if (file.getType().hasChildren()) {
111                 final FileObject[] children = file.getChildren();
112                 for (final FileObject child : children) {
113                     if (recursive) {
114                         showFile(child, newPrefix);
115                     } else {
116                         log(newPrefix + child.getName().getBaseName());
117                     }
118                 }
119             }
120         }
121     }
122 
123     /**
124      * Writes the content of the file to Ant log.
125      */
126     private void logContent(final FileObject file, final String prefix) throws Exception {
127         try (
128             final InputStream instr = file.getContent().getInputStream();
129             final BufferedReader reader = new BufferedReader(new InputStreamReader(instr)); )
130         {
131             while (true) {
132                 final String line = reader.readLine();
133                 if (line == null) {
134                     break;
135                 }
136                 log(prefix + line);
137             }
138         }
139     }
140 }