Open fivesmallq opened 8 years ago
@ptyagi108 maybe you can use filter to process this.
.extract("config.pollInterval", xpath("//activity/config/pollInterval/text()"))
//if pollInterval is null set to default '5'
.filter(value -> value == null ? value : "5")
.extract("config.compressFile", xpath("//activity/config/compressFile/text()"))
or you can set the default value to the config field ?
Pls check...I have updated my comments..filter may not work...
Please see my updated comments, Basic issue is how to handle polymorphism with this library.
@ptyagi108 OK, I Will think about it.
Currentely i have made some changes to library in my local to get my case working , please suggest how can i make this implementation better.
public interface Extractor<T> {
T extract(String data);
}
im.nll.data.extractor.Extractors#extractBean
private <T> T extractBean(String html, Class<T> clazz) {
// only support String type
if (clazz.equals(String.class)) {
return (T) new String(html);
}
T entity = Reflect.on(clazz).create().get();
for (Map.Entry<String, List<Extractor>> one : extractorsMap.entrySet()) {
String name = one.getKey();
List<Extractor> extractors = one.getValue();
String result = html;
for (Extractor extractor : extractors) {
if(!(extractor.extract(result) instanceof String))
{
Reflect.on(entity).set(name, extractor.extract(result));
return entity;
}
result =(String) extractor.extract(result);
}
result = filterBefore(result);
result = filter(name, result);
result = filterAfter(result);
try {
Reflect.on(entity).set(name, result);
} catch (Exception e) {
LOGGER.error("convert to bean error! can't set '{}' with '{}'", name, result, e);
}
}
return entity;
}
@Test
public void testToBeanListByXPath() throws Exception {
List
public class Language {
private String type;
private String name;
private String url;
private Book book;
@ptyagi108 maybe i should add a method called extractBean
in Extractors
and it will set the bean to the field?
Yes..this would be better solution..
This will work only when Config is known at compile time, incase Config property is chosen at run-time this solution may not work. for example in case field Config in class Activity is interface or Abstract superclass and is implemented/extended by two different classes say Config1 and Config2 and implementing class(Config1 or Config2 ) is chosen at runtime based on some condition then this solution will fail.Also Config1 and Config2 can have additional properties which are not available in superclass Config then we will not be able to populate additional properties as actual class is not known at compile time.
There should be some way of mentioning actual implementing class (in this case Config1 or Config2 ), something like ...
Where Config is Abstract class and Config1 and Config2 extends Config as below,
public abstract class Config { // common options:
public class Config1 extends Config{
// consumer options
public class Config2 extends Config {
// producer options private String compressFile;
XML
In the XML there are two Activities (activity 1 & activity 2),Now Activity 1 will be assigned Config 2 and Activity 2 will be assigned Config 1 based on pd:resourceType,