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
064                    .getObject(CoreConstants.PATTERN_RULE_REGISTRY);
065            if (contextMap != null) {
066                effectiveMap.putAll(contextMap);
067            }
068        }
069        // set the most specific map last
070        effectiveMap.putAll(instanceConverterMap);
071        return effectiveMap;
072    }
073
074    public void start() {
075        if (pattern == null || pattern.length() == 0) {
076            addError("Empty or null pattern.");
077            return;
078        }
079        try {
080            Parser<E> p = new Parser<E>(pattern);
081            if (getContext() != null) {
082                p.setContext(getContext());
083            }
084            Node t = p.parse();
085            this.head = p.compile(t, getEffectiveConverterMap());
086            if (postCompileProcessor != null) {
087                postCompileProcessor.process(context, head);
088            }
089            ConverterUtil.setContextForConverters(getContext(), head);
090            ConverterUtil.startConverters(this.head);
091            super.start();
092        } catch (ScanException sce) {
093            StatusManager sm = getContext().getStatusManager();
094            sm.add(new ErrorStatus("Failed to parse pattern \"" + getPattern() + "\".", this, sce));
095        }
096    }
097
098    public void setPostCompileProcessor(PostCompileProcessor<E> postCompileProcessor) {
099        this.postCompileProcessor = postCompileProcessor;
100    }
101
102    /**
103     *
104     * @param head
105     * @deprecated Use {@link ConverterUtil#setContextForConverters} instead. This
106     *             method will be removed in future releases.
107     */
108    protected void setContextForConverters(Converter<E> head) {
109        ConverterUtil.setContextForConverters(getContext(), head);
110    }
111
112    protected String writeLoopOnConverters(E event) {
113        StringBuilder strBuilder = new StringBuilder(INTIAL_STRING_BUILDER_SIZE);
114        Converter<E> c = head;
115        while (c != null) {
116            c.write(strBuilder, event);
117            c = c.getNext();
118        }
119        return strBuilder.toString();
120    }
121
122    public String getPattern() {
123        return pattern;
124    }
125
126    public void setPattern(String pattern) {
127        this.pattern = pattern;
128    }
129
130    public String toString() {
131        return this.getClass().getName() + "(\"" + getPattern() + "\")";
132    }
133
134    public Map<String, String> getInstanceConverterMap() {
135        return instanceConverterMap;
136    }
137
138    protected String getPresentationHeaderPrefix() {
139        return CoreConstants.EMPTY_STRING;
140    }
141
142    public boolean isOutputPatternAsHeader() {
143        return outputPatternAsHeader;
144    }
145
146    public void setOutputPatternAsHeader(boolean outputPatternAsHeader) {
147        this.outputPatternAsHeader = outputPatternAsHeader;
148    }
149
150    @Override
151    public String getPresentationHeader() {
152        if (outputPatternAsHeader)
153            return getPresentationHeaderPrefix() + pattern;
154        else
155            return super.getPresentationHeader();
156    }
157}