package org.apache.logging.log4j.core.layout;

import com.fasterxml.jackson.core.PrettyPrinter;
import com.fasterxml.jackson.core.util.DefaultPrettyPrinter;
import com.fasterxml.jackson.core.util.MinimalPrettyPrinter;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.ser.impl.SimpleBeanPropertyFilter;
import com.fasterxml.jackson.databind.ser.impl.SimpleFilterProvider;
import com.fasterxml.jackson.dataformat.xml.util.DefaultXmlPrettyPrinter;
import java.util.HashSet;
import java.util.Set;
import org.apache.logging.log4j.core.impl.Log4jLogEvent;
import org.apache.logging.log4j.core.jackson.Log4jJsonObjectMapper;
import org.apache.logging.log4j.core.jackson.Log4jXmlObjectMapper;

abstract class JacksonFactory {
   protected abstract String getPropertNameForContextMap();

   protected abstract String getPropertNameForSource();

   protected abstract PrettyPrinter newCompactPrinter();

   protected abstract ObjectMapper newObjectMapper();

   protected abstract PrettyPrinter newPrettyPrinter();

   ObjectWriter newWriter(boolean locationInfo, boolean properties, boolean compact) {
      SimpleFilterProvider filters = new SimpleFilterProvider();
      Set<String> except = new HashSet(2);
      if (!locationInfo) {
         except.add(this.getPropertNameForSource());
      }

      if (!properties) {
         except.add(this.getPropertNameForContextMap());
      }

      filters.addFilter(Log4jLogEvent.class.getName(), SimpleBeanPropertyFilter.serializeAllExcept(except));
      ObjectWriter writer = this.newObjectMapper().writer(compact ? this.newCompactPrinter() : this.newPrettyPrinter());
      return writer.with(filters);
   }

   static class XML extends JacksonFactory {
      protected String getPropertNameForContextMap() {
         return "ContextMap";
      }

      protected String getPropertNameForSource() {
         return "Source";
      }

      protected PrettyPrinter newCompactPrinter() {
         return null;
      }

      protected ObjectMapper newObjectMapper() {
         return new Log4jXmlObjectMapper();
      }

      protected PrettyPrinter newPrettyPrinter() {
         return new DefaultXmlPrettyPrinter();
      }
   }

   static class JSON extends JacksonFactory {
      protected String getPropertNameForContextMap() {
         return "contextMap";
      }

      protected String getPropertNameForSource() {
         return "source";
      }

      protected PrettyPrinter newCompactPrinter() {
         return new MinimalPrettyPrinter();
      }

      protected ObjectMapper newObjectMapper() {
         return new Log4jJsonObjectMapper();
      }

      protected PrettyPrinter newPrettyPrinter() {
         return new DefaultPrettyPrinter();
      }
   }
}
