This library generates xml hints in javascript format from JAXB annotated classes to be used with CodeMirror's xmlhint system
Currently handled JAXB annotations:
@XmlElementRef @XmlElement @XmlAttribute @XmlRootElement
CodeMirror: http://codemirror.net/
Add dependency to pom:
<dependency>
<groupId>hu.pilar</groupId>
<artifactId>codemirror-jaxb</artifactId>
<version>0.2</version>
</dependency>
Add pilar.hu repository to settings.xml or nexus:
<repository>
<id>pilar</id>
<url>http://www.pilar.hu/nexus/content/groups/public</url>
</repository>
There is a single API entry point in HintGenerator class. It was designed to be used as a singleton, either via Spring or by any other means.
HintGenerator hg = new HintGenerator(new ObjectMapper()); // pass in a jackson ObjectMapper
XmlHint hint = hg.getHintsFor(AnyJaxbAnnotatedClass.class); // the JAXB root object class
System.out.println(hint.toJson());
By default the library will generate attribute values for @XmlAttribute annotated getter methods with Boolean, boolean and enum return types. It is possible to generate more attribute value sets by implementing IAttributeValueFactory and providing the sets for each method:
HintGenerator hg = new HintGenerator(new ObjectMapper(), new IAttributeValueFactory() {
@Override
public Set<String> getValuesFor(String attributeName, Class type) {
if ("yearOfBirth".equals(attributeName)) {
return Sets.newHashSet(IntStream.range(1900, LocalDate.now().getYear()).mapToObj(Integer::toString).collect(Collectors.toSet()));
}
return null;
}
});
This library is using Reflections.org utilities to find all subclasses of a given type. If you're using the same library in your own application, you can eliminate the additional time it takes to gather data from the class loader(s) about all loaded classes by passing in your own Reflections object in the constructor:
HintGenerator hg = new HintGenerator(new ObjectMapper(), new ReflectionBasedSubclassFinder(reflections), new IAttributeValueFactory() ... );
or
HintGenerator hg = new HintGenerator(new ObjectMapper(), new ReflectionBasedSubclassFinder(reflections));
In your @Configuration class add the bean:
@Bean
public HintGenerator hintGenerator() {
return new HintGenerator(...); // use any of the previous examples
}