Search in sources :

Example 1 with Resource

use of org.apache.xmlgraphics.io.Resource in project xwiki-platform by xwiki.

the class PDFResourceResolver method getResource.

@Override
public Resource getResource(URI uri) throws IOException {
    XWikiContext xcontext = xcontextProvider.get();
    Map<String, AttachmentReference> attachmentMap = (Map<String, AttachmentReference>) xcontext.get(PdfURLFactory.PDF_EXPORT_CONTEXT_KEY);
    if (attachmentMap != null) {
        String uriString = uri.toString();
        // implemented for TemporaryResource...
        if (uriString.contains(TEX_ACTION)) {
            // Note: See the comments in FormulaMacro to understand why we do a replace...
            AttachmentReference reference = attachmentMap.get(uriString.replace(TEX_ACTION, "/download/"));
            if (reference != null) {
                // Get the generated image's input stream
                ImageStorage storage = Utils.getComponent(ImageStorage.class);
                ImageData image = storage.get(reference.getName());
                return new Resource(new ByteArrayInputStream(image.getData()));
            }
        }
        // TODO: end HACK
        AttachmentReference reference = attachmentMap.get(uriString);
        if (reference != null) {
            try {
                XWikiDocument xdoc = xcontext.getWiki().getDocument(reference.extractReference(EntityType.DOCUMENT), xcontext);
                // TODO: handle revisions
                XWikiAttachment attachment = xdoc.getAttachment(reference.extractReference(EntityType.ATTACHMENT).getName());
                return new Resource(attachment.getContentInputStream(xcontext));
            } catch (Exception e) {
                throw new IOException(String.format("Failed to resolve export URI [%s]", uriString), e);
            }
        }
    }
    return this.standardResolver.getResource(uri);
}
Also used : AttachmentReference(org.xwiki.model.reference.AttachmentReference) ImageStorage(org.xwiki.formula.ImageStorage) Resource(org.apache.xmlgraphics.io.Resource) XWikiContext(com.xpn.xwiki.XWikiContext) XWikiAttachment(com.xpn.xwiki.doc.XWikiAttachment) IOException(java.io.IOException) IOException(java.io.IOException) XWikiDocument(com.xpn.xwiki.doc.XWikiDocument) ByteArrayInputStream(java.io.ByteArrayInputStream) ImageData(org.xwiki.formula.ImageData) Map(java.util.Map)

Example 2 with Resource

use of org.apache.xmlgraphics.io.Resource in project grafikon by jub77.

the class PdfTransformer method convertResolver.

private static ResourceResolver convertResolver(URIResolver uriResolver) {
    return new ResourceResolver() {

        @Override
        public Resource getResource(URI uri) throws IOException {
            String asciiUri = uri.toASCIIString();
            InputStream is = PdfTransformer.class.getClassLoader().getResourceAsStream(asciiUri);
            if (is == null && uriResolver != null) {
                try {
                    StreamSource streamSource = (StreamSource) uriResolver.resolve(asciiUri, null);
                    if (streamSource != null) {
                        is = streamSource.getInputStream();
                    }
                } catch (TransformerException e) {
                    throw new IOException(e);
                }
            }
            return is == null ? null : new Resource(is);
        }

        @Override
        public OutputStream getOutputStream(URI uri) throws IOException {
            return null;
        }
    };
}
Also used : InputStream(java.io.InputStream) StreamSource(javax.xml.transform.stream.StreamSource) ResourceResolver(org.apache.xmlgraphics.io.ResourceResolver) Resource(org.apache.xmlgraphics.io.Resource) IOException(java.io.IOException) URI(java.net.URI)

Aggregations

IOException (java.io.IOException)2 Resource (org.apache.xmlgraphics.io.Resource)2 XWikiContext (com.xpn.xwiki.XWikiContext)1 XWikiAttachment (com.xpn.xwiki.doc.XWikiAttachment)1 XWikiDocument (com.xpn.xwiki.doc.XWikiDocument)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 URI (java.net.URI)1 Map (java.util.Map)1 StreamSource (javax.xml.transform.stream.StreamSource)1 ResourceResolver (org.apache.xmlgraphics.io.ResourceResolver)1 ImageData (org.xwiki.formula.ImageData)1 ImageStorage (org.xwiki.formula.ImageStorage)1 AttachmentReference (org.xwiki.model.reference.AttachmentReference)1