001///////////////////////////////////////////////////////////////////////////////////////////////
002// checkstyle: Checks Java source code and other text files for adherence to a set of rules.
003// Copyright (C) 2001-2023 the original author or authors.
004//
005// This library is free software; you can redistribute it and/or
006// modify it under the terms of the GNU Lesser General Public
007// License as published by the Free Software Foundation; either
008// version 2.1 of the License, or (at your option) any later version.
009//
010// This library is distributed in the hope that it will be useful,
011// but WITHOUT ANY WARRANTY; without even the implied warranty of
012// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
013// Lesser General Public License for more details.
014//
015// You should have received a copy of the GNU Lesser General Public
016// License along with this library; if not, write to the Free Software
017// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
018///////////////////////////////////////////////////////////////////////////////////////////////
019
020package com.puppycrawl.tools.checkstyle.checks;
021
022import java.io.File;
023import java.io.IOException;
024import java.io.InputStream;
025import java.nio.file.Files;
026import java.util.HashMap;
027import java.util.Map;
028import java.util.Map.Entry;
029import java.util.Properties;
030import java.util.concurrent.atomic.AtomicInteger;
031import java.util.regex.Matcher;
032import java.util.regex.Pattern;
033
034import com.puppycrawl.tools.checkstyle.StatelessCheck;
035import com.puppycrawl.tools.checkstyle.api.AbstractFileSetCheck;
036import com.puppycrawl.tools.checkstyle.api.FileText;
037
038/**
039 * <p>
040 * Detects duplicated keys in properties files.
041 * </p>
042 * <p>
043 * Rationale: Multiple property keys usually appear after merge or rebase of
044 * several branches. While there are no problems in runtime, there can be a confusion
045 * due to having different values for the duplicated properties.
046 * </p>
047 * <ul>
048 * <li>
049 * Property {@code fileExtensions} - Specify file type extension of the files to check.
050 * Type is {@code java.lang.String[]}.
051 * Default value is {@code .properties}.
052 * </li>
053 * </ul>
054 * <p>
055 * To configure the check:
056 * </p>
057 * <pre>
058 * &lt;module name=&quot;UniqueProperties&quot;/&gt;
059 * </pre>
060 * <p>
061 * Example: in foo.properties file
062 * </p>
063 * <pre>
064 * key.one=44
065 * key.two=32 // OK
066 * key.one=54 // violation
067 * </pre>
068 * <p>
069 * To configure the check to scan custom file extensions:
070 * </p>
071 * <pre>
072 * &lt;module name=&quot;UniqueProperties&quot;&gt;
073 *  &lt;property name=&quot;fileExtensions&quot; value=&quot;customProperties&quot;/&gt;
074 * &lt;/module&gt;
075 * </pre>
076 * <p>
077 * Example: in foo.customProperties file
078 * </p>
079 * <pre>
080 * key.one=44
081 * key.two=32 // OK
082 * key.one=54 // violation
083 * </pre>
084 * <p>
085 * Example: in foo.properties file
086 * </p>
087 * <pre>
088 * key.one=44
089 * key.two=32 // OK
090 * key.one=54 // OK, file is not checked
091 * </pre>
092 * <p>
093 * Parent is {@code com.puppycrawl.tools.checkstyle.Checker}
094 * </p>
095 * <p>
096 * Violation Message Keys:
097 * </p>
098 * <ul>
099 * <li>
100 * {@code properties.duplicate.property}
101 * </li>
102 * <li>
103 * {@code unable.open.cause}
104 * </li>
105 * </ul>
106 *
107 * @since 5.7
108 */
109@StatelessCheck
110public class UniquePropertiesCheck extends AbstractFileSetCheck {
111
112    /**
113     * Localization key for check violation.
114     */
115    public static final String MSG_KEY = "properties.duplicate.property";
116    /**
117     * Localization key for IO exception occurred on file open.
118     */
119    public static final String MSG_IO_EXCEPTION_KEY = "unable.open.cause";
120
121    /**
122     * Pattern matching single space.
123     */
124    private static final Pattern SPACE_PATTERN = Pattern.compile(" ");
125
126    /**
127     * Construct the check with default values.
128     */
129    public UniquePropertiesCheck() {
130        setFileExtensions("properties");
131    }
132
133    @Override
134    protected void processFiltered(File file, FileText fileText) {
135        final UniqueProperties properties = new UniqueProperties();
136        try (InputStream inputStream = Files.newInputStream(file.toPath())) {
137            properties.load(inputStream);
138        }
139        catch (IOException ex) {
140            log(1, MSG_IO_EXCEPTION_KEY, file.getPath(),
141                    ex.getLocalizedMessage());
142        }
143
144        for (Entry<String, AtomicInteger> duplication : properties
145                .getDuplicatedKeys().entrySet()) {
146            final String keyName = duplication.getKey();
147            final int lineNumber = getLineNumber(fileText, keyName);
148            // Number of occurrences is number of duplications + 1
149            log(lineNumber, MSG_KEY, keyName, duplication.getValue().get() + 1);
150        }
151    }
152
153    /**
154     * Method returns line number the key is detected in the checked properties
155     * files first.
156     *
157     * @param fileText
158     *            {@link FileText} object contains the lines to process
159     * @param keyName
160     *            key name to look for
161     * @return line number of first occurrence. If no key found in properties
162     *         file, 1 is returned
163     */
164    private static int getLineNumber(FileText fileText, String keyName) {
165        final Pattern keyPattern = getKeyPattern(keyName);
166        int lineNumber = 1;
167        final Matcher matcher = keyPattern.matcher("");
168        for (int index = 0; index < fileText.size(); index++) {
169            final String line = fileText.get(index);
170            matcher.reset(line);
171            if (matcher.matches()) {
172                break;
173            }
174            ++lineNumber;
175        }
176        // -1 as check seeks for the first duplicate occurrence in file,
177        // so it cannot be the last line.
178        if (lineNumber > fileText.size() - 1) {
179            lineNumber = 1;
180        }
181        return lineNumber;
182    }
183
184    /**
185     * Method returns regular expression pattern given key name.
186     *
187     * @param keyName
188     *            key name to look for
189     * @return regular expression pattern given key name
190     */
191    private static Pattern getKeyPattern(String keyName) {
192        final String keyPatternString = "^" + SPACE_PATTERN.matcher(keyName)
193                .replaceAll(Matcher.quoteReplacement("\\\\ ")) + "[\\s:=].*$";
194        return Pattern.compile(keyPatternString);
195    }
196
197    /**
198     * Properties subclass to store duplicated property keys in a separate map.
199     *
200     * @noinspection ClassExtendsConcreteCollection
201     * @noinspectionreason ClassExtendsConcreteCollection - we require custom
202     *      {@code put} method to find duplicate keys
203     */
204    private static class UniqueProperties extends Properties {
205
206        /** A unique serial version identifier. */
207        private static final long serialVersionUID = 1L;
208        /**
209         * Map, holding duplicated keys and their count. Keys are added here only if they
210         * already exist in Properties' inner map.
211         */
212        private final Map<String, AtomicInteger> duplicatedKeys = new HashMap<>();
213
214        /**
215         * Puts the value into properties by the key specified.
216         */
217        @Override
218        public synchronized Object put(Object key, Object value) {
219            final Object oldValue = super.put(key, value);
220            if (oldValue != null && key instanceof String) {
221                final String keyString = (String) key;
222
223                duplicatedKeys.computeIfAbsent(keyString, empty -> new AtomicInteger(0))
224                        .incrementAndGet();
225            }
226            return oldValue;
227        }
228
229        /**
230         * Retrieves a collections of duplicated properties keys.
231         *
232         * @return A collection of duplicated keys.
233         */
234        public Map<String, AtomicInteger> getDuplicatedKeys() {
235            return new HashMap<>(duplicatedKeys);
236        }
237
238    }
239
240}