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.IOException;
021import java.io.InputStream;
022import java.io.InputStreamReader;
023import java.nio.charset.Charset;
024import java.util.Date;
025
026import org.apache.commons.vfs2.FileContent;
027import org.apache.commons.vfs2.FileObject;
028import org.apache.tools.ant.BuildException;
029
030/**
031 * An Ant task that writes the details of a file to Ant's log.
032 */
033public class ShowFileTask extends VfsTask {
034
035    private static final String INDENT = "  ";
036    private String url;
037    private boolean showContent;
038    private boolean recursive;
039
040    /**
041     * Constructs a new instance.
042     */
043    public ShowFileTask() {
044        // empty
045    }
046
047    /**
048     * Executes the task.
049     *
050     * @throws BuildException if any exception is thrown.
051     */
052    @Override
053    public void execute() throws BuildException {
054        try {
055            try (FileObject file = resolveFile(url)) {
056                log("Details of " + file.getPublicURIString());
057                showFile(file, INDENT);
058            }
059        } catch (final Exception e) {
060            throw new BuildException(e);
061        }
062    }
063
064    /**
065     * Writes the content of the file to Ant log.
066     */
067    private void logContent(final FileObject file, final String prefix) throws IOException {
068        try (FileContent content = file.getContent();
069            InputStream instr = content.getInputStream();
070            BufferedReader reader = new BufferedReader(new InputStreamReader(instr, Charset.defaultCharset()))) {
071            while (true) {
072                final String line = reader.readLine();
073                if (line == null) {
074                    break;
075                }
076                log(prefix + line);
077            }
078        }
079    }
080
081    /**
082     * The URL of the file to display.
083     *
084     * @param url The url of the file.
085     */
086    public void setFile(final String url) {
087        this.url = url;
088    }
089
090    /**
091     * Recursively shows the descendants of the file.
092     *
093     * @param recursive true if descendants should be shown.
094     */
095    public void setRecursive(final boolean recursive) {
096        this.recursive = recursive;
097    }
098
099    /**
100     * Shows the content. Assumes the content is text, encoded using the platform's default encoding.
101     *
102     * @param showContent true if the content should be shown.
103     */
104    public void setShowContent(final boolean showContent) {
105        this.showContent = showContent;
106    }
107
108    /**
109     * Logs the details of a file.
110     */
111    private void showFile(final FileObject file, final String prefix) throws IOException {
112        // Write details
113        final StringBuilder msg = new StringBuilder(prefix);
114        msg.append(file.getName().getBaseName());
115        if (file.exists()) {
116            msg.append(" (");
117            msg.append(file.getType().getName());
118            msg.append(")");
119        } else {
120            msg.append(" (unknown)");
121        }
122        log(msg.toString());
123
124        if (file.exists()) {
125            final String newPrefix = prefix + INDENT;
126            if (file.getType().hasContent()) {
127                try (FileContent content = file.getContent()) {
128                    log(newPrefix + "Content-Length: " + content.getSize());
129                    log(newPrefix + "Last-Modified" + new Date(content.getLastModifiedTime()));
130                }
131                if (showContent) {
132                    log(newPrefix + "Content:");
133                    logContent(file, newPrefix);
134                }
135            }
136            if (file.getType().hasChildren()) {
137                final FileObject[] children = file.getChildren();
138                for (final FileObject child : children) {
139                    if (recursive) {
140                        showFile(child, newPrefix);
141                    } else {
142                        log(newPrefix + child.getName().getBaseName());
143                    }
144                }
145            }
146        }
147    }
148}