com.htmlhifive.tools.jslint.configure.JSLintConfig.java Source code

Java tutorial

Introduction

Here is the source code for com.htmlhifive.tools.jslint.configure.JSLintConfig.java

Source

/*
 * Copyright (C) 2012 NS Solutions Corporation
 *
 * 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 com.htmlhifive.tools.jslint.configure;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.StringUtils;
import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.runtime.CoreException;

import com.htmlhifive.tools.jslint.configure.FilterBean.FilterLevel;
import com.htmlhifive.tools.jslint.logger.JSLintPluginLogger;
import com.htmlhifive.tools.jslint.logger.JSLintPluginLoggerFactory;
import com.htmlhifive.tools.jslint.messages.Messages;

/**
 * JSLint????.
 * 
 * @author NS Solutions Corporation
 * 
 */
public class JSLintConfig {

    /**
     * ?.
     */
    private static final String CONFIG_KEY_PREFIX = "hi5.tool.jslint.";

    /**
     * jslint.js.
     */
    private static final String KEY_JSLINT_PATH = CONFIG_KEY_PREFIX + "jslint";

    /**
     * .
     */
    private static final String KEY_OPTION_PATH = CONFIG_KEY_PREFIX + "option";

    /**
     * ??????.
     */
    private static final String KEY_USE_OTHER_PROJECT = CONFIG_KEY_PREFIX + "use.other.project";

    /**
     * ?.
     */
    private static final String KEY_OTHER_PROJECT_PATH = CONFIG_KEY_PREFIX + "other.project";

    /**
     * ??.
     */
    private static final String KEY_INTERNAL_LIBRARY_LIST = CONFIG_KEY_PREFIX + "lib.internal.list";

    /**
     * ??.
     */
    private static final String KEY_EXTERNAL_LIBRARY_LIST = CONFIG_KEY_PREFIX + "lib.external.list";

    /**
     * ???.
     */
    private static final String KEY_FILTER_REGEX = CONFIG_KEY_PREFIX + "filter.regex";

    /**
     * .
     */
    private static JSLintPluginLogger logger = JSLintPluginLoggerFactory.getLogger(JSLintConfig.class);

    /**
     * .
     */
    private IFile configProp;

    /**
     * .
     */
    private final ConfigBean configBean;

    /**
     * ?.
     */
    private ConfigBean defaultConfigBean;

    /**
     * .
     * 
     * @param configProp .
     */
    JSLintConfig(IFile configProp) {

        this.configProp = configProp;
        configBean = new ConfigBean();
        load();

    }

    /**
     * ?bean?.
     * 
     * @return
     */
    void load() {

        try {
            configProp.refreshLocal(IResource.DEPTH_ZERO, null);
            if (configProp.exists()) {
                Properties properties = new Properties();
                properties.load(configProp.getContents());
                configBean.setJsLintPath(properties.getProperty(KEY_JSLINT_PATH, ""));
                configBean.setOptionFilePath(properties.getProperty(KEY_OPTION_PATH, ""));
                configBean.setUseOtherProject(
                        Boolean.parseBoolean(properties.getProperty(KEY_USE_OTHER_PROJECT, "false")));
                configBean.setOtherProjectPath(properties.getProperty(KEY_OTHER_PROJECT_PATH, ""));
                configBean.setInternalLibPaths(
                        StringUtils.split(properties.getProperty(KEY_INTERNAL_LIBRARY_LIST, ""), ","));
                configBean.setExternalLibPaths(
                        StringUtils.split(properties.getProperty(KEY_EXTERNAL_LIBRARY_LIST, ""), ","));
                // configBean.setLibList(StringUtils.split(properties.getProperty(KEY_INTERNAL_LIBRARY_LIST,
                // ""), ","));
                int i = 0;
                String filterStr = null;
                while ((filterStr = properties.getProperty(KEY_FILTER_REGEX + i)) != null) {
                    configBean.addFilterBean(convertToFilterBean(filterStr));
                    i++;
                }

            }
            defaultConfigBean = configBean.clone();
        } catch (CoreException e) {
            logger.put(Messages.EM0100, e);
        } catch (IOException e) {
            logger.put(Messages.EM0100, e);
        }

    }

    /**
     * ?????.
     * 
     * @param property ??
     * @return .
     */
    private FilterBean convertToFilterBean(String property) {

        FilterBean bean = new FilterBean();
        String[] strs = StringUtils.split(property, ",", 3);
        bean.setState(Boolean.valueOf(strs[0]));
        bean.setLevel(FilterLevel.valueOf(strs[1]));
        if (strs.length == 3) {
            bean.setRegex(strs[2]);
        } else {
            bean.setRegex("");
        }

        return bean;
    }

    /**
     * ??.jslint???.
     */
    void store() {

        Properties properties = new Properties();
        properties.setProperty(KEY_JSLINT_PATH, configBean.getJsLintPath());
        properties.setProperty(KEY_OPTION_PATH, configBean.getOptionFilePath());
        properties.setProperty(KEY_USE_OTHER_PROJECT, Boolean.toString(configBean.isUseOtherProject()));
        properties.setProperty(KEY_OTHER_PROJECT_PATH, configBean.getOtherProjectPath());
        properties.setProperty(KEY_INTERNAL_LIBRARY_LIST, StringUtils.join(configBean.getInternalLibPaths(), ','));
        properties.setProperty(KEY_EXTERNAL_LIBRARY_LIST, StringUtils.join(configBean.getExternalLibPaths(), ','));
        // properties.setProperty(KEY_LIBRARY_LIST,
        // StringUtils.join(configBean.getLibList(), ","));
        // properties.setProperty(KEY_USE_FILTER,
        // Boolean.toString(configBean.isUseFilter()));
        FilterBean[] beans = configBean.getFilterBeans();
        for (int i = 0; i < beans.length; i++) {
            properties.setProperty(KEY_FILTER_REGEX + i, beans[i].toString());
        }
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        InputStream input = null;
        try {
            properties.store(output, "");
            output.flush();
            input = new ByteArrayInputStream(output.toByteArray());
            configProp.refreshLocal(IResource.DEPTH_ZERO, null);

            if (configProp.exists()) {
                configProp.setContents(input, false, false, null);
            } else {
                configProp.create(input, false, null);
            }
        } catch (CoreException e) {
            logger.put(Messages.EM0100, e);
        } catch (IOException e) {
            logger.put(Messages.EM0100, e);
        } finally {
            IOUtils.closeQuietly(input);
            IOUtils.closeQuietly(output);
        }
    }

    /**
     * ??.
     * 
     * @return .
     */
    ConfigBean getConfigBean() {

        return configBean;
    }

    /**
     * ??.
     * 
     * @return 
     */
    ConfigBean getDefaultConfigBean() {

        return defaultConfigBean;
    }

}