1 18 package org.apache.batik.apps.rasterizer; 19 20 import java.awt.Color ; 21 import java.awt.geom.Rectangle2D ; 22 import java.io.File ; 23 import java.io.FileFilter ; 24 import java.io.FileNotFoundException ; 25 import java.io.FileOutputStream ; 26 import java.io.IOException ; 27 import java.io.InputStream ; 28 import java.io.OutputStream ; 29 import java.util.HashMap ; 30 import java.util.Map ; 31 import java.util.Vector ; 32 33 import org.apache.batik.transcoder.Transcoder; 34 import org.apache.batik.transcoder.TranscoderInput; 35 import org.apache.batik.transcoder.TranscoderOutput; 36 import org.apache.batik.transcoder.image.ImageTranscoder; 37 import org.apache.batik.transcoder.image.JPEGTranscoder; 38 import org.apache.batik.transcoder.image.PNGTranscoder; 39 40 95 public class SVGConverter { 96 100 public static final String ERROR_NO_SOURCES_SPECIFIED 104 = "SVGConverter.error.no.sources.specified"; 105 106 public static final String ERROR_CANNOT_COMPUTE_DESTINATION 112 = "SVGConverter.error.cannot.compute.destination"; 113 114 public static final String ERROR_CANNOT_USE_DST_FILE 119 = "SVGConverter.error.cannot.use.dst.file"; 120 121 public static final String ERROR_CANNOT_ACCESS_TRANSCODER 126 = "SVGConverter.error.cannot.access.transcoder"; 127 128 public static final String ERROR_SOURCE_SAME_AS_DESTINATION 134 = "SVGConverter.error.source.same.as.destination"; 135 136 public static final String ERROR_CANNOT_READ_SOURCE 140 = "SVGConverter.error.cannot.read.source"; 141 142 public static final String ERROR_CANNOT_OPEN_SOURCE 147 = "SVGConverter.error.cannot.open.source"; 148 149 public static final String ERROR_OUTPUT_NOT_WRITEABLE 155 = "SVGConverter.error.output.not.writeable"; 156 157 public static final String ERROR_CANNOT_OPEN_OUTPUT_FILE 162 = "SVGConverter.error.cannot.open.output.file"; 163 164 public static final String ERROR_UNABLE_TO_CREATE_OUTPUT_DIR 169 = "SVGConverter.error.unable.to.create.output.dir"; 170 171 public static final String ERROR_WHILE_RASTERIZING_FILE 176 = "SVGConverter.error.while.rasterizing.file"; 177 178 182 183 protected static final String SVG_EXTENSION = ".svg"; 184 185 186 protected static final float DEFAULT_QUALITY 187 = -1f; 188 189 190 protected static final float MAXIMUM_QUALITY 191 = .99F; 192 193 194 protected static final DestinationType DEFAULT_RESULT_TYPE 195 = DestinationType.PNG; 196 197 198 protected static final float DEFAULT_WIDTH = -1; 199 200 201 protected static final float DEFAULT_HEIGHT = -1; 202 203 204 protected DestinationType destinationType = DEFAULT_RESULT_TYPE; 205 206 207 protected float height = DEFAULT_HEIGHT; 208 209 210 protected float width = DEFAULT_WIDTH; 211 212 213 protected float maxHeight = DEFAULT_HEIGHT; 214 215 216 protected float maxWidth = DEFAULT_WIDTH; 217 218 219 protected float quality = DEFAULT_QUALITY; 220 221 222 protected int indexed = -1; 223 224 225 protected Rectangle2D area = null; 226 227 228 protected String language = null; 229 230 231 protected String userStylesheet = null; 232 233 234 protected float pixelUnitToMillimeter = -1f; 235 236 237 protected boolean validate = false; 238 239 240 protected boolean executeOnload = false; 241 242 243 protected String allowedScriptTypes = null; 244 245 247 protected boolean constrainScriptOrigin = true; 248 249 250 protected boolean securityOff = false; 251 252 253 protected Vector sources = null; 254 255 259 protected File dst; 260 261 262 protected Color backgroundColor = null; 263 264 265 protected String mediaType = null; 266 267 268 protected String defaultFontFamily = null; 269 270 271 protected String alternateStylesheet = null; 272 273 274 protected Vector files = new Vector (); 275 276 281 protected SVGConverterController controller; 282 283 public SVGConverter(){ 287 this(new DefaultSVGConverterController()); 288 } 289 290 public SVGConverter(SVGConverterController controller){ 294 if (controller == null){ 295 throw new IllegalArgumentException (); 296 } 297 298 this.controller = controller; 299 } 300 301 305 309 public void setDestinationType(DestinationType destinationType) { 310 if(destinationType == null){ 311 throw new IllegalArgumentException (); 312 } 313 this.destinationType = destinationType; 314 } 315 316 public DestinationType getDestinationType(){ 317 return destinationType; 318 } 319 320 325 public void setHeight(float height) { 326 this.height = height; 327 } 328 329 public float getHeight(){ 330 return height; 331 } 332 333 338 public void setWidth(float width) { 339 this.width = width; 340 } 341 342 public float getWidth(){ 343 return width; 344 } 345 346 351 public void setMaxHeight(float height) { 352 this.maxHeight = height; 353 } 354 355 public float getMaxHeight(){ 356 return maxHeight; 357 } 358 359 364 public void setMaxWidth(float width) { 365 this.maxWidth = width; 366 } 367 368 public float getMaxWidth(){ 369 return maxWidth; 370 } 371 372 377 public void setQuality(float quality) throws IllegalArgumentException { 378 if(quality >= 1){ 379 throw new IllegalArgumentException (); 380 } 381 382 this.quality = quality; 383 } 384 385 public float getQuality(){ 386 return quality; 387 } 388 389 393 public void setIndexed(int bits) throws IllegalArgumentException { 394 this.indexed = bits; 395 } 396 397 public int getIndexed(){ 398 return indexed; 399 } 400 401 406 public void setLanguage(String language){ 407 this.language = language; 408 } 409 410 public String getLanguage(){ 411 return language; 412 } 413 414 417 public void setUserStylesheet(String userStylesheet){ 418 this.userStylesheet = userStylesheet; 419 } 420 421 public String getUserStylesheet(){ 422 return userStylesheet; 423 } 424 425 431 public void setPixelUnitToMillimeter(float pixelUnitToMillimeter){ 432 this.pixelUnitToMillimeter = pixelUnitToMillimeter; 433 } 434 435 public float getPixelUnitToMillimeter(){ 436 return pixelUnitToMillimeter; 437 } 438 439 445 public void setArea(Rectangle2D area){ 446 this.area = area; 447 } 448 449 public Rectangle2D getArea(){ 450 return area; 451 } 452 453 460 public void setSources(String [] sources) { 461 if(sources == null){ 462 this.sources = null; 463 } 464 else{ 465 this.sources = new Vector (); 466 for (int i=0; i<sources.length; i++){ 467 if (sources[i] != null){ 468 this.sources.addElement(sources[i]); 469 } 470 } 471 472 if (this.sources.size() == 0){ 473 this.sources = null; 474 } 475 } 476 } 477 478 public Vector getSources(){ 479 return sources; 480 } 481 482 486 public void setDst(File dst) { 487 this.dst = dst; 488 } 489 490 public File getDst(){ 491 return dst; 492 } 493 494 499 public void setBackgroundColor(Color backgroundColor){ 500 this.backgroundColor = backgroundColor; 501 } 502 503 public Color getBackgroundColor(){ 504 return backgroundColor; 505 } 506 507 515 public void setMediaType(String mediaType){ 516 this.mediaType = mediaType; 517 } 518 519 public String getMediaType(){ 520 return mediaType; 521 } 522 523 528 public void setDefaultFontFamily(String defaultFontFamily) { 529 this.defaultFontFamily = defaultFontFamily; 530 } 531 532 public String getDefaultFontFamily() { 533 return defaultFontFamily; 534 } 535 536 542 public void setAlternateStylesheet(String alternateStylesheet){ 543 this.alternateStylesheet = alternateStylesheet; 544 } 545 546 public String getAlternateStylesheet(){ 547 return alternateStylesheet; 548 } 549 550 554 public void setValidate(boolean validate){ 555 this.validate = validate; 556 } 557 558 public boolean getValidate(){ 559 return validate; 560 } 561 562 568 public void setExecuteOnload(boolean b){ 569 this.executeOnload = b; 570 } 571 572 577 public boolean getExecuteOnload(){ 578 return executeOnload; 579 } 580 581 586 public void setAllowedScriptTypes(String allowedScriptTypes){ 587 this.allowedScriptTypes = allowedScriptTypes; 588 } 589 590 595 public String getAllowedScriptTypes(){ 596 return allowedScriptTypes; 597 } 598 599 603 public void setConstrainScriptOrigin(boolean constrainScriptOrigin){ 604 this.constrainScriptOrigin = constrainScriptOrigin; 605 } 606 607 611 public boolean getConstrainScriptOrigin(){ 612 return constrainScriptOrigin; 613 } 614 615 618 public void setSecurityOff(boolean securityOff){ 619 this.securityOff = securityOff; 620 } 621 622 625 public boolean getSecurityOff(){ 626 return securityOff; 627 } 628 629 634 protected boolean isFile(File f){ 635 if (f.exists()){ 636 return f.isFile(); 637 } else { 638 if (f.toString().toLowerCase().endsWith(destinationType.getExtension())){ 639 return true; 640 } 641 } 642 643 return false; 644 } 645 646 650 public void execute() throws SVGConverterException { 651 Vector sources = computeSources(); 655 656 Vector dstFiles = null; 658 if(sources.size() == 1 && dst != null && isFile(dst)){ 659 dstFiles = new Vector (); 660 dstFiles.addElement(dst); 661 } 662 else{ 663 dstFiles = computeDstFiles(sources); 664 } 665 666 Transcoder transcoder = destinationType.getTranscoder(); 668 if(transcoder == null) { 669 throw new SVGConverterException(ERROR_CANNOT_ACCESS_TRANSCODER, 670 new Object []{destinationType.toString()}, 671 true ); 672 } 673 674 Map hints = computeTranscodingHints(); 676 transcoder.setTranscodingHints(hints); 677 678 if(!controller.proceedWithComputedTask(transcoder, 680 hints, 681 sources, 682 dstFiles)){ 683 return; 684 } 685 686 for(int i = 0 ; i < sources.size() ; i++) { 688 SVGConverterSource currentFile 690 = (SVGConverterSource)sources.elementAt(i); 691 File outputFile = (File )dstFiles.elementAt(i); 692 693 createOutputDir(outputFile); 694 transcode(currentFile, outputFile, transcoder); 695 } 696 } 697 698 703 protected Vector computeDstFiles(Vector sources) 704 throws SVGConverterException { 705 Vector dstFiles = new Vector (); 706 if (dst != null) { 707 if (dst.exists() && dst.isFile()) { 708 throw new SVGConverterException(ERROR_CANNOT_USE_DST_FILE); 709 } 710 711 int n = sources.size(); 716 for(int i=0; i<n; i++){ 717 SVGConverterSource src = (SVGConverterSource)sources.elementAt(i); 718 File outputName = new File (dst.getPath(), 720 getDestinationFile(src.getName())); 721 dstFiles.addElement(outputName); 722 723 } 724 } else { 725 int n = sources.size(); 731 for(int i=0; i<n; i++){ 732 SVGConverterSource src = (SVGConverterSource)sources.elementAt(i); 733 if (!(src instanceof SVGConverterFileSource)) { 734 throw new SVGConverterException(ERROR_CANNOT_COMPUTE_DESTINATION, 735 new Object []{src}); 736 } 737 738 SVGConverterFileSource fs = (SVGConverterFileSource)src; 740 File outputName = new File (fs.getFile().getParent(), 741 getDestinationFile(src.getName())); 742 dstFiles.addElement(outputName); 743 } 744 745 } 746 747 return dstFiles; 748 } 749 750 755 protected Vector computeSources() throws SVGConverterException{ 756 Vector sources = new Vector (); 757 758 if (this.sources == null){ 760 throw new SVGConverterException(ERROR_NO_SOURCES_SPECIFIED); 761 } 762 763 int n = this.sources.size(); 764 for (int i=0; i<n; i++){ 765 String sourceString = (String )(this.sources.elementAt(i)); 766 File file = new File (sourceString); 767 if (file.exists()) { 768 sources.addElement(new SVGConverterFileSource(file)); 769 } else { 770 String [] fileNRef = getFileNRef(sourceString); 771 file = new File (fileNRef[0]); 772 if (file.exists()){ 773 sources.addElement(new SVGConverterFileSource(file, fileNRef[1])); 774 } else{ 775 sources.addElement(new SVGConverterURLSource(sourceString)); 776 } 777 } 778 } 779 780 return sources; 781 } 782 783 public String [] getFileNRef(String fileName){ 784 int n = fileName.lastIndexOf("#"); 785 String [] result = {fileName, ""}; 786 if (n > -1){ 787 result[0] = fileName.substring(0, n); 788 if (n+1 < fileName.length()){ 789 result[1] = fileName.substring(n+1); 790 } 791 } 792 793 return result; 794 } 795 796 800 803 protected Map computeTranscodingHints(){ 804 HashMap map = new HashMap (); 805 806 if (area != null) { 808 map.put(ImageTranscoder.KEY_AOI, area); 809 } 810 811 if (quality > 0) { 813 map.put(JPEGTranscoder.KEY_QUALITY, new Float (this.quality)); 814 } 815 816 if (indexed != -1) { 818 map.put(PNGTranscoder.KEY_INDEXED, new Integer (indexed)); 819 } 820 821 if (backgroundColor != null){ 823 map.put(ImageTranscoder.KEY_BACKGROUND_COLOR, backgroundColor); 824 } 825 826 if (height > 0) { 828 map.put(ImageTranscoder.KEY_HEIGHT, new Float (this.height)); 829 } 830 if (width > 0){ 831 map.put(ImageTranscoder.KEY_WIDTH, new Float (this.width)); 832 } 833 834 if (maxHeight > 0) { 836 map.put(ImageTranscoder.KEY_MAX_HEIGHT, new Float (this.maxHeight)); 837 } 838 if (maxWidth > 0){ 839 map.put(ImageTranscoder.KEY_MAX_WIDTH, new Float (this.maxWidth)); 840 } 841 842 if (mediaType != null){ 844 map.put(ImageTranscoder.KEY_MEDIA, mediaType); 845 } 846 847 if (defaultFontFamily != null) { 849 map.put(ImageTranscoder.KEY_DEFAULT_FONT_FAMILY, defaultFontFamily); 850 } 851 852 if (alternateStylesheet != null){ 854 map.put(ImageTranscoder.KEY_ALTERNATE_STYLESHEET, alternateStylesheet); 855 } 856 857 if (userStylesheet != null){ 859 map.put(ImageTranscoder.KEY_USER_STYLESHEET_URI, userStylesheet); 860 } 861 862 if (language != null){ 864 map.put(ImageTranscoder.KEY_LANGUAGE, language); 865 } 866 867 if (pixelUnitToMillimeter > 0){ 869 map.put(ImageTranscoder.KEY_PIXEL_UNIT_TO_MILLIMETER, 870 new Float (pixelUnitToMillimeter)); 871 } 872 873 if (validate){ 875 map.put(ImageTranscoder.KEY_XML_PARSER_VALIDATING, 876 new Boolean (validate)); 877 } 878 879 if (executeOnload) { 881 map.put(ImageTranscoder.KEY_EXECUTE_ONLOAD, new Boolean (executeOnload)); 882 } 883 884 if (allowedScriptTypes != null) { 886 map.put(ImageTranscoder.KEY_ALLOWED_SCRIPT_TYPES, allowedScriptTypes); 887 } 888 889 if (!constrainScriptOrigin) { 891 map.put(ImageTranscoder.KEY_CONSTRAIN_SCRIPT_ORIGIN, 892 new Boolean (constrainScriptOrigin)); 893 } 894 895 return map; 896 } 897 898 906 protected void transcode(SVGConverterSource inputFile, 907 File outputFile, 908 Transcoder transcoder) 909 throws SVGConverterException { 910 TranscoderInput input = null; 911 TranscoderOutput output = null; 912 OutputStream outputStream = null; 913 914 if (!controller.proceedWithSourceTranscoding(inputFile, 915 outputFile)){ 916 return; 917 } 918 919 try { 920 if (inputFile.isSameAs(outputFile.getPath())) { 921 throw new SVGConverterException(ERROR_SOURCE_SAME_AS_DESTINATION, 922 true ); 923 } 924 925 if (!inputFile.isReadable()) { 927 throw new SVGConverterException(ERROR_CANNOT_READ_SOURCE, 928 new Object []{inputFile.getName()}); 929 } 930 931 try { 932 InputStream in = inputFile.openStream(); 933 in.close(); 934 } catch(IOException ioe) { 935 throw new SVGConverterException(ERROR_CANNOT_OPEN_SOURCE, 936 new Object [] {inputFile.getName(), 937 ioe.toString()}); 938 } 939 940 input = new TranscoderInput(inputFile.getURI()); 941 942 if (!isWriteable(outputFile)) { 944 throw new SVGConverterException(ERROR_OUTPUT_NOT_WRITEABLE, 945 new Object [] {outputFile.getName()}); 946 } 947 try { 948 outputStream = new FileOutputStream (outputFile); 949 } catch(FileNotFoundException fnfe) { 950 throw new SVGConverterException(ERROR_CANNOT_OPEN_OUTPUT_FILE, 951 new Object [] {outputFile.getName()}); 952 } 953 954 output = new TranscoderOutput(outputStream); 955 } catch(SVGConverterException e){ 956 boolean proceed = controller.proceedOnSourceTranscodingFailure 957 (inputFile, outputFile, e.getErrorCode()); 958 if (proceed){ 959 return; 960 } else { 961 throw e; 962 } 963 } 964 965 boolean success = false; 967 try { 968 transcoder.transcode(input, output); 969 success = true; 970 } catch(Exception te) { 971 te.printStackTrace(); 972 try { 973 outputStream.flush(); 974 outputStream.close(); 975 } catch(IOException ioe) {} 976 977 boolean proceed = controller.proceedOnSourceTranscodingFailure 980 (inputFile, outputFile, ERROR_WHILE_RASTERIZING_FILE); 981 982 if (!proceed){ 983 throw new SVGConverterException(ERROR_WHILE_RASTERIZING_FILE, 984 new Object [] {outputFile.getName(), 985 te.getMessage()}); 986 } 987 } 988 989 try { 991 outputStream.flush(); 992 outputStream.close(); 993 } catch(IOException ioe) { 994 return; 995 } 996 997 if (success){ 998 controller.onSourceTranscodingSuccess(inputFile, outputFile); 999 } 1000 } 1001 1002 1014 protected String getDestinationFile(String file) { 1015 int suffixStart; String oldName; String newSuffix = destinationType.getExtension(); 1019 1021 oldName = file; 1022 suffixStart = oldName.lastIndexOf("."); 1024 String dest = null; 1025 if (suffixStart != -1) { 1026 dest = new String (oldName.substring(0, suffixStart) + newSuffix); 1028 } else { 1029 dest = new String (oldName + newSuffix); 1031 } 1032 1033 return dest; 1034 } 1035 1036 1043 protected void createOutputDir(File output) 1044 throws SVGConverterException { 1045 1046 File outputDir; boolean success = true; 1051 1052 String parentDir = output.getParent(); 1054 if (parentDir != null){ 1055 outputDir = new File (output.getParent()); 1056 if (outputDir.exists() == false) { 1057 success = outputDir.mkdirs(); 1059 } else { 1060 if (outputDir.isDirectory() == false) { 1061 success = outputDir.mkdirs(); 1064 } 1065 } 1066 } 1067 1068 if (!success) { 1069 throw new SVGConverterException(ERROR_UNABLE_TO_CREATE_OUTPUT_DIR); 1070 } 1071 } 1072 1073 1080 protected boolean isWriteable(File file) { 1081 if (file.exists()) { 1082 if (!file.canWrite()) { 1084 return false; 1085 } 1086 } else { 1087 try { 1091 file.createNewFile(); 1092 } catch(IOException ioe) { 1093 return false; 1094 } 1095 } 1096 return true; 1097 } 1098 1099 1103 1106 public static class SVGFileFilter implements FileFilter { 1107 public static final String SVG_EXTENSION = ".svg"; 1108 1109 public boolean accept(File file){ 1110 if (file != null && file.getName().toLowerCase().endsWith(SVG_EXTENSION)){ 1111 return true; 1112 } 1113 1114 return false; 1115 } 1116 } 1117 1118} 1119 1120 | Popular Tags |