コード例 #1
0
    public String serialize(Object value) {
      try {
        ByteArrayOutputStream bytesOut = new ByteArrayOutputStream();
        ObjectOutputStream objectOut = new ObjectOutputStream(bytesOut);
        objectOut.writeObject(value);
        objectOut.close();

        ByteArrayInputStream bytesIn = new ByteArrayInputStream(bytesOut.toByteArray());
        ByteArrayOutputStream dataOut = new ByteArrayOutputStream();

        Base64Encoder enc = new Base64Encoder(bytesIn, dataOut);
        enc.process();

        return new String(dataOut.toByteArray(), "UTF-8");
      } catch (NotSerializableException e) {
        throw new JRRuntimeException("Value is not serializable", e);
      } catch (IOException e) {
        throw new JRRuntimeException(e);
      }
    }
  /**
   * @throws JRException
   * @throws IOException
   */
  public void exportImage(JRPrintImage image) throws JRException, IOException {
    xmlWriter.startElement(JRXmlConstants.ELEMENT_image);
    xmlWriter.addAttribute(JRXmlConstants.ATTRIBUTE_scaleImage, image.getOwnScaleImageValue());
    xmlWriter.addAttribute(JRXmlConstants.ATTRIBUTE_hAlign, image.getOwnHorizontalAlignmentValue());
    xmlWriter.addAttribute(JRXmlConstants.ATTRIBUTE_vAlign, image.getOwnVerticalAlignmentValue());
    xmlWriter.addAttribute(JRXmlConstants.ATTRIBUTE_isLazy, image.isLazy(), false);
    xmlWriter.addAttribute(
        JRXmlConstants.ATTRIBUTE_onErrorType, image.getOnErrorTypeValue(), OnErrorTypeEnum.ERROR);

    if (hyperlinkProducerFactory == null) {
      xmlWriter.addEncodedAttribute(
          JRXmlConstants.ATTRIBUTE_hyperlinkType,
          image.getLinkType(),
          HyperlinkTypeEnum.NONE.getName());
      xmlWriter.addEncodedAttribute(
          JRXmlConstants.ATTRIBUTE_hyperlinkReference, image.getHyperlinkReference());
      xmlWriter.addEncodedAttribute(
          JRXmlConstants.ATTRIBUTE_hyperlinkAnchor, image.getHyperlinkAnchor());
      xmlWriter.addAttribute(JRXmlConstants.ATTRIBUTE_hyperlinkPage, image.getHyperlinkPage());
    } else {
      String reference = hyperlinkProducerFactory.produceHyperlink(image);
      if (reference != null) {
        xmlWriter.addAttribute(JRXmlConstants.ATTRIBUTE_hyperlinkType, HyperlinkTypeEnum.REFERENCE);
        xmlWriter.addEncodedAttribute(JRXmlConstants.ATTRIBUTE_hyperlinkReference, reference);
      }
    }

    xmlWriter.addAttribute(
        JRXmlConstants.ATTRIBUTE_hyperlinkTarget,
        image.getLinkTarget(),
        HyperlinkTargetEnum.SELF
            .getName()); // FIXMETARGET this exporter is used in the Flash viewer
    xmlWriter.addEncodedAttribute(
        JRXmlConstants.ATTRIBUTE_hyperlinkTooltip, image.getHyperlinkTooltip());
    xmlWriter.addEncodedAttribute(JRXmlConstants.ATTRIBUTE_anchorName, image.getAnchorName());
    xmlWriter.addAttribute(
        JRXmlConstants.ATTRIBUTE_bookmarkLevel, image.getBookmarkLevel(), JRAnchor.NO_BOOKMARK);

    exportReportElement(image);
    exportBox(image.getLineBox());
    exportGraphicElement(image);

    JRRenderable renderer = image.getRenderer();
    if (renderer != null) {
      xmlWriter.startElement(JRXmlConstants.ELEMENT_imageSource);
      xmlWriter.addAttribute(
          JRXmlConstants.ATTRIBUTE_isEmbedded, isEmbeddingImages && !image.isLazy(), false);

      String imageSource = "";

      if (renderer.getType() == JRRenderable.TYPE_SVG) {
        renderer =
            new JRWrappingSvgRenderer(
                renderer,
                new Dimension(image.getWidth(), image.getHeight()),
                ModeEnum.OPAQUE == image.getModeValue() ? image.getBackcolor() : null);
      }

      if (image.isLazy()) {
        imageSource = ((JRImageRenderer) renderer).getImageLocation();
      } else if (isEmbeddingImages) {
        try {
          ByteArrayInputStream bais = new ByteArrayInputStream(renderer.getImageData());
          ByteArrayOutputStream baos = new ByteArrayOutputStream();

          Base64Encoder encoder = new Base64Encoder(bais, baos);
          encoder.process();

          imageSource = new String(baos.toByteArray(), DEFAULT_XML_ENCODING);
        } catch (IOException e) {
          throw new JRException("Error embedding image into XML.", e);
        }
      } else {
        if (renderer.getType() == JRRenderable.TYPE_IMAGE
            && rendererToImagePathMap.containsKey(renderer)) {
          imageSource = rendererToImagePathMap.get(renderer);
        } else {
          imageSource = IMAGE_PREFIX + getNextImageId();
          imageNameToImageDataMap.put(imageSource, renderer.getImageData());

          imageSource = new File(imagesDir, imageSource).getPath();
          rendererToImagePathMap.put(renderer, imageSource);
        }
      }

      xmlWriter.writeCDATA(imageSource);
      xmlWriter.closeElement();
    }

    if (hyperlinkProducerFactory == null) {
      exportHyperlinkParameters(image);
    }

    xmlWriter.closeElement();
  }