Comment #22 on issue 291 by
matteo.l...@gmail.com: xdocreport:
ok, i create the class MyTemplateExceptionHandler implements
TemplateExceptionHandler in my project but I don't understand how to use it.
class MyTemplateExceptionHandler implements TemplateExceptionHandler {
public void handleTemplateException(TemplateException te, Environment
env, java.io.Writer out)
throws TemplateException {
try {
out.write("[ERROR: " + te.getMessage() + "]");
} catch (IOException e) {
throw new TemplateException("Failed to print error message.
Cause: " + e, env);
}
}
}
First I had only one java class that process template:
private final static String genera_Report (String nome_Template, String
nome_FileDatiXml, String numero_Richiesta) throws Exception {
String result3 = "Errore";
String nomeFileGenerato = "";
try{
// 1) Carica il file modello .odt by filling Freemarker
template engine and cache
// it to the registry
IXDocReport report = null;
OutputStream out = null;
try{
File file = new File("modelli/"+nome_Template);
FileInputStream fi = new
FileInputStream(file.getAbsolutePath());
report = XDocReportRegistry.getRegistry().loadReport( fi,
TemplateEngineKind.Freemarker );
}catch(FileNotFoundException e){
//File template non trovato
result3 = String.format("Errore generazione report: %s",
e.getMessage());
return result3;
}
// 2) Crea context Java model
IContext context = report.createContext();
try{
FileInputStream projectInputStream = null;
projectInputStream = new
FileInputStream("dati/"+nome_FileDatiXml.toLowerCase()+".xml");
InputSource projectInputSource = new
InputSource(projectInputStream);
freemarker.ext.dom.NodeModel project =
freemarker.ext.dom.NodeModel.parse( projectInputSource );
context.put( "doc", project );
}catch(FileNotFoundException e){
//File dati xml non trovato
result3 = String.format("Errore generazione report: %s",
e.getMessage());
return result3;
}
try{
// 3) Genera il report elaborato
nomeFileGenerato =
String.format("%s%s%s%s","report/",numero_Richiesta,nome_Template,"_out.odt");
out = new FileOutputStream(new File(nomeFileGenerato));
}catch(Exception e){
result3 = String.format("Errore generazione report:
FileOutputstream %s", e.getMessage());
return result3;
}
try{
report.process( context, out );
}catch(Exception e){
result3 = String.format("Errore generazione report:
processa report %s", e.getMessage());
return result3;
}
}catch ( IOException e ) {
result3 = String.format("Errore generazione report: %s",
e.getMessage());
return result3;
}catch ( XDocReportException e ) {
result3 = String.format("Errore generazione report: %s",
e.getMessage());
return result3;
}catch ( SAXException e ) {
result3 = String.format("Errore generazione report: %s",
e.getMessage());
return result3;
}catch ( ParserConfigurationException e ) {
result3 = String.format("Errore generazione report: %s",
e.getMessage());
return result3;
}catch(Exception e){
result3 = String.format("Errore generazione report: %s",
e.getMessage());
return result3;
}
//Tutto ok
File f = new File(nomeFileGenerato);
result3 = String.format("Report generato correttamente: [%s]",
f.getAbsolutePath().toString().trim());
return result3;
}
How work with new class for intercept a frequent error is when a template
refers to a variable which is not existing?