Webiyo

This page was generated using Webiyo. See its source code and unit tests.

org/webiyo/examples/viewjava/SourceFile.java

package org.webiyo.examples.viewjava;

import org.webiyo.util.Files;

import java.io.File;
import java.io.IOException;

public class SourceFile {

    private final String className;
    private final File sourceDir;
    private final String pathToFile;

    public SourceFile(File sourceRoot, String className) {
        this.className = className;
        this.sourceDir = sourceRoot;
        this.pathToFile = className.replace('.', '/') + ".java";
    }

    public SourceFile(File sourceRoot, File source) {
        this.sourceDir = sourceRoot;
        this.pathToFile = source.getPath().replace(sourceRoot.getPath() + "/", "");
        this.className = pathToFile.replace('/', '.').replaceAll("\\.java$", "");
    }

    public String getClassName() {
        return className;
    }

    public String getPackage() {
        int dotIndex = className.lastIndexOf('.');
        if (dotIndex == -1) return "";
        return className.substring(0, dotIndex);
    }

    public String getClassNameWithoutPackage() {
        int dotIndex = className.lastIndexOf('.');
        if (dotIndex == -1) return className;
        return className.substring(dotIndex + 1);
    }

    public File getFile() {
        return new File(sourceDir, pathToFile);
    }

    public String loadSource() throws IOException {
        return Files.readString(getFile());
    }

    public String getPath() {
        return pathToFile;
    }

    public boolean exists() {
        return getFile().exists();
    }

}
SourceForge