package ch.qos.logback.core.pattern;
import java.util.HashMap;
import java.util.Map;
import ch.qos.logback.core.Context;
import ch.qos.logback.core.CoreConstants;
import ch.qos.logback.core.LayoutBase;
import ch.qos.logback.core.pattern.parser.Node;
import ch.qos.logback.core.pattern.parser.Parser;
import ch.qos.logback.core.spi.ScanException;
import ch.qos.logback.core.status.ErrorStatus;
import ch.qos.logback.core.status.StatusManager;
abstract public class PatternLayoutBase<E> extends LayoutBase<E> {
static final int INTIAL_STRING_BUILDER_SIZE = 256;
Converter<E> head;
String pattern;
protected PostCompileProcessor<E> postCompileProcessor;
Map<String, String> instanceConverterMap = new HashMap<String, String>();
protected boolean = false;
abstract public Map<String, String> getDefaultConverterMap();
public Map<String, String> getEffectiveConverterMap() {
Map<String, String> effectiveMap = new HashMap<String, String>();
Map<String, String> defaultMap = getDefaultConverterMap();
if (defaultMap != null) {
effectiveMap.putAll(defaultMap);
}
Context context = getContext();
if (context != null) {
@SuppressWarnings("unchecked")
Map<String, String> contextMap = (Map<String, String>) context.getObject(CoreConstants.PATTERN_RULE_REGISTRY);
if (contextMap != null) {
effectiveMap.putAll(contextMap);
}
}
effectiveMap.putAll(instanceConverterMap);
return effectiveMap;
}
public void start() {
if (pattern == null || pattern.length() == 0) {
addError("Empty or null pattern.");
return;
}
try {
Parser<E> p = new Parser<E>(pattern);
if (getContext() != null) {
p.setContext(getContext());
}
Node t = p.parse();
this.head = p.compile(t, getEffectiveConverterMap());
if (postCompileProcessor != null) {
postCompileProcessor.process(context, head);
}
ConverterUtil.setContextForConverters(getContext(), head);
ConverterUtil.startConverters(this.head);
super.start();
} catch (ScanException sce) {
StatusManager sm = getContext().getStatusManager();
sm.add(new ErrorStatus("Failed to parse pattern \"" + getPattern() + "\".", this, sce));
}
}
public void setPostCompileProcessor(PostCompileProcessor<E> postCompileProcessor) {
this.postCompileProcessor = postCompileProcessor;
}
protected void setContextForConverters(Converter<E> head) {
ConverterUtil.setContextForConverters(getContext(), head);
}
protected String writeLoopOnConverters(E event) {
StringBuilder strBuilder = new StringBuilder(INTIAL_STRING_BUILDER_SIZE);
Converter<E> c = head;
while (c != null) {
c.write(strBuilder, event);
c = c.getNext();
}
return strBuilder.toString();
}
public String getPattern() {
return pattern;
}
public void setPattern(String pattern) {
this.pattern = pattern;
}
public String toString() {
return this.getClass().getName() + "(\"" + getPattern() + "\")";
}
public Map<String, String> getInstanceConverterMap() {
return instanceConverterMap;
}
protected String () {
return CoreConstants.EMPTY_STRING;
}
public boolean () {
return outputPatternAsHeader;
}
public void (boolean outputPatternAsHeader) {
this.outputPatternAsHeader = outputPatternAsHeader;
}
@Override
public String () {
if (outputPatternAsHeader)
return getPresentationHeaderPrefix() + pattern;
else
return super.getPresentationHeader();
}
}