com.htmlhifive.tools.jslint.engine.JSCheckerFactory.java Source code

Java tutorial

Introduction

Here is the source code for com.htmlhifive.tools.jslint.engine.JSCheckerFactory.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.engine;

import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;

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.JSLintPluginConstant;
import com.htmlhifive.tools.jslint.engine.option.CheckOption;
import com.htmlhifive.tools.jslint.logger.JSLintPluginLogger;
import com.htmlhifive.tools.jslint.logger.JSLintPluginLoggerFactory;
import com.htmlhifive.tools.jslint.messages.Messages;

/**
 * js??.
 * 
 * @author NS Solutions Corporation
 * 
 */
public final class JSCheckerFactory {

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

    /**
     * .
     */
    private JSCheckerFactory() {

        // nocreate
    }

    /**
     * Checker??.
     * 
     * @param checker ?.
     * @param options 
     * @return ?
     * @throws CoreException ?.
     */
    public static JSChecker createJSChecker(IResource checker, CheckOption[] options) throws CoreException {

        IFile chekerFile = (IFile) checker;
        String charset = chekerFile.getCharset(true);

        try {
            if (StringUtils.equals(checker.getName(), JSLintPluginConstant.JS_LINT_NAME)) {
                return new JSLint(new InputStreamReader(((IFile) checker).getContents(), charset), options);
            } else if (StringUtils.equals(checker.getName(), JSLintPluginConstant.JS_HINT_NAME)) {
                return new JSHint(new InputStreamReader(((IFile) checker).getContents(), charset), options);
            } else {
                logger.put(Messages.EM0004);
                throw new IllegalArgumentException(Messages.EM0004.getText());
            }
        } catch (UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }
}