Java 使用Apache Commons配置在属性文件中使用值列表执行变量插值,即${variable}
我正在使用ApacheCommons配置来读取属性文件,我完全能够执行变量插值,并且能够以列表的形式检索多值属性。但是,我无法正确加载具有多个值的属性,其中一个值是对另一个多值属性的引用(变量插值) 下面是我的属性文件的一个示例(我还尝试使用逗号分隔的语法): 我是如何从中读到的:Java 使用Apache Commons配置在属性文件中使用值列表执行变量插值,即${variable},java,properties,apache-commons-config,Java,Properties,Apache Commons Config,我正在使用ApacheCommons配置来读取属性文件,我完全能够执行变量插值,并且能够以列表的形式检索多值属性。但是,我无法正确加载具有多个值的属性,其中一个值是对另一个多值属性的引用(变量插值) 下面是我的属性文件的一个示例(我还尝试使用逗号分隔的语法): 我是如何从中读到的: Configuration config = new PropertiesConfiguration("myFile"); final String[] mimesArray = config.getStringAr
Configuration config = new PropertiesConfiguration("myFile");
final String[] mimesArray = config.getStringArray("doc.mime");
for(String mime : mimesArray) System.out.println(mime);
final List<Object> mimesList = config.getList("doc.mime");
System.out.println(mimesList);
Configuration config=新属性配置(“myFile”);
最终字符串[]mimesArray=config.getStringArray(“doc.mime”);
for(字符串mime:mimesArray)System.out.println(mime);
最终列表mimesList=config.getList(“doc.mime”);
System.out.println(模拟列表);
这是我通过任何一种方法(getStringArray
和getList
)获得的内容:
[application/msword,application/vnd.openxmlformats-officedocument.wordprocessingml.document,application/x-tika-msoffice]
这与我预期的不同:doc.mime和office.mime的完整内容
有人知道是否可以在我的另一个列表中插入整个值列表吗?如果是这样的话,它是如何完成的?什么是Commons配置的
public class CustomInterpolation extends StrLookup {
/*
* (non-Javadoc)
*
* @see org.apache.commons.lang.text.StrLookup#lookup(java.lang.String)
*/
@Override
public String lookup(String arg0) {
String result = null;
// Get the default delimiter.
String delimiter = ""
+ PropertiesConfiguration.getDefaultListDelimiter();
try {
// Load the properties file.
Configuration config = new PropertiesConfiguration(
"ressources/macro.properties");
if (config.containsKey(arg0)) {
// Get all values associated with the propertie.
ArrayList<Object> values = (ArrayList<Object>) config
.getList(arg0);
StringBuilder strBuild = new StringBuilder();
Iterator<Object> itr = values.iterator();
while (itr.hasNext()) {
// Append the property to the string.
strBuild.append((String) itr.next());
if (itr.hasNext()) {
// Adds the delimiter and backslash in order to retrieve
// all properties later.
strBuild.append("\\" + delimiter);
}
}
result = strBuild.toString();
}
} catch (ConfigurationException e) {
// Nothing to do here...
}
// return null or all values concatenated
return result;
}
}
/**
* The Class CustomPropertiesConfiguration.
*/
public class CustomPropertiesConfiguration extends PropertiesConfiguration {
private String delimiter;
/**
* Instantiates a new custom properties configuration.
*/
public CustomPropertiesConfiguration() {
super();
delimiter = PropertiesConfiguration.getDefaultListDelimiter()
+ "";
}
/**
* Instantiates a new custom properties configuration.
*
* @param file the file
* @throws ConfigurationException the configuration exception
*/
public CustomPropertiesConfiguration (File file) throws ConfigurationException{
super(file);
delimiter = PropertiesConfiguration.getDefaultListDelimiter()
+ "";
}
/**
* Instantiates a new custom properties configuration.
*
* @param fileName the file name
* @throws ConfigurationException the configuration exception
*/
public CustomPropertiesConfiguration(String fileName) throws ConfigurationException {
super(fileName);
delimiter = PropertiesConfiguration.getDefaultListDelimiter()
+ "";
}
/**
* Instantiates a new custom properties configuration.
*
* @param url the url
* @throws ConfigurationException the configuration exception
*/
public CustomPropertiesConfiguration(URL url) throws ConfigurationException{
super(url);
delimiter = PropertiesConfiguration.getDefaultListDelimiter()
+ "";
}
/* (non-Javadoc)
* @see org.apache.commons.configuration.AbstractConfiguration#getList(java.lang.String)
*/
@Override
public List<Object> getList(String key) {
// Get the list of values associated with the property
// Implicit call to the custom interpolation class
List<Object> properties = super.getList(key);
ArrayList<Object> extendedProperties = new ArrayList<Object>();
Iterator<Object> itrProperties = properties.iterator();
// Go through all properties and retrieve values concatenated by the custom interpolation
while (itrProperties.hasNext()) {
String propertie = (String) itrProperties.next();
if (propertie.contains(delimiter)) {
//Split concatenated values.
String[] extendedPropertiesTab = propertie.split("\\\\"+delimiter);
// Add the retrieved values to the list of values.
for (int i = 0; i< extendedPropertiesTab.length; ++i){
extendedProperties.add(extendedPropertiesTab[i]);
}
} else {
extendedProperties.add(propertie);
}
}
return extendedProperties;
}
}
正如您所发现的:当插值多值属性时,Commons配置将只解析该属性的第一个值。请参阅上的代码
我发现了一些相关问题:
:有人想要(并且得到)与你想要的完全相反的东西:只有多值属性中的第一个值
:有关多值属性插值的更多讨论:
这个问题可能没有正确的解决方案,因为
预期结果在很大程度上取决于具体的用例
解决方法:将两个列表合并到代码中
绝对比:
List-mimesList=config.getList(“doc.mime”);
List officeList=config.getList(“office.mime”);
mimesList.addAll(officeList);
System.out.println(模拟列表);
向Commons配置项目提出此问题
改变整个变量插值系统可能很困难。但他们至少可以澄清文档。好吧,我确实需要这个功能:将几个值与属性关联起来,甚至可以通过插值来检索它们 因此,我编写了一个自定义插值类和一个自定义属性配置。即使使用带有反斜杠和默认分隔符的值,也可以正常工作 这里是自定义插值类: 自定义插值类
public class CustomInterpolation extends StrLookup {
/*
* (non-Javadoc)
*
* @see org.apache.commons.lang.text.StrLookup#lookup(java.lang.String)
*/
@Override
public String lookup(String arg0) {
String result = null;
// Get the default delimiter.
String delimiter = ""
+ PropertiesConfiguration.getDefaultListDelimiter();
try {
// Load the properties file.
Configuration config = new PropertiesConfiguration(
"ressources/macro.properties");
if (config.containsKey(arg0)) {
// Get all values associated with the propertie.
ArrayList<Object> values = (ArrayList<Object>) config
.getList(arg0);
StringBuilder strBuild = new StringBuilder();
Iterator<Object> itr = values.iterator();
while (itr.hasNext()) {
// Append the property to the string.
strBuild.append((String) itr.next());
if (itr.hasNext()) {
// Adds the delimiter and backslash in order to retrieve
// all properties later.
strBuild.append("\\" + delimiter);
}
}
result = strBuild.toString();
}
} catch (ConfigurationException e) {
// Nothing to do here...
}
// return null or all values concatenated
return result;
}
}
/**
* The Class CustomPropertiesConfiguration.
*/
public class CustomPropertiesConfiguration extends PropertiesConfiguration {
private String delimiter;
/**
* Instantiates a new custom properties configuration.
*/
public CustomPropertiesConfiguration() {
super();
delimiter = PropertiesConfiguration.getDefaultListDelimiter()
+ "";
}
/**
* Instantiates a new custom properties configuration.
*
* @param file the file
* @throws ConfigurationException the configuration exception
*/
public CustomPropertiesConfiguration (File file) throws ConfigurationException{
super(file);
delimiter = PropertiesConfiguration.getDefaultListDelimiter()
+ "";
}
/**
* Instantiates a new custom properties configuration.
*
* @param fileName the file name
* @throws ConfigurationException the configuration exception
*/
public CustomPropertiesConfiguration(String fileName) throws ConfigurationException {
super(fileName);
delimiter = PropertiesConfiguration.getDefaultListDelimiter()
+ "";
}
/**
* Instantiates a new custom properties configuration.
*
* @param url the url
* @throws ConfigurationException the configuration exception
*/
public CustomPropertiesConfiguration(URL url) throws ConfigurationException{
super(url);
delimiter = PropertiesConfiguration.getDefaultListDelimiter()
+ "";
}
/* (non-Javadoc)
* @see org.apache.commons.configuration.AbstractConfiguration#getList(java.lang.String)
*/
@Override
public List<Object> getList(String key) {
// Get the list of values associated with the property
// Implicit call to the custom interpolation class
List<Object> properties = super.getList(key);
ArrayList<Object> extendedProperties = new ArrayList<Object>();
Iterator<Object> itrProperties = properties.iterator();
// Go through all properties and retrieve values concatenated by the custom interpolation
while (itrProperties.hasNext()) {
String propertie = (String) itrProperties.next();
if (propertie.contains(delimiter)) {
//Split concatenated values.
String[] extendedPropertiesTab = propertie.split("\\\\"+delimiter);
// Add the retrieved values to the list of values.
for (int i = 0; i< extendedPropertiesTab.length; ++i){
extendedProperties.add(extendedPropertiesTab[i]);
}
} else {
extendedProperties.add(propertie);
}
}
return extendedProperties;
}
}
我得到以下输出:
base.prop=>
[0]/base
[1]/root\\
[2]t,t,t,t,
first.prop=>
[0]/base
[1]/root\\
[2]t,t,t,t,
[3]/first,/base
second.prop=>
[0]/base
[1]/root\\
[2]t,t,t,t,
[3]/first,/base
[4]/second
如您所见,该解决方案能够使用反斜杠和默认分隔符“,”处理属性值。包含这两个元素的某些模式可能无法正确处理,但此解决方案应处理基本值。这是一个有点特殊的问题,可能只有熟悉commons配置(如开发或调试commons配置)的人才能回答。你查过了吗,也许吧?
base.prop = /base, /root\\\\\\\\, t\\,t\\,t\\,t\\,
first.prop = ${custom:base.prop}, /first\\,/base
second.prop = ${custom:first.prop}, /second
base.prop=>
[0]/base
[1]/root\\
[2]t,t,t,t,
first.prop=>
[0]/base
[1]/root\\
[2]t,t,t,t,
[3]/first,/base
second.prop=>
[0]/base
[1]/root\\
[2]t,t,t,t,
[3]/first,/base
[4]/second