Mercurial > hg > mpdl-group
diff software/eXist/mpdl-modules/src/de/mpg/mpiwg/berlin/mpdl/util/MpdlITextUserAgent.java @ 0:408254cf2f1d
Erstellung
author | Josef Willenborg <jwillenborg@mpiwg-berlin.mpg.de> |
---|---|
date | Wed, 24 Nov 2010 17:24:23 +0100 |
parents | |
children |
line wrap: on
line diff
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/software/eXist/mpdl-modules/src/de/mpg/mpiwg/berlin/mpdl/util/MpdlITextUserAgent.java Wed Nov 24 17:24:23 2010 +0100 @@ -0,0 +1,149 @@ +package de.mpg.mpiwg.berlin.mpdl.util; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; + +import org.apache.log4j.Logger; +import org.xhtmlrenderer.layout.SharedContext; +import org.xhtmlrenderer.pdf.ITextFSImage; +import org.xhtmlrenderer.pdf.ITextOutputDevice; +import org.xhtmlrenderer.pdf.PDFAsImage; +import org.xhtmlrenderer.resource.ImageResource; +import org.xhtmlrenderer.swing.NaiveUserAgent; + +import com.lowagie.text.Image; +import com.lowagie.text.Rectangle; +import com.lowagie.text.pdf.PdfReader; + +public class MpdlITextUserAgent extends NaiveUserAgent { + private static final int IMAGE_CACHE_CAPACITY = 32; + private static final float DEFAULT_DOTS_PER_POINT = 20f * 4f / 3f; + private static Logger LOGGER = Logger.getLogger(MpdlITextUserAgent.class); // Logs to EXIST_HOME/webapp/WEB-INF/logs/exist.log + private SharedContext sharedContext; + private ITextOutputDevice outputDevice; + + public MpdlITextUserAgent() { + super(IMAGE_CACHE_CAPACITY); + outputDevice = new ITextOutputDevice(DEFAULT_DOTS_PER_POINT); + } + + @SuppressWarnings("unchecked") + public ImageResource getImageResource(String inputUri) { + ImageResource resource = null; + String uri = resolveURI(inputUri); + resource = (ImageResource) _imageCache.get(uri); + if (resource == null) { + InputStream is = resolveAndOpenStream(uri); + if (is != null) { + try { + URL url = new URL(uri); + if (url.getPath() != null && url.getPath().toLowerCase().endsWith(".pdf")) { + PdfReader reader = outputDevice.getReader(url); + PDFAsImage image = new PDFAsImage(url); + Rectangle rect = reader.getPageSizeWithRotation(1); + image.setInitialWidth(rect.getWidth()*outputDevice.getDotsPerPoint()); + image.setInitialHeight(rect.getHeight()*outputDevice.getDotsPerPoint()); + resource = new ImageResource(image); + } else { + Image image = getImage(url); + if (image == null) + return null; + scaleToOutputResolution(image); + resource = new ImageResource(new ITextFSImage(image)); + } + _imageCache.put(uri, resource); + } catch (IOException e) { + LOGGER.error("Can't get image file: unexpected problem for URI: '" + uri + "': " + e.getMessage(), e); + } finally { + try { + if (is != null) + is.close(); + } catch (IOException e) { + // ignore + } + } + } + } + if (resource == null) { + resource = new ImageResource(null); + } + return resource; + } + + private void scaleToOutputResolution(Image image) { + float factor = sharedContext.getDotsPerPixel(); + image.scaleAbsolute(image.getPlainWidth() * factor, image.getPlainHeight() * factor); + } + + public SharedContext getSharedContext() { + return sharedContext; + } + + public void setSharedContext(SharedContext sharedContext) { + this.sharedContext = sharedContext; + } + + private Image getImage(URL url) { + Image image = null; + try { + image = Image.getInstance(url); + } catch (Exception e) { + try { + Thread.sleep(1000); + } catch (InterruptedException ee) { + // nothing + } + LOGGER.error("first retry to get image for URL '" + url.toString() + "': " + e.getMessage(), e); + try { + image = Image.getInstance(url); + } catch (Exception e2) { + try { + Thread.sleep(1000); + } catch (InterruptedException ee) { + // nothing + } + LOGGER.error("second retry to get image for URL '" + url.toString() + "': " + e.getMessage(), e); + try { + image = Image.getInstance(url); + } catch (Exception e3) { + LOGGER.error("third retry to get image for URL '" + url.toString() + "': " + e.getMessage(), e); + return null; + } + } + } + return image; + } + + protected InputStream resolveAndOpenStream(String inputUri) { + InputStream is = null; + String uri = resolveURI(inputUri); + try { + is = new URL(uri).openStream(); + } catch (Exception e) { + try { + Thread.sleep(1000); + } catch (InterruptedException ee) { + // nothing + } + LOGGER.error("first retry to open stream for URL '" + uri + "': " + e.getMessage(), e); + try { + is = new URL(uri).openStream(); + } catch (Exception e2) { + try { + Thread.sleep(1000); + } catch (InterruptedException ee) { + // nothing + } + LOGGER.error("second retry to open stream for URL '" + uri + "': " + e.getMessage(), e); + try { + is = new URL(uri).openStream(); + } catch (Exception e3) { + LOGGER.error("third retry to open stream for URL '" + uri + "': " + e.getMessage(), e); + return null; + } + } + } + return is; + } +}