1 13 package org.pentaho.plugin.jfreereport.helper; 14 15 import java.awt.Image ; 16 import java.io.File ; 17 import java.io.FileOutputStream ; 18 import java.io.IOException ; 19 import java.io.OutputStream ; 20 import java.net.URL ; 21 import java.text.MessageFormat ; 22 import java.util.HashMap ; 23 24 import org.jfree.report.ImageContainer; 25 import org.jfree.report.LocalImageContainer; 26 import org.jfree.report.URLImageContainer; 27 import org.jfree.report.modules.output.table.html.HtmlFilesystem; 28 import org.jfree.report.modules.output.table.html.ZIPHtmlFilesystem; 29 import org.jfree.report.modules.output.table.html.ref.EmptyContentReference; 30 import org.jfree.report.modules.output.table.html.ref.ExternalStyleSheetReference; 31 import org.jfree.report.modules.output.table.html.ref.HtmlImageReference; 32 import org.jfree.report.modules.output.table.html.ref.HtmlReference; 33 import org.jfree.report.modules.output.table.html.util.CounterReference; 34 import org.jfree.report.resourceloader.ImageFactory; 35 import org.jfree.report.util.ImageComparator; 36 import org.jfree.report.util.StringUtil; 37 import org.jfree.util.Log; 38 import org.jfree.util.WaitingImageObserver; 39 import org.pentaho.core.repository.IContentLocation; 40 import org.pentaho.core.system.IApplicationContext; 41 import org.pentaho.core.system.PentahoSystem; 42 import org.pentaho.messages.Messages; 43 44 import com.keypoint.PngEncoder; 45 46 51 public class PentahoHtmlFilesystem implements HtmlFilesystem 52 { 53 private String actionName; 55 56 public PentahoHtmlFilesystem() 57 { 58 } 59 60 63 private OutputStream rootStream; 64 65 68 private HashMap usedNames; 69 70 73 private HashMap usedImages; 74 75 78 private HashMap encodedImages; 79 80 83 private ImageComparator comparator; 84 85 88 private boolean copyExternalImages; 89 90 93 private boolean digestImageCompare; 94 95 private MessageFormat contentPathPattern; 96 97 106 public PentahoHtmlFilesystem (final OutputStream out, 107 final IContentLocation dataDirectory, 108 final String actionName, 109 final boolean copyExternalImages, 110 final String contentPathPattern) 111 { 112 if (out == null) 113 { 114 throw new NullPointerException (); 115 } 116 if (contentPathPattern == null) 121 { 122 throw new NullPointerException (); 123 } 124 125 this.actionName = actionName; 126 this.rootStream = out; 127 this.copyExternalImages = copyExternalImages; 128 130 this.contentPathPattern = new MessageFormat (contentPathPattern); 131 132 this.usedNames = new HashMap (); 133 this.usedImages = new HashMap (); 134 this.comparator = new ImageComparator(); 135 this.encodedImages = new HashMap (); 136 } 137 138 146 public OutputStream getRootStream () 147 throws IOException 148 { 149 return rootStream; 150 } 151 152 public String getActionName() 153 { 154 return actionName; 155 } 156 157 168 public boolean isCopyExternalImages () 169 { 170 return copyExternalImages; 171 } 172 173 184 public void setCopyExternalImages (final boolean copyExternalImages) 185 { 186 this.copyExternalImages = copyExternalImages; 187 } 188 189 198 protected boolean isSupportedImageFormat (final URL url) 199 { 200 final String file = url.getFile(); 201 if (StringUtil.endsWithIgnoreCase(file, ".jpg")) { 203 return true; 204 } 205 if (StringUtil.endsWithIgnoreCase(file, ".jpeg")) { 207 return true; 208 } 209 if (StringUtil.endsWithIgnoreCase(file, ".png")) { 211 return true; 212 } 213 if (StringUtil.endsWithIgnoreCase(file, ".gif")) { 215 return true; 216 } 217 return false; 218 } 219 220 237 public HtmlReference createImageReference (final ImageContainer reference) 238 throws IOException 239 { 240 241 if (reference instanceof URLImageContainer) 243 { 244 final URLImageContainer urlImage = (URLImageContainer) reference; 245 246 final URL url = urlImage.getSourceURL(); 248 if (url != null) 249 { 250 final String name = (String ) usedImages.get(url); 251 if (name != null) 252 { 253 return new HtmlImageReference(name); 254 } 255 256 259 if (isSupportedImageFormat(url)) 262 { 263 final String baseName = urlImage.getSourceURL().toExternalForm(); 290 usedImages.put(url, baseName); 291 return new HtmlImageReference(baseName); 292 } 295 296 299 if (urlImage.isLoadable() == false) 302 { 303 return new EmptyContentReference(); 304 } 305 306 Image image = null; 308 309 if (reference instanceof LocalImageContainer) 312 { 313 final LocalImageContainer li = (LocalImageContainer) reference; 315 image = li.getImage(); 316 } 317 if (image == null) 318 { 319 image = ImageFactory.getInstance().createImage(url); 320 } 321 if (image != null) 322 { 323 final String entryName = encodeImage(image, false); 327 usedImages.put(url, entryName); 328 return new HtmlImageReference(entryName); 329 } 330 } 331 else if (urlImage.getSourceURLString() != null) 332 { 333 return new HtmlImageReference(urlImage.getSourceURLString()); 334 } 335 } 336 337 if (reference instanceof LocalImageContainer) 341 { 342 final LocalImageContainer li = (LocalImageContainer) reference; 352 final Image image = li.getImage(); 353 if (image != null) 354 { 355 if (li.isIdentifiable()) 356 { 357 final Object identity = li.getIdentity(); 358 String name = (String ) usedImages.get(identity); 359 if (name == null) 360 { 361 name = encodeImage(image, false); 362 usedImages.put(identity, name); 363 } 364 return new HtmlImageReference(name); 365 } 366 return new HtmlImageReference(encodeImage(image, true)); 367 } 368 } 369 370 return new EmptyContentReference(); 373 } 374 375 private String getMimeType(final String extension) 376 { 377 if ("jpg".equalsIgnoreCase(extension)) { 379 return "image/jpeg"; } 381 if ("jpeg".equalsIgnoreCase(extension)) { 383 return "image/jpeg"; } 385 if ("gif".equalsIgnoreCase(extension)) { 387 return "image/gif"; } 389 if ("png".equalsIgnoreCase(extension)) { 391 return "image/png"; } 393 return null; 394 } 395 396 407 private String encodeImage (final Image image, final boolean createComparator) 408 throws IOException 409 { 410 final WaitingImageObserver obs = new WaitingImageObserver(image); 412 obs.waitImageLoaded(); 413 414 final PngEncoder encoder = new PngEncoder(image, 415 PngEncoder.ENCODE_ALPHA, PngEncoder.FILTER_NONE, 5); 416 final byte[] data = encoder.pngEncode(); 417 418 final Object object; 419 if (createComparator) 420 { 421 object = comparator.createCompareData(data, isDigestImageCompare() == false); 422 final String name = (String ) encodedImages.get(object); 423 if (name != null) 424 { 425 return name; 426 } 427 } 428 else 429 { 430 object = null; 431 } 432 433 final String fileName = createName("picture"); final String extension = "png"; 437 final String entryName = fileName + "." + extension; 439 IApplicationContext ctx = PentahoSystem.getApplicationContext(); 440 File directory = null; 441 if (ctx != null) 442 { 443 directory = new File (ctx.getFileOutputPath("system/tmp/")); } 445 else 446 { 447 Log.error(Messages.getErrorString("ABSTRACTCHARTEXPRESSION.ERROR_0006_PENTAHOSYSTEM_UNINITIALIZED")); String tempDir = System.getProperty("java.io.tmpdir"); directory = new File (tempDir); 450 } 451 452 File file = File.createTempFile("tmp_chart", ".png", directory); file.deleteOnExit(); 454 455 OutputStream out = new FileOutputStream (file); 456 out.write(data); 457 out.close(); 458 459 460 if (createComparator) 461 { 462 encodedImages.put(object, entryName); 463 } 464 465 if (ctx != null) { 466 return ctx.getBaseUrl() + "getImage?image=" + file.getName(); } 468 return null; 469 } 470 471 477 private String createName (final String base) 478 { 479 CounterReference ref = (CounterReference) usedNames.get(base); 480 if (ref == null) 481 { 482 ref = new CounterReference(); 483 usedNames.put(base, ref); 484 return base; 485 } 486 else 487 { 488 ref.increase(); 489 return base + ref.getCount(); 490 } 491 } 492 493 502 public HtmlReference createCSSReference (final String styleSheet) 503 throws IOException 504 { 505 506 IApplicationContext ctx = PentahoSystem.getApplicationContext(); 507 File directory = null; 508 if (ctx != null) 509 { 510 directory = new File (ctx.getFileOutputPath("system/tmp/")); } 512 else 513 { 514 Log.error(Messages.getErrorString("ABSTRACTCHARTEXPRESSION.ERROR_0006_PENTAHOSYSTEM_UNINITIALIZED")); String tempDir = System.getProperty("java.io.tmpdir"); directory = new File (tempDir); 517 } 518 519 File file = File.createTempFile("tmp_chart", ".css", directory); file.deleteOnExit(); 521 522 OutputStream out = new FileOutputStream (file); 523 out.write(styleSheet.getBytes()); 524 out.close(); 525 526 if (ctx != null) { 527 return new ExternalStyleSheetReference(ctx.getBaseUrl() + "getImage?image=" + file.getName()); } 529 return null; 530 } 531 532 538 public void close () 539 throws IOException 540 { 541 rootStream.flush(); 542 rootStream.close(); 543 } 544 545 552 public boolean isDigestImageCompare () 553 { 554 return digestImageCompare; 555 } 556 557 565 public void setDigestImageCompare (final boolean digestImageCompare) 566 { 567 this.digestImageCompare = digestImageCompare; 568 } 569 570 } 571 | Popular Tags |