| 1 | package org.docx4j.openpackaging.io; |
|---|
| 2 | |
|---|
| 3 | import java.io.IOException; |
|---|
| 4 | import java.io.InputStream; |
|---|
| 5 | import java.net.MalformedURLException; |
|---|
| 6 | import java.net.URI; |
|---|
| 7 | import java.net.URISyntaxException; |
|---|
| 8 | import java.net.URL; |
|---|
| 9 | import java.util.Map; |
|---|
| 10 | import java.util.TreeMap; |
|---|
| 11 | |
|---|
| 12 | import org.apache.log4j.Logger; |
|---|
| 13 | import org.docx4j.openpackaging.contenttype.ContentTypes; |
|---|
| 14 | import org.docx4j.openpackaging.exceptions.Docx4JException; |
|---|
| 15 | import org.docx4j.openpackaging.parts.ExternalTarget; |
|---|
| 16 | import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPart; |
|---|
| 17 | import org.docx4j.openpackaging.parts.WordprocessingML.ImageBmpPart; |
|---|
| 18 | import org.docx4j.openpackaging.parts.WordprocessingML.ImageGifPart; |
|---|
| 19 | import org.docx4j.openpackaging.parts.WordprocessingML.ImageJpegPart; |
|---|
| 20 | import org.docx4j.openpackaging.parts.WordprocessingML.ImagePngPart; |
|---|
| 21 | import org.docx4j.openpackaging.parts.WordprocessingML.ImageTiffPart; |
|---|
| 22 | import org.docx4j.openpackaging.parts.WordprocessingML.MetafileEmfPart; |
|---|
| 23 | import org.docx4j.openpackaging.parts.WordprocessingML.MetafileWmfPart; |
|---|
| 24 | |
|---|
| 25 | public class ExternalResourceUtils { |
|---|
| 26 | |
|---|
| 27 | /* This was removed from Load and placed in a separate class, |
|---|
| 28 | * so that Load can load, even if the VFS jar is not present. |
|---|
| 29 | */ |
|---|
| 30 | |
|---|
| 31 | private static Logger log = Logger.getLogger(ExternalResourceUtils.class); |
|---|
| 32 | protected static final Map<String, String> CONTENT_TYPE_MAP = new TreeMap<String, String>(); |
|---|
| 33 | |
|---|
| 34 | static { |
|---|
| 35 | CONTENT_TYPE_MAP.put(ContentTypes.EXTENSION_BMP, ContentTypes.IMAGE_BMP); |
|---|
| 36 | CONTENT_TYPE_MAP.put(ContentTypes.EXTENSION_EMF, ContentTypes.IMAGE_EMF); |
|---|
| 37 | CONTENT_TYPE_MAP.put(ContentTypes.EXTENSION_GIF, ContentTypes.IMAGE_GIF); |
|---|
| 38 | CONTENT_TYPE_MAP.put(ContentTypes.EXTENSION_JPG_1, ContentTypes.IMAGE_JPEG); |
|---|
| 39 | CONTENT_TYPE_MAP.put(ContentTypes.EXTENSION_JPG_2, ContentTypes.IMAGE_JPEG); |
|---|
| 40 | CONTENT_TYPE_MAP.put(ContentTypes.EXTENSION_PNG, ContentTypes.IMAGE_PNG); |
|---|
| 41 | CONTENT_TYPE_MAP.put(ContentTypes.EXTENSION_TIFF, ContentTypes.IMAGE_TIFF); |
|---|
| 42 | CONTENT_TYPE_MAP.put("tif", ContentTypes.IMAGE_TIFF); |
|---|
| 43 | CONTENT_TYPE_MAP.put(ContentTypes.EXTENSION_WMF, ContentTypes.IMAGE_WMF); |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | public static BinaryPart getExternalResource(String absoluteTarget) throws Docx4JException { |
|---|
| 47 | URI targetURI = null; |
|---|
| 48 | URL targetURL = null; |
|---|
| 49 | int p = absoluteTarget.lastIndexOf('.'); |
|---|
| 50 | String fileExtension = (p > -1 ? absoluteTarget.substring(p+1).toLowerCase() : null); |
|---|
| 51 | String contentType = (fileExtension != null ? CONTENT_TYPE_MAP.get(fileExtension) : null); |
|---|
| 52 | BinaryPart binaryPart = null; |
|---|
| 53 | InputStream inStream = null; |
|---|
| 54 | try { |
|---|
| 55 | targetURI = new URI(absoluteTarget.replace('\\', '/')); |
|---|
| 56 | } |
|---|
| 57 | catch (URISyntaxException use) { |
|---|
| 58 | throw new Docx4JException("Invalid absolute Target: '" + absoluteTarget + "'", use); |
|---|
| 59 | } |
|---|
| 60 | try { |
|---|
| 61 | targetURL = targetURI.toURL(); |
|---|
| 62 | } catch (MalformedURLException mue) { |
|---|
| 63 | throw new Docx4JException("Invalid absolute Target: '" + absoluteTarget + "'", mue); |
|---|
| 64 | } |
|---|
| 65 | try { |
|---|
| 66 | inStream = targetURL.openStream(); |
|---|
| 67 | binaryPart = createBinaryPart(absoluteTarget, contentType); |
|---|
| 68 | binaryPart.setBinaryData(inStream); |
|---|
| 69 | } catch (IOException ioe) { |
|---|
| 70 | throw new Docx4JException("Could not load external resource: '" + absoluteTarget + "'", ioe); |
|---|
| 71 | } |
|---|
| 72 | return binaryPart; |
|---|
| 73 | } |
|---|
| 74 | |
|---|
| 75 | protected static BinaryPart createBinaryPart(String absoluteTarget, String contentType) { |
|---|
| 76 | ExternalTarget externalTarget = new ExternalTarget(absoluteTarget); |
|---|
| 77 | BinaryPart ret = null; |
|---|
| 78 | if (ContentTypes.IMAGE_JPEG.equals(contentType)) |
|---|
| 79 | ret = new ImageJpegPart(externalTarget); |
|---|
| 80 | else if (ContentTypes.IMAGE_PNG.equals(contentType)) |
|---|
| 81 | ret = new ImagePngPart(externalTarget); |
|---|
| 82 | else if (ContentTypes.IMAGE_GIF.equals(contentType)) |
|---|
| 83 | ret = new ImageGifPart(externalTarget); |
|---|
| 84 | else if (ContentTypes.IMAGE_TIFF.equals(contentType)) |
|---|
| 85 | ret = new ImageTiffPart(externalTarget); |
|---|
| 86 | else if (ContentTypes.IMAGE_BMP.equals(contentType)) |
|---|
| 87 | ret = new ImageBmpPart(externalTarget); |
|---|
| 88 | else if (ContentTypes.IMAGE_EMF.equals(contentType)) |
|---|
| 89 | ret = new MetafileEmfPart(externalTarget); |
|---|
| 90 | else if (ContentTypes.IMAGE_WMF.equals(contentType)) |
|---|
| 91 | ret = new MetafileWmfPart(externalTarget); |
|---|
| 92 | else |
|---|
| 93 | ret = new BinaryPart(externalTarget); |
|---|
| 94 | |
|---|
| 95 | return ret; |
|---|
| 96 | } |
|---|
| 97 | |
|---|
| 98 | } |
|---|