开发者

Tool to find duplicate keys and value in properties file

开发者 https://www.devze.com 2022-12-12 08:55 出处:网络
is there a tool that tells me redundant keys and values that are there in my one or man开发者_如何学编程y properties file./**

is there a tool that tells me redundant keys and values that are there in my one or man开发者_如何学编程y properties file.


/**
 *  Purpose:  Properties doesn't detect duplicate keys.  So this exists.
 *  @author shaned
 */
package com.naehas.tests.configs;

import java.util.Properties;

import org.apache.log4j.Logger;

public class NaehasProperties extends Properties
{
   private static final long   serialVersionUID = 1L;

   private static final Logger log              = Logger.getLogger(NaehasProperties.class);

   public NaehasProperties()
   {
      super();
   }

   /**
    * @param defaults
    */
   public NaehasProperties(Properties defaults)
   {
      super(defaults);
   }

   /**
    * Overriding the HastTable put() so we can check for duplicates
    * 
    */
   public synchronized Object put(Object key, Object value)
   {
      // Have we seen this key before?
      //
      if (get(key) != null)
      {
         StringBuffer message = new StringBuffer("Duplicate key found: " + key + " with value: " + value);
         message.append(". Original value is: " + (String) get(key));

         log.error(message.toString());

         // Setting key to null will generate an exception and cause an exit.
         // Can not change the signature by adding a throws as it's not compatible
         // with HashTables put().
         //
         // If you commented out this line, you will see all the occurrences of the duplicate key
         // as the put will overwrite the past encounter.
         //
         key = null;
      }

      return super.put(key, value);
   }
}


There is an Ant task, RscBundleCheck, that checks for the existence of duplicate keys in a set of resource files:

http://rscbundlecheck.sourceforge.net/

This would be simple way to integrate checking for duplicate properties into your build process.


I don't know if there is an existing tool, but you should be able to write a short java program, or script in a language you are comfortable with that should do this in no time. Then you would also have it for future use.

A quick google search yielded the following http://www.javanb.com/netbeans/1/19793.html

this has a gui tool and a script that will do it.


On Netbeans there is a sort line tools plugin, which has the option to remove duplicates. Works perfectly if properties are one lined.

What is more, sorting the properties makes this file more readible.


It might be easiest just to write one: For each file, and for each property in that file, put the property key/value pair into a Map, but only after ensuring that the key is not already in the Map. If it is, print out the file name, the key, and the two values.


If you are using an IDE you might find a good tool among their plugins/features.

Eclipse has a ResourceBundle Editor plugin which manages properties files:

http://www.eclipseplugincentral.com/Web_Links-index-req-viewlink-cid-331.html

IntelliJ IDEA 8 and higher is also able to manage properties files and check for duplicate entries.


There are two nice tools that I use

  1. unival npm package: this is a command line tool to detect duplicate keys, values or lines.

    npm command to install the package: npm i unival

    Link: https://www.npmjs.com/package/unival

  2. unival extension: if you use vscode, this is a extremely helpful extension to detect duplicates on the fly.

0

精彩评论

暂无评论...
验证码 换一张
取 消