Ignore:
Timestamp:
08/07/11 10:34:49 (10 months ago)
Author:
jharrop
Message:

Alberto's patch of 3 August.

  • The ConversionImageHandler?.handleImage is declared now as throwing a Doc4jException
  • I have removed the dependencies on vfs

If the ConversionImageHandler?.handleImage throws an Exception, it will be logged as an error in the AbstractWordXmlPicture?, but it won’t be propagated. I’m not sure, but I think that an exception in handleImage should not stop the conversion process.

Removing the dependencies on vfs:

org.docx4j.model.images.DefaultConversionImageHandler? has been replaced with the following hierarchy:

org.docx4j.model.images.AbstractConversionImageHandler?

-> org.docx4j.extras.vfs.VFSConversionImageHandler

-> org.docx4j.model.images.FileConversionImageHandler?

-> org.docx4j.convert.out.html.HTMLConversionImageHandler

-> org.docx4j.convert.out.pdf.viaXSLFO.PDFConversionImageHandler

  • VFSConversionImageHandler offers the old functionality based on VFS
  • FileConversionImageHandler? is based on plain File(s), this means that it won’t be able to handle such cases where the target directory is on an ftp-server (or something comparable).
  • HTMLConversionImageHandler. In HTMLSettings there is a new Setting ‘imageTargetURI’ that allows to define a prefix for the image URI. If it is set, HTMLConversionImageHandler will use it as an prefix for the image URI, otherwise the image URI will just be the image name. This behavior replaces fixImgSrcURL.
  • PDFConversionImageHandler will always use the absolute path of the file as the image URI.

Apart from this there is a new setting in AbstractConversionSettings? (‘imageIncludeUUID’) that allows to switch on or off the UUID (default is on)

org.docx4j.openpackaging.io.LoadFromVFSZipFile
org.docx4j.openpackaging.io.SaveToVFSZipFile and
org.docx4j.utils.VFSUtils

have been moved to org.docx4j.extras.vfs, there is no replacement for them.

org.docx4j.convert.in.Doc has been split in org.docx4j.convert.in.Doc (without convert(org.apache.commons.vfs.FileObject? in)) and org.docx4j.extras.vfs.VFSDoc (with the method)

org.docx4j.openpackaging.io.ExternalResourceUtils? now uses an URLConnection instead of VFS

If you put the stuff in org.docx4j.extras.vfs in docx4j-extras, you should be able to compile docx4j without the dependency on VFS.

Some additional changes:

  • A small detail: WordXmlPictureE20, checked blip.getEmbed() == null and blip.getLink() == null, but those getters would return an empty string.
Location:
trunk/docx4j/src/main/java/org/docx4j/openpackaging
Files:
2 deleted
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/docx4j/src/main/java/org/docx4j/openpackaging/io/ExternalResourceUtils.java

    r1232 r1637  
    11package org.docx4j.openpackaging.io; 
    22 
    3 import org.apache.commons.vfs.FileContent; 
    4 import org.apache.commons.vfs.FileObject; 
    5 import org.apache.commons.vfs.FileSystemException; 
    6 import org.apache.commons.vfs.VFS; 
     3import java.io.IOException; 
     4import java.io.InputStream; 
     5import java.net.MalformedURLException; 
     6import java.net.URI; 
     7import java.net.URISyntaxException; 
     8import java.net.URL; 
     9import java.util.Map; 
     10import java.util.TreeMap; 
     11 
    712import org.apache.log4j.Logger; 
     13import org.docx4j.openpackaging.contenttype.ContentTypes; 
    814import org.docx4j.openpackaging.exceptions.Docx4JException; 
    915import org.docx4j.openpackaging.parts.ExternalTarget; 
    1016import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPart; 
     17import org.docx4j.openpackaging.parts.WordprocessingML.ImageBmpPart; 
    1118import org.docx4j.openpackaging.parts.WordprocessingML.ImageGifPart; 
    1219import org.docx4j.openpackaging.parts.WordprocessingML.ImageJpegPart; 
    1320import org.docx4j.openpackaging.parts.WordprocessingML.ImagePngPart; 
     21import org.docx4j.openpackaging.parts.WordprocessingML.ImageTiffPart; 
     22import org.docx4j.openpackaging.parts.WordprocessingML.MetafileEmfPart; 
     23import org.docx4j.openpackaging.parts.WordprocessingML.MetafileWmfPart; 
    1424 
    1525public class ExternalResourceUtils { 
     
    2030 
    2131        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        } 
    2245         
    2346        public static BinaryPart getExternalResource(String absoluteTarget) throws Docx4JException { 
    24  
     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; 
    2554                try { 
    26                         FileObject fo = VFS.getManager().resolveFile(absoluteTarget); 
    27                          
    28                         ExternalTarget externalTarget = new ExternalTarget(absoluteTarget); 
    29                          
    30                         // Assume it is a binary part, though there is no reason in principle 
    31                         // that it couldn't be an XML part..                     
    32                         BinaryPart bp; 
    33                         if (absoluteTarget.toLowerCase().endsWith(".gif" )) { 
    34                                  
    35                                 bp = new ImageGifPart(externalTarget);  
    36                                  
    37                         } else if  (absoluteTarget.toLowerCase().endsWith(".jpeg" ) 
    38                                         || absoluteTarget.toLowerCase().endsWith(".jpg" )) { 
    39                                  
    40                                 bp = new ImageJpegPart(externalTarget);  
    41                                  
    42                         } else if (absoluteTarget.toLowerCase().endsWith(".png" )) { 
    43                                  
    44                                 bp = new ImagePngPart(externalTarget);  
    45                                  
    46                         } else { 
    47                                 log.warn("Using simple BinaryPart for " + absoluteTarget); 
    48                                 bp = new BinaryPart(externalTarget); 
    49                         } 
    50                          
    51                         FileContent fc = fo.getContent(); 
    52                         bp.setBinaryData(fc.getInputStream());                   
    53                          
    54                         return bp; 
    55                          
    56                 } catch (FileSystemException exc) { 
    57                         exc.printStackTrace(); 
    58                         throw new Docx4JException("Couldn't get FileObject", exc);                       
     55                        targetURI = new URI(absoluteTarget.replace('\\', '/')); 
    5956                } 
    60                  
     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; 
    6173        } 
    6274         
     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        } 
    6397 
    6498} 
  • trunk/docx4j/src/main/java/org/docx4j/openpackaging/parts/WordprocessingML/MetafileEmfPart.java

    r1487 r1637  
    11package org.docx4j.openpackaging.parts.WordprocessingML; 
    22 
    3 import java.io.File; 
    4 import java.io.FileInputStream; 
    5 import java.io.FileOutputStream; 
    6 import java.io.OutputStream; 
    7  
    83import org.docx4j.openpackaging.exceptions.InvalidFormatException; 
     4import org.docx4j.openpackaging.parts.ExternalTarget; 
    95import org.docx4j.openpackaging.parts.PartName; 
    106import org.docx4j.openpackaging.parts.relationships.Namespaces; 
    11 import org.docx4j.utils.BufferUtil; 
    127 
    138/** 
     
    6156        } 
    6257         
     58        public MetafileEmfPart(ExternalTarget externalTarget) { 
     59                super(externalTarget); 
     60                init(); 
     61        }        
     62         
    6363        public void init() { 
    6464                // Used if this Part is added to [Content_Types].xml  
  • trunk/docx4j/src/main/java/org/docx4j/openpackaging/parts/WordprocessingML/MetafilePart.java

    r1487 r1637  
    22 
    33import org.docx4j.openpackaging.exceptions.InvalidFormatException; 
     4import org.docx4j.openpackaging.parts.ExternalTarget; 
    45import org.docx4j.openpackaging.parts.PartName; 
    56 
     
    1617        } 
    1718         
     19        public MetafilePart(ExternalTarget externalTarget) { 
     20                super(externalTarget); 
     21        }        
    1822 
    1923} 
  • trunk/docx4j/src/main/java/org/docx4j/openpackaging/parts/WordprocessingML/MetafileWmfPart.java

    r1078 r1637  
    11package org.docx4j.openpackaging.parts.WordprocessingML; 
    22 
    3 import java.io.File; 
    4 import java.io.FileInputStream; 
    5 import java.io.FileNotFoundException; 
    6 import java.io.FileOutputStream; 
    7 import java.io.IOException; 
    83import java.io.InputStream; 
    9 import java.io.OutputStream; 
    10  
    11 import javax.xml.transform.OutputKeys; 
    12 import javax.xml.transform.Transformer; 
    13 import javax.xml.transform.TransformerFactory; 
    14 import javax.xml.transform.dom.DOMSource; 
    15 import javax.xml.transform.stream.StreamResult; 
    164 
    175import net.arnx.wmf2svg.gdi.svg.SvgGdi; 
     
    197 
    208import org.docx4j.openpackaging.exceptions.InvalidFormatException; 
     9import org.docx4j.openpackaging.parts.ExternalTarget; 
    2110import org.docx4j.openpackaging.parts.PartName; 
    2211import org.docx4j.openpackaging.parts.relationships.Namespaces; 
     
    6453        } 
    6554         
     55        public MetafileWmfPart(ExternalTarget externalTarget) { 
     56                super(externalTarget); 
     57                init(); 
     58        }        
     59         
    6660        public void init() { 
    6761                // Used if this Part is added to [Content_Types].xml  
Note: See TracChangeset for help on using the changeset viewer.