You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MultiFileRenderingUtil.java 2.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. package org.apache.fop.render.bitmap;
  2. import java.io.BufferedOutputStream;
  3. import java.io.File;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;
  6. import java.io.OutputStream;
  7. /**
  8. * This utility class helps renderers who generate one file per page,
  9. * like the PNG renderer.
  10. */
  11. public class MultiFileRenderingUtil {
  12. /** The file syntax prefix, eg. "page" will output "page1.png" etc */
  13. private String filePrefix;
  14. private String fileExtension;
  15. /** The output directory where images are to be written */
  16. private File outputDir;
  17. /**
  18. * Creates a new instance.
  19. * <p>
  20. * The file name must not have an extension, or must have extension "png",
  21. * and its last period must not be at the start (empty file prefix).
  22. * @param ext the extension to be used
  23. * @param outputFile the output file or null if there's no such information
  24. */
  25. public MultiFileRenderingUtil(String ext, File outputFile) {
  26. this.fileExtension = ext;
  27. // the file provided on the command line
  28. if (outputFile == null) {
  29. //No filename information available. Only the first page will be rendered.
  30. outputDir = null;
  31. filePrefix = null;
  32. } else {
  33. outputDir = outputFile.getParentFile();
  34. // extracting file name syntax
  35. String s = outputFile.getName();
  36. int i = s.lastIndexOf(".");
  37. if (i > 0) {
  38. // Make sure that the file extension was "png"
  39. String extension = s.substring(i + 1).toLowerCase();
  40. if (!ext.equals(extension)) {
  41. throw new IllegalArgumentException("Invalid file extension ('"
  42. + extension + "') specified");
  43. }
  44. } else if (i == -1) {
  45. i = s.length();
  46. } else { // i == 0
  47. throw new IllegalArgumentException("Invalid file name ('"
  48. + s + "') specified");
  49. }
  50. if (s.charAt(i - 1) == '1') {
  51. i--; // getting rid of the "1"
  52. }
  53. filePrefix = s.substring(0, i);
  54. }
  55. }
  56. public OutputStream createOutputStream(int pageNumber) throws IOException {
  57. if (filePrefix == null) {
  58. return null;
  59. } else {
  60. File f = new File(outputDir,
  61. filePrefix + (pageNumber + 1) + "." + fileExtension);
  62. OutputStream os = new BufferedOutputStream(new FileOutputStream(f));
  63. return os;
  64. }
  65. }
  66. }