/*
 * Decompiled with CFR 0_110.
 * 
 * Could not load the following classes:
 *  android.content.Context
 *  android.content.res.AssetManager
 *  android.content.res.Resources
 *  android.content.res.Resources$NotFoundException
 *  android.util.Log
 *  java.io.IOException
 *  java.io.InputStream
 *  java.lang.Boolean
 *  java.lang.Class
 *  java.lang.ClassCastException
 *  java.lang.ClassNotFoundException
 *  java.lang.IllegalAccessException
 *  java.lang.IllegalArgumentException
 *  java.lang.InstantiationException
 *  java.lang.Object
 *  java.lang.String
 *  java.util.ArrayList
 *  java.util.HashMap
 *  java.util.Iterator
 *  java.util.List
 *  java.util.Properties
 *  java.util.StringTokenizer
 */
package com.google.code.microlog4android.config;

import android.content.Context;
import android.content.res.AssetManager;
import android.content.res.Resources;
import android.util.Log;
import com.google.code.microlog4android.Level;
import com.google.code.microlog4android.Logger;
import com.google.code.microlog4android.appender.Appender;
import com.google.code.microlog4android.appender.FileAppender;
import com.google.code.microlog4android.format.Formatter;
import com.google.code.microlog4android.format.PatternFormatter;
import com.google.code.microlog4android.repository.DefaultLoggerRepository;
import com.google.code.microlog4android.repository.LoggerRepository;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;
import java.util.StringTokenizer;

public class PropertyConfigurator {
    public static final String[] APPENDER_ALIASES;
    public static final String[] APPENDER_CLASS_NAMES;
    public static final String APPENDER_PREFIX_KEY = "microlog.appender";
    public static String DEFAULT_PROPERTIES_FILENAME;
    public static final String FILE_APPENDER_APPEND_KEY = "microlog.appender.FileAppender.Append";
    public static final String FILE_APPENDER_FILE_NAME_KEY = "microlog.appender.FileAppender.File";
    public static final String[] FORMATTER_ALIASES;
    public static final String[] FORMATTER_CLASS_NAMES;
    public static final String FORMATTER_PREFIX_KEY = "microlog.formatter";
    public static final String LOGGER_PREFIX_KEY = "microlog.logger";
    public static final String LOG_LEVEL_PREFIX_KEY = "microlog.level";
    public static final String MICROLOG_PREFIX = "microlog";
    public static final String PATTERN_LAYOUT_PREFIX_KEY = "microlog.formatter.PatternFormatter.pattern";
    public static final String ROOT_LOGGER_KEY = "microlog.rootLogger";
    private static final String TAG = "Microlog.PropertyConfiguration";
    public static final String TAG_PREFIX_KEY = "microlog.tag";
    private static final HashMap<String, String> appenderAliases;
    private static final HashMap<String, String> formatterAliases;
    private Context context;
    private LoggerRepository loggerRepository;

    static {
        DEFAULT_PROPERTIES_FILENAME = "microlog.properties";
        APPENDER_ALIASES = new String[]{"LogCatAppender", "FileAppender"};
        APPENDER_CLASS_NAMES = new String[]{"com.google.code.microlog4android.appender.LogCatAppender", "com.google.code.microlog4android.appender.FileAppender"};
        FORMATTER_ALIASES = new String[]{"SimpleFormatter", "PatternFormatter"};
        FORMATTER_CLASS_NAMES = new String[]{"com.google.code.microlog4android.format.SimpleFormatter", "com.google.code.microlog4android.format.PatternFormatter"};
        appenderAliases = new HashMap(43);
        formatterAliases = new HashMap(21);
    }

    /*
     * Enabled aggressive block sorting
     */
    private PropertyConfigurator(Context context) {
        int n = 0;
        do {
            if (n >= APPENDER_ALIASES.length) break;
            appenderAliases.put((Object)APPENDER_ALIASES[n], (Object)APPENDER_CLASS_NAMES[n]);
            ++n;
        } while (true);
        int n2 = 0;
        do {
            if (n2 >= FORMATTER_ALIASES.length) {
                this.context = context;
                this.loggerRepository = DefaultLoggerRepository.INSTANCE;
                return;
            }
            formatterAliases.put((Object)FORMATTER_ALIASES[n2], (Object)FORMATTER_CLASS_NAMES[n2]);
            ++n2;
        } while (true);
    }

    /*
     * Enabled force condition propagation
     * Lifted jumps to return sites
     */
    private void addAppender(String string2, Properties properties) {
        Logger logger = this.loggerRepository.getRootLogger();
        String string3 = (String)appenderAliases.get((Object)string2);
        if (string3 == null) {
            string3 = string2;
        }
        Appender appender = (Appender)Class.forName((String)string3).newInstance();
        if (appender == null) return;
        try {
            if (appender instanceof FileAppender) {
                super.setPropertiesForFileAppender(appender, properties);
            }
            Log.i((String)"Microlog.PropertyConfiguration", (String)("Adding appender " + appender.getClass().getName()));
            logger.addAppender(appender);
            return;
        }
        catch (ClassNotFoundException var11_6) {
            Log.e((String)"Microlog.PropertyConfiguration", (String)("Failed to find appender class: " + (Object)var11_6));
            return;
        }
        catch (IllegalAccessException var9_7) {
            Log.e((String)"Microlog.PropertyConfiguration", (String)("No access to appender class: " + (Object)var9_7));
            return;
        }
        catch (InstantiationException var7_8) {
            Log.e((String)"Microlog.PropertyConfiguration", (String)("Failed to instantiate appender class: " + (Object)var7_8));
            return;
        }
        catch (ClassCastException var5_9) {
            Log.e((String)"Microlog.PropertyConfiguration", (String)("Specified appender class does not implement the Appender interface: " + (Object)var5_9));
            return;
        }
    }

    private void configureSimpleStyle(Properties properties) {
        super.setLevel(properties);
        super.setAppenders(super.parseAppenderString(properties.getProperty("microlog.appender", "LogCatAppender")), properties);
        super.setFormatter(properties);
    }

    public static PropertyConfigurator getConfigurator(Context context) {
        if (context == null) {
            throw new IllegalArgumentException("The context must not be null");
        }
        return new PropertyConfigurator(context);
    }

    private Properties loadProperties(InputStream inputStream) throws IOException {
        Properties properties = new Properties();
        properties.load(inputStream);
        return properties;
    }

    private List<String> parseAppenderString(String string2) {
        StringTokenizer stringTokenizer = new StringTokenizer(string2, ";,");
        ArrayList arrayList = new ArrayList();
        while (stringTokenizer.hasMoreElements()) {
            arrayList.add((Object)((String)stringTokenizer.nextElement()));
        }
        return arrayList;
    }

    private void setAppenders(List<String> list, Properties properties) {
        Iterator iterator = list.iterator();
        while (iterator.hasNext()) {
            super.addAppender((String)iterator.next(), properties);
        }
        return;
    }

    /*
     * Unable to fully structure code
     * Enabled aggressive block sorting
     * Enabled unnecessary exception pruning
     * Enabled aggressive exception aggregation
     * Lifted jumps to return sites
     */
    private void setFormatter(Properties var1) {
        var2_2 = var1.getProperty("microlog.formatter", "PatternFormatter");
        var3_3 = null;
        if (var2_2 == null || (var3_3 = (String)PropertyConfigurator.formatterAliases.get((Object)var2_2)) == null) {
            var3_3 = var2_2;
        }
        try {
            var12_4 = (Formatter)Class.forName((String)var3_3).newInstance();
            if (var12_4 instanceof PatternFormatter) {
                var16_5 = var1.getProperty("microlog.formatter.PatternFormatter.pattern", "%r %c{1} [%P] %m %T");
                ((PatternFormatter)var12_4).setPattern(var16_5);
            }
            if (var12_4 == null) return;
            var13_6 = this.loggerRepository.getRootLogger();
            var14_7 = var13_6.getNumberOfAppenders();
            var15_8 = 0;
            ** GOTO lbl29
        }
        catch (ClassNotFoundException var10_9) {
            Log.e((String)"Microlog.PropertyConfiguration", (String)("Failed to find Formatter class: " + (Object)var10_9));
            return;
        }
        catch (InstantiationException var8_10) {
            Log.e((String)"Microlog.PropertyConfiguration", (String)("Failed to instantiate formtter: " + (Object)var8_10));
            return;
        }
        catch (IllegalAccessException var6_11) {
            Log.e((String)"Microlog.PropertyConfiguration", (String)("No access to formatter class: " + (Object)var6_11));
            return;
        }
        catch (ClassCastException var4_12) {
            Log.e((String)"Microlog.PropertyConfiguration", (String)("Specified formatter class does not implement the Formatter interface: " + (Object)var4_12));
            return;
        }
lbl-1000: // 1 sources:
        {
            var13_6.getAppender(var15_8).setFormatter(var12_4);
            ++var15_8;
lbl29: // 2 sources:
            ** while (var15_8 < var14_7)
        }
lbl30: // 1 sources:
    }

    private void setLevel(Properties properties) {
        Level level = super.stringToLevel((String)properties.get((Object)"microlog.level"));
        if (level != null) {
            this.loggerRepository.getRootLogger().setLevel(level);
            Log.i((String)"Microlog.PropertyConfiguration", (String)("Root level: " + (Object)((Object)this.loggerRepository.getRootLogger().getLevel())));
        }
    }

    private void setPropertiesForFileAppender(Appender appender, Properties properties) {
        String string2 = properties.getProperty("microlog.appender.FileAppender.File", "microlog.txt");
        ((FileAppender)appender).setFileName(string2);
        boolean bl = Boolean.parseBoolean((String)properties.getProperty("microlog.appender.FileAppender.Append", "true"));
        ((FileAppender)appender).setAppend(bl);
    }

    private void startConfiguration(Properties properties) {
        if (properties.containsKey((Object)"microlog.rootLogger")) {
            Log.i((String)"Microlog.PropertyConfiguration", (String)"Modern configuration not yet supported");
            return;
        }
        Log.i((String)"Microlog.PropertyConfiguration", (String)"Configure using the simple style (aka classic style)");
        super.configureSimpleStyle(properties);
    }

    private Level stringToLevel(String string2) {
        return Level.valueOf(string2);
    }

    public void configure() {
        this.configure(DEFAULT_PROPERTIES_FILENAME);
    }

    public void configure(int n) {
        Resources resources = this.context.getResources();
        try {
            super.startConfiguration(super.loadProperties(resources.openRawResource(n)));
            return;
        }
        catch (Resources.NotFoundException var5_3) {
            Log.e((String)"Microlog.PropertyConfiguration", (String)("Did not find the microlog properties resource. Hint: this should be in the /res/raw directory " + (Object)var5_3));
            return;
        }
        catch (IOException var3_4) {
            Log.e((String)"Microlog.PropertyConfiguration", (String)("Failed to read the microlog properties resource." + (Object)var3_4));
            return;
        }
    }

    public void configure(String string2) {
        AssetManager assetManager = this.context.getResources().getAssets();
        try {
            super.startConfiguration(super.loadProperties(assetManager.open(string2)));
            return;
        }
        catch (IOException var3_3) {
            Log.e((String)"Microlog.PropertyConfiguration", (String)("Failed to open the microlog properties file. Hint: the file should be in the /assets directory " + string2 + " " + (Object)var3_3));
            return;
        }
    }
}

