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: */
017: package org.apache.commons.vfs.tasks;
018:
019: import org.apache.commons.vfs.FileContent;
020: import org.apache.commons.vfs.FileObject;
021: import org.apache.tools.ant.BuildException;
022:
023: import java.io.BufferedReader;
024: import java.io.InputStream;
025: import java.io.InputStreamReader;
026: import java.util.Date;
027:
028: /**
029: * An Ant task that writes the details of a file to Ant's log.
030: *
031: * @author <a href="mailto:adammurdoch@apache.org">Adam Murdoch</a>
032: * @version $Revision: 480428 $ $Date: 2006-11-28 22:15:24 -0800 (Tue, 28 Nov 2006) $
033: */
034: public class ShowFileTask extends VfsTask {
035: private String url;
036: private boolean showContent;
037: private boolean recursive;
038: private static final String INDENT = " ";
039:
040: /**
041: * The URL of the file to display.
042: */
043: public void setFile(final String url) {
044: this .url = url;
045: }
046:
047: /**
048: * Shows the content. Assumes the content is text, encoded using the
049: * platform's default encoding.
050: */
051: public void setShowContent(final boolean showContent) {
052: this .showContent = showContent;
053: }
054:
055: /**
056: * Recursively shows the decendents of the file.
057: */
058: public void setRecursive(final boolean recursive) {
059: this .recursive = recursive;
060: }
061:
062: /**
063: * Executes the task.
064: */
065: public void execute() throws BuildException {
066: try {
067: final FileObject file = resolveFile(url);
068: log("Details of " + file.getName().getURI());
069: showFile(file, INDENT);
070: } catch (final Exception e) {
071: throw new BuildException(e);
072: }
073: }
074:
075: /**
076: * Logs the details of a file.
077: */
078: private void showFile(final FileObject file, final String prefix)
079: throws Exception {
080: // Write details
081: StringBuffer msg = new StringBuffer(prefix);
082: msg.append(file.getName().getBaseName());
083: if (file.exists()) {
084: msg.append(" (");
085: msg.append(file.getType().getName());
086: msg.append(")");
087: } else {
088: msg.append(" (unknown)");
089: }
090: log(msg.toString());
091:
092: if (file.exists()) {
093: final String newPrefix = prefix + INDENT;
094: if (file.getType().hasContent()) {
095: final FileContent content = file.getContent();
096: log(newPrefix + "Content-Length: " + content.getSize());
097: log(newPrefix + "Last-Modified"
098: + new Date(content.getLastModifiedTime()));
099: if (showContent) {
100: log(newPrefix + "Content:");
101: logContent(file, newPrefix);
102: }
103: }
104: if (file.getType().hasChildren()) {
105: final FileObject[] children = file.getChildren();
106: for (int i = 0; i < children.length; i++) {
107: FileObject child = children[i];
108: if (recursive) {
109: showFile(child, newPrefix);
110: } else {
111: log(newPrefix + child.getName().getBaseName());
112: }
113: }
114: }
115: }
116: }
117:
118: /**
119: * Writes the content of the file to Ant log.
120: */
121: private void logContent(final FileObject file, final String prefix)
122: throws Exception {
123: final InputStream instr = file.getContent().getInputStream();
124: try {
125: final BufferedReader reader = new BufferedReader(
126: 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: }
|