Ejemplo n.º 1
0
  /**
   * <code>FOTreeBuilder</code> constructor
   *
   * @param outputFormat the MIME type of the output format to use (ex. "application/pdf").
   * @param foUserAgent the {@link FOUserAgent} in effect for this process
   * @param stream the <code>OutputStream</code> to direct the results to
   * @throws FOPException if the <code>FOTreeBuilder</code> cannot be properly created
   */
  public FOTreeBuilder(String outputFormat, FOUserAgent foUserAgent, OutputStream stream)
      throws FOPException {

    this.userAgent = foUserAgent;
    this.elementMappingRegistry = userAgent.getFactory().getElementMappingRegistry();
    // This creates either an AreaTreeHandler and ultimately a Renderer, or
    // one of the RTF-, MIF- etc. Handlers.
    foEventHandler =
        foUserAgent.getRendererFactory().createFOEventHandler(foUserAgent, outputFormat, stream);
    builderContext = new FOTreeBuilderContext();
    builderContext.setPropertyListMaker(
        new PropertyListMaker() {
          public PropertyList make(FObj fobj, PropertyList parentPropertyList) {
            return new StaticPropertyList(fobj, parentPropertyList);
          }
        });
  }
  /** {@inheritDoc} */
  public void activateLayout() {
    initialize();

    FOUserAgent userAgent = pageSeq.getUserAgent();
    ImageManager imageManager = userAgent.getFactory().getImageManager();

    String uri = getExternalDocument().getSrc();
    Integer firstPageIndex = ImageUtil.getPageIndexFromURI(uri);
    boolean hasPageIndex = (firstPageIndex != null);

    try {
      ImageInfo info = imageManager.getImageInfo(uri, userAgent.getImageSessionContext());

      Object moreImages = info.getCustomObjects().get(ImageInfo.HAS_MORE_IMAGES);
      boolean hasMoreImages = moreImages != null && !Boolean.FALSE.equals(moreImages);

      Dimension intrinsicSize = info.getSize().getDimensionMpt();
      ImageLayout layout = new ImageLayout(getExternalDocument(), this, intrinsicSize);

      PageSequence pageSequence = new PageSequence(null);
      transferExtensions(pageSequence);
      areaTreeHandler.getAreaTreeModel().startPageSequence(pageSequence);
      if (log.isDebugEnabled()) {
        log.debug("Starting layout");
      }

      makePageForImage(info, layout);

      if (!hasPageIndex && hasMoreImages) {
        if (log.isTraceEnabled()) {
          log.trace("Starting multi-page processing...");
        }
        URI originalURI;
        try {
          originalURI = new URI(URISpecification.escapeURI(uri));
          int pageIndex = 1;
          while (hasMoreImages) {
            URI tempURI =
                new URI(
                    originalURI.getScheme(),
                    originalURI.getSchemeSpecificPart(),
                    "page=" + Integer.toString(pageIndex + 1));
            if (log.isTraceEnabled()) {
              log.trace("Subimage: " + tempURI.toASCIIString());
            }
            ImageInfo subinfo =
                imageManager.getImageInfo(
                    tempURI.toASCIIString(), userAgent.getImageSessionContext());

            moreImages = subinfo.getCustomObjects().get(ImageInfo.HAS_MORE_IMAGES);
            hasMoreImages = moreImages != null && !Boolean.FALSE.equals(moreImages);

            intrinsicSize = subinfo.getSize().getDimensionMpt();
            layout = new ImageLayout(getExternalDocument(), this, intrinsicSize);

            makePageForImage(subinfo, layout);

            pageIndex++;
          }
        } catch (URISyntaxException e) {
          getResourceEventProducer().uriError(this, uri, e, getExternalDocument().getLocator());
          return;
        }
      }
    } catch (FileNotFoundException fnfe) {
      getResourceEventProducer().imageNotFound(this, uri, fnfe, getExternalDocument().getLocator());
    } catch (IOException ioe) {
      getResourceEventProducer().imageIOError(this, uri, ioe, getExternalDocument().getLocator());
    } catch (ImageException ie) {
      getResourceEventProducer().imageError(this, uri, ie, getExternalDocument().getLocator());
    }
  }