1 16 package org.apache.commons.vfs.tasks; 17 18 import org.apache.commons.vfs.FileContent; 19 import org.apache.commons.vfs.FileObject; 20 import org.apache.tools.ant.BuildException; 21 22 import java.io.BufferedReader ; 23 import java.io.InputStream ; 24 import java.io.InputStreamReader ; 25 import java.util.Date ; 26 27 32 public class ShowFileTask 33 extends VfsTask 34 { 35 private String url; 36 private boolean showContent; 37 private boolean recursive; 38 private static final String INDENT = " "; 39 40 43 public void setFile(final String url) 44 { 45 this.url = url; 46 } 47 48 52 public void setShowContent(final boolean showContent) 53 { 54 this.showContent = showContent; 55 } 56 57 60 public void setRecursive(final boolean recursive) 61 { 62 this.recursive = recursive; 63 } 64 65 68 public void execute() throws BuildException 69 { 70 try 71 { 72 final FileObject file = resolveFile(url); 73 log("Details of " + file.getName().getURI()); 74 showFile(file, INDENT); 75 } 76 catch (final Exception e) 77 { 78 throw new BuildException(e); 79 } 80 } 81 82 85 private void showFile(final FileObject file, final String prefix) throws Exception 86 { 87 StringBuffer msg = new StringBuffer (prefix); 89 msg.append(file.getName().getBaseName()); 90 if (file.exists()) 91 { 92 msg.append(" ("); 93 msg.append(file.getType().getName()); 94 msg.append(")"); 95 } 96 else 97 { 98 msg.append(" (unknown)"); 99 } 100 log(msg.toString()); 101 102 if (file.exists()) 103 { 104 final String newPrefix = prefix + INDENT; 105 if (file.getType().hasContent()) 106 { 107 final FileContent content = file.getContent(); 108 log(newPrefix + "Content-Length: " + content.getSize()); 109 log(newPrefix + "Last-Modified" + new Date (content.getLastModifiedTime())); 110 if (showContent) 111 { 112 log(newPrefix + "Content:"); 113 logContent(file, newPrefix); 114 } 115 } 116 if (file.getType().hasChildren()) 117 { 118 final FileObject[] children = file.getChildren(); 119 for (int i = 0; i < children.length; i++) 120 { 121 FileObject child = children[i]; 122 if (recursive) 123 { 124 showFile(child, newPrefix); 125 } 126 else 127 { 128 log(newPrefix + child.getName().getBaseName()); 129 } 130 } 131 } 132 } 133 } 134 135 138 private void logContent(final FileObject file, final String prefix) 139 throws Exception 140 { 141 final InputStream instr = file.getContent().getInputStream(); 142 try 143 { 144 final BufferedReader reader = new BufferedReader (new InputStreamReader (instr)); 145 while (true) 146 { 147 final String line = reader.readLine(); 148 if (line == null) 149 { 150 break; 151 } 152 log(prefix + line); 153 } 154 } 155 finally 156 { 157 instr.close(); 158 } 159 } 160 } 161 | Popular Tags |