コード例 #1
0
ファイル: IVViewTest.java プロジェクト: FreemanZhang/birt
 protected String renderPage(IReportDocument doc, long pageNo) throws Exception {
   ByteArrayOutputStream buffer = new ByteArrayOutputStream();
   assertTrue(pageNo <= doc.getPageCount());
   IRenderTask renderTask = engine.createRenderTask(doc);
   try {
     HTMLRenderOption options = new HTMLRenderOption();
     options.setOutputFormat("html");
     options.setOutputStream(buffer);
     renderTask.setRenderOption(options);
     renderTask.setPageNumber((long) pageNo);
     renderTask.render();
     List errors = renderTask.getErrors();
     assertEquals(0, errors.size());
   } finally {
     renderTask.close();
   }
   return new String(buffer.toString("UTF-8"));
 }
コード例 #2
0
ファイル: RptEngineImpl.java プロジェクト: TheTypoMaster/mbsa
  /** Заполнение отчёта */
  private void fillReport(RptMain report, RptProperties properties) {
    String reportDoc = generateReportFileName(report, properties);

    IReportDocument rd = null;
    byte[] reportResult = null;

    try {
      IRunTask runTask = engine.createRunTask(design);
      setupEngineTask(runTask, properties);
      try {
        runTask.run(reportDoc);
      } finally {
        runTask.close();
      }

      rd = engine.openReportDocument(reportDoc);

      IRenderTask rendTask = engine.createRenderTask(rd);
      setupRenderTask(rendTask, properties);

      try {
        rendTask.render();

        reportResult =
            ((ByteArrayOutputStream) rendTask.getRenderOption().getOutputStream()).toByteArray();
      } finally {
        rendTask.close();
      }
    } catch (EngineException e) {
      if (e.getCause() instanceof ApplicationException) throw (ApplicationException) e.getCause();
      else throw new ReportException(Messages.getInstance().getMessage(Messages.BIRT_ERROR), e);
    } finally {
      if (rd != null) rd.close();
      File f = new File(reportDoc);
      f.delete();
      f = null;
    }

    showReport(report, reportResult, properties);
  }
コード例 #3
0
  @SuppressWarnings("unchecked")
  protected void renderMergedOutputModel(
      Map<String, Object> modelData, HttpServletRequest request, HttpServletResponse response)
      throws Exception {
    FileInputStream fis = null;
    IReportRunnable runnable = null;
    IReportDocument document = null;
    try {

      if (this.reportParameters == null) this.reportParameters = new HashMap<String, Object>();

      for (String k : modelData.keySet()) this.reportParameters.put(k, modelData.get(k));

      // 2) reportName property
      // 1) report name parameter is available, use that

      String reportName;
      reportName =
          StringUtils.hasText(this.reportName)
              ? this.reportName
              : request.getParameter(this.reportNameRequestParameter); // 'cat'
      String fullReportName = canonicalizeName(reportName);

      String documentName;
      documentName =
          StringUtils.hasText(this.documentName)
              ? this.documentName
              : request.getParameter(this.documentNameRequestParameter); // 'cat'
      String fullDocumentName = canonicalizeDocName(documentName);
      if (documentName == null) {
        fullDocumentName = reportName.replaceAll(".rptdesign", ".rptdocument");
      }

      String format;
      if (this.reportOutputFormat != null) {
        format = this.reportOutputFormat;
      } else {
        format = request.getParameter(this.reportFormatRequestParameter);
      }
      ServletContext sc =
          request.getServletContext(); // / avoid creating an HTTP session if possible.

      if (format == null) {
        format = "html";
      }

      Map<String, Object> mapOfOptions = new HashMap<String, Object>();
      mapOfOptions.put(
          IModuleOption.RESOURCE_FOLDER_KEY,
          birtViewResourcePathCallback.resourceDirectory(sc, request, reportName));
      mapOfOptions.put(IModuleOption.PARSER_SEMANTIC_CHECK_KEY, Boolean.FALSE);

      // set content type
      String contentType = birtEngine.getMIMEType(format);
      response.setContentType(contentType);
      setContentType(contentType);

      Map<String, Object> appContextMap = new HashMap<String, Object>();
      appContextMap.put(EngineConstants.APPCONTEXT_BIRT_VIEWER_HTTPSERVET_REQUEST, request);

      if (this.dataSource != null) {
        appContextMap.put(IConnectionFactory.PASS_IN_CONNECTION, this.dataSource.getConnection());

        if (this.closeDataSourceConnection)
          appContextMap.put(IConnectionFactory.CLOSE_PASS_IN_CONNECTION, Boolean.TRUE);
      }

      IEngineTask task = null;
      String pathForReport =
          birtViewResourcePathCallback.pathForReport(sc, request, fullReportName);
      fis = new FileInputStream(pathForReport);
      runnable = birtEngine.openReportDesign(fullReportName, fis, mapOfOptions);

      if (runnable != null && this.taskType == AbstractSingleFormatBirtView.RUNRENDERTASK) {
        task = birtEngine.createRunAndRenderTask(runnable);
        task.setParameterValues(discoverAndSetParameters(runnable, request));
        IRunAndRenderTask runAndRenderTask = (IRunAndRenderTask) task;
        IRenderOption options = null == this.renderOption ? new RenderOption() : this.renderOption;
        options.setActionHandler(actionHandler);
        IRenderOption returnedRenderOptions =
            renderReport(
                modelData,
                request,
                response,
                this.birtViewResourcePathCallback,
                appContextMap,
                reportName,
                format,
                options);
        for (String k : appContextMap.keySet())
          runAndRenderTask.getAppContext().put(k, appContextMap.get(k));
        runAndRenderTask.setRenderOption(returnedRenderOptions);
        runAndRenderTask.run();
        runAndRenderTask.close();
      } else {

        // Run then Render
        if (runnable != null) {
          task = birtEngine.createRunTask(runnable);
          task.setParameterValues(discoverAndSetParameters(runnable, request));
          IRunTask runTask = (IRunTask) task;
          for (String k : appContextMap.keySet())
            runTask.getAppContext().put(k, appContextMap.get(k));
          String pathForDocument =
              birtViewResourcePathCallback.pathForDocument(sc, request, fullDocumentName);
          runTask.run(pathForDocument);
          runTask.close();
          document = birtEngine.openReportDocument(fullDocumentName, pathForDocument, mapOfOptions);
          task = birtEngine.createRenderTask(document);
          IRenderTask renderTask = (IRenderTask) task;
          IRenderOption options =
              null == this.renderOption ? new RenderOption() : this.renderOption;
          options.setActionHandler(actionHandler);
          IRenderOption returnedRenderOptions =
              renderReport(
                  modelData,
                  request,
                  response,
                  this.birtViewResourcePathCallback,
                  appContextMap,
                  reportName,
                  format,
                  options);
          for (String k : appContextMap.keySet())
            renderTask.getAppContext().put(k, appContextMap.get(k));
          if (renderRange != null) {

            renderTask.setPageRange(renderRange);
          }
          renderTask.setRenderOption(returnedRenderOptions);
          renderTask.render();
          renderTask.close();
          document.close();
        }
      }

    } catch (Throwable th) {
      throw new RuntimeException(th); // nothing useful to do here
    } finally {
      if (null != fis) IOUtils.closeQuietly(fis);
      if (null != document) document.close();
    }
  }