diff options
author | Jeremias Maerki <jeremias@apache.org> | 2008-08-07 16:01:31 +0000 |
---|---|---|
committer | Jeremias Maerki <jeremias@apache.org> | 2008-08-07 16:01:31 +0000 |
commit | 5ca81a092a81980009c1f8d331a2184e847cc96b (patch) | |
tree | 8aeb3ef02bc4cf7b80fea3856ac5392abef68b41 /src/java/org/apache/fop/render/intermediate/AbstractIFPainter.java | |
parent | e2c7140243263b2d7d27074c68004fbb5ffa05fa (diff) | |
download | xmlgraphics-fop-5ca81a092a81980009c1f8d331a2184e847cc96b.tar.gz xmlgraphics-fop-5ca81a092a81980009c1f8d331a2184e847cc96b.zip |
Implemented foreign-object support for the new intermediate format.
Note: foreign namespace are only properly processed if there is an ImagePreloader and, where necessary, one or more supporting ImageConverters and/or ImageHandlers.
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/branches/Temp_AreaTreeNewDesign@683637 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org/apache/fop/render/intermediate/AbstractIFPainter.java')
-rw-r--r-- | src/java/org/apache/fop/render/intermediate/AbstractIFPainter.java | 131 |
1 files changed, 131 insertions, 0 deletions
diff --git a/src/java/org/apache/fop/render/intermediate/AbstractIFPainter.java b/src/java/org/apache/fop/render/intermediate/AbstractIFPainter.java index f5e43229b..c7d6a578b 100644 --- a/src/java/org/apache/fop/render/intermediate/AbstractIFPainter.java +++ b/src/java/org/apache/fop/render/intermediate/AbstractIFPainter.java @@ -22,15 +22,40 @@ package org.apache.fop.render.intermediate; import java.awt.Dimension; import java.awt.Rectangle; import java.awt.geom.AffineTransform; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.Map; + +import javax.xml.transform.dom.DOMSource; + +import org.w3c.dom.Document; + +import org.apache.commons.logging.Log; +import org.apache.commons.logging.LogFactory; + +import org.apache.xmlgraphics.image.loader.ImageException; +import org.apache.xmlgraphics.image.loader.ImageInfo; +import org.apache.xmlgraphics.image.loader.ImageManager; +import org.apache.xmlgraphics.image.loader.ImageSessionContext; +import org.apache.xmlgraphics.image.loader.util.ImageUtil; import org.apache.fop.apps.FOUserAgent; +import org.apache.fop.events.ResourceEventProducer; +import org.apache.fop.render.ImageHandler; import org.apache.fop.render.ImageHandlerRegistry; +import org.apache.fop.render.RenderingContext; /** * Abstract base class for IFPainter implementations. */ public abstract class AbstractIFPainter implements IFPainter { + /** logging instance */ + private static Log log = LogFactory.getLog(AbstractIFPainter.class); + + /** non-URI that can be used in feedback messages that an image is an instream-object */ + protected static final String INSTREAM_OBJECT_URI = "(instream-object)"; + private FOUserAgent userAgent; /** Image handler registry */ @@ -77,4 +102,110 @@ public abstract class AbstractIFPainter implements IFPainter { startGroup(combine(transforms)); } + /** + * Creates a new RenderingContext instance. + * @return the new rendering context. + */ + protected abstract RenderingContext createRenderingContext(); + + /** + * Loads a preloaded image and draws it using a suitable image handler. + * @param info the information object of the preloaded image + * @param rect the rectangle in which to paint the image + * @throws ImageException if there's an error while processing the image + * @throws IOException if there's an I/O error while loading the image + */ + protected void drawImageUsingImageHandler(ImageInfo info, Rectangle rect) + throws ImageException, IOException { + ImageManager manager = getUserAgent().getFactory().getImageManager(); + ImageSessionContext sessionContext = getUserAgent().getImageSessionContext(); + + //Load and convert the image to a supported format + RenderingContext context = createRenderingContext(); + Map hints = ImageUtil.getDefaultHints(sessionContext); + org.apache.xmlgraphics.image.loader.Image img = manager.getImage( + info, imageHandlerRegistry.getSupportedFlavors(context), + hints, sessionContext); + + //First check for a dynamically registered handler + ImageHandler handler = imageHandlerRegistry.getHandler(context, img); + if (handler == null) { + throw new UnsupportedOperationException( + "No ImageHandler available for image: " + + info + " (" + img.getClass().getName() + ")"); + } + + if (log.isDebugEnabled()) { + log.debug("Using ImageHandler: " + handler.getClass().getName()); + } + try { + //TODO foreign attributes + handler.handleImage(context, img, rect); + } catch (IOException ioe) { + ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get( + getUserAgent().getEventBroadcaster()); + eventProducer.imageWritingError(this, ioe); + return; + } + } + + /** + * Default drawing method for handling an image referenced by a URI. + * @param uri the image's URI + * @param rect the rectangle in which to paint the image + */ + protected void drawImageUsingURI(String uri, Rectangle rect) { + ImageManager manager = getUserAgent().getFactory().getImageManager(); + ImageInfo info = null; + try { + ImageSessionContext sessionContext = getUserAgent().getImageSessionContext(); + info = manager.getImageInfo(uri, sessionContext); + + drawImageUsingImageHandler(info, rect); + } catch (ImageException ie) { + ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get( + getUserAgent().getEventBroadcaster()); + eventProducer.imageError(this, (info != null ? info.toString() : uri), ie, null); + } catch (FileNotFoundException fe) { + ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get( + getUserAgent().getEventBroadcaster()); + eventProducer.imageNotFound(this, (info != null ? info.toString() : uri), fe, null); + } catch (IOException ioe) { + ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get( + getUserAgent().getEventBroadcaster()); + eventProducer.imageIOError(this, (info != null ? info.toString() : uri), ioe, null); + } + } + + /** + * Default drawing method for handling a foreign object in the form of a DOM document. + * @param doc the DOM document containing the foreign object + * @param rect the rectangle in which to paint the image + */ + protected void drawImageUsingDocument(Document doc, Rectangle rect) { + ImageManager manager = getUserAgent().getFactory().getImageManager(); + ImageInfo info = null; + try { + info = manager.preloadImage(null, new DOMSource(doc)); + + drawImageUsingImageHandler(info, rect); + } catch (ImageException ie) { + ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get( + getUserAgent().getEventBroadcaster()); + eventProducer.imageError(this, + (info != null ? info.toString() : INSTREAM_OBJECT_URI), ie, null); + } catch (FileNotFoundException fe) { + ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get( + getUserAgent().getEventBroadcaster()); + eventProducer.imageNotFound(this, + (info != null ? info.toString() : INSTREAM_OBJECT_URI), fe, null); + } catch (IOException ioe) { + ResourceEventProducer eventProducer = ResourceEventProducer.Provider.get( + getUserAgent().getEventBroadcaster()); + eventProducer.imageIOError(this, + (info != null ? info.toString() : INSTREAM_OBJECT_URI), ioe, null); + } + } + + } |