You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@commons.apache.org by "Fabien Nisol (JIRA)" <ji...@apache.org> on 2011/03/31 16:48:05 UTC

[jira] [Created] (CONFIGURATION-442) SubsetConfiguration does not properly list attributes when a delimiter is set

SubsetConfiguration does not properly list attributes when a delimiter is set
-----------------------------------------------------------------------------

                 Key: CONFIGURATION-442
                 URL: https://issues.apache.org/jira/browse/CONFIGURATION-442
             Project: Commons Configuration
          Issue Type: Bug
    Affects Versions: 1.6
         Environment: all
            Reporter: Fabien Nisol


imagine a XmlConfiguration like this:
{code}
<properties>
  <prop1>
    <prop2>
      <prop
         attr1="attr1"
         attr2="attr2"/>
    </prop2>
  </prop1>
</properties>
{code}

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

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

gives the result:

{code}
@attr1]=null
@attr2]=null
{code}

it should give the result

{code}
[@attr1]=attr1
[@attr2]=attr2
{code}

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

{code}
 /**
     * 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;
        }
    }
{code}

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 :

{code}
/**
     * 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;
        }
    }

{code}



--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Commented] (CONFIGURATION-442) SubsetConfiguration does not properly list attributes when a delimiter is set

Posted by "Fabien Nisol (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/CONFIGURATION-442?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13017574#comment-13017574 ] 

Fabien Nisol commented on CONFIGURATION-442:
--------------------------------------------

Thanks, I'll try this class to check if it fixes our problems

> SubsetConfiguration does not properly list attributes when a delimiter is set
> -----------------------------------------------------------------------------
>
>                 Key: CONFIGURATION-442
>                 URL: https://issues.apache.org/jira/browse/CONFIGURATION-442
>             Project: Commons Configuration
>          Issue Type: Bug
>    Affects Versions: 1.6
>         Environment: all
>            Reporter: Fabien Nisol
>
> imagine a XmlConfiguration like this:
> {code}
> <properties>
>   <prop1>
>     <prop2>
>       <prop
>          attr1="attr1"
>          attr2="attr2"/>
>     </prop2>
>   </prop1>
> </properties>
> {code}
> If subset get instanciated to the end of the hierarchy, with a specific delimiter, getKeys() won't return the correct keys:
> {code}
> ...
> XMLConfiguration config = new XMLConfiguration("test/test.xml");
> Configuration subset = new SubsetConfiguration(config,"prop1.prop2.prop",".");
> ConfigurationUtils.dump(subset, System.err);
> ...
> {code}
> gives the result:
> {code}
> @attr1]=null
> @attr2]=null
> {code}
> it should give the result
> {code}
> [@attr1]=attr1
> [@attr2]=attr2
> {code}
> The wrong dump is a side effect of the wrong implementation of the _getChildKey_ method of SubsetConfiguration
> {code}
>  /**
>      * 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;
>         }
>     }
> {code}
> 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 :
> {code}
> /**
>      * 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;
>         }
>     }
> {code}

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Resolved] (CONFIGURATION-442) SubsetConfiguration does not properly list attributes when a delimiter is set

Posted by "Oliver Heger (JIRA)" <ji...@apache.org>.
     [ https://issues.apache.org/jira/browse/CONFIGURATION-442?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

Oliver Heger resolved CONFIGURATION-442.
----------------------------------------

       Resolution: Won't Fix
    Fix Version/s: 1.7

There is a workaround for this problem.

> SubsetConfiguration does not properly list attributes when a delimiter is set
> -----------------------------------------------------------------------------
>
>                 Key: CONFIGURATION-442
>                 URL: https://issues.apache.org/jira/browse/CONFIGURATION-442
>             Project: Commons Configuration
>          Issue Type: Bug
>    Affects Versions: 1.6
>         Environment: all
>            Reporter: Fabien Nisol
>             Fix For: 1.7
>
>
> imagine a XmlConfiguration like this:
> {code}
> <properties>
>   <prop1>
>     <prop2>
>       <prop
>          attr1="attr1"
>          attr2="attr2"/>
>     </prop2>
>   </prop1>
> </properties>
> {code}
> If subset get instanciated to the end of the hierarchy, with a specific delimiter, getKeys() won't return the correct keys:
> {code}
> ...
> XMLConfiguration config = new XMLConfiguration("test/test.xml");
> Configuration subset = new SubsetConfiguration(config,"prop1.prop2.prop",".");
> ConfigurationUtils.dump(subset, System.err);
> ...
> {code}
> gives the result:
> {code}
> @attr1]=null
> @attr2]=null
> {code}
> it should give the result
> {code}
> [@attr1]=attr1
> [@attr2]=attr2
> {code}
> The wrong dump is a side effect of the wrong implementation of the _getChildKey_ method of SubsetConfiguration
> {code}
>  /**
>      * 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;
>         }
>     }
> {code}
> 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 :
> {code}
> /**
>      * 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;
>         }
>     }
> {code}

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

[jira] [Commented] (CONFIGURATION-442) SubsetConfiguration does not properly list attributes when a delimiter is set

Posted by "Oliver Heger (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/CONFIGURATION-442?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13079568#comment-13079568 ] 

Oliver Heger commented on CONFIGURATION-442:
--------------------------------------------

Does the suggested workaround solve your problem? Then I would close this issue.

> SubsetConfiguration does not properly list attributes when a delimiter is set
> -----------------------------------------------------------------------------
>
>                 Key: CONFIGURATION-442
>                 URL: https://issues.apache.org/jira/browse/CONFIGURATION-442
>             Project: Commons Configuration
>          Issue Type: Bug
>    Affects Versions: 1.6
>         Environment: all
>            Reporter: Fabien Nisol
>
> imagine a XmlConfiguration like this:
> {code}
> <properties>
>   <prop1>
>     <prop2>
>       <prop
>          attr1="attr1"
>          attr2="attr2"/>
>     </prop2>
>   </prop1>
> </properties>
> {code}
> If subset get instanciated to the end of the hierarchy, with a specific delimiter, getKeys() won't return the correct keys:
> {code}
> ...
> XMLConfiguration config = new XMLConfiguration("test/test.xml");
> Configuration subset = new SubsetConfiguration(config,"prop1.prop2.prop",".");
> ConfigurationUtils.dump(subset, System.err);
> ...
> {code}
> gives the result:
> {code}
> @attr1]=null
> @attr2]=null
> {code}
> it should give the result
> {code}
> [@attr1]=attr1
> [@attr2]=attr2
> {code}
> The wrong dump is a side effect of the wrong implementation of the _getChildKey_ method of SubsetConfiguration
> {code}
>  /**
>      * 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;
>         }
>     }
> {code}
> 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 :
> {code}
> /**
>      * 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;
>         }
>     }
> {code}

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

        

[jira] [Commented] (CONFIGURATION-442) SubsetConfiguration does not properly list attributes when a delimiter is set

Posted by "Oliver Heger (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/CONFIGURATION-442?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13017563#comment-13017563 ] 

Oliver Heger commented on CONFIGURATION-442:
--------------------------------------------

Thanks for the clearification.

In this case I would suggest that you switch from {{ConfigurationFactory}} to {{DefaultConfigurationBuilder}}. {{DefaultConfigurationBuilder}} is intended to be a full replacement for {{ConfigurationFactory}}. Its API is very similar, and it can process the same configuration definition files. It provides some more features and internally converts all configurations to hierarchical ones. More information is available in the user's guide at http://commons.apache.org/configuration/userguide/howto_configurationbuilder.html. This should solve the problems with iterating over the keys of the sub configuration.

Because {{DefaultConfigurationBuilder}} is more powerful than {{ConfigurationFactory}}, we will probably deprecate the latter at some point of time. (And we certainly have to update our documentation to emphasize this.) Therefore I am not too keen to fix this bug if the alternative I suggested works for you.

> SubsetConfiguration does not properly list attributes when a delimiter is set
> -----------------------------------------------------------------------------
>
>                 Key: CONFIGURATION-442
>                 URL: https://issues.apache.org/jira/browse/CONFIGURATION-442
>             Project: Commons Configuration
>          Issue Type: Bug
>    Affects Versions: 1.6
>         Environment: all
>            Reporter: Fabien Nisol
>
> imagine a XmlConfiguration like this:
> {code}
> <properties>
>   <prop1>
>     <prop2>
>       <prop
>          attr1="attr1"
>          attr2="attr2"/>
>     </prop2>
>   </prop1>
> </properties>
> {code}
> If subset get instanciated to the end of the hierarchy, with a specific delimiter, getKeys() won't return the correct keys:
> {code}
> ...
> XMLConfiguration config = new XMLConfiguration("test/test.xml");
> Configuration subset = new SubsetConfiguration(config,"prop1.prop2.prop",".");
> ConfigurationUtils.dump(subset, System.err);
> ...
> {code}
> gives the result:
> {code}
> @attr1]=null
> @attr2]=null
> {code}
> it should give the result
> {code}
> [@attr1]=attr1
> [@attr2]=attr2
> {code}
> The wrong dump is a side effect of the wrong implementation of the _getChildKey_ method of SubsetConfiguration
> {code}
>  /**
>      * 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;
>         }
>     }
> {code}
> 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 :
> {code}
> /**
>      * 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;
>         }
>     }
> {code}

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Commented] (CONFIGURATION-442) SubsetConfiguration does not properly list attributes when a delimiter is set

Posted by "Oliver Heger (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/CONFIGURATION-442?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13016484#comment-13016484 ] 

Oliver Heger commented on CONFIGURATION-442:
--------------------------------------------

Is there a special reason why you instantiate the {{SubsetConfiguration}} directly rather than calling the {{subset()}} method of the configuration?

{{HirachicalConfiguration}} overrides {{subset()}} to construct a specialized hierarchical subset configuration. This configuration should return a correct iterator for its keys.

You are probably right that {{SubsetConfiguration}} does not play well with a hierarchical configuration, but this is the reason why {{HierarchicalConfiguration}} provides an alternative implementation of the {{subset()}} method. So the intended usage scenario is to call {{subset()}} on the target configuration rather than creating the {{SubsetConfiguration}} manually.

Would this solve your problem?

> SubsetConfiguration does not properly list attributes when a delimiter is set
> -----------------------------------------------------------------------------
>
>                 Key: CONFIGURATION-442
>                 URL: https://issues.apache.org/jira/browse/CONFIGURATION-442
>             Project: Commons Configuration
>          Issue Type: Bug
>    Affects Versions: 1.6
>         Environment: all
>            Reporter: Fabien Nisol
>
> imagine a XmlConfiguration like this:
> {code}
> <properties>
>   <prop1>
>     <prop2>
>       <prop
>          attr1="attr1"
>          attr2="attr2"/>
>     </prop2>
>   </prop1>
> </properties>
> {code}
> If subset get instanciated to the end of the hierarchy, with a specific delimiter, getKeys() won't return the correct keys:
> {code}
> ...
> XMLConfiguration config = new XMLConfiguration("test/test.xml");
> Configuration subset = new SubsetConfiguration(config,"prop1.prop2.prop",".");
> ConfigurationUtils.dump(subset, System.err);
> ...
> {code}
> gives the result:
> {code}
> @attr1]=null
> @attr2]=null
> {code}
> it should give the result
> {code}
> [@attr1]=attr1
> [@attr2]=attr2
> {code}
> The wrong dump is a side effect of the wrong implementation of the _getChildKey_ method of SubsetConfiguration
> {code}
>  /**
>      * 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;
>         }
>     }
> {code}
> 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 :
> {code}
> /**
>      * 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;
>         }
>     }
> {code}

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Commented] (CONFIGURATION-442) SubsetConfiguration does not properly list attributes when a delimiter is set

Posted by "Fabien Nisol (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/CONFIGURATION-442?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13017490#comment-13017490 ] 

Fabien Nisol commented on CONFIGURATION-442:
--------------------------------------------

The test was just to point you to the good direction. We don't directly instanciate a SubsetConfiguration, it is done if it happens that your base configuration is a CompositeConfiguration

example:
{code}
public class SimpleTest

{

    public static void main(String[] args)
    {
        try
        {
            ConfigurationFactory factory = new ConfigurationFactory("config.xml");
                        
            Configuration config = factory.getConfiguration();
            
            String prefix = "prop1.prop2.prop";
            Configuration subconf = config.subset(prefix);
            System.out.printf("%s (%s) content:%n",subconf.getClass(),prefix);
            ConfigurationUtils.dump(subconf,System.out);
            System.out.println();
            

        } catch (ConfigurationException e)
        {
            e.printStackTrace();
        }
    }
}
{code}
{code:title=config.xml}
<configuration>
	<system/>
	<xml fileName="test.xml"/>
</configuration>
{code}

{code:title=test.xml}
<properties>
	<prop1>
		<prop2>
			<prop
				attr1="${user.home}"
				attr2="attr2"
			/>
		</prop2>
	</prop1>
</properties>
{code}

executing the test gives the following result:

{code:title=stdout}
class org.apache.commons.configuration.SubsetConfiguration (prop1.prop2.prop) content:
@attr1]=null
@attr2]=null
{code}

Since ConfigurationUtils lists the keys through the getKeys() method

{code:title=ConfigurationUtils.java::dump()}
public static void dump(Configuration configuration, PrintWriter out)
    {
        Iterator keys = configuration.getKeys();
        while (keys.hasNext())
        {
            String key = (String) keys.next();
            Object value = configuration.getProperty(key);
            out.print(key);
            out.print("=");
            out.print(value);

            if (keys.hasNext())
            {
                out.println();
            }
        }

        out.flush();
    }
{code}

So there is definitely a bug in the getKeys() implementation, which is of no use if someone tries to use it. (Which is our case by the way)

> SubsetConfiguration does not properly list attributes when a delimiter is set
> -----------------------------------------------------------------------------
>
>                 Key: CONFIGURATION-442
>                 URL: https://issues.apache.org/jira/browse/CONFIGURATION-442
>             Project: Commons Configuration
>          Issue Type: Bug
>    Affects Versions: 1.6
>         Environment: all
>            Reporter: Fabien Nisol
>
> imagine a XmlConfiguration like this:
> {code}
> <properties>
>   <prop1>
>     <prop2>
>       <prop
>          attr1="attr1"
>          attr2="attr2"/>
>     </prop2>
>   </prop1>
> </properties>
> {code}
> If subset get instanciated to the end of the hierarchy, with a specific delimiter, getKeys() won't return the correct keys:
> {code}
> ...
> XMLConfiguration config = new XMLConfiguration("test/test.xml");
> Configuration subset = new SubsetConfiguration(config,"prop1.prop2.prop",".");
> ConfigurationUtils.dump(subset, System.err);
> ...
> {code}
> gives the result:
> {code}
> @attr1]=null
> @attr2]=null
> {code}
> it should give the result
> {code}
> [@attr1]=attr1
> [@attr2]=attr2
> {code}
> The wrong dump is a side effect of the wrong implementation of the _getChildKey_ method of SubsetConfiguration
> {code}
>  /**
>      * 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;
>         }
>     }
> {code}
> 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 :
> {code}
> /**
>      * 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;
>         }
>     }
> {code}

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira

[jira] [Commented] (CONFIGURATION-442) SubsetConfiguration does not properly list attributes when a delimiter is set

Posted by "Fabien Nisol (JIRA)" <ji...@apache.org>.
    [ https://issues.apache.org/jira/browse/CONFIGURATION-442?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=13079572#comment-13079572 ] 

Fabien Nisol commented on CONFIGURATION-442:
--------------------------------------------

It does ! Thanks :)

> SubsetConfiguration does not properly list attributes when a delimiter is set
> -----------------------------------------------------------------------------
>
>                 Key: CONFIGURATION-442
>                 URL: https://issues.apache.org/jira/browse/CONFIGURATION-442
>             Project: Commons Configuration
>          Issue Type: Bug
>    Affects Versions: 1.6
>         Environment: all
>            Reporter: Fabien Nisol
>
> imagine a XmlConfiguration like this:
> {code}
> <properties>
>   <prop1>
>     <prop2>
>       <prop
>          attr1="attr1"
>          attr2="attr2"/>
>     </prop2>
>   </prop1>
> </properties>
> {code}
> If subset get instanciated to the end of the hierarchy, with a specific delimiter, getKeys() won't return the correct keys:
> {code}
> ...
> XMLConfiguration config = new XMLConfiguration("test/test.xml");
> Configuration subset = new SubsetConfiguration(config,"prop1.prop2.prop",".");
> ConfigurationUtils.dump(subset, System.err);
> ...
> {code}
> gives the result:
> {code}
> @attr1]=null
> @attr2]=null
> {code}
> it should give the result
> {code}
> [@attr1]=attr1
> [@attr2]=attr2
> {code}
> The wrong dump is a side effect of the wrong implementation of the _getChildKey_ method of SubsetConfiguration
> {code}
>  /**
>      * 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;
>         }
>     }
> {code}
> 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 :
> {code}
> /**
>      * 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;
>         }
>     }
> {code}

--
This message is automatically generated by JIRA.
For more information on JIRA, see: http://www.atlassian.com/software/jira