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