egovframework.rte.fdl.filehandling.EgovFileUtil.java Source code

Java tutorial

Introduction

Here is the source code for egovframework.rte.fdl.filehandling.EgovFileUtil.java

Source

/*
 * Copyright 2008-2009 MOPAS(Ministry of Public Administration and Security).
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package egovframework.rte.fdl.filehandling;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.commons.vfs.FileContent;
import org.apache.commons.vfs.FileName;
import org.apache.commons.vfs.FileObject;
import org.apache.commons.vfs.FileSystemException;
import org.apache.commons.vfs.FileSystemManager;
import org.apache.commons.vfs.FileType;
import org.apache.commons.vfs.Selectors;
import org.apache.commons.vfs.VFS;
import org.apache.regexp.RE;

import egovframework.rte.fdl.string.EgovStringUtil;

/**
 * ? ?   ?
 * <p>
 * <b>NOTE:</b> ?     ??.
 * @author   
 * @since 2009.06.01
 * @version 1.0
 * @see <pre>
 *  == ?(Modification Information) ==
 *   
 *   ?      ?           
 *  -------    --------    ---------------------------
 *   2009.06.01              ?
 * 
 * </pre>
 */
public class EgovFileUtil {

    /**
     * <p>
     * ? ? ? ?    Log ?
     * </p>
     */

    private static final Log log = LogFactory.getLog(EgovFileUtil.class);

    private static FileObject basefile;
    private static FileSystemManager manager;

    static {
        try {
            manager = VFS.getManager();
            basefile = manager.resolveFile(System.getProperty("user.dir"));
        } catch (FileSystemException e) {
            log.error("EgovFileUtil : " + e.getMessage());
        }
    }

    /**
     * <p>
     *  ? ? ?  .
     * </p>
     * @param cmd
     *        <code>String</code>
     * @return  
     * @throws FileSystemException
     */
    public static int rm(final String cmd) throws FileSystemException {
        int result = -1;

        try {
            final FileObject file = manager.resolveFile(basefile, cmd);

            result = file.delete(Selectors.SELECT_SELF_AND_CHILDREN);

            log.debug("result is " + result);

        } catch (FileSystemException e) {
            log.error(e.toString());
            throw new FileSystemException(e);
        }

        return result;
    }

    /**
     * <p>
     *  ? ?? ?  .
     * </p>
     * @param source
     *        <code>String</code>
     * @param target
     *        <code>String</code>
     * @throws Exception
     */
    public static void cp(String source, String target) throws Exception {

        try {
            final FileObject src = manager.resolveFile(basefile, source);
            FileObject dest = manager.resolveFile(basefile, target);

            if (dest.exists() && dest.getType() == FileType.FOLDER) {
                dest = dest.resolveFile(src.getName().getBaseName());
            }

            dest.copyFrom(src, Selectors.SELECT_ALL);
        } catch (FileSystemException fse) {
            log.error(fse.toString());
            ;
            throw new FileSystemException(fse);
        }
    }

    /**
     * <p>
     *  ? ?? ?  ??.
     * </p>
     * @param source
     *        <code>String</code>
     * @param target
     *        <code>String</code>
     * @throws Exception
     */
    public static void mv(String source, String target) throws Exception {

        try {
            final FileObject src = manager.resolveFile(basefile, source);
            FileObject dest = manager.resolveFile(basefile, target);

            if (dest.exists() && dest.getType() == FileType.FOLDER) {
                dest = dest.resolveFile(src.getName().getBaseName());
            }

            src.moveTo(dest);
        } catch (FileSystemException fse) {
            log.error(fse.toString());
            ;
            throw new FileSystemException(fse);
        }
    }

    /**
     * <p>
     *   .
     * </p>
     * @return  
     */
    public static FileName pwd() {
        return basefile.getName();
    }

    /**
     * <p>
     * ?? ?  ? .
     * </p>
     * @param filepath
     *        <code>String</code>
     * @return
     * @throws Exception
     */
    public static long touch(final String filepath) throws Exception {

        long currentTime = 0;
        final FileObject file = manager.resolveFile(basefile, filepath);

        if (!file.exists()) {
            file.createFile();
        }

        file.getContent().setLastModifiedTime(currentTime = System.currentTimeMillis());

        return currentTime;
    }

    /**
     * <p>
     *  ?    ??.
     * </p>
     * @param changDirectory
     *        <code>String</code>
     * @throws Exception
     */
    public static void cd(final String changDirectory) throws Exception {
        final String path;
        if (!EgovStringUtil.isNull(changDirectory)) {
            path = changDirectory;
        } else {
            path = System.getProperty("user.home");
        }

        // Locate and validate the folder
        FileObject tmp = manager.resolveFile(basefile, path);

        if (tmp.exists()) {
            basefile = tmp;
        } else {
            log.info("Folder does not exist: " + tmp.getName());
        }
        log.info("Current folder is " + basefile.getName());
    }

    /**
     * <p>
     *  ? ??? .
     * </p>
     * @param cmd
     *        <code>String[]</code>
     * @return ? ? ?
     * @throws FileSystemException
     */
    public List ls(final String[] cmd) throws FileSystemException {
        List list = new ArrayList();

        int pos = 1;
        final boolean recursive;
        if (cmd.length > pos && cmd[pos].equals("-R")) {
            recursive = true;
            pos++;
        } else {
            recursive = false;
        }

        final FileObject file;
        if (cmd.length > pos) {
            file = manager.resolveFile(basefile, cmd[pos]);
        } else {
            file = basefile;
        }

        if (file.getType() == FileType.FOLDER) {
            // List the contents
            log.info("Contents of " + file.getName());
            log.info(listChildren(file, recursive, ""));
            // list.add(file.getName());
        } else {
            // Stat the file
            log.info(file.getName());
            final FileContent content = file.getContent();
            log.info("Size: " + content.getSize() + " bytes.");
            final DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM);
            final String lastMod = dateFormat.format(new Date(content.getLastModifiedTime()));
            log.info("Last modified: " + lastMod);
        }

        return list;
    }

    /**
     * <p>
     *  ?   ?? .
     * </p>
     * @param dir
     *        <code>FileObject</code>
     * @param recursive
     *        <code>boolean</code>
     * @param prefix
     *        <code>String</code>
     * @return  ?
     * @throws FileSystemException
     */
    private StringBuffer listChildren(final FileObject dir, final boolean recursive, final String prefix)
            throws FileSystemException {
        StringBuffer line = new StringBuffer();
        final FileObject[] children = dir.getChildren();

        for (int i = 0; i < children.length; i++) {
            final FileObject child = children[i];
            // log.info(prefix +
            // child.getName().getBaseName());
            line.append(prefix).append(child.getName().getBaseName());

            if (child.getType() == FileType.FOLDER) {
                // log.info("/");
                line.append("/");
                if (recursive) {
                    line.append(listChildren(child, recursive, prefix + "    "));
                }
            } else {
                line.append("");
            }
        }

        return line;
    }

    /**
     * <p>
     * ?? ?.
     * </p>
     * @param file
     *        <code>File</code>
     * @return  
     * @throws IOException
     */
    public static String readFile(File file) throws IOException {
        BufferedInputStream in = new BufferedInputStream(new FileInputStream(file));
        return readFileContent(in);
    }

    /**
     * <p>
     * String  ?? ? ?.
     * </p>
     * @param the
     *        file input stream.
     *        <code>InputStrea</code>
     * @return ? 
     * @throws IOException
     */
    public static String readFileContent(InputStream in) throws IOException {
        StringBuffer buf = new StringBuffer();

        for (int i = in.read(); i != -1; i = in.read()) {
            buf.append((char) i);
        }

        return buf.toString();
    }

    /**
     * <p>
     * String ? ?? ? ?.
     * </p>
     * @param file
     *        <code>File</code>
     * @param encoding
     *        <code>String</code>
     * @return ? 
     * @throws IOException
     */
    @SuppressWarnings("unchecked")
    public static String readFile(File file, String encoding) throws IOException {
        StringBuffer sb = new StringBuffer();

        List<String> lines = FileUtils.readLines(file, encoding);

        for (Iterator<String> it = lines.iterator();;) {
            sb.append(it.next());

            if (it.hasNext()) {
                sb.append("");
            } else {
                break;
            }
        }

        return sb.toString();
    }

    /**
     * <p>
     * ? ? ? .
     * </p>
     * @param file
     *        <code>File</code>
     * @param text
     *        <code>String</code>
     */
    public static void writeFile(File file, String text) {
        FileWriter writer = null;

        try {
            writer = new FileWriter(file);
            writer.write(text);
        } catch (Exception e) {
            log.error("Error creating File: " + file.getName() + ":" + e);
            return;
        } finally {
            try {
                writer.close();
            } catch (Exception e) {

            }
        }
    }

    /**
     * <p>
     * ? ? ? .
     * </p>
     * @param fileName
     *        <code>String</code>
     * @param text
     *        <code>String</code>
     */
    public static void writeFile(String fileName, String text) {
        writeFile(new File(fileName), text);
    }

    public static void writeFile(String fileName, String data, String encoding) throws IOException {
        FileUtils.writeStringToFile(new File(fileName), data, encoding);
    }

    /*
     * Saves the content to the file.   ...?
     * public static void saveFile(String filename,
     * String content) throws IOException {
     * FileOutputStream fos = null;
     * BufferedOutputStream bos = null; try { fos = new
     * FileOutputStream(filename); bos = new
     * BufferedOutputStream(fos);
     * bos.write(content.getBytes(), 0,
     * content.length()); } finally { if (bos != null)
     * bos.close(); if (fos != null) fos.close(); } }
     */

    /**
     * <p>
     * byte ?? ? ?.
     * <p>
     * @param file
     *        <code>FileObject</code>
     * @return ? 
     * @throws IOException
     */
    public static byte[] getContent(final FileObject file) throws IOException {
        final FileContent content = file.getContent();
        final int size = (int) content.getSize();
        final byte[] buf = new byte[size];

        final InputStream in = content.getInputStream();
        try {
            int read = 0;
            for (int pos = 0; pos < size && read >= 0; pos += read) {
                read = in.read(buf, pos, size - pos);
            }
        } finally {
            in.close();
        }

        return buf;
    }

    /**
     * <p>
     * ? ?? OutputStream  .
     * </p>
     * @param file
     * @param outstr
     * @throws IOException
     */
    public static void writeContent(final FileObject file, final OutputStream outstr) throws IOException {
        final InputStream instr = file.getContent().getInputStream();
        try {
            final byte[] buffer = new byte[1024];
            while (true) {
                final int nread = instr.read(buffer);
                if (nread < 0) {
                    break;
                }
                outstr.write(buffer, 0, nread);
            }
        } finally {
            instr.close();
        }
    }

    // Create the output stream via getContent(),
    // to pick up the validation it does
    /**
     * <p>
     * ? ? ? ?? .
     * </p>
     * @param srcFile
     *        <code>FileObject</code>
     * @param destFile
     *        <code>FileObject</code>
     * @throws IOException
     */
    public static void copyContent(final FileObject srcFile, final FileObject destFile) throws IOException {
        final OutputStream outstr = destFile.getContent().getOutputStream();
        try {
            writeContent(srcFile, outstr);
        } finally {
            outstr.close();
        }
    }

    /**
     * <p>
     * ?? ? .
     * </p>
     * @param filename
     *        <code>String</code>
     * @return ??
     */
    public static String getFileExtension(String filename) {
        return FilenameUtils.getExtension(filename);
    }

    /**
     * <p>
     * ??  ?.
     * </p>
     * @param filename
     *        <code>String</code>
     * @return 
     */
    public static boolean isExistsFile(String filename) {
        File file = new File(filename);
        return file.exists();
    }

    /**
     * <p>
     * ?  ?? .
     * </p>
     * @param filename
     *        <code>String</code>
     * @return
     */
    public static String stripFilename(String filename) {
        return FilenameUtils.getBaseName(filename);
    }

    /**
     * <p>
     *  ?? .
     * </p>
     * @param file
     *        <code>File</code>
     * @throws IOException
     */
    public static void delete(File file) throws IOException {
        if (file.isDirectory()) {
            // it's a folder, list children first
            File[] children = file.listFiles();
            for (int i = 0; i < children.length; i++) {
                delete(children[i]);
            }
        }
        if (!file.delete()) {
            throw new IOException("Unable to delete " + file.getPath());
        }
    }

    /**
     * <p>
     * ? ?? ?.
     * </p>
     * @param fileName
     *        <code>String</code>
     * @param newline
     *        <code>boolean</code>
     * @return ? 
     * @throws FileNotFoundException
     * @throws IOException
     */
    public static StringBuffer readTextFile(String fileName, boolean newline)
            throws FileNotFoundException, IOException {
        File file = new File(fileName);
        if (!file.exists()) {
            throw new FileNotFoundException();
        }

        StringBuffer buf = new StringBuffer();
        BufferedReader in = null;
        try {
            in = new BufferedReader(new FileReader(file));

            String str;
            while ((str = in.readLine()) != null) {
                buf.append(str);
                if (newline) {
                    buf.append(System.getProperty("line.separator"));
                }
            }
        } catch (IOException e) {
            // log.error(e, module);
            throw e;
        } finally {
            if (in != null) {
                try {
                    in.close();
                } catch (IOException e) {
                    // log.error(e, module);
                }
            }
        }

        return buf;
    }

    /**
     * <p>
     * ? ? ? .
     * </p>
     * @param filepath
     *        <code>String</code>
     * @return ? ?
     * @throws Exception
     */
    public static FileObject getFileObject(final String filepath) throws Exception {
        FileSystemManager mgr = VFS.getManager();

        return mgr.resolveFile(mgr.resolveFile(System.getProperty("user.dir")), filepath);
    }

    /**
     * <p>
     *  ?  ?? .
     * </p>
     * @param search
     *        <code>Object[]</code>
     * @param pattern
     *        <code>String</code>
     * @return ? ?
     * @throws Exception
     */
    public static List<String> grep(final Object[] search, final String pattern) throws Exception {
        RE searchPattern = new RE(pattern);

        String[] strings = searchPattern.grep(search);
        List<String> list = new ArrayList<String>();

        for (int i = 0; i < strings.length; i++) {
            list.add(strings[i]);
        }

        return list;
    }

    /**
     * <p>
     *  ?  ?? .
     * </p>
     * @param file
     *        <code>File</code>
     * @param pattern
     *        <code>String</code>
     * @return ? ?
     * @throws Exception
     */
    @SuppressWarnings("unchecked")
    public static List<String> grep(final File file, final String pattern) throws Exception {
        RE searchPattern = new RE(pattern);

        List<String> lists = FileUtils.readLines(file);
        Object[] search = lists.toArray();

        String[] strings = searchPattern.grep(search);
        List<String> list = new ArrayList<String>();

        for (int i = 0; i < strings.length; i++) {
            list.add(strings[i]);
        }

        return list;
    }

}