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.config.Configuration;
022import org.apache.logging.log4j.core.config.plugins.Plugin;
023import org.apache.logging.log4j.core.layout.PatternLayout;
024import org.apache.logging.log4j.util.PerformanceSensitive;
025import org.apache.logging.log4j.util.StringBuilders;
026
027/**
028 * Equals ignore case pattern converter.
029 */
030@Plugin(name = "equalsIgnoreCase", category = PatternConverter.CATEGORY)
031@ConverterKeys({ "equalsIgnoreCase" })
032@PerformanceSensitive("allocation")
033public final class EqualsIgnoreCaseReplacementConverter extends EqualsBaseReplacementConverter {
034
035    /**
036     * Gets an instance of the class.
037     *
038     * @param config
039     *            The current Configuration.
040     * @param options
041     *            pattern options, an array of three elements: pattern, testString, and substitution.
042     * @return instance of class.
043     */
044    public static EqualsIgnoreCaseReplacementConverter newInstance(final Configuration config, final String[] options) {
045        if (options.length != 3) {
046            LOGGER.error("Incorrect number of options on equalsIgnoreCase. Expected 3 received " + options.length);
047            return null;
048        }
049        if (options[0] == null) {
050            LOGGER.error("No pattern supplied on equalsIgnoreCase");
051            return null;
052        }
053        if (options[1] == null) {
054            LOGGER.error("No test string supplied on equalsIgnoreCase");
055            return null;
056        }
057        if (options[2] == null) {
058            LOGGER.error("No substitution supplied on equalsIgnoreCase");
059            return null;
060        }
061        final String p = options[1];
062        final PatternParser parser = PatternLayout.createPatternParser(config);
063        final List<PatternFormatter> formatters = parser.parse(options[0]);
064        return new EqualsIgnoreCaseReplacementConverter(formatters, p, options[2], parser);
065    }
066
067    /**
068     * Construct the converter.
069     *
070     * @param formatters   The PatternFormatters to generate the text to manipulate.
071     * @param testString   The test string.
072     * @param substitution The substitution string.
073     * @param parser        The PatternParser.
074     */
075    private EqualsIgnoreCaseReplacementConverter(final List<PatternFormatter> formatters, final String testString,
076            final String substitution, final PatternParser parser) {
077        super("equalsIgnoreCase", "equalsIgnoreCase", formatters, testString, substitution, parser);
078    }
079
080    @Override
081    protected boolean equals(final String str, final StringBuilder buff, final int from, final int len) {
082        return StringBuilders.equalsIgnoreCase(str, 0, str.length(), buff, from, len);
083    }
084}