Uploaded image for project: 'Commons Configuration'
  1. Commons Configuration
  2. CONFIGURATION-442

SubsetConfiguration does not properly list attributes when a delimiter is set

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Closed
    • Major
    • Resolution: Won't Fix
    • 1.6
    • 1.7
    • None
    • None
    • all

    Description

      imagine a XmlConfiguration like this:

      <properties>
        <prop1>
          <prop2>
            <prop
               attr1="attr1"
               attr2="attr2"/>
          </prop2>
        </prop1>
      </properties>
      

      If subset get instanciated to the end of the hierarchy, with a specific delimiter, getKeys() won't return the correct keys:

      ...
      XMLConfiguration config = new XMLConfiguration("test/test.xml");
      Configuration subset = new SubsetConfiguration(config,"prop1.prop2.prop",".");
      ConfigurationUtils.dump(subset, System.err);
      ...
      

      gives the result:

      @attr1]=null
      @attr2]=null
      

      it should give the result

      [@attr1]=attr1
      [@attr2]=attr2
      

      The wrong dump is a side effect of the wrong implementation of the getChildKey method of SubsetConfiguration

       /**
           * Return the key in the subset configuration associated to the specified
           * key in the parent configuration.
           *
           * @param key The key in the parent configuration.
           * @return the key in the context of this subset configuration
           */
          protected String getChildKey(String key)
          {
              if (!key.startsWith(prefix))
              {
                  throw new IllegalArgumentException("The parent key '" + key + "' is not in the subset.");
              }
              else
              {
                  String modifiedKey = null;
                  if (key.length() == prefix.length())
                  {
                      modifiedKey = "";
                  }
                  else
                  {
                      int i = prefix.length() + (delimiter != null ? delimiter.length() : 0);
                      modifiedKey = key.substring(i);
                  }
      
                  return modifiedKey;
              }
          }
      

      In this code, the else part is wrong. In a hierarchical configuration, the attribute delimiter is '[' and is removed here.

      I think a more correct code would be :

      /**
           * Return the key in the subset configuration associated to the specified
           * key in the parent configuration.
           *
           * @param key The key in the parent configuration.
           * @return the key in the context of this subset configuration
           */
          protected String getChildKey(String key)
          {
              if (!key.startsWith(prefix))
              {
                  throw new IllegalArgumentException("The parent key '" + key + "' is not in the subset.");
              }
              else
              {
                  String modifiedKey = null;
                  if (key.length() == prefix.length())
                  {
                      modifiedKey = "";
                  }
                  else
                  {
                      modifiedKey = key.substring(prefix.length());
                      if(delimiter!=null && modifiedKey.startsWith(delimiter))
                      {
                          modifiedKey=modifiedKey.substring(delimiter.length());
                      }
                  }
      
                  return modifiedKey;
              }
          }
      
      

      Attachments

        Activity

          People

            Unassigned Unassigned
            fabien_nisol Fabien Nisol
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved: