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.example;
018
019import java.text.DateFormat;
020import java.util.Date;
021
022import org.apache.commons.vfs2.FileObject;
023import org.apache.commons.vfs2.FileSystemException;
024import org.apache.commons.vfs2.FileSystemManager;
025import org.apache.commons.vfs2.FileType;
026import org.apache.commons.vfs2.VFS;
027
028/**
029 * Example which prints all properties of the file passed as first parameter.
030 */
031public final class ShowProperties {
032    /** Maximum number of children to show. */
033    private static final int SHOW_MAX = 5;
034
035    /**
036     * Invokes this example from the command line.
037     *
038     * @param args Arguments TODO
039     */
040    public static void main(final String[] args) {
041        if (args.length == 0) {
042            System.err.println("Please pass the name of a file as parameter.");
043            System.err.println("e.g. java org.apache.commons.vfs2.example.ShowProperties LICENSE.txt");
044            return;
045        }
046        for (final String arg : args) {
047            try {
048                final FileSystemManager mgr = VFS.getManager();
049                System.out.println();
050                System.out.println("Parsing: " + arg);
051                final FileObject file = mgr.resolveFile(arg);
052                System.out.println("URL: " + file.getURL());
053                System.out.println("getName(): " + file.getName());
054                System.out.println("BaseName: " + file.getName().getBaseName());
055                System.out.println("Extension: " + file.getName().getExtension());
056                System.out.println("Path: " + file.getName().getPath());
057                System.out.println("Scheme: " + file.getName().getScheme());
058                System.out.println("URI: " + file.getName().getURI());
059                System.out.println("Root URI: " + file.getName().getRootURI());
060                System.out.println("Parent: " + file.getName().getParent());
061                System.out.println("Type: " + file.getType());
062                System.out.println("Exists: " + file.exists());
063                System.out.println("Readable: " + file.isReadable());
064                System.out.println("Writeable: " + file.isWriteable());
065                System.out.println("Root path: " + file.getFileSystem().getRoot().getName().getPath());
066                if (file.exists()) {
067                    if (file.getType().equals(FileType.FILE)) {
068                        System.out.println("Size: " + file.getContent().getSize() + " bytes");
069                    } else if (file.getType().equals(FileType.FOLDER) && file.isReadable()) {
070                        final FileObject[] children = file.getChildren();
071                        System.out.println("Directory with " + children.length + " files");
072                        for (int iterChildren = 0; iterChildren < children.length; iterChildren++) {
073                            System.out.println("#" + iterChildren + ": " + children[iterChildren].getName());
074                            if (iterChildren > SHOW_MAX) {
075                                break;
076                            }
077                        }
078                    }
079                    System.out.println("Last modified: " + DateFormat.getInstance().format(new Date(file.getContent().getLastModifiedTime())));
080                } else {
081                    System.out.println("The file does not exist");
082                }
083                file.close();
084            } catch (final FileSystemException ex) {
085                ex.printStackTrace();
086            }
087        }
088    }
089
090    private ShowProperties() {
091        /* main class not instantiated. */
092    }
093
094}