Package org.jboss.fresh.shell.commands

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

package org.jboss.fresh.shell.commands;

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

import java.io.PrintWriter;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.LinkedList;


// EX OK

public class LsLnsExe extends AbstractExecutable {
  private static transient org.apache.log4j.Logger log = org.apache.log4j.Logger.getLogger(LsLnsExe.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";

  /**
   Displays wanted info about the target file.
   */


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

    if (helpRequested()) {
      PrintWriter out = new PrintWriter(new BufferWriter(getStdOut()));
      out.print("  Usage: lslns [--help] [-out=string|filename|fileinfo] TARGET\n");
      out.print("    --help : this help\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;
    FileName target = null;
    Collection result = new HashSet();


    if (params.length <= 2) {
      for (int i = 0; i < params.length; i++) {
        String param = params[i];

        if (param.startsWith(OUTPUT))
          out = param.substring(OUTPUT.length());

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

          target = path;
        }
      }
    }

    if ((params.length >= 3) || (target == null)) {
      if (canThrowEx()) {
        throw new RuntimeException("Usage: lslns [-out=string|filename|fileinfo] TARGET");
      } else {
        PrintWriter pw = new PrintWriter(new BufferWriter(getStdOut()));
        pw.println("Usage: lslns [-out=string|filename|fileinfo] TARGET");
      }
    } else {
      Collection links = vfs.getLinks(shell.getUserCtx(), target, true);
      Iterator it = links.iterator();

      if (out.equals(OUTPUT_STRING)) {
        result = new LinkedList();

        while (it.hasNext()) {
          FileInfo info = (FileInfo) it.next();
          result.add(info.getFileName().toString() + "\n");
        }

      } else if (out.equals(OUTPUT_FILEINFO))
        result = links;

      else if (out.equals(OUTPUT_FILENAME)) {
        while (it.hasNext()) {
          FileInfo info = (FileInfo) it.next();
          result.add(info.getFileName());
        }
      }

      it = result.iterator();
      while (it.hasNext()) {
        Object obj = it.next();
        oout.writeObject(obj);
        System.out.println(obj);
      }
    }

    finish:
    oout.close();

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

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

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.