FilesTraversal.java 1.46 KB
/*
 * Decompiled with CFR 0_118.
 */
package com.adobe.granite.infocollector.impl;

import com.adobe.granite.infocollector.impl.FilesHandler;
import java.io.File;
import java.io.IOException;
import java.net.URI;

final class FilesTraversal {
    private final File home;

    public FilesTraversal(String slingHomePath) {
        this.home = new File(slingHomePath);
    }

    public void traverse(FilesHandler handler) throws IOException {
        this.traverse(this.home, handler);
    }

    public void traverse(String fileName, FilesHandler handler) throws IOException {
        this.traverse(new File(this.home, fileName), handler);
    }

    private void traverse(File file, FilesHandler handler) throws IOException {
        if (file == null || !file.exists()) {
            return;
        }
        if (file.isDirectory()) {
            handler.beginDirectory(file);
            for (File child : file.listFiles()) {
                this.traverse(child, handler);
            }
            handler.endDirectory(file);
            return;
        }
        handler.onFile(file);
    }

    public File getHome() {
        return this.home;
    }

    public File getFile(String name) {
        return new File(this.home, name);
    }

    public String relativize(File file) {
        String name = this.home.toURI().relativize(file.toURI()).getPath();
        if (file.isDirectory()) {
            name = name.endsWith("/") ? name : name + '/';
        }
        return name;
    }
}