Changeset 1680


Ignore:
Timestamp:
10/07/11 03:32:33 (8 months ago)
Author:
jharrop
Message:

createImagePart from FilePath? - refactored a bit

Location:
trunk/docx4j/src
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/docx4j/src/main/java/org/docx4j/openpackaging/parts/WordprocessingML/BinaryPartAbstractImage.java

    r1679 r1680  
    3232import java.io.InputStream; 
    3333import java.io.OutputStream; 
     34import java.net.MalformedURLException; 
     35import java.net.URL; 
    3436import java.util.List; 
    3537 
     
    6264import org.docx4j.openpackaging.parts.relationships.RelationshipsPart; 
    6365import org.docx4j.relationships.Relationship; 
    64 import org.docx4j.wml.SectPr; 
    65 import org.docx4j.wml.SectPr.PgMar; 
    66 import org.docx4j.wml.SectPr.PgSz; 
    6766 
    6867public abstract class BinaryPartAbstractImage extends BinaryPart { 
     
    148147        } 
    149148         
    150     /* 
     149    /** 
    151150     * Possibility to put directly an image filePath instead of giving an image byte array 
    152151     * @param wordMLPackage 
    153      * @param filePath 
     152     * @param imageFile 
    154153     *  
    155154     */ 
    156155    public static BinaryPartAbstractImage createImagePart(WordprocessingMLPackage wordMLPackage, 
    157             String filePath) throws Exception { 
    158  
    159         return createImagePartFromFilePath(wordMLPackage, 
    160                 wordMLPackage.getMainDocumentPart(), filePath); 
     156                File imageFile) throws Exception { 
     157 
     158        return createImagePart(wordMLPackage, 
     159                wordMLPackage.getMainDocumentPart(), imageFile); 
    161160 
    162161    } 
     
    224223        log.debug("created tmp file: " + tmpImageFile.getAbsolutePath()); 
    225224                                 
    226                 ImageInfo info = ensureFormatIsSupported(tmpImageFile.getAbsolutePath(), tmpImageFile, bytes); 
     225                ImageInfo info = ensureFormatIsSupported(tmpImageFile, bytes, true); 
    227226                 
    228227                // In the absence of an exception, tmpImageFile now contains an image  
     
    277276        } 
    278277 
    279     /* 
    280      * Default, we suppose that image is load in a Byte Array (using function createImagePart) so isLoad is true. 
    281      * If we use createImagePartFromFilePath, then isLoad is false 
    282      */ 
    283     static boolean isLoad = true; 
    284278 
    285279        /** 
     
    295289     * @throws Exception 
    296290     */ 
    297     public static BinaryPartAbstractImage createImagePartFromFilePath( 
     291    public static BinaryPartAbstractImage createImagePart( 
    298292            OpcPackage opcPackage, 
    299             Part sourcePart, String filePath) throws Exception { 
    300  
    301         final File locFile = new File(filePath); 
     293            Part sourcePart, File imageFile) throws Exception { 
     294 
    302295        final byte[] locByte = new byte[1]; 
     296 
    303297        //We are in the case that image is not load (no byte Array) so isLoad is false 
    304         isLoad = false; 
    305         //Here, filePath doesn't represent the tmpFile but the path of the image to load 
    306         ImageInfo info = ensureFormatIsSupported(filePath, locFile, locByte); 
     298        ImageInfo info = ensureFormatIsSupported(imageFile, locByte, false); 
    307299 
    308300        ContentTypeManager ctm = opcPackage.getContentTypeManager(); 
     
    325317                + " with name " + imagePart.getPartName().toString()); 
    326318 
    327         FileInputStream fis = new FileInputStream(locFile); 
     319        FileInputStream fis = new FileInputStream(imageFile); 
    328320        imagePart.setBinaryData(fis); 
    329321 
     
    336328    } 
    337329 
     330        private static ImageInfo ensureFormatIsSupported(File imageFile, byte[] bytes, boolean isLoad) throws Docx4JException, MalformedURLException { 
     331                return ensureFormatIsSupported(imageFile.toURI().toURL(),  imageFile,  bytes, isLoad); 
     332        } 
     333         
    338334    /** 
    339335         * @param bytes 
     
    345341         * @throws InterruptedException 
    346342         */ 
    347         private static ImageInfo ensureFormatIsSupported(String uri, File imageFile, byte[] bytes) throws Docx4JException { 
     343        private static ImageInfo ensureFormatIsSupported(URL url, File imageFile, byte[] bytes, boolean isLoad) throws Docx4JException { 
    348344                 
    349345                FileOutputStream fos; 
     
    354350                try { 
    355351                        try { 
    356                                 info = getImageInfo(uri); 
    357                                  
     352                                info = getImageInfo(url); 
     353 
    358354                                // Debug ... note that these figures  
    359355                                // aren't necessarily accurate for EPS 
     
    423419                                // We need to refresh image info  
    424420                                imageManager.getCache().clearCache(); 
    425                 info = getImageInfo(imageFile.getAbsolutePath()); 
     421                info = getImageInfo(new URL(imageFile.getAbsolutePath())); 
    426422                                 
    427423                                // Debug ... 
     
    444440         */ 
    445441        public static BinaryPartAbstractImage createLinkedImagePart(WordprocessingMLPackage wordMLPackage,  
    446                         String fileurl) throws Exception { 
     442                        URL fileurl) throws Exception { 
    447443                 
    448444                return createLinkedImagePart(wordMLPackage, 
     
    452448        /** 
    453449         * Create a linked image part, and attach it as a rel of the specified source part 
    454          * (eg a header part) 
     450         * (eg a header part). 
     451         *  
     452         * The current behaviour is that the part is added to the package, but  
     453         * since the target mode of the rel is external, the part is redundant.  
    455454         *  
    456455         * @param wordMLPackage 
    457456         * @param sourcePart 
    458          * @param fileurl 
     457         * @param url 
    459458         * @return 
    460459         * @throws Exception 
    461460         */ 
    462461        public static BinaryPartAbstractImage createLinkedImagePart(OpcPackage opcPackage,  
    463                         Part sourcePart, String fileurl) throws Exception { 
    464                  
    465         ImageInfo info = ensureFormatIsSupported(fileurl, null, null); 
     462                        Part sourcePart, URL url) throws Exception { 
     463                 
     464                log.debug("Incoming url for linked image: " + url.toString() ); 
     465                 
     466        ImageInfo info = ensureFormatIsSupported(url, null, null, false); // final param doesn't matter in this case 
    466467 
    467468                ContentTypeManager ctm = opcPackage.getContentTypeManager(); 
     
    475476                                info.getMimeType(),  
    476477                createImageName(opcPackage, sourcePart, proposedRelId, ext), null); 
     478                 
     479                // NB: contents never populated 
    477480                                 
    478481                log.debug("created part " + imagePart.getClass().getName() 
    479482                                + " with name " + imagePart.getPartName().toString()); 
    480483 
    481                 imagePart.rel = sourcePart.addTargetPart(imagePart); 
     484                imagePart.rel = sourcePart.addTargetPart(imagePart); // want to create rel with suitable name; side effect is to add part 
    482485                imagePart.rel.setTargetMode("External"); 
    483486 
     
    485488                                imagePart);                      
    486489                 
    487                 if (!fileurl.startsWith("file:///") && new File(fileurl).isFile()) { 
    488                         imagePart.rel.setTarget("file:///" + fileurl); 
    489                 } else { 
    490                         imagePart.rel.setTarget(fileurl); 
    491                 } 
     490//              if (!url.getProtocol().equals("file") && new File(url.toString() ).isFile()) { 
     491//                      imagePart.rel.setTarget("file:///" + url); 
     492//              } else { 
     493//                      imagePart.rel.setTarget(url.toString()); 
     494//              } 
     495                imagePart.rel.setTarget(url.toString()); 
    492496 
    493497                imagePart.setImageInfo(info); 
     498                 
    494499                return imagePart; 
    495500        }        
     
    680685            + "xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\""; 
    681686         
    682         public static ImageInfo getImageInfo(String uri) throws Exception { 
     687        public static ImageInfo getImageInfo(URL url) throws Exception { 
    683688                 
    684689                // XmlGraphics images caches images by their URI; 
     
    689694                                imageManager.getImageContext(), null); 
    690695 
    691                 ImageInfo info = imageManager.getImageInfo(uri, sessionContext); 
     696                ImageInfo info = imageManager.getImageInfo(url.toString(), sessionContext); 
    692697                 
    693698                // Note that these figures do not appear to be reliable for EPS 
     
    729734                //String uri = "/tmp/img4448.img"; 
    730735                 
    731                 ImageInfo ii = getImageInfo(uri); 
     736                ImageInfo ii = getImageInfo(new URL(uri)); 
    732737                 
    733738                displayImageInfo(ii); 
Note: See TracChangeset for help on using the changeset viewer.