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.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          // Write details
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 }