1. Project Clover database Tue Dec 20 2016 21:24:09 CET
  2. Package com.xpn.xwiki.pdf.impl

File PDFURIResolver.java

 

Coverage histogram

../../../../../img/srcFileCovDistChart7.png
64% of files have more coverage

Code metrics

8
17
2
1
121
54
7
0.41
8.5
2
3.5

Classes

Class Line # Actions
PDFURIResolver 56 17 0% 7 10
0.629629663%
 

Contributing tests

This file is covered by 2 tests. .

Source view

1    /*
2    * See the NOTICE file distributed with this work for additional
3    * information regarding copyright ownership.
4    *
5    * This is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU Lesser General Public License as
7    * published by the Free Software Foundation; either version 2.1 of
8    * the License, or (at your option) any later version.
9    *
10    * This software is distributed in the hope that it will be useful,
11    * but WITHOUT ANY WARRANTY; without even the implied warranty of
12    * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13    * Lesser General Public License for more details.
14    *
15    * You should have received a copy of the GNU Lesser General Public
16    * License along with this software; if not, write to the Free
17    * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
18    * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
19    */
20    package com.xpn.xwiki.pdf.impl;
21   
22    import java.io.ByteArrayInputStream;
23    import java.util.Map;
24   
25    import javax.xml.transform.Source;
26    import javax.xml.transform.TransformerException;
27    import javax.xml.transform.URIResolver;
28    import javax.xml.transform.stream.StreamSource;
29   
30    import org.xwiki.formula.ImageData;
31    import org.xwiki.formula.ImageStorage;
32    import org.xwiki.model.EntityType;
33    import org.xwiki.model.reference.AttachmentReference;
34   
35    import com.xpn.xwiki.XWikiContext;
36    import com.xpn.xwiki.doc.XWikiAttachment;
37    import com.xpn.xwiki.doc.XWikiDocument;
38    import com.xpn.xwiki.web.Utils;
39   
40    /**
41    * Resolves URIs sent by Apache FOP to embed images in the exported PDF. The strategy is the following:
42    * <ul>
43    * <li>When an attachment is rendered during the export (specifically when {@code pdf.vm} is rendered), the
44    * {@link PdfURLFactory} is called and it saves the Attachment Entity Reference in a map in the XWiki Context</li>
45    * <li>When Apache FOP embeds an image it calls this URI Resolver and we try to locate the Attachment Entity
46    * Reference from that map and return the attachment stream.</li>
47    * <li>Attachment links do not call the Resolver and are thus exported correctly using a full URL to the XWiki
48    * server</li>
49    * </ul>
50    *
51    * @version $Id: 91acdb8af85632ebcde5d3f20e8cb4f494bac402 $
52    * @since 5.0RC1
53    * @deprecated since 7.4M2, use {@link PDFResourceResolver} component instead
54    */
55    @Deprecated
 
56    public class PDFURIResolver implements URIResolver
57    {
58    private static final String TEX_ACTION = "/tex/";
59   
60    /**
61    * @see #PDFURIResolver(com.xpn.xwiki.XWikiContext)
62    */
63    private Map<String, AttachmentReference> attachmentMap;
64   
65    /**
66    * @see #PDFURIResolver(com.xpn.xwiki.XWikiContext)
67    */
68    private XWikiContext context;
69   
70    /**
71    * @param context the XWiki Context from where we try to find the attachment map saved in the {@link PdfURLFactory}
72    * earlier on
73    */
 
74  2 toggle public PDFURIResolver(XWikiContext context)
75    {
76  2 this.attachmentMap = (Map<String, AttachmentReference>) context.get(PdfURLFactory.PDF_EXPORT_CONTEXT_KEY);
77  2 this.context = context;
78    }
79   
 
80  2 toggle @Override
81    public Source resolve(String href, String base) throws TransformerException
82    {
83  2 if (this.attachmentMap != null) {
84   
85    // TODO: HACK
86    // We're going through the getAttachmentURL() API so that when the PdfURLFactory is used, the generated
87    // image is saved and then embedded in the exported PDF thanks to PDFURIResolver. In the future we need
88    // to remove this hack by introduce a proper Resource for generated image (say TemporaryResource),
89    // implement a TemporaryResourceSerializer<URL> and introduce a ResourceLoader interface and have it
90    // implemented for TemporaryResource...
91  1 if (href.contains(TEX_ACTION)) {
92    // Note: See the comments in FormulaMacro to understand why we do a replace...
93  0 AttachmentReference reference = this.attachmentMap.get(href.replace(TEX_ACTION, "/download/"));
94  0 if (reference != null) {
95    // Get the generated image's input stream
96  0 ImageStorage storage = Utils.getComponent(ImageStorage.class);
97  0 ImageData image = storage.get(reference.getName());
98  0 return new StreamSource(new ByteArrayInputStream(image.getData()));
99    }
100    }
101    // TODO: end HACK
102   
103  1 AttachmentReference reference = this.attachmentMap.get(href);
104  1 if (reference != null) {
105  1 try {
106  1 XWikiDocument xdoc = this.context.getWiki().getDocument(
107    reference.extractReference(EntityType.DOCUMENT), this.context);
108    // TODO: handle revisions
109  1 XWikiAttachment attachment = xdoc.getAttachment(
110    reference.extractReference(EntityType.ATTACHMENT).getName());
111  1 return new StreamSource(attachment.getContentInputStream(this.context));
112    } catch (Exception e) {
113  0 throw new TransformerException(String.format("Failed to resolve export URI [%s]", href), e);
114    }
115    }
116    }
117   
118    // Defaults to the default URI Resolver in FO
119  1 return null;
120    }
121    }