diff options
author | Simon Pepping <spepping@apache.org> | 2006-10-09 10:34:32 +0000 |
---|---|---|
committer | Simon Pepping <spepping@apache.org> | 2006-10-09 10:34:32 +0000 |
commit | 167ab74d942b4e20c2712921a711b3d5a8f3aeb4 (patch) | |
tree | 545993d10877ba116e2d092f9bcfa1192a840317 /src/java/org/apache/fop | |
parent | ee1706ac5f0a773b31c12f0291fd7e30f22a7840 (diff) | |
download | xmlgraphics-fop-167ab74d942b4e20c2712921a711b3d5a8f3aeb4.tar.gz xmlgraphics-fop-167ab74d942b4e20c2712921a711b3d5a8f3aeb4.zip |
Prevent a StringIndexOutOfBoundsException when the PNG output file
name does not have an extension. Patch submitted by Matt Healy <mjh at
cs dot rit dot edu>, see bug 40655.
git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@454331 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/java/org/apache/fop')
-rw-r--r-- | src/java/org/apache/fop/render/bitmap/PNGRenderer.java | 30 |
1 files changed, 25 insertions, 5 deletions
diff --git a/src/java/org/apache/fop/render/bitmap/PNGRenderer.java b/src/java/org/apache/fop/render/bitmap/PNGRenderer.java index 42d7c2840..82eb65023 100644 --- a/src/java/org/apache/fop/render/bitmap/PNGRenderer.java +++ b/src/java/org/apache/fop/render/bitmap/PNGRenderer.java @@ -48,6 +48,9 @@ public class PNGRenderer extends Java2DRenderer { /** The MIME type for png-Rendering */ public static final String MIME_TYPE = MimeConstants.MIME_PNG; + /** The file extension expected for PNG files */ + private static final String PNG_FILE_EXTENSION = "png"; + /** The file syntax prefix, eg. "page" will output "page1.png" etc */ private String filePrefix; @@ -71,10 +74,15 @@ public class PNGRenderer extends Java2DRenderer { /** * Sets the output directory, either from the outfile specified on the - * command line, or from the directory specified in configuration file. Also - * sets the file name syntax, eg. "page" + * command line, or from the directory specified in configuration file. + * Also sets the file name syntax, eg. "page". + * The file name must not have an extension, or must have extension "png", + * and its last period must not be at the start (empty file prefix). + * + * @throws IOException if an invalid output file name was specified + * (e.g., with an extension other than '.png') */ - private void setOutputDirectory() { + private void setOutputDirectory() throws IOException { // the file provided on the command line File f = getUserAgent().getOutputFile(); @@ -88,12 +96,24 @@ public class PNGRenderer extends Java2DRenderer { // extracting file name syntax String s = f.getName(); int i = s.lastIndexOf("."); + if (i > 0) { + // Make sure that the file extension was "png" + String extension = s.substring(i + 1).toLowerCase(); + if (!PNG_FILE_EXTENSION.equals(extension)) { + throw new IOException("Invalid file extension ('" + + extension + "') specified"); + } + } else if (i == -1) { + i = s.length(); + } else { // i == 0 + throw new IOException("Invalid file name ('" + + s + "') specified"); + } if (s.charAt(i - 1) == '1') { i--; // getting rid of the "1" } filePrefix = s.substring(0, i); } - } /** @see org.apache.fop.render.Renderer#stopRenderer() */ @@ -147,7 +167,7 @@ public class PNGRenderer extends Java2DRenderer { return null; } else { File f = new File(outputDir, - filePrefix + (pageNumber + 1) + ".png"); + filePrefix + (pageNumber + 1) + "." + PNG_FILE_EXTENSION); try { OutputStream os = new BufferedOutputStream(new FileOutputStream(f)); return os; |