001/* 002 * Licensed to the Apache Software Foundation (ASF) under one or more 003 * contributor license agreements. See the NOTICE file distributed with 004 * this work for additional information regarding copyright ownership. 005 * The ASF licenses this file to You under the Apache License, Version 2.0 006 * (the "License"); you may not use this file except in compliance with 007 * the License. You may obtain a copy of the License at 008 * 009 * http://www.apache.org/licenses/LICENSE-2.0 010 * 011 * Unless required by applicable law or agreed to in writing, software 012 * distributed under the License is distributed on an "AS IS" BASIS, 013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 014 * See the License for the specific language governing permissions and 015 * limitations under the License. 016 */ 017package org.apache.commons.vfs2.tasks; 018 019import java.io.BufferedReader; 020import java.io.InputStream; 021import java.io.InputStreamReader; 022import java.util.Date; 023 024import org.apache.commons.vfs2.FileContent; 025import org.apache.commons.vfs2.FileObject; 026import org.apache.tools.ant.BuildException; 027 028/** 029 * An Ant task that writes the details of a file to Ant's log. 030 */ 031public class ShowFileTask extends VfsTask { 032 private static final String INDENT = " "; 033 private String url; 034 private boolean showContent; 035 private boolean recursive; 036 037 /** 038 * The URL of the file to display. 039 * 040 * @param url The url of the file. 041 */ 042 public void setFile(final String url) { 043 this.url = url; 044 } 045 046 /** 047 * Shows the content. Assumes the content is text, encoded using the platform's default encoding. 048 * 049 * @param showContent true if the content should be shown. 050 */ 051 public void setShowContent(final boolean showContent) { 052 this.showContent = showContent; 053 } 054 055 /** 056 * Recursively shows the descendants of the file. 057 * 058 * @param recursive true if descendants should be shown. 059 */ 060 public void setRecursive(final boolean recursive) { 061 this.recursive = recursive; 062 } 063 064 /** 065 * Executes the task. 066 * 067 * @throws BuildException if any exception is thrown. 068 */ 069 @Override 070 public void execute() throws BuildException { 071 try { 072 final FileObject file = resolveFile(url); 073 log("Details of " + file.getPublicURIString()); 074 showFile(file, INDENT); 075 } catch (final Exception e) { 076 throw new BuildException(e); 077 } 078 } 079 080 /** 081 * Logs the details of a file. 082 */ 083 private void showFile(final FileObject file, final String prefix) throws Exception { 084 // Write details 085 final StringBuilder msg = new StringBuilder(prefix); 086 msg.append(file.getName().getBaseName()); 087 if (file.exists()) { 088 msg.append(" ("); 089 msg.append(file.getType().getName()); 090 msg.append(")"); 091 } else { 092 msg.append(" (unknown)"); 093 } 094 log(msg.toString()); 095 096 if (file.exists()) { 097 final String newPrefix = prefix + INDENT; 098 if (file.getType().hasContent()) { 099 final FileContent content = file.getContent(); 100 log(newPrefix + "Content-Length: " + content.getSize()); 101 log(newPrefix + "Last-Modified" + new Date(content.getLastModifiedTime())); 102 if (showContent) { 103 log(newPrefix + "Content:"); 104 logContent(file, newPrefix); 105 } 106 } 107 if (file.getType().hasChildren()) { 108 final FileObject[] children = file.getChildren(); 109 for (final FileObject child : children) { 110 if (recursive) { 111 showFile(child, newPrefix); 112 } else { 113 log(newPrefix + child.getName().getBaseName()); 114 } 115 } 116 } 117 } 118 } 119 120 /** 121 * Writes the content of the file to Ant log. 122 */ 123 private void logContent(final FileObject file, final String prefix) throws Exception { 124 final InputStream instr = file.getContent().getInputStream(); 125 try { 126 final BufferedReader reader = new BufferedReader(new InputStreamReader(instr)); 127 while (true) { 128 final String line = reader.readLine(); 129 if (line == null) { 130 break; 131 } 132 log(prefix + line); 133 } 134 } finally { 135 instr.close(); 136 } 137 } 138}