Package org.jboss.fresh.shell.impl

Source Code of org.jboss.fresh.shell.impl.FPExpand

//UNIX VARIANT
package org.jboss.fresh.shell.impl;

import org.jboss.fresh.vfs.FileInfo;
import org.jboss.fresh.vfs.FileName;
import org.jboss.fresh.shell.Shell;
import org.jboss.fresh.shell.ShellException;
import gnu.regexp.RE;
import gnu.regexp.REException;

import java.util.Iterator;
import java.util.List;
import java.util.Vector;

import org.apache.log4j.Logger;


public class FPExpand {
  private static final Logger log = Logger.getLogger(FPExpand.class);
  FileName user_dir;
  Vector matches = new Vector();
  Shell sh;

//EMPTY CONSTRUCTOR
  public FPExpand(Shell shell) throws ShellException {
    sh = shell;
    user_dir = new FileName(shell.getEnvProperty("PWD"));
  }

//CONSTRUCTOR WITH USER DIRECTORY
  public FPExpand(Shell shell, String ud) {
    sh = shell;
    user_dir = new FileName(ud);
  }

//GETTER METHOD MATCHES AND REPLACE ALL \t WITH .
  public Vector getMatches() {
    return matches;
  }

//GETTER USER_DIRECTORY
  public String getUserDir() {
    return user_dir.toString();
  }

//SIMPLE MATCHING OF LIST OF STRINGS AND PATTERN
  public Vector applyPattern(String pattern, List fileInfos) {
    Vector v = new Vector();
    try {
//log.debug("applyPat: pattern: " + pattern);
      RE re = new RE(pattern);
      String temp = null;

      Iterator it = fileInfos.iterator();
      while (it.hasNext()) {
        FileInfo finf = (FileInfo) it.next();
//log.debug("applyPat: candidate: " + finf.getFileName().getName());
        temp = finf.getFileName().getName().toString().replace('.', '\t');
        temp = temp.replace('-', '\n');

        if (re.isMatch(temp)) {
          v.add(finf);
//log.debug("Match! " + finf.getFileName().getName());
        }
      }
    } catch (REException e) {
      log.error(e.getMessage(),e);
//log.debug("You're pattern isn't well-formed");
    }

    return v;
  }

//POSSIBLE CASES :
//         a) PATTERN STARTS WITH ./
//         b) PATTERN STARTS WITH ../
//         c) PATTERN STARTS WITH /  (IN TERMS OF UNIX FS THIS IS ROOT DIRECTORY)
  public void expandPattern(String pattern, FileName dir) throws Exception {

    int second;
    List listing;
//log.debug("Usao sam u expandPattern i pattern je "+pattern+", a dir je "+dir);
    if (pattern.startsWith("\t/")) {
//log.debug("Pattern starts with \\t/");
      expandPattern(pattern.substring(2, pattern.length()), dir);
    } else if (pattern.startsWith("\t\t/")) {
//log.debug("Pattern starts with \\t\\t/");
      if (dir == null) {
// FILE : get parent of a file represented by string
        dir = user_dir.getPath();
        if (dir == null) throw new Exception("You can't go above root");
      } else {
// FILE : get parent of a file represented by string
        dir = dir.getPath();
        if (dir == null) throw new Exception("You can't go above root");
      }
      expandPattern(pattern.substring(3, pattern.length()), dir);
    } else {
//log.debug(">>> Nemam \\t tabulator  ");
      if (pattern.startsWith("/")) {
        second = pattern.indexOf("/", 1);
      } else {
        second = pattern.indexOf("/", 0);
      }

      Vector temp = new Vector();
      if (dir == null) {
        dir = user_dir;
      }

//log.debug("dir je sada "+dir+", a second je "+second);
// FILE : list contents of a dir
      listing = sh.getVFS().list(sh.getUserCtx(), dir, false);
//      System.out.println("list() results: " + listing);
/*
      List list = sh.getVFS().list(dir);
      Map nameFInf=new HashMap();
      Iterator it=list.iterator();
      while(it.hasNext()) {
        FileInfo inf=(FileInfo)it.next();
        nameFInf.put(inf.getFileName(), inf);
      }
*/

      if (second != -1) {
//log.debug("Second != -1 NOT FINAL");
        if (pattern.startsWith("/")) {
          temp = applyPattern(pattern.substring(1, second), listing);
        } else {
          temp = applyPattern(pattern.substring(0, second), listing);
        }

        if (temp.isEmpty()) {
//log.debug("Results is empty.");
          return;
        } else {
/*
          Map nameFInf=new HashMap();
          Iterator it=temp.iterator();
          while(it.hasNext()) {
            FileInfo inf=(FileInfo)it.next();
            nameFInf.put(inf.getFileName(), inf);
          }
*/
          Iterator it = temp.iterator();
          while (it.hasNext()) {

            FileInfo cur = (FileInfo) it.next();

//log.debug("isDir: " + cur.getFileName().toString() + "   :  " + cur.isDirectory());
// FILE : is file directory
// shit tule moramo cel listing dobiti in potem skozenj preverjati.
// Kaj moramo je pogledati ali je ta re� direktorij
// to je najla�je tako, da jo poskusimo najti v listingu in pokli�emo isDirectory()

            if (cur.isDirectory())
              expandPattern(pattern.substring(second + 1, pattern.length()), cur.getFileName());
// FILE : is file directory
          }
        }
      } else {
//log.debug("Second == -1 FINAL");
        if (pattern.startsWith("/")) {
          temp = applyPattern(pattern.substring(1, pattern.length()), listing);
        } else {
          temp = applyPattern(pattern, listing);
        }

//log.debug("Packing results.");
        if (!temp.isEmpty()) {
          Iterator it = temp.iterator();
          while (it.hasNext()) {
            matches.add(it.next());
//log.debug(matches.get(matches.size()-1));
          }
        }
//log.debug("Done packing results results.");
        return;
      }

    }
  }


//CHANGE OF ALL ? AND . IN TERMS OF REG_EXP AND ./
  public String convertPattern(String p) {
    String change = "[A-Za-z0-9\t#%^+=\n@]*";

    p = p.replace('.', '\t');
    p = p.replace('-', '\n');
    p = p.replace('?', '.');

    StringBuffer b = new StringBuffer();
    int k = 0;
    int i = p.indexOf('*', k);

    while (i != -1) {
      b.append(p.substring(k, i)).append(change);
      k = i + 1;
      i = p.indexOf('*', k);
    }

    b.append(p.substring(k, p.length()));
    p = b.toString();


    return p;

  }


  public void expand(String pattern) throws Exception {
    if (pattern.startsWith("/")) {
      pattern = convertPattern(pattern);
//log.debug("pattern je poceo sa / i posle convert je "+pattern);
      expandPattern(pattern, new FileName("/"));
    } else {
      pattern = convertPattern(pattern);
//log.debug("pattern je nema pocetak ili je poceo sa . i posle convert je "+pattern);
      expandPattern(pattern, null);
    }
  }

/*
  public static void main(String[] args) throws Exception
  {
    if (args.length != 1)
      System.out.println("You have to call this program with wildcard pattern as the only argument");

//log.debug("args[0] je "+args[0]);
    FPExpand fpe = new FPExpand();
    fpe.expand(args[0]);

    Vector v = fpe.getMatches();

System.out.println("\n");
System.out.println("------------------------ Official matches ---------------------------");
           Iterator it=v.iterator();
           while(it.hasNext()) {
             System.out.println(it.next());
           }

  }
*/
}

 
TOP

Related Classes of org.jboss.fresh.shell.impl.FPExpand

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.