Samstag, 10. Oktober 2009

Set Apache Element Construction Set to pretty print

Unfortunately there is no easy way to overwrite the default settings
for the Element Construction Set to make it print pretty HTML.

After playing around with the only official way which is to set a comandline option:
For example, java -Decs.properties="my.ecs.properties"
and trying
System.setProperty("ecs.properties", "ecs.properties");

I fell back to using reflection.

This little piece of code does the trick:


try {
Field fld = ECSDefaults.class.getDeclaredField("defaults");
fld.setAccessible(true);
ECSDefaults ed = (ECSDefaults) fld.get(null);
Field pp = ECSDefaults.class.getDeclaredField("pretty_print");
pp.setAccessible(true);
pp.setBoolean(ed, Boolean.TRUE);

} catch (Exception ex) {
Logger.getLogger(Report.class.getName()).log(Level.SEVERE, null, ex);
}

IMPORTANT: Add this BEFORE you construct your document,
i.e.: Html h = new Html();
otherwise it doesn't work.

Keine Kommentare: