001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.pattern;
015
016import java.util.HashMap;
017import java.util.Map;
018
019import ch.qos.logback.core.Context;
020import ch.qos.logback.core.CoreConstants;
021import ch.qos.logback.core.LayoutBase;
022import ch.qos.logback.core.pattern.parser.Node;
023import ch.qos.logback.core.pattern.parser.Parser;
024import ch.qos.logback.core.spi.ScanException;
025import ch.qos.logback.core.status.ErrorStatus;
026import ch.qos.logback.core.status.StatusManager;
027
028abstract public class PatternLayoutBase<E> extends LayoutBase<E> {
029
030    static final int INTIAL_STRING_BUILDER_SIZE = 256;
031    Converter<E> head;
032    String pattern;
033    protected PostCompileProcessor<E> postCompileProcessor;
034    
035    Map<String, String> instanceConverterMap = new HashMap<String, String>();
036    protected boolean outputPatternAsHeader = false;
037    
038    /**
039     * Concrete implementations of this class are responsible for elaborating the
040     * mapping between pattern words and converters.
041     * 
042     * @return A map associating pattern words to the names of converter classes
043     */
044    abstract public Map<String, String> getDefaultConverterMap();
045
046    /**
047     * Returns a map where the default converter map is merged with the map
048     * contained in the context.
049     */
050    public Map<String, String> getEffectiveConverterMap() {
051        Map<String, String> effectiveMap = new HashMap<String, String>();
052
053        // add the least specific map fist
054        Map<String, String> defaultMap = getDefaultConverterMap();
055        if (defaultMap != null) {
056            effectiveMap.putAll(defaultMap);
057        }
058
059        // contextMap is more specific than the default map
060        Context context = getContext();
061        if (context != null) {
062            @SuppressWarnings("unchecked")
063            Map<String, String> contextMap = (Map<String, String>) context.getObject(CoreConstants.PATTERN_RULE_REGISTRY);
064            if (contextMap != null) {
065                effectiveMap.putAll(contextMap);
066            }
067        }
068        // set the most specific map last
069        effectiveMap.putAll(instanceConverterMap);
070        return effectiveMap;
071    }
072
073    public void start() {
074        if (pattern == null || pattern.length() == 0) {
075            addError("Empty or null pattern.");
076            return;
077        }
078        try {
079            Parser<E> p = new Parser<E>(pattern);
080            if (getContext() != null) {
081                p.setContext(getContext());
082            }
083            Node t = p.parse();
084            this.head = p.compile(t, getEffectiveConverterMap());
085            if (postCompileProcessor != null) {
086                postCompileProcessor.process(context, head);
087            }
088            ConverterUtil.setContextForConverters(getContext(), head);
089            ConverterUtil.startConverters(this.head);
090            super.start();
091        } catch (ScanException sce) {
092            StatusManager sm = getContext().getStatusManager();
093            sm.add(new ErrorStatus("Failed to parse pattern \"" + getPattern() + "\".", this, sce));
094        }
095    }
096
097    public void setPostCompileProcessor(PostCompileProcessor<E> postCompileProcessor) {
098        this.postCompileProcessor = postCompileProcessor;
099    }
100
101    /**
102     *
103     * @param head
104     * @deprecated  Use {@link ConverterUtil#setContextForConverters} instead. This method will
105     *  be removed in future releases.
106     */
107    protected void setContextForConverters(Converter<E> head) {
108        ConverterUtil.setContextForConverters(getContext(), head);
109    }
110
111    protected String writeLoopOnConverters(E event) {
112        StringBuilder strBuilder = new StringBuilder(INTIAL_STRING_BUILDER_SIZE);
113        Converter<E> c = head;
114        while (c != null) {
115            c.write(strBuilder, event);
116            c = c.getNext();
117        }
118        return strBuilder.toString();
119    }
120
121    public String getPattern() {
122        return pattern;
123    }
124
125    public void setPattern(String pattern) {
126        this.pattern = pattern;
127    }
128
129    public String toString() {
130        return this.getClass().getName() + "(\"" + getPattern() + "\")";
131    }
132
133    public Map<String, String> getInstanceConverterMap() {
134        return instanceConverterMap;
135    }
136
137    protected String getPresentationHeaderPrefix() {
138        return CoreConstants.EMPTY_STRING;
139    }
140
141    public boolean isOutputPatternAsHeader() {
142        return outputPatternAsHeader;
143    }
144
145    public void setOutputPatternAsHeader(boolean outputPatternAsHeader) {
146        this.outputPatternAsHeader = outputPatternAsHeader;
147    }
148
149    @Override
150    public String getPresentationHeader() {
151        if (outputPatternAsHeader)
152            return getPresentationHeaderPrefix() + pattern;
153        else
154            return super.getPresentationHeader();
155    }
156}