Changeset 1680 for trunk/docx4j/src/main/java/org/docx4j/openpackaging
- Timestamp:
- 10/07/11 03:32:33 (8 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/docx4j/src/main/java/org/docx4j/openpackaging/parts/WordprocessingML/BinaryPartAbstractImage.java
r1679 r1680 32 32 import java.io.InputStream; 33 33 import java.io.OutputStream; 34 import java.net.MalformedURLException; 35 import java.net.URL; 34 36 import java.util.List; 35 37 … … 62 64 import org.docx4j.openpackaging.parts.relationships.RelationshipsPart; 63 65 import org.docx4j.relationships.Relationship; 64 import org.docx4j.wml.SectPr;65 import org.docx4j.wml.SectPr.PgMar;66 import org.docx4j.wml.SectPr.PgSz;67 66 68 67 public abstract class BinaryPartAbstractImage extends BinaryPart { … … 148 147 } 149 148 150 /* 149 /** 151 150 * Possibility to put directly an image filePath instead of giving an image byte array 152 151 * @param wordMLPackage 153 * @param filePath152 * @param imageFile 154 153 * 155 154 */ 156 155 public static BinaryPartAbstractImage createImagePart(WordprocessingMLPackage wordMLPackage, 157 String filePath) throws Exception {158 159 return createImagePart FromFilePath(wordMLPackage,160 wordMLPackage.getMainDocumentPart(), filePath);156 File imageFile) throws Exception { 157 158 return createImagePart(wordMLPackage, 159 wordMLPackage.getMainDocumentPart(), imageFile); 161 160 162 161 } … … 224 223 log.debug("created tmp file: " + tmpImageFile.getAbsolutePath()); 225 224 226 ImageInfo info = ensureFormatIsSupported(tmpImageFile .getAbsolutePath(), tmpImageFile, bytes);225 ImageInfo info = ensureFormatIsSupported(tmpImageFile, bytes, true); 227 226 228 227 // In the absence of an exception, tmpImageFile now contains an image … … 277 276 } 278 277 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 false282 */283 static boolean isLoad = true;284 278 285 279 /** … … 295 289 * @throws Exception 296 290 */ 297 public static BinaryPartAbstractImage createImagePart FromFilePath(291 public static BinaryPartAbstractImage createImagePart( 298 292 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 302 295 final byte[] locByte = new byte[1]; 296 303 297 //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); 307 299 308 300 ContentTypeManager ctm = opcPackage.getContentTypeManager(); … … 325 317 + " with name " + imagePart.getPartName().toString()); 326 318 327 FileInputStream fis = new FileInputStream( locFile);319 FileInputStream fis = new FileInputStream(imageFile); 328 320 imagePart.setBinaryData(fis); 329 321 … … 336 328 } 337 329 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 338 334 /** 339 335 * @param bytes … … 345 341 * @throws InterruptedException 346 342 */ 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 { 348 344 349 345 FileOutputStream fos; … … 354 350 try { 355 351 try { 356 info = getImageInfo(ur i);357 352 info = getImageInfo(url); 353 358 354 // Debug ... note that these figures 359 355 // aren't necessarily accurate for EPS … … 423 419 // We need to refresh image info 424 420 imageManager.getCache().clearCache(); 425 info = getImageInfo( imageFile.getAbsolutePath());421 info = getImageInfo(new URL(imageFile.getAbsolutePath())); 426 422 427 423 // Debug ... … … 444 440 */ 445 441 public static BinaryPartAbstractImage createLinkedImagePart(WordprocessingMLPackage wordMLPackage, 446 Stringfileurl) throws Exception {442 URL fileurl) throws Exception { 447 443 448 444 return createLinkedImagePart(wordMLPackage, … … 452 448 /** 453 449 * 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. 455 454 * 456 455 * @param wordMLPackage 457 456 * @param sourcePart 458 * @param fileurl457 * @param url 459 458 * @return 460 459 * @throws Exception 461 460 */ 462 461 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 466 467 467 468 ContentTypeManager ctm = opcPackage.getContentTypeManager(); … … 475 476 info.getMimeType(), 476 477 createImageName(opcPackage, sourcePart, proposedRelId, ext), null); 478 479 // NB: contents never populated 477 480 478 481 log.debug("created part " + imagePart.getClass().getName() 479 482 + " with name " + imagePart.getPartName().toString()); 480 483 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 482 485 imagePart.rel.setTargetMode("External"); 483 486 … … 485 488 imagePart); 486 489 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()); 492 496 493 497 imagePart.setImageInfo(info); 498 494 499 return imagePart; 495 500 } … … 680 685 + "xmlns:wp=\"http://schemas.openxmlformats.org/drawingml/2006/wordprocessingDrawing\""; 681 686 682 public static ImageInfo getImageInfo( String uri) throws Exception {687 public static ImageInfo getImageInfo(URL url) throws Exception { 683 688 684 689 // XmlGraphics images caches images by their URI; … … 689 694 imageManager.getImageContext(), null); 690 695 691 ImageInfo info = imageManager.getImageInfo(ur i, sessionContext);696 ImageInfo info = imageManager.getImageInfo(url.toString(), sessionContext); 692 697 693 698 // Note that these figures do not appear to be reliable for EPS … … 729 734 //String uri = "/tmp/img4448.img"; 730 735 731 ImageInfo ii = getImageInfo( uri);736 ImageInfo ii = getImageInfo(new URL(uri)); 732 737 733 738 displayImageInfo(ii);
Note: See TracChangeset
for help on using the changeset viewer.
