com.jsuper.compiler.parser.InputSource.java Source code

Java tutorial

Introduction

Here is the source code for com.jsuper.compiler.parser.InputSource.java

Source

/*
 * Copyright (C) 2013 Super Programming Language
 *
 * 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.jsuper.compiler.parser;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import org.apache.commons.io.IOUtils;
import org.apache.commons.lang.Validate;

/**
 *
 * @author ccadete
 *
 */
public class InputSource {
    /** Name of the source. May be null. */
    private String name;
    /** The file of the source. May be null. */
    private File file;
    /** Cached content. */
    private char[] content;

    public InputSource(final String name, final File file) throws IOException {
        Validate.notNull(file);

        this.name = name;
        this.file = file;
        this.content = readFile(file);
    }

    public InputSource(final String name, final char[] content) {
        Validate.notNull(content);

        this.name = name;
        this.file = null;
        this.content = content;
    }

    private char[] readFile(File file) throws IOException {
        char[] result = null;

        InputStream input = null;
        try {
            input = new BufferedInputStream(new FileInputStream(file));
            result = IOUtils.toCharArray(input);
        } finally {
            IOUtils.closeQuietly(input);
        }

        return result;
    }

    /**
     * Get the name of the source code.
     *
     * @return The name of the source code.
     */
    public String getName() {
        return name;
    }

    /**
     * Get the content of the source as a char arrar.
     *
     * @return The content of the source.
     */
    public char[] getContent() {
        return content;
    }

    /**
     * Get the file of the source.
     *
     * @return The file of the source. May be null.
     */
    public File getFile() {
        return file;
    }

}