package com.github.mustachejava.resolver;

import com.github.mustachejava.MustacheResolver;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.URI;
import java.nio.charset.StandardCharsets;

/* JADX INFO: loaded from: classes.dex */
public class ClasspathResolver implements MustacheResolver {
    private final String resourceRoot;

    public ClasspathResolver() {
        this.resourceRoot = null;
    }

    public ClasspathResolver(String str) {
        this.resourceRoot = str;
    }

    @Override // com.github.mustachejava.MustacheResolver
    public Reader getReader(String str) {
        ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader();
        String path = URI.create(concatResourceRootAndResourceName(str)).normalize().getPath();
        InputStream resourceAsStream = contextClassLoader.getResourceAsStream(path);
        if (resourceAsStream == null) {
            resourceAsStream = ClasspathResolver.class.getClassLoader().getResourceAsStream(path);
        }
        if (resourceAsStream != null) {
            return new BufferedReader(new InputStreamReader(resourceAsStream, StandardCharsets.UTF_8));
        }
        return null;
    }

    private String concatResourceRootAndResourceName(String str) {
        if (this.resourceRoot == null || str == null) {
            return str;
        }
        if (str.startsWith("/") && this.resourceRoot.endsWith("/")) {
            return new StringBuilder().append(this.resourceRoot.substring(0, r3.length() - 1)).append(str).toString();
        }
        if ((!str.startsWith("/") || this.resourceRoot.endsWith("/")) && (str.startsWith("/") || !this.resourceRoot.endsWith("/"))) {
            return this.resourceRoot + "/" + str;
        }
        return this.resourceRoot + str;
    }
}
