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 */
017package org.apache.logging.log4j.core.pattern;
018
019import java.util.List;
020
021import org.apache.logging.log4j.core.LogEvent;
022import org.apache.logging.log4j.core.config.Configuration;
023import org.apache.logging.log4j.core.config.plugins.Plugin;
024import org.apache.logging.log4j.core.layout.PatternLayout;
025import org.apache.logging.log4j.util.PerformanceSensitive;
026
027/**
028 * VariablesNotEmpty pattern converter.
029 */
030@Plugin(name = "notEmpty", category = PatternConverter.CATEGORY)
031@ConverterKeys({ "notEmpty", "varsNotEmpty", "variablesNotEmpty", })
032@PerformanceSensitive("allocation")
033public final class VariablesNotEmptyReplacementConverter extends LogEventPatternConverter {
034
035    private final List<PatternFormatter> formatters;
036
037    /**
038     * Constructs the converter.
039     *
040     * @param formatters
041     *            The PatternFormatters to generate the text to manipulate.
042     */
043    private VariablesNotEmptyReplacementConverter(final List<PatternFormatter> formatters) {
044        super("notEmpty", "notEmpty");
045        this.formatters = formatters;
046    }
047
048    /**
049     * Gets an instance of the class.
050     *
051     * @param config
052     *            The current Configuration.
053     * @param options
054     *            pattern options, may be null.
055     * @return instance of class.
056     */
057    public static VariablesNotEmptyReplacementConverter newInstance(final Configuration config,
058            final String[] options) {
059        if (options.length != 1) {
060            LOGGER.error("Incorrect number of options on varsNotEmpty. Expected 1 received " + options.length);
061            return null;
062        }
063        if (options[0] == null) {
064            LOGGER.error("No pattern supplied on varsNotEmpty");
065            return null;
066        }
067        final PatternParser parser = PatternLayout.createPatternParser(config);
068        final List<PatternFormatter> formatters = parser.parse(options[0]);
069        return new VariablesNotEmptyReplacementConverter(formatters);
070    }
071
072    /**
073     * {@inheritDoc}
074     */
075    @Override
076    public void format(final LogEvent event, final StringBuilder toAppendTo) {
077        final int start = toAppendTo.length();
078        boolean allVarsEmpty = true;
079        boolean hasVars = false;
080        for (int i = 0; i < formatters.size(); i++) {
081            final PatternFormatter formatter = formatters.get(i);
082            final int formatterStart = toAppendTo.length();
083            formatter.format(event, toAppendTo);
084            if (formatter.getConverter().isVariable()) {
085                hasVars = true;
086                allVarsEmpty = allVarsEmpty && (toAppendTo.length() == formatterStart);
087            }
088        }
089        if (!hasVars || allVarsEmpty) {
090            toAppendTo.setLength(start); // remove formatter results
091        }
092    }
093}