0

I have a CompositeConfiguration object which has some property configurations and some XMLConfigurations.For the XMLConfiguration, the DefaultExpressionEngine is set as XPathExpressionEngine

One of the .properties file has properties starting with "." eg : .propertyName="name"

CompositeConfiguration compositeConfiguration = new CompositeConfiguration();

XMLConfiguration xmlConfig = new XMLConfiguration("xmlFile.xml");
xmlConfig.setDefaultExpressionEngine(new XPathExpressionEngine());

compositeConfiguration.addConfiguration(xmlConfig);

compositeConfiguration.addConfiguration(new PropertiesConfiguration(new File("file1.properties")));
compositeConfiguration.addConfiguration(new PropertiesConfiguration(new File("file2.properties")));
compositeConfiguration.addConfiguration(new PropertiesConfiguration(new File("file3.properties"))); 
compositeConfiguration.getProperty(".propertyName");

I get the following exception:

org.apache.commons.jxpath.JXPathInvalidSyntaxException: Invalid XPath: '.propertyName'. Syntax error after "."
at org.apache.commons.jxpath.ri.Parser.parseExpression(Parser.java:63)
at org.apache.commons.jxpath.ri.JXPathContextReferenceImpl.compileExpression(JXPathContextReferenceImpl.java:249)
at org.apache.commons.jxpath.ri.JXPathContextReferenceImpl.iteratePointers(JXPathContextReferenceImpl.java:616)
at org.apache.commons.jxpath.JXPathContext.selectNodes(JXPathContext.java:686)
at org.apache.commons.configuration.tree.xpath.XPathExpressionEngine.query(XPathExpressionEngine.java:183)
at org.apache.commons.configuration.HierarchicalConfiguration.fetchNodeList(HierarchicalConfiguration.java:958)
at org.apache.commons.configuration.AbstractHierarchicalFileConfiguration.fetchNodeList(AbstractHierarchicalFileConfiguration.java:439)
at org.apache.commons.configuration.HierarchicalConfiguration.getProperty(HierarchicalConfiguration.java:344)
at org.apache.commons.configuration.AbstractHierarchicalFileConfiguration.getProperty(AbstractHierarchicalFileConfiguration.java:392)
at org.apache.commons.configuration.HierarchicalConfiguration.containsKey(HierarchicalConfiguration.java:725)
at org.apache.commons.configuration.AbstractHierarchicalFileConfiguration.containsKey(AbstractHierarchicalFileConfiguration.java:360)
at org.apache.commons.configuration.CompositeConfiguration.getProperty(CompositeConfiguration.java:261)

This exception is because CompositeConfiguration goes through each configuration and call getProperty on that configuration with the key which is ".propertyName" in this case. When the XPathExpression Engine is used to get the value from the XMLConfiguration, it breaks saying invalid xpath.

This seems like an easily possible/common scenario. Has anyone else faced similar issue ? Is there any way to get around this problem ?

Any other configuration types I could use ? I am using CompositeConfiguration because I want to use give priority to properties loaded first.

Any hints will be of great help to me please.

Thanks, Tejas

1 Answers1

0

Just guessing, but have you already tried to change

compositeConfiguration.addConfiguration(new PropertiesConfiguration(new File("file1.properties")));  

into

compositeConfiguration.addConfiguration(new PropertiesConfiguration("file1.properties"));  

for all three properties?

Maybe it'll just throw the next error but that's the notation described here: http://commons.apache.org/proper/commons-configuration/userguide/overview.html#Mixing_Configuration_Sources

And on SO: How to load multiple configuration files using apache common configuration(java)

Community
  • 1
  • 1
matthias_h
  • 11,356
  • 9
  • 22
  • 40