2013-07-24 31 views
0
<properties> 
    <property name="p1">v1</property> 
    <property name="p2">v2</property> 
</properties> 

我想 {"p1"="v1", "p2"="v2"}解析成Properties對象這個默認值的屬性。我能夠使用普通消化器做到這一點。解析爲使用共享沼氣池

forPattern("properties").createObject() 
     .ofType(Properties.class); 
forPattern("properties/property") 
     .callMethod("put").withParamCount(2) 
     .withParamTypes(Object.class, Object.class).then() 
     .callParam().fromAttribute("name").ofIndex(0).then() 
     .callParam().ofIndex(1); 

另外我還想通過我有的默認對象。即,而不是new Properties(),我需要做new Properties(defaults)來創建屬性實例。我知道.usingConstructor(Properties.class).then(),但沒有找到一種方法來通過現有的Properties對象作爲參數。

我正在使用v3.3.2。 任何幫助是極大的讚賞

回答

0

好吧,我想通了,這是可以做到使用FactoryCreateRule

class PropertiesFactory extends AbstractObjectCreationFactory<Properties> { 

    Properties defaultProperties; 

    public PropertiesFactory(Properties defaultProperties) { 
     super(); 
     this.defaultProperties = defaultProperties; 
    } 

    @Override 
    public Properties createObject(Attributes attributes) throws Exception { 
     return new Properties(defaultProperties); 
    } 

} 

... 
... 

forPattern("properties").factoryCreate() 
     .usingFactory(new PropertiesFactory(defaultProperties)); 
forPattern("properties/property") 
     .callMethod("put").withParamCount(2) 
     .withParamTypes(Object.class, Object.class).then() 
     .callParam().fromAttribute("name").ofIndex(0).then() 
     .callParam().ofIndex(1);