You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@commons.apache.org by Thorbjørn Ravn Andersen <th...@gmail.com> on 2007/01/24 19:50:42 UTC

[configuration] XML, DefaultConfigurationBuilder: configurationAt gives different result than fully qualified keys

Hi,

I am trying to migrate to commons configuration for all the usual 
reasons, but have run into the following problem which I would 
appreciate if a more knowledgeable person than me would glance at.  I 
want to migrate a set of property files into a single XML-file to have a 
more maintainable system, while keeping the code as similar as possible.


The following code gives me an incorrect result:

        SubnodeConfiguration c2 = 
configuration.configurationAt("environments/environment[@name='dvl9']");

The keys appear to be correct of c2, but the values are all empty. 

This is against SVN of today, as the 1.3 release hits a JIRA.

-- output --

1 configurations:
environments/environment/env/@name = [server.name, server.type, 
server.program.lib]
environments/environment/env/@value = [server, AS/400, 
/QSYS.LIB/DVEROBJ.LIB/]
environments/environment/@name = [dvl9]
-----
env/@name = []
env/@value = []
@name = []


--  configuration file --

<?xml version="1.0" encoding="ISO-8859-1" ?>

<configuration>
    <header>
        <result>
            <expressionEngine
                
config-class="org.apache.commons.configuration.tree.xpath.XPathExpressionEngine" 
/>
        </result>
    </header>
    <override>
        <xml config-name="s" fileName="environments.xml"
            config-at="environments">
        </xml>
    </override>
    <additional>

        <!-- Configuration declarations that form a union configuration -->
    </additional>
</configuration>

-- environments.xml ----

<!-- four soft default environments -->
<environments>
    <environment name="dvl9">
        <env name="server.name" value="server" />
        <env name="server.type" value="AS/400" />
        <env name="server.program.lib" value="/QSYS.LIB/DVEROBJ.LIB/" />
    </environment>
</environments>

-- code --

package bad;


import java.util.Iterator;

import org.apache.commons.configuration.CombinedConfiguration;
import org.apache.commons.configuration.ConfigurationException;
import org.apache.commons.configuration.DefaultConfigurationBuilder;
import org.apache.commons.configuration.SubnodeConfiguration;

public class ConfigurationUtils {
   
    public CombinedConfiguration getConfiguration() {
        try {
            return getConfiguration_();
        } catch (ConfigurationException e) {
            throw new RuntimeException("bad configuration", e);
        }
    }
   
    public CombinedConfiguration getConfiguration_() throws 
ConfigurationException {
        DefaultConfigurationBuilder builder = new 
DefaultConfigurationBuilder();
        builder.setFileName("config.xml");
        return builder.getConfiguration(true);
    }
   
    public static void main(String[] args) {
        CombinedConfiguration configuration = new 
ConfigurationUtils().getConfiguration();
        System.out.println(configuration.getNumberOfConfigurations() + " 
configurations:");
        Iterator keys = configuration.getKeys();
        while(keys.hasNext()) {
            String next = (String) keys.next();
            System.out.println(next + " = " + configuration.getList(next));
        }
        System.out.println("-----");
        SubnodeConfiguration c2 = 
configuration.configurationAt("environments/environment[@name='dvl9']");
        Iterator keys2 = c2.getKeys();
        while(keys2.hasNext()) {
            String next = (String) keys2.next();
            System.out.println(next + " = " + configuration.getList(next));
        }
    }
}

==
I would appreciate any pointers and hints helping me figuring out what I 
have misunderstood.

-- 
  Thorbjørn Ravn Andersen

Re: [configuration] XML, DefaultConfigurationBuilder: configurationAt gives different result than fully qualified keys

Posted by Thorbjørn Ravn Andersen <th...@gmail.com>.
Oliver Heger skrev  den 24-01-2007 21:55:
>
> Instead of
> configuration.getList(next);
> it should be
> c2.getList(next);
>
You are absolutely correct.   A typical cut-and-paste error which you 
cannot see by yourself :-(

After correcting it, I get proper results.

I appreciate your time and prompt response :)



-- 
  Thorbjørn

Re: [configuration] XML, DefaultConfigurationBuilder: configurationAt gives different result than fully qualified keys

Posted by Oliver Heger <ol...@oliver-heger.de>.
Hi,

maybe this is a typo, but the problem seems to be in the last while() 
loop of your test program:

 >        SubnodeConfiguration c2 =
 > configuration.configurationAt("environments/environment[@name='dvl9']");
 >        Iterator keys2 = c2.getKeys();
 >        while(keys2.hasNext()) {
 >            String next = (String) keys2.next();
 >            System.out.println(next + " = " + 
configuration.getList(next));
 >        }

Instead of
configuration.getList(next);
it should be
c2.getList(next);

I hope this is all!
Oliver

Thorbjørn Ravn Andersen wrote:
> Hi,
> 
> I am trying to migrate to commons configuration for all the usual 
> reasons, but have run into the following problem which I would 
> appreciate if a more knowledgeable person than me would glance at.  I 
> want to migrate a set of property files into a single XML-file to have a 
> more maintainable system, while keeping the code as similar as possible.
> 
> 
> The following code gives me an incorrect result:
> 
>        SubnodeConfiguration c2 = 
> configuration.configurationAt("environments/environment[@name='dvl9']");
> 
> The keys appear to be correct of c2, but the values are all empty.
> This is against SVN of today, as the 1.3 release hits a JIRA.
> 
> -- output --
> 
> 1 configurations:
> environments/environment/env/@name = [server.name, server.type, 
> server.program.lib]
> environments/environment/env/@value = [server, AS/400, 
> /QSYS.LIB/DVEROBJ.LIB/]
> environments/environment/@name = [dvl9]
> -----
> env/@name = []
> env/@value = []
> @name = []
> 
> 
> --  configuration file --
> 
> <?xml version="1.0" encoding="ISO-8859-1" ?>
> 
> <configuration>
>    <header>
>        <result>
>            <expressionEngine
>                
> config-class="org.apache.commons.configuration.tree.xpath.XPathExpressionEngine" 
> />
>        </result>
>    </header>
>    <override>
>        <xml config-name="s" fileName="environments.xml"
>            config-at="environments">
>        </xml>
>    </override>
>    <additional>
> 
>        <!-- Configuration declarations that form a union configuration -->
>    </additional>
> </configuration>
> 
> -- environments.xml ----
> 
> <!-- four soft default environments -->
> <environments>
>    <environment name="dvl9">
>        <env name="server.name" value="server" />
>        <env name="server.type" value="AS/400" />
>        <env name="server.program.lib" value="/QSYS.LIB/DVEROBJ.LIB/" />
>    </environment>
> </environments>
> 
> -- code --
> 
> package bad;
> 
> 
> import java.util.Iterator;
> 
> import org.apache.commons.configuration.CombinedConfiguration;
> import org.apache.commons.configuration.ConfigurationException;
> import org.apache.commons.configuration.DefaultConfigurationBuilder;
> import org.apache.commons.configuration.SubnodeConfiguration;
> 
> public class ConfigurationUtils {
>      public CombinedConfiguration getConfiguration() {
>        try {
>            return getConfiguration_();
>        } catch (ConfigurationException e) {
>            throw new RuntimeException("bad configuration", e);
>        }
>    }
>      public CombinedConfiguration getConfiguration_() throws 
> ConfigurationException {
>        DefaultConfigurationBuilder builder = new 
> DefaultConfigurationBuilder();
>        builder.setFileName("config.xml");
>        return builder.getConfiguration(true);
>    }
>      public static void main(String[] args) {
>        CombinedConfiguration configuration = new 
> ConfigurationUtils().getConfiguration();
>        System.out.println(configuration.getNumberOfConfigurations() + " 
> configurations:");
>        Iterator keys = configuration.getKeys();
>        while(keys.hasNext()) {
>            String next = (String) keys.next();
>            System.out.println(next + " = " + configuration.getList(next));
>        }
>        System.out.println("-----");
>        SubnodeConfiguration c2 = 
> configuration.configurationAt("environments/environment[@name='dvl9']");
>        Iterator keys2 = c2.getKeys();
>        while(keys2.hasNext()) {
>            String next = (String) keys2.next();
>            System.out.println(next + " = " + configuration.getList(next));
>        }
>    }
> }
> 
> ==
> I would appreciate any pointers and hints helping me figuring out what I 
> have misunderstood.
> 


---------------------------------------------------------------------
To unsubscribe, e-mail: commons-user-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-user-help@jakarta.apache.org