Package abstrasy.libraries.io

Source Code of abstrasy.libraries.io.External_FileSystemAdapter

package abstrasy.libraries.io;

/**
* Abstrasy Interpreter
*
* Copyright : Copyright (c) 2006-2012, Luc Bruninx.
*
* Concédée sous licence EUPL, version 1.1 uniquement (la «Licence»).
*
* Vous ne pouvez utiliser la présente oeuvre que conformément à la Licence.
* Vous pouvez obtenir une copie de la Licence à l’adresse suivante:
*
*   http://www.osor.eu/eupl
*
* Sauf obligation légale ou contractuelle écrite, le logiciel distribué sous
* la Licence est distribué "en l’état", SANS GARANTIES OU CONDITIONS QUELLES
* QU’ELLES SOIENT, expresses ou implicites.
*
* Consultez la Licence pour les autorisations et les restrictions
* linguistiques spécifiques relevant de la Licence.
*
*
* @author Luc Bruninx
* @version 1.0
*/


import abstrasy.Bivalence;
import abstrasy.Node;

import abstrasy.externals.AExtCachable;
import abstrasy.externals.AExtClonable;

import abstrasy.interpreter.InterpreterException;
import abstrasy.interpreter.ORef;
import abstrasy.interpreter.StdErrors;

import abstrasy.privates.Private_FilenameFilter;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;

public class External_FileSystemAdapter implements AExtClonable, AExtCachable {


  public External_FileSystemAdapter() {
  }

    private static final boolean isLink(File src) {
        if (src == null || !src.exists()) {
            return false;
        }
        try {
            return !src.getCanonicalFile().equals(src.getAbsoluteFile());
        }
        catch (IOException e) {
            return false;
        }
    }

  private static final boolean copyFile(String source, String target) {
    boolean error = false;
    FileInputStream srf = null;
    FileOutputStream mrf = null;
        File mf=new File(target);
    try {
            checkDir(mf.getParent());
            if(!mf.exists()){
                mf.createNewFile();
            }
            else{
                mf.delete();
                mf.createNewFile();
            }
      srf = new FileInputStream(source);
      mrf = new FileOutputStream(mf);
      byte[] sb = new byte[65536];
      while (true) {
        int stb = srf.read(sb);
        if (stb == -1) {
          break;
        }
        mrf.write(sb, 0, stb);
      }
      srf.close();
      mrf.close();
      //
    }
    catch (IOException e) {
            e.printStackTrace();
      error = true;
    }
    return error;
  }

  private static final void checkDir(String dir) {
    File fdir = new File(dir);
    if (!fdir.exists()) {
      fdir.mkdirs();
    }
  }

  private static final boolean copyDir(String srcDir, String tarDir) {
        //System.out.println("SrcDir="+srcDir);
        //System.out.println("tarDir:"+tarDir);
    boolean error = false;
    File src = new File(srcDir);
    if (!src.isFile()) {
            //System.out.println("Src="+src.getAbsoluteFile());
      String[] dirlst = src.list();
      for (int i = 0; i < dirlst.length; i++) {
                //System.out.println("Src1="+absDir(src) + dirlst[i]);
                //System.out.println("Tar1="+tarDir + File.separator + dirlst[i]);
        File tmp = new File(absDir(src) + dirlst[i]);
        File tartmp = new File(tarDir + File.separator + dirlst[i]);
        if (!tmp.isFile()) {
          //System.out.println(" -Copy Dir : " + tmp.getAbsolutePath() + " -> " + tartmp.getAbsolutePath());
          checkDir(tartmp.getAbsolutePath());
          error = copyDir(tmp.getAbsolutePath(), tartmp.getAbsolutePath());
          if (error) {
            i = dirlst.length;
          }
        }
        else {
          error = copyFile(tmp.getAbsolutePath(), tartmp.getAbsolutePath());
          if (error) {
            i = dirlst.length;
          }
        }
      }
    }
    else if (src.isFile()) {
      error = copyFile(srcDir, tarDir);
    }
    return error;
  }

  private static final String absDir(File src) {
    String dir = src.getAbsolutePath();
    if (dir.endsWith(".")) {
      dir = dir.substring(0, dir.length() - 1);
    }
    if (!dir.endsWith(File.separator)) {
      dir = dir.concat(File.separator);
    }
    if (!(new File(dir).isDirectory())) {
      dir = ".";
    }
    return dir;
  }

  public Node external_is_exists(Node startAt) throws Exception {
    startAt.isGoodArgsLength(true, 2);
    File f = new File(startAt.getSubNode(1, Node.TYPE_STRING).getString());
    return new Node(f.exists() ? Node.TRUE: Node.FALSE);
  }

  public Node external_is_can_read(Node startAt) throws Exception {
    startAt.isGoodArgsLength(true, 2);
    File f = new File(startAt.getSubNode(1, Node.TYPE_STRING).getString());
        return new Node(f.canRead() ? Node.TRUE: Node.FALSE);
  }

  public Node external_is_can_write(Node startAt) throws Exception {
    startAt.isGoodArgsLength(true, 2);
    File f = new File(startAt.getSubNode(1, Node.TYPE_STRING).getString());
        return new Node(f.canWrite() ? Node.TRUE: Node.FALSE);
  }

  public Node external_is_file(Node startAt) throws Exception {
    startAt.isGoodArgsLength(true, 2);
    File f = new File(startAt.getSubNode(1, Node.TYPE_STRING).getString());
        return new Node(f.isFile() ? Node.TRUE: Node.FALSE);
  }

  public Node external_is_directory(Node startAt) throws Exception {
    startAt.isGoodArgsLength(true, 2);
    File f = new File(startAt.getSubNode(1, Node.TYPE_STRING).getString());
        return new Node(f.isDirectory() ? Node.TRUE: Node.FALSE);
  }

    public Node external_is_link(Node startAt) throws Exception {
    startAt.isGoodArgsLength(true, 2);
    File f = new File(startAt.getSubNode(1, Node.TYPE_STRING).getString());
    return new Node(isLink(f) ? Node.TRUE: Node.FALSE);
  }
   
  public Node external_is_hidden(Node startAt) throws Exception {
    startAt.isGoodArgsLength(true, 2);
    File f = new File(startAt.getSubNode(1, Node.TYPE_STRING).getString());
        return new Node(f.isHidden() ? Node.TRUE: Node.FALSE);
  }

  public Node external_file_size(Node startAt) throws Exception {
    startAt.isGoodArgsLength(true, 2);
    File f = new File(startAt.getSubNode(1, Node.TYPE_STRING).getString());
    return new Node(f.length());
  }

  public Node external_last_modified(Node startAt) throws Exception {
    startAt.isGoodArgsLength(true, 2);
    File f = new File(startAt.getSubNode(1, Node.TYPE_STRING).getString());
    return new Node(f.lastModified());
  }

  public Node external_file_name(Node startAt) throws Exception {
    startAt.isGoodArgsLength(true, 2);
    File f = new File(startAt.getSubNode(1, Node.TYPE_STRING).getString());
    return new Node(f.getName());
  }

  public Node external_file_path(Node startAt) throws Exception {
    startAt.isGoodArgsLength(true, 2);
    File f = new File(startAt.getSubNode(1, Node.TYPE_STRING).getString());
    return new Node(f.getParent());
  }


  public Node external_is_match(Node startAt) throws Exception {
    startAt.isGoodArgsLength(true, 3);
    File f = new File(startAt.getSubNode(1, Node.TYPE_STRING).getString());
    Private_FilenameFilter filefilter = new Private_FilenameFilter();
    filefilter.setMaskList(startAt.getSubNode(2, Node.TYPE_CLIST));
        return new Node(filefilter.accept(f) ? Node.TRUE: Node.FALSE);
  }

  public Node external_separator(Node startAt) throws Exception {
    startAt.isGoodArgsLength(true, 1);
    return new Node(File.separator);
  }

  public Node external_list_roots(Node startAt) throws Exception {
    startAt.isGoodArgsLength(true, 1);
    File[] roots = File.listRoots();
        Node res = Node.createCList();
    for (int i = 0; i < roots.length; i++) {
      res.addElement(new Node(roots[i].getAbsolutePath()));
    }
    return res;
  }


  public Node external_list_files(Node startAt) throws Exception {
    startAt.isGoodArgsLength(false, 2);
    if (startAt.size() > 3) {
      throw new InterpreterException(StdErrors.Argument_count_mismatch);
    }
    Private_FilenameFilter filefilter = null;
    File f = new File(startAt.getSubNode(1, Node.TYPE_STRING).getString());
    if (startAt.size() == 3) {
      filefilter = new Private_FilenameFilter();
      filefilter.setMaskList(startAt.getSubNode(2, Node.TYPE_CLIST));
    }
        Node res = Node.createCList();
    File[] fl;
    if (filefilter == null) {
      fl = f.listFiles();
    }
    else {
      fl = f.listFiles(filefilter);
    }
    for (int i = 0; i < fl.length; i++) {
      res.addElement(new Node(fl[i].getName()));
    }
    return res;
  }

    private static void _walk_files(File f, Node fx) throws Exception {
        File[] fl;
    fl = f.listFiles();
    for (int i = 0; i < fl.length; i++) {
      if (fl[i].isDirectory() && fl[i].getCanonicalPath().equals(fl[i].getAbsolutePath())) {
                _walk_files(fl[i],fx);
            }
            else{
                Node expr = Node.createExpr();
                expr.addElement(fx);
                expr.addElement(new Node(fl[i].getAbsolutePath()));
                expr.exec(true);
            }
        }
    }
   
  public Node external_walk_files(Node startAt) throws Exception {
    startAt.isGoodArgsCnt(3);
    File f = new File(startAt.getSubNode(1, Node.TYPE_STRING).getString());
        Node lambda = startAt.getSubNode(2, Node.TYPE_FUNCTION);
    _walk_files(f,lambda);
    return null;
  }

  public Node external_user_path(Node startAt) throws Exception {
    startAt.isGoodArgsLength(true, 1);
    return new Node(absDir(new File(System.getProperty("user.dir", "."))));
  }

  public Node external_home_path(Node startAt) throws Exception {
    startAt.isGoodArgsLength(true, 1);
    return new Node(absDir(new File(System.getProperty("user.home", "."))));
  }

  public Node external_delete(Node startAt) throws Exception {
    startAt.isGoodArgsLength(true, 2);
    File f = new File(startAt.getSubNode(1, Node.TYPE_STRING).getString());
    return new Node((f.delete()) ? Node.TRUE: Node.FALSE);
  }

  public Node external_move(Node startAt) throws Exception {
    startAt.isGoodArgsLength(true, 3);
    File f = new File(startAt.getSubNode(1, Node.TYPE_STRING).getString());
    File f2 = new File(startAt.getSubNode(2, Node.TYPE_STRING).getString());
    return new Node((f.renameTo(f2)) ? Node.TRUE: Node.FALSE);
  }

  public Node external_copy_file(Node startAt) throws Exception {
    startAt.isGoodArgsCnt(3);
    File f = new File(startAt.getSubNode(1, Node.TYPE_STRING).getString());
    File f2 = new File(startAt.getSubNode(2, Node.TYPE_STRING).getString());
    return new Node((External_FileSystemAdapter.copyFile(f.getAbsolutePath(), f2.getAbsolutePath())) ? Node.FALSE: Node.TRUE);
  }

  public Node external_copy_directory(Node startAt) throws Exception {
    startAt.isGoodArgsCnt(3);
    File f = new File(startAt.getSubNode(1, Node.TYPE_STRING).getString());
    File f2 = new File(startAt.getSubNode(2, Node.TYPE_STRING).getString());
    return new Node((External_FileSystemAdapter.copyDir(f.getAbsolutePath(), f2.getAbsolutePath())) ? Node.FALSE: Node.TRUE);
  }

  public Node external_make_directory(Node startAt) throws Exception {
    startAt.isGoodArgsCnt(2);
    File f = new File(startAt.getSubNode(1, Node.TYPE_STRING).getString());
    return new Node((f.mkdirs()) ? Node.TRUE: Node.FALSE);
  }

  public Node external_toURL(Node startAt) throws Exception {
    startAt.isGoodArgsCnt(2);
    File f = new File(startAt.getSubNode(1, Node.TYPE_STRING).getString());
    return new Node(f.toURI().toURL().toString());
  }
   
    public Object clone_my_self(Bivalence bival) {
    return this;
  }

    /*
     * ----------------------------------------------------------------------------
     *
     * Optimisation par cache d'instanciantion (mars 2012) rev 1.0-6321.0
     *
     * ----------------------------------------------------------------------------
     */


    private static ORef _optim_symbols_cache_ = new ORef();
   
    @Override
    public Object getSymbolsCache() {
        return _optim_symbols_cache_.getRef();
    }

    @Override
    public void setSymbolsCache(Object cache) {
        _optim_symbols_cache_.setRef(cache);
    }
}
TOP

Related Classes of abstrasy.libraries.io.External_FileSystemAdapter

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.