Example #1
0
  /** Create a zip file containing all the files (masterreport, subreports, images) */
  private void writeZip(final OutputStream outStream) throws Exception {
    File zipfile = null;
    final List<String> newImagesFilenames = new LinkedList<String>();
    final JasperReport[] jra = reportCard.getRichReportJRA();

    try {
      // create zip file
      zipfile = File.createTempFile("cmdbuild_report_export", ".zip");
      final ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipfile));

      // get jasperdesign for master report
      final JasperReport masterReport = jra[0];
      final JasperDesign jd = jasperReportToJasperDesign(masterReport);

      // add images to zip file
      final List<JRDesignImage> designImages = getImages(jd);
      final InputStream[] isa = reportCard.getImagesISA();
      final String[] origImagesName =
          reportCard.getImagesName() != null
              ? reportCard.getImagesName()
              : new String[isa.length]; // original names with check for
      // backward compatibility
      for (int i = 0; i < isa.length; i++) {

        String imageFilename;
        if (origImagesName[i] != null) {
          imageFilename = origImagesName[i];
        } else { // backward compatibility
          imageFilename = "image" + (i + 1) + "." + getImageFormatName(isa[i]);
          origImagesName[i] = imageFilename;
        }

        // update image filename
        setImageFilename(designImages.get(i), imageFilename);

        // write image
        final InputStream is = isa[i];
        newImagesFilenames.add(imageFilename);
        zos.putNextEntry(new ZipEntry(imageFilename));
        for (int b = is.read(); b != -1; b = is.read()) {
          zos.write(b);
        }
        zos.closeEntry();
      }

      // add reports (jrxml) to zip file
      for (int i = 0; i < jra.length; i++) {

        // add ZIP entry to output stream.
        final String filename = jra[i].getName() + ".jrxml";
        try {
          zos.putNextEntry(new ZipEntry(filename));
        } catch (final ZipException e) {
          Log.REPORT.warn("error while zipping elements", e);
          continue;
        }

        // if master report, update images and subreports paths
        if (i == 0) {
          // update images
          prepareDesignImagesForZipExport(designImages, origImagesName);

          // update subreports
          prepareDesignSubreportsForZipExport(getSubreports(jd), jra);

          // compile updated jasperdesign
          final JasperReport newjr = JasperCompileManager.compileReport(jd);

          // replace jasperreport obj with the new one
          jra[0] = newjr;
        }

        // write to zip
        JRXmlWriter.writeReport(jra[i], zos, "UTF-8");

        // complete the entry
        zos.closeEntry();
      }
      // close zip stream
      zos.close();

      // send final zip to output stream
      final FileInputStream fis = new FileInputStream(zipfile);
      for (int b = fis.read(); b != -1; b = fis.read()) {
        outStream.write(b);
      }
      fis.close();

    } finally {
      // delete zip file
      if (zipfile != null && zipfile.exists()) {
        zipfile.delete();
      }
    }
  }
 /**
  * Generates the XML representation of the report design object supplied as parameter and writes
  * it to the specified output stream, using the "UTF-8" encoding.
  *
  * @param report source report design object
  * @param outputStream output stream to write the XML report design representation to
  * @see net.sf.jasperreports.engine.xml.JRXmlWriter
  */
 public static void writeReportToXmlStream(JRReport report, OutputStream outputStream)
     throws JRException {
   JRXmlWriter.writeReport(report, outputStream, "UTF-8");
 }
 /**
  * Generates the XML representation of the report design object supplied as parameter using the
  * "UTF-8" enconding.
  *
  * @param report source report design object
  * @return XML representation of the report design
  * @see net.sf.jasperreports.engine.xml.JRXmlWriter
  */
 public static String writeReportToXml(JRReport report) {
   return JRXmlWriter.writeReport(report, "UTF-8");
 }
 /**
  * Generates the XML representation of the report design supplied as the first parameter and place
  * it in the file specified by the second parameter. The result is "UTF-8" encoded.
  *
  * @param report source report design object
  * @param destFileName output file name to write the XML report design representation to
  * @see net.sf.jasperreports.engine.xml.JRXmlWriter
  */
 public static void writeReportToXmlFile(JRReport report, String destFileName) throws JRException {
   JRXmlWriter.writeReport(report, destFileName, "UTF-8");
 }