package com.github.mustachejava;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;

/* JADX INFO: loaded from: classes.dex */
public class FallbackMustacheFactory extends DefaultMustacheFactory {
    private Object[] resourceRoots;

    public FallbackMustacheFactory(String str, String str2) {
        this(str, str2);
    }

    public FallbackMustacheFactory(File file, File file2) {
        this(file, file2);
    }

    public FallbackMustacheFactory(Object... objArr) {
        ArrayList arrayList = new ArrayList();
        for (Object obj : objArr) {
            if (obj instanceof String) {
                String str = (String) obj;
                arrayList.add(str.endsWith("/") ? str : str + "/");
            } else if (obj instanceof File) {
                File file = (File) obj;
                if (!file.exists()) {
                    throw new MustacheException(file + " does not exist");
                }
                if (!file.isDirectory()) {
                    throw new MustacheException(file + " is not a directory");
                }
                arrayList.add(obj);
            } else if (obj != null) {
                throw new MustacheException("Invalid constructor parameter: " + obj.toString());
            }
        }
        this.resourceRoots = arrayList.toArray();
    }

    @Override // com.github.mustachejava.DefaultMustacheFactory, com.github.mustachejava.MustacheFactory
    public Reader getReader(String str) {
        File file;
        Exception e = null;
        for (Object obj : this.resourceRoots) {
            try {
                InputStream resourceAsStream = obj instanceof String ? Thread.currentThread().getContextClassLoader().getResourceAsStream(((String) obj) + str) : null;
                if (resourceAsStream == null) {
                    if (obj instanceof String) {
                        file = new File((String) obj, str);
                    } else if (obj instanceof File) {
                        file = new File((File) obj, str);
                    } else {
                        file = new File(str);
                    }
                    if (file.exists() && file.isFile()) {
                        try {
                            resourceAsStream = new FileInputStream(file);
                        } catch (FileNotFoundException e2) {
                            throw new MustacheException("Found file, could not open: " + file, e2);
                        }
                    }
                }
                if (resourceAsStream == null) {
                    throw new MustacheNotFoundException(str);
                }
                return new BufferedReader(new InputStreamReader(resourceAsStream, StandardCharsets.UTF_8));
            } catch (Exception e3) {
                e = e3;
            }
        }
        throw new MustacheNotFoundException(str, e);
    }
}
