我正在使用Apache Commons Configuration来读取属性文件,我完全能够进行变量插值,并且还可以将多值属性作为列表进行检索。但是,我无法正确加载具有多个值的属性,其中一个属性是另一个多值属性的引用(变量插值)。
以下是我的属性文件示例(我也尝试使用逗号分隔语法):
doc.mime=application/msword
doc.mime=application/vnd.openxmlformats-officedocument.wordprocessingml.document
doc.mime=${office.mime}
office.mime=application/x-tika-msoffice
office.mime=application/x-tika-ooxml
我是如何阅读的:
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);
这是我使用任何一种方法(getStringArray
和getList
)获得的内容:
[application/msword, application/vnd.openxmlformats-officedocument.wordprocessingml.document, application/x-tika-msoffice]
这与我的预期不同:doc.mime
和office.mime
的完整内容
有谁知道是否可以在我的其他列表中插入整个值列表?如果是这样,它是如何完成的?
答案 0 :(得分:4)
Commons配置的功能
正如您所发现的:当插入多值属性时,Commons Configuration将仅解析该属性的第一个值。请参阅AbstractConfiguration#resolveContainerStore() line 1177上的代码。
我发现了一些相关问题:
CONFIGURATION-28:有人想要(和得到)与你想要的完全相反:只有多值属性中的第一个值。
CONFIGURATION-55:关于多值属性插值的更多讨论:
这个问题可能没有正确的解决办法,因为 预期结果很大程度上取决于具体的用例
解决方法:合并代码中的两个列表
绝对比customizing interpolation更容易:
List<Object> mimesList = config.getList("doc.mime");
List<Object> officeList = config.getList("office.mime");
mimesList.addAll(officeList);
System.out.println(mimesList);
使用Commons Configuration项目提出此问题
更改整个变量插值系统可能很困难。但他们至少可以澄清文件。
答案 1 :(得分:1)
好吧,我确实需要这个功能:将几个值与属性相关联,并且即使通过插值也可以检索它们。
所以我编写了一个自定义插值类和一个自定义属性配置。即使使用带反斜杠和默认分隔符的值也能正常工作。
所以这里是自定义插值类:
CustomInterPolation类
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;
}
}
现在,为了正确使用这个customInterpolation类,我们需要使用自定义属性配置:
CustomPropertiesConfiguration类
/**
* 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;
}
}
这里是一个小型的主要课程:
public class TestMacro {
/**
* @param args
*/
public static void main(String[] args) {
// Load properties file :
try {
// Add an interpolation to the configuration.
// The string "custom" will be used to find value to interpolate
// with the custom interpolation
ConfigurationInterpolator.registerGlobalLookup("custom",
new CustomInterpolation());
// Set the properties configuration.
Configuration config = new CustomPropertiesConfiguration(
"ressources/macro.properties");
String baseProp = "base.prop";
String firstProp = "first.prop";
String secondProp = "second.prop";
ArrayList<Object> values = (ArrayList<Object>) config
.getList(baseProp);
System.out.println(baseProp + "=>");
for (int i = 0; i < values.size(); ++i) {
System.out.println("[" + i + "]" + values.get(i));
}
System.out.println();
values = (ArrayList<Object>) config.getList(firstProp);
System.out.println(firstProp + "=>");
for (int i = 0; i < values.size(); ++i) {
System.out.println("[" + i + "]" + values.get(i));
}
System.out.println();
values = (ArrayList<Object>) config.getList(secondProp);
System.out.println(secondProp + "=>");
for (int i = 0; i < values.size(); ++i) {
System.out.println("[" + i + "]" + values.get(i));
}
} catch (ConfigurationException e) {
e.printStackTrace();
}
}
}
对于测试,我使用了以下属性文件:
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
正如您所看到的,解决方案能够处理属性&#39;带反斜杠的值和默认分隔符&#39;,&#39;。某些包含这两个元素的模式可能无法正确处理,但此解决方案应该处理基本值。