com.htmlhifive.tools.jslint.parse.JsFileInfo.java Source code

Java tutorial

Introduction

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

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

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

import com.htmlhifive.tools.jslint.JSLintPluginConstant;

/**
 * .
 * 
 * @author NS Solutions Corporation
 * 
 */
public class JsFileInfo implements Cloneable {

    /**
     * ?.
     */
    private int lineCount;

    /**
     * ?.
     */
    private String libraryStr;

    /**
     * .
     */
    public JsFileInfo() {

    }

    /**
     * .
     * 
     * @param jsFile js
     * @throws CoreException ?.
     * @throws IOException .
     */
    public JsFileInfo(IFile jsFile) throws CoreException, IOException {

        append(jsFile);

    }

    /**
     * ?.
     * 
     * @param jsfile ?
     * @throws IOException 
     */
    public void append(File jsfile) throws IOException {

        if (!StringUtils.endsWith(jsfile.getName(), "." + JSLintPluginConstant.EXTENTION_JS)) {
            throw new IllegalArgumentException();
        }
        BufferedReader reader = new BufferedReader(new FileReader(jsfile));
        append(reader);
    }

    /**
     * ?.
     * 
     * @param reader .
     * @throws IOException .
     */
    public void append(BufferedReader reader) throws IOException {

        StringBuilder sb = new StringBuilder();
        if (libraryStr != null) {
            sb.append(libraryStr);
        }
        try {
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
                sb.append("\n");
                lineCount++;

            }
        } finally {
            if (reader != null) {
                reader.close();
            }
        }
        libraryStr = sb.toString();

    }

    /**
     * ?.
     * 
     * @param info 
     */
    public void append(JsFileInfo info) {

        StringBuilder sb = new StringBuilder();
        sb.append(this.libraryStr);
        sb.append(info.libraryStr);
        this.lineCount = this.lineCount + info.lineCount;
        this.libraryStr = sb.toString();
    }

    /**
     * ?.
     * 
     * @param jsFile .
     * @throws IOException 
     * @throws CoreException ?
     */
    public void append(IFile jsFile) throws IOException, CoreException {

        if (!StringUtils.equals(jsFile.getFileExtension(), JSLintPluginConstant.EXTENTION_JS)) {
            throw new IllegalArgumentException();
        }
        InputStream is = jsFile.getContents();
        String cs = jsFile.getCharset(true);
        if (is == null || cs == null) {
            throw new IllegalArgumentException();
        }
        BufferedReader reader = new BufferedReader(new InputStreamReader(is, cs));
        append(reader);

    }

    /**
     * ???.
     * 
     * @return ?
     */
    public int getLineCount() {

        return lineCount;
    }

    /**
     * ???.
     * 
     * @return ?
     */
    public String getSourceStr() {

        return libraryStr;
    }

    @Override
    public JsFileInfo clone() {

        try {
            return (JsFileInfo) super.clone();
        } catch (CloneNotSupportedException e) {
            throw new AssertionError();
        }
    }

}