Package org.jboss.fresh.shell.commands

Source Code of org.jboss.fresh.shell.commands.LsExe

package org.jboss.fresh.shell.commands;

import org.jboss.fresh.io.BufferObjectWriter;
import org.jboss.fresh.io.BufferWriter;
import org.jboss.fresh.shell.AbstractExecutable;
import org.jboss.fresh.util.StringUtils;
import org.jboss.fresh.vfs.FileInfo;
import org.jboss.fresh.vfs.FileName;
import org.jboss.fresh.vfs.VFS;

import java.io.PrintWriter;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;


// EX OK

public class LsExe extends AbstractExecutable {
    private static transient org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(LsExe.class);

    public static final String OUTPUT = "-out=";
    public static final String OUTPUT_STRING = "string";
    public static final String OUTPUT_FILENAME = "filename";
    public static final String OUTPUT_FILEINFO = "fileinfo";

    public static final String OPTION_LONG = "-l";

    private static Integer pathLen = new Integer(34);
    private static Integer modifiedLen = new Integer(16);
    private static Integer typeLen = new Integer(5);
    /*
     private static Integer lenghtLen = new Integer(10);
     private static Integer linkLen = new Integer(20);
    */

    /**
     * Displays the contents of the specified directory.
     */

    public void process(String exename, String[] params) throws Exception {
        log.debug("entered");

        if (helpRequested()) {
            PrintWriter out = new PrintWriter(new BufferWriter(getStdOut()));
            out.print("Usage: ls [--help] [-ex] [-out=OUTPUT] [-l] [--help]\n");
            out.print("       OUTPUT = string | filename | fileinfo; String is default.\n");
            out.print("       -l = long output; Makes difference only with -out=string.\n");
            out.print("       --help = Prints this.\n");
            out.close();
            log.debug("done");
            return;
        }

        BufferObjectWriter oout = new BufferObjectWriter(getStdOut());
        VFS vfs = shell.getVFS();

        FileName pwd = new FileName(shell.getEnvProperty("PWD"));

        String out = OUTPUT_STRING;
        boolean output = false;        // was out param already processed
        boolean longOut = false;
        boolean rslvLinks = true;
        // boolean help = false;

        List paths = new LinkedList();

        for (int i = 0; i < params.length; i++) {

            String param = params[i];

            if (!longOut && param.equals(OPTION_LONG))
                longOut = true;

            else if (!output && param.startsWith(OUTPUT)) {
                out = param.substring(OUTPUT.length());
                output = true;

            }
/*       else if (param.equals(HELP)) {
        help = true;
        oout.writeObject("Usage: ls [--help] [-ex] [-out=OUTPUT] [-l] [--help]\n");
        oout.writeObject("       OUTPUT = string | filename | fileinfo; String is default.\n");
        oout.writeObject("       -l = long output; Makes difference only with -out=string.\n");
        oout.writeObject("       --help = Prints this.\n");
        oout.close();
        System.out.println("[LsExe] : done.");
        return;

      }
*/
            else {
                FileName path = new FileName(param);
                if (path.isRelative())
                    path = pwd.absolutize(path);

//        path = vfs.resolve(shell.getUserCtx(), path, true);
                paths.add(path);
            }
        }

        // if no paths given, list working dir
        if (paths.isEmpty())
            paths.add(pwd);


        // now we have all paths resolved and absolute in FileName form in the 'paths' list
        //log.debug("Paths: " + paths);


        List expPaths = new LinkedList();
        Iterator it = paths.iterator();
        while (it.hasNext()) {
            FileName filename = (FileName) it.next();
            List children = vfs.list(shell.getUserCtx(), filename, !rslvLinks);
            //log.debug("Expanding children: " + children);
            expPaths.addAll(children);
        }
        paths = expPaths;

        //log.debug("Paths after expanding: " + paths);

        //log.debug("long output = " +longOut);
        //log.debug("output = " +out);
        //log.debug("pwd = " +pwd);
        //log.debug("paths = ");
        //StringUtils.printCollection(paths);

        List result = null;
        if (out.equals(OUTPUT_STRING)) {
            result = new LinkedList();
            it = paths.iterator();

            while (it.hasNext()) {
                FileInfo info = (FileInfo) it.next();

                if (longOut) {
                    List fields, lengths;
                    fields = new LinkedList();
                    lengths = new LinkedList();

                    // field pathname
                    fields.add(info.getFileName().toString());
                    lengths.add(pathLen);

                    // last modified
                    fields.add(info.isDirectory() ? "DIR" : info.isLink() ? "LINK" : "FILE");
                    lengths.add(typeLen);

                    // modified
                    fields.add(info.getLastModified().toString());
                    lengths.add(modifiedLen);


                    if (info.isLink()) {
                        // link
                        fields.add(info.getTarget().toString());
                        lengths.add(new Integer(0));

                    } else {
                        // length
                        fields.add(new Long(info.getLength()).toString());
                        lengths.add(new Integer(0));
                    }

//          System.out.println("Adding to results (1): " + StringUtils.row(fields, lengths)+"\n");
                    result.add(StringUtils.row(fields, lengths) + "\r\n");

                } else {
//          System.out.println("Adding to results (2) info: " + info);
//          System.out.println("Adding to results (2) info.FileName: " + info.getFileName());
//          System.out.println("Adding to results (2): " + info.getFileName().toString() + "\n");
                    result.add(info.getFileName().toString() + "\r\n");
                }
            }

        } else if (out.equals(OUTPUT_FILEINFO)) {
//      System.out.println("Setting paths as result: " + paths);
            result = paths;

        } else if (out.equals(OUTPUT_FILENAME)) {
//      System.out.println("Iterating paths and replacing infos with names: " + paths);
            result = new LinkedList();
            it = paths.iterator();

            while (it.hasNext()) {
                FileInfo info = (FileInfo) it.next();
                result.add(info.getFileName());
            }
        }

        it = result.iterator();
        while (it.hasNext()) {
            Object obj = it.next();
//      System.out.println("Writing to out: ");
//      System.out.println(obj);
            oout.writeObject(obj);

        }


        finish:
        oout.close();

        log.debug("done");
  }
}
TOP

Related Classes of org.jboss.fresh.shell.commands.LsExe

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.