|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +/* $Id$ */ |
| 19 | +package org.apache.fop.pdf; |
| 20 | + |
| 21 | +import java.io.ByteArrayInputStream; |
| 22 | +import java.io.ByteArrayOutputStream; |
| 23 | +import java.io.File; |
| 24 | +import java.io.IOException; |
| 25 | +import java.io.OutputStream; |
| 26 | +import java.net.URI; |
| 27 | + |
| 28 | +import javax.xml.transform.Result; |
| 29 | +import javax.xml.transform.Source; |
| 30 | +import javax.xml.transform.Transformer; |
| 31 | +import javax.xml.transform.TransformerFactory; |
| 32 | +import javax.xml.transform.sax.SAXResult; |
| 33 | +import javax.xml.transform.stream.StreamSource; |
| 34 | + |
| 35 | +import org.junit.Assert; |
| 36 | +import org.junit.Test; |
| 37 | + |
| 38 | +import org.apache.xmlgraphics.image.loader.Image; |
| 39 | +import org.apache.xmlgraphics.image.loader.ImageFlavor; |
| 40 | +import org.apache.xmlgraphics.image.loader.cache.ImageCache; |
| 41 | +import org.apache.xmlgraphics.image.loader.impl.ImageRawPNG; |
| 42 | +import org.apache.xmlgraphics.image.loader.impl.ImageRawStream; |
| 43 | +import org.apache.xmlgraphics.io.Resource; |
| 44 | +import org.apache.xmlgraphics.io.ResourceResolver; |
| 45 | + |
| 46 | +import org.apache.fop.apps.FOUserAgent; |
| 47 | +import org.apache.fop.apps.Fop; |
| 48 | +import org.apache.fop.apps.FopFactory; |
| 49 | +import org.apache.fop.apps.FopFactoryBuilder; |
| 50 | +import org.apache.fop.apps.MimeConstants; |
| 51 | +import org.apache.fop.apps.io.ResourceResolverFactory; |
| 52 | + |
| 53 | +public class PDFImageCacheTestCase { |
| 54 | + @Test |
| 55 | + public void testCache() throws Exception { |
| 56 | + final int[] count = {0}; |
| 57 | + ResourceResolver customResolver = new ResourceResolver() { |
| 58 | + ResourceResolver resourceResolver = ResourceResolverFactory.createDefaultResourceResolver(); |
| 59 | + public Resource getResource(URI uri) throws IOException { |
| 60 | + count[0]++; |
| 61 | + return resourceResolver.getResource(uri); |
| 62 | + } |
| 63 | + public OutputStream getOutputStream(URI uri) throws IOException { |
| 64 | + return resourceResolver.getOutputStream(uri); |
| 65 | + } |
| 66 | + }; |
| 67 | + FopFactoryBuilder builder = new FopFactoryBuilder(new File(".").toURI(), customResolver); |
| 68 | + FopFactory fopFactory = builder.build(); |
| 69 | + ByteArrayOutputStream bos = buildPDF(fopFactory); |
| 70 | + Assert.assertTrue(bos.toString().contains("/Subtype /Image")); |
| 71 | + Assert.assertEquals(1, count[0]); |
| 72 | + deleteImageData(fopFactory); |
| 73 | + bos = buildPDF(fopFactory); |
| 74 | + Assert.assertTrue(bos.toString().contains("/Subtype /Image")); |
| 75 | + Assert.assertEquals(1, count[0]); |
| 76 | + } |
| 77 | + |
| 78 | + private void deleteImageData(FopFactory fopFactory) { |
| 79 | + ImageCache imageCache = fopFactory.newFOUserAgent().getImageManager().getCache(); |
| 80 | + Image image = imageCache.getImage("test/resources/images/fop-logo-color-24bit.png", ImageFlavor.RAW_PNG); |
| 81 | + ImageRawPNG raw = new ImageRawPNG(image.getInfo(), null, null, 1, null); |
| 82 | + raw.setInputStreamFactory(new ImageRawStream.ByteArrayStreamFactory(new byte[0])); |
| 83 | + imageCache.putImage(raw); |
| 84 | + } |
| 85 | + |
| 86 | + private ByteArrayOutputStream buildPDF(FopFactory fopFactory) throws Exception { |
| 87 | + String fo = "<fo:root xmlns:fo=\"http://www.w3.org/1999/XSL/Format\">\n" |
| 88 | + + " <fo:layout-master-set>\n" |
| 89 | + + " <fo:simple-page-master master-name=\"simple\" page-height=\"27.9cm\" page-width=\"21.6cm\">\n" |
| 90 | + + " <fo:region-body />\n" |
| 91 | + + " </fo:simple-page-master>\n" |
| 92 | + + " </fo:layout-master-set>\n" |
| 93 | + + " <fo:page-sequence master-reference=\"simple\">\n" |
| 94 | + + " <fo:flow flow-name=\"xsl-region-body\">\n" |
| 95 | + + "<fo:block><fo:external-graphic src=\"test/resources/images/fop-logo-color-24bit.png\"/></fo:block>\n" |
| 96 | + + "</fo:flow>\n" |
| 97 | + + " </fo:page-sequence>\n" |
| 98 | + + "</fo:root>\n"; |
| 99 | + FOUserAgent userAgent = fopFactory.newFOUserAgent(); |
| 100 | + ByteArrayOutputStream bos = new ByteArrayOutputStream(); |
| 101 | + Fop fop = fopFactory.newFop(MimeConstants.MIME_PDF, userAgent, bos); |
| 102 | + Transformer transformer = TransformerFactory.newInstance().newTransformer(); |
| 103 | + Source src = new StreamSource(new ByteArrayInputStream(fo.getBytes())); |
| 104 | + Result res = new SAXResult(fop.getDefaultHandler()); |
| 105 | + transformer.transform(src, res); |
| 106 | + return bos; |
| 107 | + } |
| 108 | +} |
0 commit comments