π Digester on Steroids.
Map data to your POJOs in a beautiful way. Supports mapping from multiple sources:
- HTML (scraping), using XPath, ID or CSS selector.
- JSON Path
- Maps
- Property files
Download the latest JAR or grab via Maven:
<dependency>
<groupId>org.brunocvcunha.digesteroids</groupId>
<artifactId>digesteroids</artifactId>
<version>0.3</version>
</dependency>
or Gradle:
compile 'org.brunocvcunha.digesteroids:digesteroids:0.3'
Snapshots of the development version are available in Sonatype's snapshots
repository.
Digesteroids requires at minimum Java 8.
example.html
<html>
<head>
<title>Digesteroids Test</title>
</head>
<body>
<p id="fname">Bruno Candido Volpato da Cunha</p>
<p id="age">24</p>
<p class="address1">65535 University Ave</p>
<p><span>Palo Alto</span></p>
</body>
</html>
PersonPOJO.java
@DigesterEntity
public class PersonPOJO {
@DigesterMapping(source = "htmlExample", refType = ReferenceTypeEnum.HTML_ID, value = "fname")
private String name;
@DigesterMapping(source = "htmlExample", refType = ReferenceTypeEnum.HTML_ID, value = "age")
private Integer age;
@DigesterMapping(source = "htmlExample", refType = ReferenceTypeEnum.PASS_THROUGH, value = "")
private AddressPOJO address;
}
@DigesterEntity
public class AddressPOJO {
@DigesterMapping(source = "htmlExample", refType = ReferenceTypeEnum.HTML_CSS, value = "p.address1")
private String address1;
@DigesterMapping(source = "htmlExample", refType = ReferenceTypeEnum.HTML_XPATH, value = "p > span")
private String city;
}
Usage
Digesteroids digister = new Digesteroids();
PersonPOJO person = digister.convertHTMLToType("htmlExample", getClass().getResourceAsStream("/example.html"), PersonPOJO.class);
assertEquals("Bruno Candido Volpato da Cunha", person.getName());
assertEquals("65535 University Ave", person.getAddress().getAddress1());
assertEquals("Palo Alto", person.getAddress().getCity());
POJOs:
@DigesterEntity
public class PersonPOJO {
// Field can have multiple @DigesterMapping
@DigesterMapping(source = "htmlExample", refType = ReferenceTypeEnum.HTML_ID, value = "fname")
@DigesterMapping(value = "fullName")
private String name;
@DigesterMapping(source = "htmlExample", refType = ReferenceTypeEnum.HTML_ID, value = "age")
@DigesterMapping(value = "age")
private Integer age;
@DigesterMapping(value = "personAddress")
private AddressPOJO address;
}
@DigesterEntity
public class AddressPOJO {
@DigesterMapping(value = "address")
private String address1;
@DigesterMapping(value = "addressCity")
private String city;
}
Usage (Map to POJO):
Digesteroids digister = new Digesteroids();
Map<String, Object> personMap = new LinkedHashMap<>();
personMap.put("fullName", "Bruno");
personMap.put("age", "24");
Map<String, Object> addressMap = new LinkedHashMap<>();
addressMap.put("address", "65535 University Ave");
addressMap.put("addressCity", "Palo Alto");
personMap.put("personAddress", addressMap);
PersonPOJO person = digister.convertObjectToType(personMap, PersonPOJO.class);
assertEquals("Bruno", person.getName());
assertEquals("65535 University Ave", person.getAddress().getAddress1());
assertEquals("Palo Alto", person.getAddress().getCity());