dev.argent.hive.SqlScriptLoader.java Source code

Java tutorial

Introduction

Here is the source code for dev.argent.hive.SqlScriptLoader.java

Source

/*
 * @(#)SqlScriptLoader.java $version 2014. 2. 20.
 * .SqlScriptLoader.java
 */

/**
 * @author ddkkinf@naver.com
 */
/*
 * @(#)SqlScriptLoader.java $version 2013. 10. 2.
 *
 * Copyright 2007 NHN Corp. All rights Reserved. 
 * NHN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 */

package dev.argent.hive;

import java.io.File;
import java.io.IOException;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;

/**
 * @author YongHun.Kim@nhn.com
 */
public class SqlScriptLoader {
    private static final Logger log = LoggerFactory.getLogger(SqlScriptLoader.class);

    @SuppressWarnings("unchecked")
    public static String[] getScripts(Class<?> clazz, String sqlFileName) {
        try {
            String fileName = clazz.getResource(sqlFileName).getFile();
            String comments = stripComments((List<String>) FileUtils.readLines(new File(fileName)));
            return StringUtils.delimitedListToStringArray(comments, ";");
        } catch (IOException e) {
            log.warn(e.getMessage(), e);
            throw new IllegalArgumentException("SqlFile load failed. fileName : " + sqlFileName, e);
        }
    }

    protected static String stripComments(List<String> list) {
        StringBuffer buffer = new StringBuffer();
        for (String line : list) {
            if (!line.startsWith("//") && !line.startsWith("--")) {
                buffer.append(line + "\n");
            }
        }
        return buffer.toString();
    }
}