From: Jeremias Maerki Date: Tue, 10 Sep 2002 11:21:32 +0000 (+0000) Subject: Fixed checkstyle violations (we're down at 8500 now) X-Git-Tag: Alt-Design-integration-base~405 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=1e79030d3150aec920052c94b18cc46816110cfa;p=xmlgraphics-fop.git Fixed checkstyle violations (we're down at 8500 now) git-svn-id: https://svn.apache.org/repos/asf/xmlgraphics/fop/trunk@195164 13f79535-47bb-0310-9956-ffa450edef68 --- diff --git a/src/org/apache/fop/image/analyser/BMPReader.java b/src/org/apache/fop/image/analyser/BMPReader.java index 86fd8b77f..b15fd731c 100644 --- a/src/org/apache/fop/image/analyser/BMPReader.java +++ b/src/org/apache/fop/image/analyser/BMPReader.java @@ -1,32 +1,36 @@ /* * $Id$ - * Copyright (C) 2001 The Apache Software Foundation. All rights reserved. + * Copyright (C) 2001-2002 The Apache Software Foundation. All rights reserved. * For details on use and redistribution please refer to the * LICENSE file included with these sources. */ - package org.apache.fop.image.analyser; // Java import java.io.BufferedInputStream; import java.io.IOException; +// FOP import org.apache.fop.image.FopImage; import org.apache.fop.fo.FOUserAgent; /** * ImageReader object for BMP image type. - * @author Pankaj Narula - * @version 1.0 + * + * @author Pankaj Narula + * @version $Id$ */ public class BMPReader implements ImageReader { - static protected final int BMP_SIG_LENGTH = 26; - public FopImage.ImageInfo verifySignature(String uri, BufferedInputStream fis, - FOUserAgent ua) throws IOException { - byte[] header = getDefaultHeader(fis); - boolean supported = ((header[0] == (byte) 0x42) && - (header[1] == (byte) 0x4d)); + /** Length of the BMP header */ + protected static final int BMP_SIG_LENGTH = 26; + + /** @see org.apache.fop.image.analyser.ImageReader */ + public FopImage.ImageInfo verifySignature(String uri, BufferedInputStream bis, + FOUserAgent ua) throws IOException { + byte[] header = getDefaultHeader(bis); + boolean supported = ((header[0] == (byte) 0x42) + && (header[1] == (byte) 0x4d)); if (supported) { return getDimension(header); } else { @@ -34,11 +38,16 @@ public class BMPReader implements ImageReader { } } + /** + * Returns the MIME type supported by this implementation. + * + * @return The MIME type + */ public String getMimeType() { return "image/bmp"; } - protected FopImage.ImageInfo getDimension(byte[] header) { + private FopImage.ImageInfo getDimension(byte[] header) { FopImage.ImageInfo info = new FopImage.ImageInfo(); info.mimeType = getMimeType(); @@ -47,20 +56,21 @@ public class BMPReader implements ImageReader { int byte2 = header[19] & 0xff; int byte3 = header[20] & 0xff; int byte4 = header[21] & 0xff; - long l = (long)((byte4 << 24) | (byte3 << 16) | - (byte2 << 8) | byte1); - info.width = (int)(l & 0xffffffff); + long l = (long) ((byte4 << 24) | (byte3 << 16) + | (byte2 << 8) | byte1); + info.width = (int) (l & 0xffffffff); byte1 = header[22] & 0xff; byte2 = header[23] & 0xff; byte3 = header[24] & 0xff; byte4 = header[25] & 0xff; - l = (long)((byte4 << 24) | (byte3 << 16) | (byte2 << 8) | byte1); - info.height = (int)(l & 0xffffffff); + l = (long) ((byte4 << 24) | (byte3 << 16) | (byte2 << 8) | byte1); + info.height = (int) (l & 0xffffffff); return info; } - protected byte[] getDefaultHeader(BufferedInputStream imageStream) throws IOException { + private byte[] getDefaultHeader(BufferedInputStream imageStream) + throws IOException { byte[] header = new byte[BMP_SIG_LENGTH]; try { imageStream.mark(BMP_SIG_LENGTH + 1); @@ -69,11 +79,12 @@ public class BMPReader implements ImageReader { } catch (IOException ex) { try { imageStream.reset(); - } catch (IOException exbis) {} + } catch (IOException exbis) { + // throw the original exception, not this one + } throw ex; } return header; } -} - +} \ No newline at end of file diff --git a/src/org/apache/fop/image/analyser/EPSReader.java b/src/org/apache/fop/image/analyser/EPSReader.java index 0e7948fb2..4a0621a76 100644 --- a/src/org/apache/fop/image/analyser/EPSReader.java +++ b/src/org/apache/fop/image/analyser/EPSReader.java @@ -1,47 +1,41 @@ /* * $Id$ - * Copyright (C) 2001 The Apache Software Foundation. All rights reserved. + * Copyright (C) 2001-2002 The Apache Software Foundation. All rights reserved. * For details on use and redistribution please refer to the * LICENSE file included with these sources. */ - package org.apache.fop.image.analyser; // Java import java.io.BufferedInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; -import java.io.InputStream; -import java.net.URL; - -import org.xml.sax.InputSource; -import org.xml.sax.XMLReader; +// FOP import org.apache.fop.image.FopImage; import org.apache.fop.image.EPSImage; import org.apache.fop.fo.FOUserAgent; /** * ImageReader object for EPS document image type. + * + * @version $Id$ */ public class EPSReader implements ImageReader { - private long getLong(byte[] buf, int idx) { - int b1 = buf[idx] & 0xff; - int b2 = buf[idx + 1] & 0xff; - int b3 = buf[idx + 2] & 0xff; - int b4 = buf[idx + 3] & 0xff; + private static final byte[] EPS_HEADER_ASCII = "%!PS".getBytes(); + private static final byte[] BOUNDINGBOX = "%%BoundingBox: ".getBytes(); - return (long)((b4 << 24) | (b3 << 16) | (b2 << 8) | b1); - } + /** @see org.apache.fop.image.analyser.ImageReader */ + public FopImage.ImageInfo verifySignature(String uri, BufferedInputStream bis, + FOUserAgent ua) throws IOException { - public FopImage.ImageInfo verifySignature(String uri, BufferedInputStream fis, - FOUserAgent ua) throws IOException { boolean isEPS = false; - fis.mark(32); + + bis.mark(32); byte[] header = new byte[30]; - fis.read(header, 0, 30); - fis.reset(); + bis.read(header, 0, 30); + bis.reset(); EPSImage.EPSData data = new EPSImage.EPSData(); @@ -60,8 +54,10 @@ public class EPSReader implements ImageReader { } else { // Check if plain ascii byte[] epsh = "%!PS".getBytes(); - if (epsh[0] == header[0] && epsh[1] == header[1] && - epsh[2] == header[2] && epsh[3] == header[3]) { + if (EPS_HEADER_ASCII[0] == header[0] + && EPS_HEADER_ASCII[1] == header[1] + && EPS_HEADER_ASCII[2] == header[2] + && EPS_HEADER_ASCII[3] == header[3]) { data.isAscii = true; isEPS = true; } @@ -71,12 +67,12 @@ public class EPSReader implements ImageReader { FopImage.ImageInfo info = new FopImage.ImageInfo(); info.mimeType = getMimeType(); info.data = data; - readEPSImage(fis, data); + readEPSImage(bis, data); data.bbox = readBBox(data); if (data.bbox != null) { - info.width = (int)(data.bbox[2] - data.bbox[0]); - info.height = (int)(data.bbox[3] - data.bbox[1]); + info.width = (int) (data.bbox[2] - data.bbox[0]); + info.height = (int) (data.bbox[3] - data.bbox[1]); return info; } else { // Ain't eps if no BoundingBox @@ -87,23 +83,47 @@ public class EPSReader implements ImageReader { return null; } - /** read the eps file and extract eps part */ - private void readEPSImage(BufferedInputStream fis, EPSImage.EPSData data) throws IOException { + /** + * Returns the MIME type supported by this implementation. + * + * @return The MIME type + */ + public String getMimeType() { + return "image/eps"; + } + + private long getLong(byte[] buf, int idx) { + int b1 = buf[idx] & 0xff; + int b2 = buf[idx + 1] & 0xff; + int b3 = buf[idx + 2] & 0xff; + int b4 = buf[idx + 3] & 0xff; + + return (long) ((b4 << 24) | (b3 << 16) | (b2 << 8) | b1); + } + + /** + * Read the eps file and extract eps part. + * + * @param bis The InputStream + * @param data EPSData object to write the results to + * @exception IOException If an I/O error occurs + */ + private void readEPSImage(BufferedInputStream bis, EPSImage.EPSData data) + throws IOException { ByteArrayOutputStream baos = new ByteArrayOutputStream(); byte[] file; byte[] readBuf = new byte[20480]; - int bytes_read; + int bytesRead; int index = 0; boolean cont = true; - try { - while ((bytes_read = fis.read(readBuf)) != -1) { - baos.write(readBuf, 0, bytes_read); + while ((bytesRead = bis.read(readBuf)) != -1) { + baos.write(readBuf, 0, bytesRead); } } catch (java.io.IOException ex) { - throw new IOException("Error while loading EPS image " + - ex.getMessage()); + throw new IOException("Error while loading EPS image: " + + ex.getMessage()); } file = baos.toByteArray(); @@ -117,36 +137,45 @@ public class EPSReader implements ImageReader { data.epsFile = new byte[(int) data.psLength]; System.arraycopy(file, 0, data.rawEps, 0, data.rawEps.length); System.arraycopy(data.rawEps, (int) data.psStart, data.epsFile, 0, - (int) data.psLength); + (int) data.psLength); } } - /* Get embedded preview or null */ + /** + * Get embedded TIFF preview or null. + * + * @param data The EPS payload + * @return The embedded preview + */ public byte[] getPreview(EPSImage.EPSData data) { if (data.preview == null) { if (data.tiffLength > 0) { data.preview = new byte[(int) data.tiffLength]; System.arraycopy(data.rawEps, (int) data.tiffStart, data.preview, 0, - (int) data.tiffLength); + (int) data.tiffLength); } } return data.preview; } - /** Extract bounding box from eps part + /** + * Extract bounding box from eps part. + * + * @param data The EPS payload + * @return An Array of four coordinates making up the bounding box */ private long[] readBBox(EPSImage.EPSData data) { long[] mbbox = null; int idx = 0; - byte[] bbxName = "%%BoundingBox: ".getBytes(); boolean found = false; - while (!found && (data.epsFile.length > (idx + bbxName.length))) { + while (!found && (data.epsFile.length > (idx + BOUNDINGBOX.length))) { boolean sfound = true; int i = idx; - for (i = idx; sfound && (i - idx) < bbxName.length; i++) { - if (bbxName[i - idx] != data.epsFile[i]) + for (i = idx; sfound && (i - idx) < BOUNDINGBOX.length; i++) { + if (BOUNDINGBOX[i - idx] != data.epsFile[i]) { sfound = false; + } } if (sfound) { found = true; @@ -156,9 +185,9 @@ public class EPSReader implements ImageReader { } } - if (!found) + if (!found) { return mbbox; - + } mbbox = new long[4]; idx += readLongString(data, mbbox, 0, idx); @@ -171,15 +200,16 @@ public class EPSReader implements ImageReader { private int readLongString(EPSImage.EPSData data, long[] mbbox, int i, int idx) { while (idx < data.epsFile.length && (data.epsFile[idx] == 32)) { - idx++; + idx++; } int nidx = idx; // check also for ANSI46(".") to identify floating point values - while (nidx < data.epsFile.length && - ((data.epsFile[nidx] >= 48 && data.epsFile[nidx] <= 57) || - (data.epsFile[nidx] == 45) || (data.epsFile[nidx] == 46) )) { + while (nidx < data.epsFile.length + && ((data.epsFile[nidx] >= 48 && data.epsFile[nidx] <= 57) + || (data.epsFile[nidx] == 45) + || (data.epsFile[nidx] == 46))) { nidx++; } @@ -188,17 +218,14 @@ public class EPSReader implements ImageReader { String ns = new String(num); //if( ns.indexOf(".") != -1 ) { - // do something like logging a warning + // do something like logging a warning //} // then parse the double and round off to the next math. Integer - mbbox[i] = (long) Math.ceil( Double.parseDouble( ns ) ); + mbbox[i] = (long) Math.ceil(Double.parseDouble(ns)); return (1 + nidx - idx); } - public String getMimeType() { - return "image/eps"; - } } diff --git a/src/org/apache/fop/image/analyser/GIFReader.java b/src/org/apache/fop/image/analyser/GIFReader.java index 00ca6e532..4b4a0cf57 100644 --- a/src/org/apache/fop/image/analyser/GIFReader.java +++ b/src/org/apache/fop/image/analyser/GIFReader.java @@ -1,47 +1,58 @@ /* * $Id$ - * Copyright (C) 2001 The Apache Software Foundation. All rights reserved. + * Copyright (C) 2001-2002 The Apache Software Foundation. All rights reserved. * For details on use and redistribution please refer to the * LICENSE file included with these sources. */ - package org.apache.fop.image.analyser; // Java import java.io.BufferedInputStream; import java.io.IOException; +// FOP import org.apache.fop.image.FopImage; import org.apache.fop.fo.FOUserAgent; /** * ImageReader object for GIF image type. - * @author Pankaj Narula - * @version 1.0 + * + * @author Pankaj Narula + * @version $Id$ */ public class GIFReader implements ImageReader { - static protected final int GIF_SIG_LENGTH = 10; - public FopImage.ImageInfo verifySignature(String uri, BufferedInputStream fis, - FOUserAgent ua) throws IOException { - byte[] header = getDefaultHeader(fis); - boolean supported = ((header[0] == 'G') && (header[1] == 'I') && - (header[2] == 'F') && (header[3] == '8') && - (header[4] == '7' || header[4] == '9') && - (header[5] == 'a')); + private static final int GIF_SIG_LENGTH = 10; + + /** @see org.apache.fop.image.analyser.ImageReader */ + public FopImage.ImageInfo verifySignature(String uri, BufferedInputStream bis, + FOUserAgent ua) throws IOException { + byte[] header = getDefaultHeader(bis); + boolean supported = ((header[0] == 'G') + && (header[1] == 'I') + && (header[2] == 'F') + && (header[3] == '8') + && (header[4] == '7' || header[4] == '9') + && (header[5] == 'a')); if (supported) { FopImage.ImageInfo info = getDimension(header); info.mimeType = getMimeType(); return info; - } else + } else { return null; + } } + /** + * Returns the MIME type supported by this implementation. + * + * @return The MIME type + */ public String getMimeType() { return "image/gif"; } - protected FopImage.ImageInfo getDimension(byte[] header) { + private FopImage.ImageInfo getDimension(byte[] header) { FopImage.ImageInfo info = new FopImage.ImageInfo(); // little endian notation int byte1 = header[6] & 0xff; @@ -54,7 +65,8 @@ public class GIFReader implements ImageReader { return info; } - protected byte[] getDefaultHeader(BufferedInputStream imageStream) throws IOException { + private byte[] getDefaultHeader(BufferedInputStream imageStream) + throws IOException { byte[] header = new byte[GIF_SIG_LENGTH]; try { imageStream.mark(GIF_SIG_LENGTH + 1); @@ -63,11 +75,12 @@ public class GIFReader implements ImageReader { } catch (IOException ex) { try { imageStream.reset(); - } catch (IOException exbis) {} + } catch (IOException exbis) { + // throw the original exception, not this one + } throw ex; } return header; } -} - +} \ No newline at end of file diff --git a/src/org/apache/fop/image/analyser/ImageReader.java b/src/org/apache/fop/image/analyser/ImageReader.java index c1a5ac285..8137cc239 100644 --- a/src/org/apache/fop/image/analyser/ImageReader.java +++ b/src/org/apache/fop/image/analyser/ImageReader.java @@ -1,40 +1,43 @@ /* * $Id$ - * Copyright (C) 2001 The Apache Software Foundation. All rights reserved. + * Copyright (C) 2001-2002 The Apache Software Foundation. All rights reserved. * For details on use and redistribution please refer to the * LICENSE file included with these sources. */ - package org.apache.fop.image.analyser; // Java import java.io.BufferedInputStream; import java.io.IOException; +// FOP import org.apache.fop.image.FopImage; import org.apache.fop.fo.FOUserAgent; /** * ImageReader objects read image headers to determine the image size. - * @author Pankaj Narula - * @version 1.0 + * + * @author Pankaj Narula + * @version $Id$ */ public interface ImageReader { /** - * Verify image type. - * If the stream does not contain image data expected by - * the reader it must reset the stream to the start. This - * is so that the next reader can start reading from the start. - * The reader must not close the stream unless it can handle - * the image and it has read the information. + * Verify image type. If the stream does not contain image data expected by + * the reader it must reset the stream to the start. This is so that the + * next reader can start reading from the start. The reader must not close + * the stream unless it can handle the image and it has read the + * information. * - * @param bis Image buffered input stream - * @return true if image type is the handled one - * @exception IOException io error + * @param bis Image buffered input stream + * @param uri URI to the image + * @param ua The user agent + * @return true if image type is the handled one + * @exception IOException if an I/O error occurs */ - public FopImage.ImageInfo verifySignature(String uri, BufferedInputStream bis, - FOUserAgent ua) throws IOException; + FopImage.ImageInfo verifySignature(String uri, BufferedInputStream bis, + FOUserAgent ua) + throws IOException; } diff --git a/src/org/apache/fop/image/analyser/ImageReaderFactory.java b/src/org/apache/fop/image/analyser/ImageReaderFactory.java index 66d6aeb35..124182a0b 100644 --- a/src/org/apache/fop/image/analyser/ImageReaderFactory.java +++ b/src/org/apache/fop/image/analyser/ImageReaderFactory.java @@ -1,10 +1,9 @@ /* * $Id$ - * Copyright (C) 2001 The Apache Software Foundation. All rights reserved. + * Copyright (C) 2001-2002 The Apache Software Foundation. All rights reserved. * For details on use and redistribution please refer to the * LICENSE file included with these sources. */ - package org.apache.fop.image.analyser; // Java @@ -13,39 +12,52 @@ import java.io.BufferedInputStream; import java.io.IOException; import java.util.ArrayList; +// FOP import org.apache.fop.image.FopImage; import org.apache.fop.fo.FOUserAgent; /** * Factory for ImageReader objects. - * @author Pankaj Narula - * @version 1.0 + * + * @author Pankaj Narula + * @version $Id$ */ public class ImageReaderFactory { - static protected ArrayList formats = new ArrayList(); + + private static ArrayList formats = new ArrayList(); + static { - formats.add(new JPEGReader()); - formats.add(new BMPReader()); - formats.add(new GIFReader()); - formats.add(new PNGReader()); - formats.add(new TIFFReader()); - formats.add(new EPSReader()); + registerFormat(new JPEGReader()); + registerFormat(new BMPReader()); + registerFormat(new GIFReader()); + registerFormat(new PNGReader()); + registerFormat(new TIFFReader()); + registerFormat(new EPSReader()); // the xml parser through batik closes the stream when finished // so there is a workaround in the SVGReader - formats.add(new SVGReader()); - formats.add(new XMLReader()); - }; + registerFormat(new SVGReader()); + registerFormat(new XMLReader()); + } - // TODO - a way to add other readers + /** + * Registers a new ImageReader. + * + * @param reader An ImageReader instance + */ + public static void registerFormat(ImageReader reader) { + formats.add(reader); + } /** * ImageReader maker. - * @param in image input stream - * @return ImageReader object - * image type is not supported + * + * @param uri URI to the image + * @param in image input stream + * @param ua user agent + * @return An ImageInfo object describing the image */ - static public FopImage.ImageInfo make(String uri, InputStream in, - FOUserAgent ua) { + public static FopImage.ImageInfo make(String uri, InputStream in, + FOUserAgent ua) { ImageReader reader; BufferedInputStream bis = new BufferedInputStream(in); @@ -53,14 +65,14 @@ public class ImageReaderFactory { for (int count = 0; count < formats.size(); count++) { reader = (ImageReader) formats.get(count); FopImage.ImageInfo info = reader.verifySignature(uri, bis, ua); - if(info != null) { + if (info != null) { return info; } } } catch (IOException ex) { ua.getLogger().error( - "Error while recovering Image Informations (" + - uri + ") : " + ex.getMessage()); + "Error while recovering Image Informations (" + + uri + ")", ex); } return null; } diff --git a/src/org/apache/fop/image/analyser/JPEGReader.java b/src/org/apache/fop/image/analyser/JPEGReader.java index eda900f1d..bddbc2040 100644 --- a/src/org/apache/fop/image/analyser/JPEGReader.java +++ b/src/org/apache/fop/image/analyser/JPEGReader.java @@ -1,23 +1,24 @@ /* * $Id$ - * Copyright (C) 2001 The Apache Software Foundation. All rights reserved. + * Copyright (C) 2001-2002 The Apache Software Foundation. All rights reserved. * For details on use and redistribution please refer to the * LICENSE file included with these sources. */ - package org.apache.fop.image.analyser; // Java import java.io.BufferedInputStream; import java.io.IOException; +// FOP import org.apache.fop.image.FopImage; import org.apache.fop.fo.FOUserAgent; /** * ImageReader object for JPEG image type. + * * @author Pankaj Narula - * @version 1.0 + * @version $Id$ */ public class JPEGReader implements ImageReader { @@ -28,23 +29,24 @@ public class JPEGReader implements ImageReader { * inside the APPn marker. And we don't want to confuse those dimensions with * the image dimensions. */ - static protected final int MARK = 0xff; // Beginneing of a Marker - static protected final int NULL = 0x00; // Special case for 0xff00 - static protected final int SOF1 = 0xc0; // Baseline DCT - static protected final int SOF2 = 0xc1; // Extended Sequential DCT - static protected final int SOF3 = 0xc2; // Progrssive DCT only PDF 1.3 - static protected final int SOFA = 0xca; // Progressice DCT only PDF 1.3 - static protected final int APP0 = 0xe0; // Application marker, JFIF - static protected final int APPF = 0xef; // Application marker - static protected final int SOS = 0xda; // Start of Scan - static protected final int SOI = 0xd8; // start of Image - static protected final int JPG_SIG_LENGTH = 2; + private static final int MARK = 0xff; // Beginning of a Marker + private static final int NULL = 0x00; // Special case for 0xff00 + private static final int SOF1 = 0xc0; // Baseline DCT + private static final int SOF2 = 0xc1; // Extended Sequential DCT + private static final int SOF3 = 0xc2; // Progrssive DCT only PDF 1.3 + private static final int SOFA = 0xca; // Progressice DCT only PDF 1.3 + private static final int APP0 = 0xe0; // Application marker, JFIF + private static final int APPF = 0xef; // Application marker + private static final int SOS = 0xda; // Start of Scan + private static final int SOI = 0xd8; // start of Image + private static final int JPG_SIG_LENGTH = 2; + /** @see org.apache.fop.image.analyser.ImageReader */ public FopImage.ImageInfo verifySignature(String uri, BufferedInputStream fis, FOUserAgent ua) throws IOException { byte[] header = getDefaultHeader(fis); - boolean supported = ((header[0] == (byte) 0xff) && - (header[1] == (byte) 0xd8)); + boolean supported = ((header[0] == (byte) 0xff) + && (header[1] == (byte) 0xd8)); if (supported) { FopImage.ImageInfo info = getDimension(fis); info.mimeType = getMimeType(); @@ -54,11 +56,16 @@ public class JPEGReader implements ImageReader { } } + /** + * Returns the MIME type supported by this implementation. + * + * @return The MIME type + */ public String getMimeType() { return "image/jpeg"; } - protected byte[] getDefaultHeader(BufferedInputStream imageStream) throws IOException { + private byte[] getDefaultHeader(BufferedInputStream imageStream) throws IOException { byte[] header = new byte[JPG_SIG_LENGTH]; try { imageStream.mark(JPG_SIG_LENGTH + 1); @@ -67,13 +74,15 @@ public class JPEGReader implements ImageReader { } catch (IOException ex) { try { imageStream.reset(); - } catch (IOException exbis) {} + } catch (IOException exbis) { + // throw the original exception, not this one + } throw ex; } return header; } - protected FopImage.ImageInfo getDimension(BufferedInputStream imageStream) throws IOException { + private FopImage.ImageInfo getDimension(BufferedInputStream imageStream) throws IOException { FopImage.ImageInfo info = new FopImage.ImageInfo(); try { int marker = NULL; @@ -81,12 +90,13 @@ public class JPEGReader implements ImageReader { outer: while (imageStream.available() > 0) { while ((marker = imageStream.read()) != MARK) { - ; + //nop, simply skip } + do { marker = imageStream.read(); - } while (marker == MARK) - ; + } while (marker == MARK); + switch (marker) { case SOI: break; @@ -103,26 +113,29 @@ public class JPEGReader implements ImageReader { default: length = this.read2bytes(imageStream); skipped = this.skip(imageStream, length - 2); - if (skipped != length - 2) + if (skipped != length - 2) { throw new IOException("Skipping Error"); + } } } } catch (IOException ioe) { try { imageStream.reset(); - } catch (IOException exbis) {} + } catch (IOException exbis) { + // throw the original exception, not this one + } throw ioe; } return info; } - protected int read2bytes(BufferedInputStream imageStream) throws IOException { + private int read2bytes(BufferedInputStream imageStream) throws IOException { int byte1 = imageStream.read(); int byte2 = imageStream.read(); - return (int)((byte1 << 8) | byte2); + return (int) ((byte1 << 8) | byte2); } - protected long skip(BufferedInputStream imageStream, long n) throws IOException { + private long skip(BufferedInputStream imageStream, long n) throws IOException { long discarded = 0; while (discarded != n) { imageStream.read(); @@ -131,5 +144,4 @@ public class JPEGReader implements ImageReader { return discarded; // scope for exception } -} - +} \ No newline at end of file diff --git a/src/org/apache/fop/image/analyser/PNGReader.java b/src/org/apache/fop/image/analyser/PNGReader.java index b41f19e37..277ed2d5e 100644 --- a/src/org/apache/fop/image/analyser/PNGReader.java +++ b/src/org/apache/fop/image/analyser/PNGReader.java @@ -1,35 +1,42 @@ /* * $Id$ - * Copyright (C) 2001 The Apache Software Foundation. All rights reserved. + * Copyright (C) 2001-2002 The Apache Software Foundation. All rights reserved. * For details on use and redistribution please refer to the * LICENSE file included with these sources. */ - package org.apache.fop.image.analyser; // Java import java.io.BufferedInputStream; import java.io.IOException; +// FOP import org.apache.fop.image.FopImage; import org.apache.fop.fo.FOUserAgent; /** * ImageReader object for PNG image type. - * @author Pankaj Narula - * @version 1.0 + * + * @author Pankaj Narula + * @version $Id$ */ public class PNGReader implements ImageReader { - static protected final int PNG_SIG_LENGTH = 24; - public FopImage.ImageInfo verifySignature(String uri, BufferedInputStream fis, - FOUserAgent ua) throws IOException { - byte[] header = getDefaultHeader(fis); - boolean supported = ((header[0] == (byte) 0x89) && - (header[1] == (byte) 0x50) && (header[2] == (byte) 0x4e) && - (header[3] == (byte) 0x47) && (header[4] == (byte) 0x0d) && - (header[5] == (byte) 0x0a) && - (header[6] == (byte) 0x1a) && (header[7] == (byte) 0x0a)); + private static final int PNG_SIG_LENGTH = 24; + + /** @see org.apache.fop.image.analyser.ImageReader */ + public FopImage.ImageInfo verifySignature(String uri, BufferedInputStream bis, + FOUserAgent ua) throws IOException { + byte[] header = getDefaultHeader(bis); + boolean supported = ((header[0] == (byte) 0x89) + && (header[1] == (byte) 0x50) + && (header[2] == (byte) 0x4e) + && (header[3] == (byte) 0x47) + && (header[4] == (byte) 0x0d) + && (header[5] == (byte) 0x0a) + && (header[6] == (byte) 0x1a) + && (header[7] == (byte) 0x0a)); + if (supported) { FopImage.ImageInfo info = getDimension(header); info.mimeType = getMimeType(); @@ -39,11 +46,16 @@ public class PNGReader implements ImageReader { } } + /** + * Returns the MIME type supported by this implementation. + * + * @return The MIME type + */ public String getMimeType() { return "image/png"; } - protected FopImage.ImageInfo getDimension(byte[] header) { + private FopImage.ImageInfo getDimension(byte[] header) { FopImage.ImageInfo info = new FopImage.ImageInfo(); // png is always big endian @@ -51,20 +63,23 @@ public class PNGReader implements ImageReader { int byte2 = header[17] & 0xff; int byte3 = header[18] & 0xff; int byte4 = header[19] & 0xff; - long l = (long)((byte1 << 24) | (byte2 << 16) | - (byte3 << 8) | byte4); - info.width = (int)(l); + long l = (long) ((byte1 << 24) + | (byte2 << 16) + | (byte3 << 8) + | (byte4)); + info.width = (int) l; byte1 = header[20] & 0xff; byte2 = header[21] & 0xff; byte3 = header[22] & 0xff; byte4 = header[23] & 0xff; - l = (long)((byte1 << 24) | (byte2 << 16) | (byte3 << 8) | byte4); - info.height = (int)(l); + l = (long) ((byte1 << 24) | (byte2 << 16) | (byte3 << 8) | byte4); + info.height = (int) l; return info; } - protected byte[] getDefaultHeader(BufferedInputStream imageStream) throws IOException { + private byte[] getDefaultHeader(BufferedInputStream imageStream) + throws IOException { byte[] header = new byte[PNG_SIG_LENGTH]; try { imageStream.mark(PNG_SIG_LENGTH + 1); @@ -73,11 +88,12 @@ public class PNGReader implements ImageReader { } catch (IOException ex) { try { imageStream.reset(); - } catch (IOException exbis) {} + } catch (IOException exbis) { + // throw the original exception, not this one + } throw ex; } return header; } -} - +} \ No newline at end of file diff --git a/src/org/apache/fop/image/analyser/SVGReader.java b/src/org/apache/fop/image/analyser/SVGReader.java index 8c3eac50a..8f4c7d979 100644 --- a/src/org/apache/fop/image/analyser/SVGReader.java +++ b/src/org/apache/fop/image/analyser/SVGReader.java @@ -1,85 +1,72 @@ /* * $Id$ - * Copyright (C) 2001 The Apache Software Foundation. All rights reserved. + * Copyright (C) 2001-2002 The Apache Software Foundation. All rights reserved. * For details on use and redistribution please refer to the * LICENSE file included with these sources. */ - package org.apache.fop.image.analyser; // Java import java.io.BufferedInputStream; import java.io.IOException; +import java.io.InputStream; +import java.awt.geom.AffineTransform; +import org.w3c.dom.Element; import org.w3c.dom.svg.SVGDocument; -import org.w3c.dom.svg.SVGSVGElement; - -// FOP -import org.apache.fop.image.XMLImage; - -import org.xml.sax.InputSource; -import org.xml.sax.XMLReader; +// Batik import org.apache.batik.dom.svg.SAXSVGDocumentFactory; - -import org.apache.batik.dom.svg.*; -import org.w3c.dom.*; -import org.w3c.dom.svg.*; -import org.w3c.dom.svg.SVGLength; -import org.apache.batik.bridge.*; -import org.apache.batik.swing.svg.*; -import org.apache.batik.swing.gvt.*; -import org.apache.batik.gvt.*; -import org.apache.batik.gvt.renderer.*; -import org.apache.batik.gvt.filter.*; -import org.apache.batik.gvt.event.*; - -import org.w3c.dom.DOMImplementation; +import org.apache.batik.dom.svg.SVGOMDocument; +import org.apache.batik.bridge.BridgeContext; +import org.apache.batik.bridge.UnitProcessor; import org.apache.batik.dom.svg.SVGDOMImplementation; -import java.io.File; -import java.io.InputStream; -import java.net.URL; -import java.util.List; -import java.util.ArrayList; -import java.awt.geom.AffineTransform; -import java.awt.Point; -import java.awt.geom.Dimension2D; -import java.awt.Dimension; - +// FOP +import org.apache.fop.image.XMLImage; import org.apache.fop.image.FopImage; import org.apache.fop.fo.FOUserAgent; import org.apache.fop.svg.SVGUserAgent; -/** - * ImageReader object for SVG document image type. - */ +/** ImageReader object for SVG document image type. */ public class SVGReader implements ImageReader { + + /** SVG's MIME type */ public static final String SVG_MIME_TYPE = "image/svg+xml"; - boolean batik = true; - public SVGReader() { - } + private boolean batik = true; + /** @see org.apache.fop.image.analyser.ImageReader */ public FopImage.ImageInfo verifySignature(String uri, BufferedInputStream fis, - FOUserAgent ua) throws IOException { + FOUserAgent ua) + throws IOException { return loadImage(uri, fis, ua); } + /** + * Returns the MIME type supported by this implementation. + * + * @return The MIME type + */ public String getMimeType() { return SVG_MIME_TYPE; } /** - * This means the external svg document will be loaded twice. - * Possibly need a slightly different design for the image stuff. + * This means the external svg document will be loaded twice. Possibly need + * a slightly different design for the image stuff. + * + * @param uri @todo Description of the Parameter + * @param fis @todo Description of the Parameter + * @param ua @todo Description of the Parameter + * @return @todo Description of the Return Value */ - protected FopImage.ImageInfo loadImage(String uri, BufferedInputStream fis, - FOUserAgent ua) { - if(batik) { + private FopImage.ImageInfo loadImage(String uri, BufferedInputStream bis, + FOUserAgent ua) { + if (batik) { try { Loader loader = new Loader(); - return loader.getImage(uri, fis, ua); + return loader.getImage(uri, bis, ua); } catch (NoClassDefFoundError e) { batik = false; //ua.getLogger().error("Batik not in class path", e); @@ -90,13 +77,13 @@ public class SVGReader implements ImageReader { } /** - * This method is put in another class so that the classloader - * does not attempt to load batik related classes when constructing - * the SVGReader class. + * This method is put in another class so that the classloader does not + * attempt to load batik related classes when constructing the SVGReader + * class. */ class Loader { private FopImage.ImageInfo getImage(String uri, InputStream fis, - FOUserAgent ua) { + FOUserAgent ua) { // parse document and get the size attributes of the svg element try { @@ -107,46 +94,49 @@ public class SVGReader implements ImageReader { String start = new String(b); fis.reset(); - if(start.equals("