package org.apache.logging.log4j.core.config.plugins.util;

import java.io.File;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.MalformedURLException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.charset.Charset;
import java.security.Provider;
import java.security.Security;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.regex.Pattern;
import javax.xml.bind.DatatypeConverter;
import org.apache.logging.log4j.Level;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.core.Filter;
import org.apache.logging.log4j.core.layout.HtmlLayout;
import org.apache.logging.log4j.core.net.Facility;
import org.apache.logging.log4j.core.net.Protocol;
import org.apache.logging.log4j.core.util.Assert;
import org.apache.logging.log4j.core.util.Loader;
import org.apache.logging.log4j.status.StatusLogger;
import org.apache.logging.log4j.util.EnglishEnums;

public final class TypeConverters {
   private static final Logger LOGGER = StatusLogger.getLogger();
   private final Map<Class<?>, TypeConverter<?>> registry;

   public static Object convert(String s, Class<?> clazz, Object defaultValue) {
      TypeConverter<?> converter = findTypeConverter((Class)Assert.requireNonNull(clazz, "No class specified to convert to."));
      if (converter == null) {
         throw new IllegalArgumentException("No type converter found for class: " + clazz.getName());
      } else if (s == null) {
         return parseDefaultValue(converter, defaultValue);
      } else {
         try {
            return converter.convert(s);
         } catch (Exception var5) {
            LOGGER.warn("Error while converting string [{}] to type [{}]. Using default value [{}].", s, clazz, defaultValue, var5);
            return parseDefaultValue(converter, defaultValue);
         }
      }
   }

   public static TypeConverter<?> findTypeConverter(Class<?> clazz) {
      return (TypeConverter)TypeConverters.Holder.INSTANCE.registry.get(clazz);
   }

   private static Object parseDefaultValue(TypeConverter<?> converter, Object defaultValue) {
      if (defaultValue == null) {
         return null;
      } else if (!(defaultValue instanceof String)) {
         return defaultValue;
      } else {
         try {
            return converter.convert((String)defaultValue);
         } catch (Exception var3) {
            LOGGER.debug("Can't parse default value [{}] for type [{}].", defaultValue, converter.getClass(), var3);
            return null;
         }
      }
   }

   public static void registerTypeConverter(Class<?> clazz, TypeConverter<?> converter) {
      TypeConverters.Holder.INSTANCE.registry.put(clazz, converter);
   }

   private TypeConverters() {
      this.registry = new ConcurrentHashMap();
      this.registry.put(Boolean.class, new TypeConverters.BooleanConverter());
      this.registry.put(Byte.class, new TypeConverters.ByteConverter());
      this.registry.put(Character.class, new TypeConverters.CharacterConverter());
      this.registry.put(Double.class, new TypeConverters.DoubleConverter());
      this.registry.put(Float.class, new TypeConverters.FloatConverter());
      this.registry.put(Integer.class, new TypeConverters.IntegerConverter());
      this.registry.put(Long.class, new TypeConverters.LongConverter());
      this.registry.put(Short.class, new TypeConverters.ShortConverter());
      this.registry.put(Boolean.TYPE, this.registry.get(Boolean.class));
      this.registry.put(Byte.TYPE, new TypeConverters.ByteConverter());
      this.registry.put(char[].class, new TypeConverters.CharArrayConverter());
      this.registry.put(Double.TYPE, this.registry.get(Double.class));
      this.registry.put(Float.TYPE, this.registry.get(Float.class));
      this.registry.put(Integer.TYPE, this.registry.get(Integer.class));
      this.registry.put(Long.TYPE, this.registry.get(Long.class));
      this.registry.put(Short.TYPE, this.registry.get(Short.class));
      this.registry.put(byte[].class, new TypeConverters.ByteArrayConverter());
      this.registry.put(Character.TYPE, new TypeConverters.CharacterConverter());
      this.registry.put(BigInteger.class, new TypeConverters.BigIntegerConverter());
      this.registry.put(BigDecimal.class, new TypeConverters.BigDecimalConverter());
      this.registry.put(String.class, new TypeConverters.StringConverter());
      this.registry.put(Charset.class, new TypeConverters.CharsetConverter());
      this.registry.put(File.class, new TypeConverters.FileConverter());
      this.registry.put(URL.class, new TypeConverters.UrlConverter());
      this.registry.put(URI.class, new TypeConverters.UriConverter());
      this.registry.put(Class.class, new TypeConverters.ClassConverter());
      this.registry.put(Pattern.class, new TypeConverters.PatternConverter());
      this.registry.put(Provider.class, new TypeConverters.SecurityProviderConverter());
      this.registry.put(Level.class, new TypeConverters.LevelConverter());
      this.registry.put(Filter.Result.class, new TypeConverters.EnumConverter(Filter.Result.class));
      this.registry.put(Facility.class, new TypeConverters.EnumConverter(Facility.class));
      this.registry.put(Protocol.class, new TypeConverters.EnumConverter(Protocol.class));
      this.registry.put(HtmlLayout.FontSize.class, new TypeConverters.EnumConverter(HtmlLayout.FontSize.class));
   }

   // $FF: synthetic method
   TypeConverters(Object x0) {
      this();
   }

   public static class UrlConverter implements TypeConverter<URL> {
      public URL convert(String s) throws MalformedURLException {
         return new URL(s);
      }
   }

   public static class UriConverter implements TypeConverter<URI> {
      public URI convert(String s) throws URISyntaxException {
         return new URI(s);
      }
   }

   public static class StringConverter implements TypeConverter<String> {
      public String convert(String s) {
         return s;
      }
   }

   public static class ShortConverter implements TypeConverter<Short> {
      public Short convert(String s) {
         return Short.valueOf(s);
      }
   }

   public static class SecurityProviderConverter implements TypeConverter<Provider> {
      public Provider convert(String s) {
         return Security.getProvider(s);
      }
   }

   public static class PatternConverter implements TypeConverter<Pattern> {
      public Pattern convert(String s) {
         return Pattern.compile(s);
      }
   }

   public static class LongConverter implements TypeConverter<Long> {
      public Long convert(String s) {
         return Long.valueOf(s);
      }
   }

   public static class LevelConverter implements TypeConverter<Level> {
      public Level convert(String s) {
         return Level.valueOf(s);
      }
   }

   public static class IntegerConverter implements TypeConverter<Integer> {
      public Integer convert(String s) {
         return Integer.valueOf(s);
      }
   }

   private static final class Holder {
      private static final TypeConverters INSTANCE = new TypeConverters();
   }

   public static class FloatConverter implements TypeConverter<Float> {
      public Float convert(String s) {
         return Float.valueOf(s);
      }
   }

   public static class FileConverter implements TypeConverter<File> {
      public File convert(String s) {
         return new File(s);
      }
   }

   public static class EnumConverter<E extends Enum<E>> implements TypeConverter<E> {
      private final Class<E> clazz;

      private EnumConverter(Class<E> clazz) {
         this.clazz = clazz;
      }

      public E convert(String s) {
         return EnglishEnums.valueOf(this.clazz, s);
      }

      // $FF: synthetic method
      EnumConverter(Class x0, Object x1) {
         this(x0);
      }
   }

   public static class DoubleConverter implements TypeConverter<Double> {
      public Double convert(String s) {
         return Double.valueOf(s);
      }
   }

   public static class ClassConverter implements TypeConverter<Class<?>> {
      public Class<?> convert(String s) throws ClassNotFoundException {
         return Loader.loadClass(s);
      }
   }

   public static class CharsetConverter implements TypeConverter<Charset> {
      public Charset convert(String s) {
         return Charset.forName(s);
      }
   }

   public static class CharArrayConverter implements TypeConverter<char[]> {
      public char[] convert(String s) {
         return s.toCharArray();
      }
   }

   public static class CharacterConverter implements TypeConverter<Character> {
      public Character convert(String s) {
         if (s.length() != 1) {
            throw new IllegalArgumentException("Character string must be of length 1: " + s);
         } else {
            return s.toCharArray()[0];
         }
      }
   }

   public static class ByteConverter implements TypeConverter<Byte> {
      public Byte convert(String s) {
         return Byte.valueOf(s);
      }
   }

   public static class ByteArrayConverter implements TypeConverter<byte[]> {
      private static final String PREFIX_0x = "0x";
      private static final String PREFIX_BASE64 = "Base64:";

      public byte[] convert(String value) {
         byte[] bytes;
         if (value != null && !value.isEmpty()) {
            String lexicalXSDHexBinary;
            if (value.startsWith("Base64:")) {
               lexicalXSDHexBinary = value.substring("Base64:".length());
               bytes = DatatypeConverter.parseBase64Binary(lexicalXSDHexBinary);
            } else if (value.startsWith("0x")) {
               lexicalXSDHexBinary = value.substring("0x".length());
               bytes = DatatypeConverter.parseHexBinary(lexicalXSDHexBinary);
            } else {
               bytes = value.getBytes(Charset.defaultCharset());
            }
         } else {
            bytes = new byte[0];
         }

         return bytes;
      }
   }

   public static class BooleanConverter implements TypeConverter<Boolean> {
      public Boolean convert(String s) {
         return Boolean.valueOf(s);
      }
   }

   public static class BigIntegerConverter implements TypeConverter<BigInteger> {
      public BigInteger convert(String s) {
         return new BigInteger(s);
      }
   }

   public static class BigDecimalConverter implements TypeConverter<BigDecimal> {
      public BigDecimal convert(String s) {
         return new BigDecimal(s);
      }
   }
}
