001    /*
002     * Licensed to the Apache Software Foundation (ASF) under one or more
003     * contributor license agreements. See the NOTICE file distributed with
004     * this work for additional information regarding copyright ownership.
005     * The ASF licenses this file to You under the Apache license, Version 2.0
006     * (the "License"); you may not use this file except in compliance with
007     * the License. You may obtain a copy of the License at
008     *
009     *      http://www.apache.org/licenses/LICENSE-2.0
010     *
011     * Unless required by applicable law or agreed to in writing, software
012     * distributed under the License is distributed on an "AS IS" BASIS,
013     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014     * See the license for the specific language governing permissions and
015     * limitations under the license.
016     */
017    package org.apache.logging.log4j.core.pattern;
018    
019    import org.apache.logging.log4j.core.LogEvent;
020    import org.apache.logging.log4j.core.config.Configuration;
021    import org.apache.logging.log4j.core.util.OptionConverter;
022    
023    
024    /**
025     * Formats a string literal.
026     */
027    public final class LiteralPatternConverter extends LogEventPatternConverter implements ArrayPatternConverter {
028        /**
029         * String literal.
030         */
031        private final String literal;
032    
033        private final Configuration config;
034    
035        private final boolean substitute;
036    
037        /**
038         * Create a new instance.
039         *
040         * @param config The Configuration.
041         * @param literal string literal.
042         * @param convertBackslashes if {@code true}, backslash characters are treated as escape characters and character
043         *            sequences like "\" followed by "t" (backslash+t) are converted to special characters like '\t' (tab).
044         */
045        public LiteralPatternConverter(final Configuration config, final String literal, final boolean convertBackslashes) {
046            super("Literal", "literal");
047            this.literal = convertBackslashes ? OptionConverter.convertSpecialChars(literal) : literal; // LOG4J2-829
048            this.config = config;
049            substitute = config != null && literal.contains("${");
050        }
051    
052        /**
053         * {@inheritDoc}
054         */
055        @Override
056        public void format(final LogEvent event, final StringBuilder toAppendTo) {
057            toAppendTo.append(substitute ? config.getStrSubstitutor().replace(event, literal) : literal);
058        }
059        
060        /**
061         * {@inheritDoc}
062         */
063        @Override
064        public void format(final Object obj, final StringBuilder output) {
065            output.append(substitute ? config.getStrSubstitutor().replace(literal) : literal);
066        }
067    
068        /**
069         * {@inheritDoc}
070         */
071        @Override
072        public void format(final StringBuilder output, final Object... objects) {
073            output.append(substitute ? config.getStrSubstitutor().replace(literal) : literal);
074        }
075    
076        public String getLiteral() {
077            return literal;
078        }
079    }