protected void writeTemplates(Report report, Element root) { Map<String, ReportTemplate> reportTemplates = report.getReportTemplates(); Element reportTemplatesElement = root.addElement("templates"); for (ReportTemplate reportTemplate : reportTemplates.values()) { Element reportTemplateElement = reportTemplatesElement.addElement("template"); reportTemplateElement.addAttribute("code", reportTemplate.getCode()); reportTemplateElement.addAttribute("documentName", reportTemplate.getDocumentName()); reportTemplateElement.addAttribute("documentPath", reportTemplate.getDocumentPath()); reportTemplateElement.addAttribute("outputType", reportTemplate.getOutputType().getId()); reportTemplateElement.addAttribute("outputNamePattern", reportTemplate.getOutputNamePattern()); } }
protected void generateReport(Report report, ReportTemplate reportTemplate, ReportOutputType outputType, OutputStream outputStream, Map<String, Object> handledParams, BandData rootBand) { String extension = StringUtils.substringAfterLast(reportTemplate.getDocumentName(), "."); if (reportTemplate.isCustom()) { try { byte[] bytes = reportTemplate.getCustomReport().createReport(report, rootBand, handledParams); IOUtils.write(bytes, outputStream); } catch (IOException e) { throw new ReportingException(format("An error occurred while processing custom template [%s].", reportTemplate.getDocumentName()), e); } } else { FormatterFactoryInput factoryInput = new FormatterFactoryInput(extension, rootBand, reportTemplate, outputType, outputStream); ReportFormatter formatter = formatterFactory.createFormatter(factoryInput); formatter.renderDocument(); } }
protected Template getTemplate() { try { String templateContent = IOUtils.toString(reportTemplate.getDocumentContent(), StandardCharsets.UTF_8); StringTemplateLoader stringLoader = new StringTemplateLoader(); stringLoader.putTemplate(reportTemplate.getDocumentName(), templateContent); Configuration fmConfiguration = new Configuration(); fmConfiguration.setTemplateLoader(stringLoader); fmConfiguration.setDefaultEncoding("UTF-8"); Template htmlTemplate = fmConfiguration.getTemplate(reportTemplate.getDocumentName()); htmlTemplate.setObjectWrapper(objectWrapper); return htmlTemplate; } catch (Exception e) { throw wrapWithReportingException("An error occurred while creating freemarker template", e); } } }
protected String resolveOutputFileName(Report report, ReportTemplate reportTemplate, ReportOutputType outputType, BandData rootBand) { String outputNamePattern = reportTemplate.getOutputNamePattern(); String outputName = reportTemplate.getDocumentName(); Pattern pattern = Pattern.compile("\\$\\{([A-z0-9_]+)\\.([A-z0-9_]+)\\}"); if (StringUtils.isNotBlank(outputNamePattern)) { if (ReportOutputType.custom != reportTemplate.getOutputType()) { ReportOutputType finalOutputType = (outputType != null ) ? outputType : reportTemplate.getOutputType(); outputName = format("%s.%s", StringUtils.substringBeforeLast(outputName, "."), finalOutputType.getId());
public XInputStream getXInputStream(ReportTemplate reportTemplate) { try { return new OfficeInputStream(IOUtils.toByteArray(reportTemplate.getDocumentContent())); } catch (java.io.IOException e) { throw new OpenOfficeException("An error occurred while converting template to XInputStream", e); } }
private String getExtension(ReportTemplate reportTemplate) { String[] split = reportTemplate.getDocumentName().split("\\."); return split[split.length - 1].toLowerCase(); } }
public ReportBuilder template(ReportTemplate reportTemplate) { Preconditions.checkNotNull(reportTemplate, "\"reportTemplate\" parameter can not be null"); report.reportTemplates.put(reportTemplate.getCode(), reportTemplate); return this; }
protected AbstractFormatter(FormatterFactoryInput formatterFactoryInput) { Preconditions.checkNotNull("\"rootBand\" parameter can not be null", formatterFactoryInput.getRootBand()); Preconditions.checkNotNull("\"reportTemplate\" parameter can not be null", formatterFactoryInput.getReportTemplate()); this.rootBand = formatterFactoryInput.getRootBand(); this.reportTemplate = formatterFactoryInput.getReportTemplate(); this.outputType = (formatterFactoryInput.getOutputType() != null) ? formatterFactoryInput.getOutputType() : reportTemplate.getOutputType(); this.outputStream = formatterFactoryInput.getOutputStream(); this.contentInliners.add(new BitmapContentInliner()); this.contentInliners.add(new HtmlContentInliner()); this.contentInliners.add(new ImageContentInliner()); }
protected void writeHtmlDocument(BandData rootBand, OutputStream outputStream) { Map templateModel = getTemplateModel(rootBand); if (reportTemplate.isGroovy()) { groovy.text.Template htmlTemplate = getGroovyTemplate(); Writer htmlWriter = new OutputStreamWriter(outputStream); try { htmlTemplate.make(templateModel).writeTo(htmlWriter); htmlWriter.close(); } catch (ReportingException e) { throw e; } catch (Exception e) { throw wrapWithReportingException("An error occurred while rendering html document.", e); } } else { freemarker.template.Template htmlTemplate = getFreemarkerTemplate(); Writer htmlWriter = new OutputStreamWriter(outputStream); try { htmlTemplate.process(templateModel, htmlWriter); htmlWriter.close(); } catch (TemplateException fmException) { throw wrapWithReportingException("FreeMarkerException: " + fmException.getMessage()); } catch (ReportingException e) { throw e; } catch (Exception e) { throw wrapWithReportingException("An error occurred while rendering html document.", e); } } }
protected String resolveOutputFileName(Report report, ReportTemplate reportTemplate, ReportOutputType outputType, BandData rootBand) { String outputNamePattern = reportTemplate.getOutputNamePattern(); String outputName = reportTemplate.getDocumentName(); Pattern pattern = Pattern.compile("\\$\\{([A-z0-9_]+)\\.([A-z0-9_]+)\\}"); if (StringUtils.isNotBlank(outputNamePattern)) { if (ReportOutputType.custom != reportTemplate.getOutputType()) { ReportOutputType finalOutputType = (outputType != null ) ? outputType : reportTemplate.getOutputType(); outputName = format("%s.%s", StringUtils.substringBeforeLast(outputName, "."), finalOutputType.getId());
public XInputStream getXInputStream(ReportTemplate reportTemplate) { try { return new OfficeInputStream(IOUtils.toByteArray(reportTemplate.getDocumentContent())); } catch (java.io.IOException e) { throw new OpenOfficeException("An error occurred while converting template to XInputStream", e); } }
private String getExtension(ReportTemplate reportTemplate) { String[] split = reportTemplate.getDocumentName().split("\\."); return split[split.length - 1].toLowerCase(); } }
public ReportBuilder template(ReportTemplate reportTemplate) { Preconditions.checkNotNull(reportTemplate, "\"reportTemplate\" parameter can not be null"); report.reportTemplates.put(reportTemplate.getCode(), reportTemplate); return this; }
protected AbstractFormatter(FormatterFactoryInput formatterFactoryInput) { checkNotNull(formatterFactoryInput.getRootBand(), "\"rootBand\" parameter can not be null"); checkNotNull(formatterFactoryInput.getReportTemplate(), "\"reportTemplate\" parameter can not be null"); this.rootBand = formatterFactoryInput.getRootBand(); this.reportTemplate = formatterFactoryInput.getReportTemplate(); this.outputType = (formatterFactoryInput.getOutputType() != null) ? formatterFactoryInput.getOutputType() : reportTemplate.getOutputType(); this.outputStream = formatterFactoryInput.getOutputStream(); }
protected void writeTemplates(Report report, Element root) { Map<String, ReportTemplate> reportTemplates = report.getReportTemplates(); Element reportTemplatesElement = root.addElement("templates"); for (ReportTemplate reportTemplate : reportTemplates.values()) { Element reportTemplateElement = reportTemplatesElement.addElement("template"); reportTemplateElement.addAttribute("code", reportTemplate.getCode()); reportTemplateElement.addAttribute("documentName", reportTemplate.getDocumentName()); reportTemplateElement.addAttribute("documentPath", reportTemplate.getDocumentPath()); reportTemplateElement.addAttribute("outputType", reportTemplate.getOutputType().getId()); reportTemplateElement.addAttribute("outputNamePattern", reportTemplate.getOutputNamePattern()); } }
protected freemarker.template.Template getFreemarkerTemplate() { try { String templateContent = IOUtils.toString(reportTemplate.getDocumentContent(), StandardCharsets.UTF_8); StringTemplateLoader stringLoader = new StringTemplateLoader(); stringLoader.putTemplate(reportTemplate.getDocumentName(), templateContent); Configuration fmConfiguration = new Configuration(); fmConfiguration.setTemplateLoader(stringLoader); fmConfiguration.setDefaultEncoding("UTF-8"); freemarker.template.Template htmlTemplate = fmConfiguration.getTemplate(reportTemplate.getDocumentName()); htmlTemplate.setObjectWrapper(objectWrapper); return htmlTemplate; } catch (Exception e) { throw wrapWithReportingException("An error occurred while creating freemarker template", e); } }
protected void generateReport(Report report, ReportTemplate reportTemplate, ReportOutputType outputType, OutputStream outputStream, Map<String, Object> handledParams, BandData rootBand) { String extension = StringUtils.substringAfterLast(reportTemplate.getDocumentName(), "."); if (reportTemplate.isCustom()) { try { byte[] bytes = reportTemplate.getCustomReport().createReport(report, rootBand, handledParams); IOUtils.write(bytes, outputStream); } catch (IOException e) { throw new ReportingException(format("An error occurred while processing custom template [%s].", reportTemplate.getDocumentName()), e); } } else { FormatterFactoryInput factoryInput = new FormatterFactoryInput(extension, rootBand, reportTemplate, outputType, outputStream); ReportFormatter formatter = formatterFactory.createFormatter(factoryInput); formatter.renderDocument(); } }
protected void loadDocument(OfficeResourceProvider ooResourceProvider) throws com.sun.star.lang.IllegalArgumentException, IOException { xComponent = ooResourceProvider.loadXComponent(reportTemplate.getDocumentContent()); officeComponent = new OfficeComponent(ooResourceProvider, xComponent); }
protected ReportingException wrapWithReportingException(String message, Exception e) { if (e instanceof ReportingInterruptedException) { return (ReportingInterruptedException) e; } else { return new ReportFormattingException(message + ". Template name [" + reportTemplate.getDocumentName() + "]", e); } }
protected Map<String, ReportTemplate> parseTemplates(Element rootElement) throws IOException { Element templatesElement = rootElement.element("templates"); List<Element> templates = templatesElement.elements("template"); Map<String, ReportTemplate> templateMap = new HashMap<String, ReportTemplate>(); for (Element template : templates) { String code = template.attribute("code").getText(); String documentName = template.attribute("documentName").getText(); String documentPath = template.attribute("documentPath").getText(); String outputType = template.attribute("outputType").getText(); String outputNamePattern = template.attribute("outputNamePattern").getText(); ReportTemplate reportTemplate = new ReportTemplateBuilder() .code(code) .documentName(documentName) .documentPath(documentPath) .documentContent(getDocumentContent(documentPath)) .outputType(ReportOutputType.getOutputTypeById(outputType)) .outputNamePattern(outputNamePattern).build(); templateMap.put(reportTemplate.getCode(), reportTemplate); } return templateMap; }