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 3.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. /* $Id$ */
  18. package org.apache.fop.render.bitmap;
  19. import java.io.BufferedOutputStream;
  20. import java.io.File;
  21. import java.io.FileOutputStream;
  22. import java.io.IOException;
  23. import java.io.OutputStream;
  24. /**
  25. * This utility class helps renderers who generate one file per page,
  26. * like the PNG renderer.
  27. */
  28. public class MultiFileRenderingUtil {
  29. /** The file syntax prefix, eg. "page" will output "page1.png" etc */
  30. private String filePrefix;
  31. private String fileExtension;
  32. /** The output directory where images are to be written */
  33. private File outputDir;
  34. /**
  35. * Creates a new instance.
  36. * <p>
  37. * The file name must not have an extension, or must have extension "png",
  38. * and its last period must not be at the start (empty file prefix).
  39. * @param ext the extension to be used
  40. * @param outputFile the output file or null if there's no such information
  41. */
  42. public MultiFileRenderingUtil(String ext, File outputFile) {
  43. this.fileExtension = ext;
  44. // the file provided on the command line
  45. if (outputFile == null) {
  46. //No filename information available. Only the first page will be rendered.
  47. outputDir = null;
  48. filePrefix = null;
  49. } else {
  50. outputDir = outputFile.getParentFile();
  51. // extracting file name syntax
  52. String s = outputFile.getName();
  53. int i = s.lastIndexOf(".");
  54. if (i > 0) {
  55. // Make sure that the file extension was "png"
  56. String extension = s.substring(i + 1).toLowerCase();
  57. if (!ext.equals(extension)) {
  58. throw new IllegalArgumentException("Invalid file extension ('"
  59. + extension + "') specified");
  60. }
  61. } else if (i == -1) {
  62. i = s.length();
  63. } else { // i == 0
  64. throw new IllegalArgumentException("Invalid file name ('"
  65. + s + "') specified");
  66. }
  67. if (s.charAt(i - 1) == '1') {
  68. i--; // getting rid of the "1"
  69. }
  70. filePrefix = s.substring(0, i);
  71. }
  72. }
  73. /**
  74. * Creates a new {@link OutputStream} for the given page number.
  75. * @param pageNumber the page number (zero-based)
  76. * @return the output stream for the page
  77. * @throws IOException if there's an I/O error while setting up the output stream
  78. */
  79. public OutputStream createOutputStream(int pageNumber) throws IOException {
  80. if (filePrefix == null) {
  81. return null;
  82. } else {
  83. File f = new File(outputDir,
  84. filePrefix + (pageNumber + 1) + "." + fileExtension);
  85. OutputStream os = new BufferedOutputStream(new FileOutputStream(f));
  86. return os;
  87. }
  88. }
  89. }