diff options
Diffstat (limited to 'src/java/org/apache/poi/util')
-rw-r--r-- | src/java/org/apache/poi/util/BitFieldFactory.java | 5 | ||||
-rw-r--r-- | src/java/org/apache/poi/util/CloseIgnoringInputStream.java | 4 | ||||
-rw-r--r-- | src/java/org/apache/poi/util/DrawingDump.java | 12 | ||||
-rw-r--r-- | src/java/org/apache/poi/util/HexDump.java | 11 | ||||
-rw-r--r-- | src/java/org/apache/poi/util/IOUtils.java | 253 | ||||
-rw-r--r-- | src/java/org/apache/poi/util/NullLogger.java | 432 | ||||
-rw-r--r-- | src/java/org/apache/poi/util/POILogFactory.java | 62 | ||||
-rw-r--r-- | src/java/org/apache/poi/util/POILogger.java | 618 | ||||
-rw-r--r-- | src/java/org/apache/poi/util/StringUtil.java | 3 |
9 files changed, 260 insertions, 1140 deletions
diff --git a/src/java/org/apache/poi/util/BitFieldFactory.java b/src/java/org/apache/poi/util/BitFieldFactory.java index 4159074d76..047d5cb1da 100644 --- a/src/java/org/apache/poi/util/BitFieldFactory.java +++ b/src/java/org/apache/poi/util/BitFieldFactory.java @@ -21,11 +21,8 @@ package org.apache.poi.util; import java.util.*; /** - * Returns immutable Btfield instances. - * - * @author Jason Height (jheight at apache dot org) + * Returns immutable Bitfield instances. */ - public class BitFieldFactory { private static Map<Integer, BitField> instances = new HashMap<Integer, BitField>(); diff --git a/src/java/org/apache/poi/util/CloseIgnoringInputStream.java b/src/java/org/apache/poi/util/CloseIgnoringInputStream.java index f4896a8312..66e42c258c 100644 --- a/src/java/org/apache/poi/util/CloseIgnoringInputStream.java +++ b/src/java/org/apache/poi/util/CloseIgnoringInputStream.java @@ -20,13 +20,11 @@ package org.apache.poi.util; import java.io.FilterInputStream; import java.io.InputStream; -import org.apache.poi.poifs.filesystem.POIFSFileSystem; - /** * A wrapper around an {@link InputStream}, which * ignores close requests made to it. * - * Useful with {@link POIFSFileSystem}, where you want + * Useful with {@link org.apache.poi.poifs.filesystem.POIFSFileSystem}, where you want * to control the close yourself. */ public class CloseIgnoringInputStream extends FilterInputStream { diff --git a/src/java/org/apache/poi/util/DrawingDump.java b/src/java/org/apache/poi/util/DrawingDump.java index 1055ab2da0..4eb6a2aff2 100644 --- a/src/java/org/apache/poi/util/DrawingDump.java +++ b/src/java/org/apache/poi/util/DrawingDump.java @@ -18,12 +18,12 @@ package org.apache.poi.util; +import java.io.File; +import java.io.IOException; + import org.apache.poi.hssf.usermodel.HSSFSheet; import org.apache.poi.hssf.usermodel.HSSFWorkbook; -import org.apache.poi.poifs.filesystem.POIFSFileSystem; - -import java.io.FileInputStream; -import java.io.IOException; +import org.apache.poi.poifs.filesystem.NPOIFSFileSystem; /** * Dump out the aggregated escher records @@ -32,8 +32,8 @@ public class DrawingDump { public static void main( String[] args ) throws IOException { - POIFSFileSystem fs = - new POIFSFileSystem(new FileInputStream(args[0])); + NPOIFSFileSystem fs = + new NPOIFSFileSystem(new File(args[0])); HSSFWorkbook wb = new HSSFWorkbook(fs); try { System.out.println( "Drawing group:" ); diff --git a/src/java/org/apache/poi/util/HexDump.java b/src/java/org/apache/poi/util/HexDump.java index 3c086ba606..02d9b6ae30 100644 --- a/src/java/org/apache/poi/util/HexDump.java +++ b/src/java/org/apache/poi/util/HexDump.java @@ -245,12 +245,15 @@ public class HexDump { { StringBuffer retVal = new StringBuffer(); retVal.append('['); - for(int x = 0; x < value.length; x++) + if (value != null && value.length > 0) { - if (x>0) { - retVal.append(", "); + for(int x = 0; x < value.length; x++) + { + if (x>0) { + retVal.append(", "); + } + retVal.append(toHex(value[x])); } - retVal.append(toHex(value[x])); } retVal.append(']'); return retVal.toString(); diff --git a/src/java/org/apache/poi/util/IOUtils.java b/src/java/org/apache/poi/util/IOUtils.java index 4f18214c46..f1d5a2378d 100644 --- a/src/java/org/apache/poi/util/IOUtils.java +++ b/src/java/org/apache/poi/util/IOUtils.java @@ -22,120 +22,149 @@ import java.io.Closeable; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.io.PushbackInputStream; import java.nio.ByteBuffer; import java.nio.channels.ReadableByteChannel; import java.util.zip.CRC32; import java.util.zip.Checksum; +import org.apache.poi.EmptyFileException; + public final class IOUtils { + private static final POILogger logger = POILogFactory.getLogger( IOUtils.class ); + + private IOUtils() { + // no instances of this class + } + + /** + * Peeks at the first 8 bytes of the stream. Returns those bytes, but + * with the stream unaffected. Requires a stream that supports mark/reset, + * or a PushbackInputStream. If the stream has >0 but <8 bytes, + * remaining bytes will be zero. + * @throws EmptyFileException if the stream is empty + */ + public static byte[] peekFirst8Bytes(InputStream stream) throws IOException, EmptyFileException { + // We want to peek at the first 8 bytes + stream.mark(8); + + byte[] header = new byte[8]; + int read = IOUtils.readFully(stream, header); + + if (read < 1) + throw new EmptyFileException(); + + // Wind back those 8 bytes + if(stream instanceof PushbackInputStream) { + PushbackInputStream pin = (PushbackInputStream)stream; + pin.unread(header); + } else { + stream.reset(); + } + + return header; + } + + /** + * Reads all the data from the input stream, and returns the bytes read. + */ + public static byte[] toByteArray(InputStream stream) throws IOException { + ByteArrayOutputStream baos = new ByteArrayOutputStream(); + + byte[] buffer = new byte[4096]; + int read = 0; + while (read != -1) { + read = stream.read(buffer); + if (read > 0) { + baos.write(buffer, 0, read); + } + } + + return baos.toByteArray(); + } + + /** + * Returns an array (that shouldn't be written to!) of the + * ByteBuffer. Will be of the requested length, or possibly + * longer if that's easier. + */ + public static byte[] toByteArray(ByteBuffer buffer, int length) { + if(buffer.hasArray() && buffer.arrayOffset() == 0) { + // The backing array should work out fine for us + return buffer.array(); + } + + byte[] data = new byte[length]; + buffer.get(data); + return data; + } + + /** + * Helper method, just calls <tt>readFully(in, b, 0, b.length)</tt> + */ + public static int readFully(InputStream in, byte[] b) throws IOException { + return readFully(in, b, 0, b.length); + } + + /** + * Same as the normal <tt>in.read(b, off, len)</tt>, but tries to ensure + * that the entire len number of bytes is read. + * <p> + * If the end of file is reached before any bytes are read, returns -1. If + * the end of the file is reached after some bytes are read, returns the + * number of bytes read. If the end of the file isn't reached before len + * bytes have been read, will return len bytes. + */ + public static int readFully(InputStream in, byte[] b, int off, int len) throws IOException { + int total = 0; + while (true) { + int got = in.read(b, off + total, len - total); + if (got < 0) { + return (total == 0) ? -1 : total; + } + total += got; + if (total == len) { + return total; + } + } + } - private static final POILogger logger = POILogFactory - .getLogger( IOUtils.class ); - - private IOUtils() { - // no instances of this class - } - - /** - * Reads all the data from the input stream, and returns the bytes read. - */ - public static byte[] toByteArray(InputStream stream) throws IOException { - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - - byte[] buffer = new byte[4096]; - int read = 0; - while (read != -1) { - read = stream.read(buffer); - if (read > 0) { - baos.write(buffer, 0, read); - } - } - - return baos.toByteArray(); - } - - /** - * Returns an array (that shouldn't be written to!) of the - * ByteBuffer. Will be of the requested length, or possibly - * longer if that's easier. - */ - public static byte[] toByteArray(ByteBuffer buffer, int length) { - if(buffer.hasArray() && buffer.arrayOffset() == 0) { - // The backing array should work out fine for us - return buffer.array(); - } - - byte[] data = new byte[length]; - buffer.get(data); - return data; - } - - /** - * Helper method, just calls <tt>readFully(in, b, 0, b.length)</tt> - */ - public static int readFully(InputStream in, byte[] b) throws IOException { - return readFully(in, b, 0, b.length); - } - - /** - * Same as the normal <tt>in.read(b, off, len)</tt>, but tries to ensure - * that the entire len number of bytes is read. - * <p> - * If the end of file is reached before any bytes are read, returns -1. If - * the end of the file is reached after some bytes are read, returns the - * number of bytes read. If the end of the file isn't reached before len - * bytes have been read, will return len bytes. - */ - public static int readFully(InputStream in, byte[] b, int off, int len) throws IOException { - int total = 0; - while (true) { - int got = in.read(b, off + total, len - total); - if (got < 0) { - return (total == 0) ? -1 : total; - } - total += got; - if (total == len) { - return total; - } - } - } - - /** - * Same as the normal <tt>channel.read(b)</tt>, but tries to ensure - * that the entire len number of bytes is read. - * <p> - * If the end of file is reached before any bytes are read, returns -1. If - * the end of the file is reached after some bytes are read, returns the - * number of bytes read. If the end of the file isn't reached before len - * bytes have been read, will return len bytes. - */ - public static int readFully(ReadableByteChannel channel, ByteBuffer b) throws IOException { - int total = 0; - while (true) { - int got = channel.read(b); - if (got < 0) { - return (total == 0) ? -1 : total; - } - total += got; - if (total == b.capacity() || b.position() == b.capacity()) { - return total; - } - } - } - - /** - * Copies all the data from the given InputStream to the OutputStream. It - * leaves both streams open, so you will still need to close them once done. - */ - public static void copy(InputStream inp, OutputStream out) throws IOException { - byte[] buff = new byte[4096]; - int count; - while ((count = inp.read(buff)) != -1) { - if (count > 0) { - out.write(buff, 0, count); - } - } - } + /** + * Same as the normal <tt>channel.read(b)</tt>, but tries to ensure + * that the entire len number of bytes is read. + * <p> + * If the end of file is reached before any bytes are read, returns -1. If + * the end of the file is reached after some bytes are read, returns the + * number of bytes read. If the end of the file isn't reached before len + * bytes have been read, will return len bytes. + */ + public static int readFully(ReadableByteChannel channel, ByteBuffer b) throws IOException { + int total = 0; + while (true) { + int got = channel.read(b); + if (got < 0) { + return (total == 0) ? -1 : total; + } + total += got; + if (total == b.capacity() || b.position() == b.capacity()) { + return total; + } + } + } + + /** + * Copies all the data from the given InputStream to the OutputStream. It + * leaves both streams open, so you will still need to close them once done. + */ + public static void copy(InputStream inp, OutputStream out) throws IOException { + byte[] buff = new byte[4096]; + int count; + while ((count = inp.read(buff)) != -1) { + if (count > 0) { + out.write(buff, 0, count); + } + } + } public static long calculateChecksum(byte[] data) { Checksum sum = new CRC32(); @@ -150,14 +179,10 @@ public final class IOUtils { * @param closeable * resource to close */ - public static void closeQuietly( final Closeable closeable ) - { - try - { + public static void closeQuietly( final Closeable closeable ) { + try { closeable.close(); - } - catch ( Exception exc ) - { + } catch ( Exception exc ) { logger.log( POILogger.ERROR, "Unable to close resource: " + exc, exc ); } diff --git a/src/java/org/apache/poi/util/NullLogger.java b/src/java/org/apache/poi/util/NullLogger.java index 95c8c5d446..24643c8fde 100644 --- a/src/java/org/apache/poi/util/NullLogger.java +++ b/src/java/org/apache/poi/util/NullLogger.java @@ -27,12 +27,10 @@ package org.apache.poi.util; * @author Glen Stampoultzis (glens at apache.org) * @author Nicola Ken Barozzi (nicolaken at apache.org) */ -public class NullLogger extends POILogger -{ +public class NullLogger extends POILogger { @Override - public void initialize(final String cat) - { - //do nothing + public void initialize(final String cat){ + // do nothing } /** @@ -45,147 +43,7 @@ public class NullLogger extends POILogger @Override public void log(final int level, final Object obj1) { - //do nothing - } - - /** - * Check if a logger is enabled to log at the specified level - * - * @param level One of DEBUG, INFO, WARN, ERROR, FATAL - */ - - @Override - public boolean check(final int level) - { - return false; - } - - /** - * Log a message. Lazily appends Object parameters together. - * - * @param level One of DEBUG, INFO, WARN, ERROR, FATAL - * @param obj1 first object to place in the message - * @param obj2 second object to place in the message - */ - - @Override - public void log(final int level, final Object obj1, final Object obj2) - { - //do nothing - } - - /** - * Log a message. Lazily appends Object parameters together. - * - * @param level One of DEBUG, INFO, WARN, ERROR, FATAL - * @param obj1 first Object to place in the message - * @param obj2 second Object to place in the message - * @param obj3 third Object to place in the message - */ - - @Override - public void log(final int level, final Object obj1, final Object obj2, - final Object obj3) - { - //do nothing - } - - /** - * Log a message. Lazily appends Object parameters together. - * - * @param level One of DEBUG, INFO, WARN, ERROR, FATAL - * @param obj1 first Object to place in the message - * @param obj2 second Object to place in the message - * @param obj3 third Object to place in the message - * @param obj4 fourth Object to place in the message - */ - - @Override - public void log(final int level, final Object obj1, final Object obj2, - final Object obj3, final Object obj4) - { - //do nothing - } - - /** - * Log a message. Lazily appends Object parameters together. - * - * @param level One of DEBUG, INFO, WARN, ERROR, FATAL - * @param obj1 first Object to place in the message - * @param obj2 second Object to place in the message - * @param obj3 third Object to place in the message - * @param obj4 fourth Object to place in the message - * @param obj5 fifth Object to place in the message - */ - - @Override - public void log(final int level, final Object obj1, final Object obj2, - final Object obj3, final Object obj4, final Object obj5) - { - //do nothing - } - - /** - * Log a message. Lazily appends Object parameters together. - * - * @param level One of DEBUG, INFO, WARN, ERROR, FATAL - * @param obj1 first Object to place in the message - * @param obj2 second Object to place in the message - * @param obj3 third Object to place in the message - * @param obj4 fourth Object to place in the message - * @param obj5 fifth Object to place in the message - * @param obj6 sixth Object to place in the message - */ - - @Override - public void log(final int level, final Object obj1, final Object obj2, - final Object obj3, final Object obj4, final Object obj5, - final Object obj6) - { - //do nothing - } - - /** - * Log a message. Lazily appends Object parameters together. - * - * @param level One of DEBUG, INFO, WARN, ERROR, FATAL - * @param obj1 first Object to place in the message - * @param obj2 second Object to place in the message - * @param obj3 third Object to place in the message - * @param obj4 fourth Object to place in the message - * @param obj5 fifth Object to place in the message - * @param obj6 sixth Object to place in the message - * @param obj7 seventh Object to place in the message - */ - - @Override - public void log(final int level, final Object obj1, final Object obj2, - final Object obj3, final Object obj4, final Object obj5, - final Object obj6, final Object obj7) - { - //do nothing - } - - /** - * Log a message. Lazily appends Object parameters together. - * - * @param level One of DEBUG, INFO, WARN, ERROR, FATAL - * @param obj1 first Object to place in the message - * @param obj2 second Object to place in the message - * @param obj3 third Object to place in the message - * @param obj4 fourth Object to place in the message - * @param obj5 fifth Object to place in the message - * @param obj6 sixth Object to place in the message - * @param obj7 seventh Object to place in the message - * @param obj8 eighth Object to place in the message - */ - - @Override - public void log(final int level, final Object obj1, final Object obj2, - final Object obj3, final Object obj4, final Object obj5, - final Object obj6, final Object obj7, final Object obj8) - { - //do nothing + // do nothing } /** @@ -195,289 +53,19 @@ public class NullLogger extends POILogger * @param obj1 The object to log. This is converted to a string. * @param exception An exception to be logged */ - - @Override - public void log(final int level, final Object obj1, - final Throwable exception) - { - //do nothing - } - - /** - * Log a message. Lazily appends Object parameters together. - * - * @param level One of DEBUG, INFO, WARN, ERROR, FATAL - * @param obj1 first Object to place in the message - * @param obj2 second Object to place in the message - * @param exception An exception to be logged - */ - - @Override - public void log(final int level, final Object obj1, final Object obj2, - final Throwable exception) - { - //do nothing + public void log(int level, Object obj1, final Throwable exception) { + // do nothing } + /** - * Log a message. Lazily appends Object parameters together. - * - * @param level One of DEBUG, INFO, WARN, ERROR, FATAL - * @param obj1 first Object to place in the message - * @param obj2 second Object to place in the message - * @param obj3 third object to place in the message - * @param exception An error message to be logged - */ - - @Override - public void log(final int level, final Object obj1, final Object obj2, - final Object obj3, final Throwable exception) - { - //do nothing - } - - /** - * Log a message. Lazily appends Object parameters together. - * - * @param level One of DEBUG, INFO, WARN, ERROR, FATAL - * @param obj1 first Object to place in the message - * @param obj2 second Object to place in the message - * @param obj3 third object to place in the message - * @param obj4 fourth object to place in the message - * @param exception An exception to be logged - */ - - @Override - public void log(final int level, final Object obj1, final Object obj2, - final Object obj3, final Object obj4, - final Throwable exception) - { - //do nothing - } - - /** - * Log a message. Lazily appends Object parameters together. - * - * @param level One of DEBUG, INFO, WARN, ERROR, FATAL - * @param obj1 first Object to place in the message - * @param obj2 second Object to place in the message - * @param obj3 third object to place in the message - * @param obj4 fourth object to place in the message - * @param obj5 fifth object to place in the message - * @param exception An exception to be logged - */ - - @Override - public void log(final int level, final Object obj1, final Object obj2, - final Object obj3, final Object obj4, final Object obj5, - final Throwable exception) - { - //do nothing - } - - /** - * Log a message. Lazily appends Object parameters together. - * - * @param level One of DEBUG, INFO, WARN, ERROR, FATAL - * @param obj1 first Object to place in the message - * @param obj2 second Object to place in the message - * @param obj3 third object to place in the message - * @param obj4 fourth object to place in the message - * @param obj5 fifth object to place in the message - * @param obj6 sixth object to place in the message - * @param exception An exception to be logged - */ - - @Override - public void log(final int level, final Object obj1, final Object obj2, - final Object obj3, final Object obj4, final Object obj5, - final Object obj6, final Throwable exception) - { - //do nothing - } - - /** - * Log a message. Lazily appends Object parameters together. - * - * @param level One of DEBUG, INFO, WARN, ERROR, FATAL - * @param obj1 first Object to place in the message - * @param obj2 second Object to place in the message - * @param obj3 third object to place in the message - * @param obj4 fourth object to place in the message - * @param obj5 fifth object to place in the message - * @param obj6 sixth object to place in the message - * @param obj7 seventh object to place in the message - * @param exception An exception to be logged - */ - - @Override - public void log(final int level, final Object obj1, final Object obj2, - final Object obj3, final Object obj4, final Object obj5, - final Object obj6, final Object obj7, - final Throwable exception) - { - //do nothing - } - - /** - * Log a message. Lazily appends Object parameters together. - * - * @param level One of DEBUG, INFO, WARN, ERROR, FATAL - * @param obj1 first Object to place in the message - * @param obj2 second Object to place in the message - * @param obj3 third object to place in the message - * @param obj4 fourth object to place in the message - * @param obj5 fifth object to place in the message - * @param obj6 sixth object to place in the message - * @param obj7 seventh object to place in the message - * @param obj8 eighth object to place in the message - * @param exception An exception to be logged - */ - - @Override - public void log(final int level, final Object obj1, final Object obj2, - final Object obj3, final Object obj4, final Object obj5, - final Object obj6, final Object obj7, final Object obj8, - final Throwable exception) - { - //do nothing - } - - /** - * Logs a formated message. The message itself may contain % - * characters as place holders. This routine will attempt to match - * the placeholder by looking at the type of parameter passed to - * obj1.<p> - * - * If the parameter is an array, it traverses the array first and - * matches parameters sequentially against the array items. - * Otherwise the parameters after <code>message</code> are matched - * in order.<p> - * - * If the place holder matches against a number it is printed as a - * whole number. This can be overridden by specifying a precision - * in the form %n.m where n is the padding for the whole part and - * m is the number of decimal places to display. n can be excluded - * if desired. n and m may not be more than 9.<p> - * - * If the last parameter (after flattening) is a Throwable it is - * logged specially. - * - * @param level One of DEBUG, INFO, WARN, ERROR, FATAL - * @param message The message to log. - * @param obj1 The first object to match against. - */ - - @Override - public void logFormatted(final int level, final String message, - final Object obj1) - { - //do nothing - } - - /** - * Logs a formated message. The message itself may contain % - * characters as place holders. This routine will attempt to match - * the placeholder by looking at the type of parameter passed to - * obj1.<p> - * - * If the parameter is an array, it traverses the array first and - * matches parameters sequentially against the array items. - * Otherwise the parameters after <code>message</code> are matched - * in order.<p> - * - * If the place holder matches against a number it is printed as a - * whole number. This can be overridden by specifying a precision - * in the form %n.m where n is the padding for the whole part and - * m is the number of decimal places to display. n can be excluded - * if desired. n and m may not be more than 9.<p> - * - * If the last parameter (after flattening) is a Throwable it is - * logged specially. - * - * @param level One of DEBUG, INFO, WARN, ERROR, FATAL - * @param message The message to log. - * @param obj1 The first object to match against. - * @param obj2 The second object to match against. - */ - - @Override - public void logFormatted(final int level, final String message, - final Object obj1, final Object obj2) - { - //do nothing - } - - /** - * Logs a formated message. The message itself may contain % - * characters as place holders. This routine will attempt to match - * the placeholder by looking at the type of parameter passed to - * obj1.<p> - * - * If the parameter is an array, it traverses the array first and - * matches parameters sequentially against the array items. - * Otherwise the parameters after <code>message</code> are matched - * in order.<p> - * - * If the place holder matches against a number it is printed as a - * whole number. This can be overridden by specifying a precision - * in the form %n.m where n is the padding for the whole part and - * m is the number of decimal places to display. n can be excluded - * if desired. n and m may not be more than 9.<p> - * - * If the last parameter (after flattening) is a Throwable it is - * logged specially. - * - * @param level One of DEBUG, INFO, WARN, ERROR, FATAL - * @param message The message to log. - * @param obj1 The first object to match against. - * @param obj2 The second object to match against. - * @param obj3 The third object to match against. - */ - - @Override - public void logFormatted(final int level, final String message, - final Object obj1, final Object obj2, - final Object obj3) - { - //do nothing - } - - /** - * Logs a formated message. The message itself may contain % - * characters as place holders. This routine will attempt to match - * the placeholder by looking at the type of parameter passed to - * obj1.<p> - * - * If the parameter is an array, it traverses the array first and - * matches parameters sequentially against the array items. - * Otherwise the parameters after <code>message</code> are matched - * in order.<p> - * - * If the place holder matches against a number it is printed as a - * whole number. This can be overridden by specifying a precision - * in the form %n.m where n is the padding for the whole part and - * m is the number of decimal places to display. n can be excluded - * if desired. n and m may not be more than 9.<p> - * - * If the last parameter (after flattening) is a Throwable it is - * logged specially. + * Check if a logger is enabled to log at the specified level * * @param level One of DEBUG, INFO, WARN, ERROR, FATAL - * @param message The message to log. - * @param obj1 The first object to match against. - * @param obj2 The second object to match against. - * @param obj3 The third object to match against. - * @param obj4 The forth object to match against. */ - @Override - public void logFormatted(final int level, final String message, - final Object obj1, final Object obj2, - final Object obj3, final Object obj4) - { - //do nothing + public boolean check(final int level) { + return false; } - } diff --git a/src/java/org/apache/poi/util/POILogFactory.java b/src/java/org/apache/poi/util/POILogFactory.java index 641949c391..7af73e3a87 100644 --- a/src/java/org/apache/poi/util/POILogFactory.java +++ b/src/java/org/apache/poi/util/POILogFactory.java @@ -15,7 +15,7 @@ See the License for the specific language governing permissions and limitations under the License. ==================================================================== */ - + package org.apache.poi.util; @@ -30,20 +30,18 @@ import java.util.Map; * @author Marc Johnson (mjohnson at apache dot org) * @author Nicola Ken Barozzi (nicolaken at apache.org) */ - -public class POILogFactory -{ - +@Internal +public final class POILogFactory { /** * Map of POILogger instances, with classes as keys */ - private static Map<String,POILogger> _loggers = new HashMap<String,POILogger>();; + private static Map<String,POILogger> _loggers = new HashMap<String,POILogger>(); /** * A common instance of NullLogger, as it does nothing * we only need the one */ - private static POILogger _nullLogger = new NullLogger(); + private static final POILogger _nullLogger = new NullLogger(); /** * The name of the class to use. Initialised the * first time we need it @@ -53,9 +51,7 @@ public class POILogFactory /** * Construct a POILogFactory. */ - private POILogFactory() - { - } + private POILogFactory() {} /** * Get a logger, based on a class name @@ -64,12 +60,10 @@ public class POILogFactory * * @return a POILogger for the specified class */ - - public static POILogger getLogger(final Class<?> theclass) - { + public static POILogger getLogger(final Class<?> theclass) { return getLogger(theclass.getName()); } - + /** * Get a logger, based on a String * @@ -77,11 +71,9 @@ public class POILogFactory * * @return a POILogger for the specified class */ - - public static POILogger getLogger(final String cat) - { + public static POILogger getLogger(final String cat) { POILogger logger = null; - + // If we haven't found out what logger to use yet, // then do so now // Don't look it up until we're first asked, so @@ -91,40 +83,40 @@ public class POILogFactory try { _loggerClassName = System.getProperty("org.apache.poi.util.POILogger"); } catch(Exception e) {} - + // Use the default logger if none specified, // or none could be fetched if(_loggerClassName == null) { - _loggerClassName = _nullLogger.getClass().getName(); + _loggerClassName = _nullLogger.getClass().getName(); } } - + // Short circuit for the null logger, which // ignores all categories if(_loggerClassName.equals(_nullLogger.getClass().getName())) { return _nullLogger; } - + // Fetch the right logger for them, creating - // it if that's required - if (_loggers.containsKey(cat)) { - logger = _loggers.get(cat); - } else { + // it if that's required + logger = _loggers.get(cat); + if (logger == null) { try { - @SuppressWarnings("unchecked") - Class<? extends POILogger> loggerClass = - (Class<? extends POILogger>)Class.forName(_loggerClassName); - logger = loggerClass.newInstance(); - logger.initialize(cat); + @SuppressWarnings("unchecked") + Class<? extends POILogger> loggerClass = + (Class<? extends POILogger>) Class.forName(_loggerClassName); + logger = loggerClass.newInstance(); + logger.initialize(cat); } catch(Exception e) { - // Give up and use the null logger - logger = _nullLogger; + // Give up and use the null logger + logger = _nullLogger; + _loggerClassName = _nullLogger.getClass().getName(); } - + // Save for next time _loggers.put(cat, logger); } return logger; } -} // end public class POILogFactory
\ No newline at end of file +}
\ No newline at end of file diff --git a/src/java/org/apache/poi/util/POILogger.java b/src/java/org/apache/poi/util/POILogger.java index 4088ec290c..d892366e7e 100644 --- a/src/java/org/apache/poi/util/POILogger.java +++ b/src/java/org/apache/poi/util/POILogger.java @@ -30,6 +30,7 @@ import java.util.List; * @author Glen Stampoultzis (glens at apache.org) * @author Nicola Ken Barozzi (nicolaken at apache.org) */ +@Internal public abstract class POILogger { public static final int DEBUG = 1; @@ -49,7 +50,7 @@ public abstract class POILogger { * package. You need a POILogger? Go to the POILogFactory for one */ POILogger() { - // no fields to initialise + // no fields to initialize } abstract public void initialize(String cat); @@ -60,7 +61,7 @@ public abstract class POILogger { * @param level One of DEBUG, INFO, WARN, ERROR, FATAL * @param obj1 The object to log. This is converted to a string. */ - abstract public void log(int level, Object obj1); + abstract protected void log(int level, Object obj1); /** * Log a message @@ -69,8 +70,7 @@ public abstract class POILogger { * @param obj1 The object to log. This is converted to a string. * @param exception An exception to be logged */ - abstract public void log(int level, Object obj1, - final Throwable exception); + abstract protected void log(int level, Object obj1, final Throwable exception); /** @@ -82,344 +82,32 @@ public abstract class POILogger { /** * Log a message. Lazily appends Object parameters together. + * If the last parameter is a {@link Throwable} it is logged specially. * * @param level One of DEBUG, INFO, WARN, ERROR, FATAL - * @param obj1 first object to place in the message - * @param obj2 second object to place in the message + * @param objs the objects to place in the message */ - public void log(int level, Object obj1, Object obj2) - { - if (check(level)) - { - log(level, new StringBuffer(32).append(obj1).append(obj2)); - } - } - - /** - * Log a message. Lazily appends Object parameters together. - * - * @param level One of DEBUG, INFO, WARN, ERROR, FATAL - * @param obj1 first Object to place in the message - * @param obj2 second Object to place in the message - * @param obj3 third Object to place in the message - */ - public void log(int level, Object obj1, Object obj2, - Object obj3) - { - - - if (check(level)) - { - log(level, - new StringBuffer(48).append(obj1).append(obj2) - .append(obj3)); - } - } - - /** - * Log a message. Lazily appends Object parameters together. - * - * @param level One of DEBUG, INFO, WARN, ERROR, FATAL - * @param obj1 first Object to place in the message - * @param obj2 second Object to place in the message - * @param obj3 third Object to place in the message - * @param obj4 fourth Object to place in the message - */ - public void log(int level, Object obj1, Object obj2, - Object obj3, Object obj4) - { - - - if (check(level)) - { - log(level, - new StringBuffer(64).append(obj1).append(obj2) - .append(obj3).append(obj4)); - } - } - - /** - * Log a message. Lazily appends Object parameters together. - * - * @param level One of DEBUG, INFO, WARN, ERROR, FATAL - * @param obj1 first Object to place in the message - * @param obj2 second Object to place in the message - * @param obj3 third Object to place in the message - * @param obj4 fourth Object to place in the message - * @param obj5 fifth Object to place in the message - */ - public void log(int level, Object obj1, Object obj2, - Object obj3, Object obj4, Object obj5) - { - - - if (check(level)) - { - log(level, - new StringBuffer(80).append(obj1).append(obj2) - .append(obj3).append(obj4).append(obj5)); - } - } - - /** - * Log a message. Lazily appends Object parameters together. - * - * @param level One of DEBUG, INFO, WARN, ERROR, FATAL - * @param obj1 first Object to place in the message - * @param obj2 second Object to place in the message - * @param obj3 third Object to place in the message - * @param obj4 fourth Object to place in the message - * @param obj5 fifth Object to place in the message - * @param obj6 sixth Object to place in the message - */ - public void log(int level, Object obj1, Object obj2, - Object obj3, Object obj4, Object obj5, - Object obj6) - { - - - if (check(level)) - { - log(level , - new StringBuffer(96).append(obj1).append(obj2) - .append(obj3).append(obj4).append(obj5).append(obj6)); - } - } - - /** - * Log a message. Lazily appends Object parameters together. - * - * @param level One of DEBUG, INFO, WARN, ERROR, FATAL - * @param obj1 first Object to place in the message - * @param obj2 second Object to place in the message - * @param obj3 third Object to place in the message - * @param obj4 fourth Object to place in the message - * @param obj5 fifth Object to place in the message - * @param obj6 sixth Object to place in the message - * @param obj7 seventh Object to place in the message - */ - public void log(int level, Object obj1, Object obj2, - Object obj3, Object obj4, Object obj5, - Object obj6, Object obj7) - { - - - if (check(level)) - { - log(level, - new StringBuffer(112).append(obj1).append(obj2) - .append(obj3).append(obj4).append(obj5).append(obj6) - .append(obj7)); - } - } - - /** - * Log a message. Lazily appends Object parameters together. - * - * @param level One of DEBUG, INFO, WARN, ERROR, FATAL - * @param obj1 first Object to place in the message - * @param obj2 second Object to place in the message - * @param obj3 third Object to place in the message - * @param obj4 fourth Object to place in the message - * @param obj5 fifth Object to place in the message - * @param obj6 sixth Object to place in the message - * @param obj7 seventh Object to place in the message - * @param obj8 eighth Object to place in the message - */ - public void log(int level, Object obj1, Object obj2, - Object obj3, Object obj4, Object obj5, - Object obj6, Object obj7, Object obj8) - { - - - if (check(level)) - { - log(level, - new StringBuffer(128).append(obj1).append(obj2) - .append(obj3).append(obj4).append(obj5).append(obj6) - .append(obj7).append(obj8)); - } - } - - /** - * Log an exception, without a message - * - * @param level One of DEBUG, INFO, WARN, ERROR, FATAL - * @param exception An exception to be logged - */ - public void log(int level, final Throwable exception) - { - log(level, null, exception); - } - - /** - * Log a message. Lazily appends Object parameters together. - * - * @param level One of DEBUG, INFO, WARN, ERROR, FATAL - * @param obj1 first Object to place in the message - * @param obj2 second Object to place in the message - * @param exception An exception to be logged - */ - public void log(int level, Object obj1, Object obj2, - final Throwable exception) - { - - - if (check(level)) - { - log(level, new StringBuffer(32).append(obj1).append(obj2), - exception); - } - } - - /** - * Log a message. Lazily appends Object parameters together. - * - * @param level One of DEBUG, INFO, WARN, ERROR, FATAL - * @param obj1 first Object to place in the message - * @param obj2 second Object to place in the message - * @param obj3 third object to place in the message - * @param exception An error message to be logged - */ - public void log(int level, Object obj1, Object obj2, - Object obj3, final Throwable exception) - { - - - if (check(level)) - { - log(level, new StringBuffer(48).append(obj1).append(obj2) - .append(obj3), exception); - } - } - - /** - * Log a message. Lazily appends Object parameters together. - * - * @param level One of DEBUG, INFO, WARN, ERROR, FATAL - * @param obj1 first Object to place in the message - * @param obj2 second Object to place in the message - * @param obj3 third object to place in the message - * @param obj4 fourth object to place in the message - * @param exception An exception to be logged - */ - public void log(int level, Object obj1, Object obj2, - Object obj3, Object obj4, - final Throwable exception) - { - - - if (check(level)) - { - log(level, new StringBuffer(64).append(obj1).append(obj2) - .append(obj3).append(obj4), exception); - } - } - - /** - * Log a message. Lazily appends Object parameters together. - * - * @param level One of DEBUG, INFO, WARN, ERROR, FATAL - * @param obj1 first Object to place in the message - * @param obj2 second Object to place in the message - * @param obj3 third object to place in the message - * @param obj4 fourth object to place in the message - * @param obj5 fifth object to place in the message - * @param exception An exception to be logged - */ - public void log(int level, Object obj1, Object obj2, - Object obj3, Object obj4, Object obj5, - final Throwable exception) - { - - - if (check(level)) - { - log(level, new StringBuffer(80).append(obj1).append(obj2) - .append(obj3).append(obj4).append(obj5), exception); - } - } - - /** - * Log a message. Lazily appends Object parameters together. - * - * @param level One of DEBUG, INFO, WARN, ERROR, FATAL - * @param obj1 first Object to place in the message - * @param obj2 second Object to place in the message - * @param obj3 third object to place in the message - * @param obj4 fourth object to place in the message - * @param obj5 fifth object to place in the message - * @param obj6 sixth object to place in the message - * @param exception An exception to be logged - */ - public void log(int level, Object obj1, Object obj2, - Object obj3, Object obj4, Object obj5, - Object obj6, final Throwable exception) - { - - - if (check(level)) - { - log(level , new StringBuffer(96).append(obj1) - .append(obj2).append(obj3).append(obj4).append(obj5) - .append(obj6), exception); - } - } - - /** - * Log a message. Lazily appends Object parameters together. - * - * @param level One of DEBUG, INFO, WARN, ERROR, FATAL - * @param obj1 first Object to place in the message - * @param obj2 second Object to place in the message - * @param obj3 third object to place in the message - * @param obj4 fourth object to place in the message - * @param obj5 fifth object to place in the message - * @param obj6 sixth object to place in the message - * @param obj7 seventh object to place in the message - * @param exception An exception to be logged - */ - public void log(int level, Object obj1, Object obj2, - Object obj3, Object obj4, Object obj5, - Object obj6, Object obj7, - final Throwable exception) - { - - - if (check(level)) - { - log(level, new StringBuffer(112).append(obj1).append(obj2) - .append(obj3).append(obj4).append(obj5).append(obj6) - .append(obj7), exception); + public void log(int level, Object... objs) { + if (!check(level)) return; + StringBuilder sb = new StringBuilder(32); + Throwable lastEx = null; + for (int i=0; i<objs.length; i++) { + if (i == objs.length-1 && objs[i] instanceof Throwable) { + lastEx = (Throwable)objs[i]; + } else { + sb.append(objs[i]); + } } - } - - /** - * Log a message. Lazily appends Object parameters together. - * - * @param level One of DEBUG, INFO, WARN, ERROR, FATAL - * @param obj1 first Object to place in the message - * @param obj2 second Object to place in the message - * @param obj3 third object to place in the message - * @param obj4 fourth object to place in the message - * @param obj5 fifth object to place in the message - * @param obj6 sixth object to place in the message - * @param obj7 seventh object to place in the message - * @param obj8 eighth object to place in the message - * @param exception An exception to be logged - */ - public void log(int level, Object obj1, Object obj2, - Object obj3, Object obj4, Object obj5, - Object obj6, Object obj7, Object obj8, - final Throwable exception) - { - - - if (check(level)) - { - log(level, new StringBuffer(128).append(obj1).append(obj2) - .append(obj3).append(obj4).append(obj5).append(obj6) - .append(obj7).append(obj8), exception); + + String msg = sb.toString(); + msg = msg.replaceAll("[\r\n]+", " "); // log forging escape + + // somehow this ambiguity works and doesn't lead to a loop, + // but it's confusing ... + if (lastEx == null) { + log(level, msg); + } else { + log(level, msg, lastEx); } } @@ -445,239 +133,67 @@ public abstract class POILogger { * * @param level One of DEBUG, INFO, WARN, ERROR, FATAL * @param message The message to log. - * @param obj1 The first object to match against. + * @param unflatParams The objects to match against. */ - public void logFormatted(int level, String message, - Object obj1) - { - commonLogFormatted(level, message, new Object[] - { - obj1 - }); - } + public void logFormatted(int level, String message, Object... unflatParams) { + if (!check(level)) return; + Object[] params = flattenArrays(unflatParams); + String msg = StringUtil.format(message, params); + msg = msg.replaceAll("[\r\n]+", " "); // log forging escape - /** - * Logs a formated message. The message itself may contain % - * characters as place holders. This routine will attempt to match - * the placeholder by looking at the type of parameter passed to - * obj1.<p> - * - * If the parameter is an array, it traverses the array first and - * matches parameters sequentially against the array items. - * Otherwise the parameters after <code>message</code> are matched - * in order.<p> - * - * If the place holder matches against a number it is printed as a - * whole number. This can be overridden by specifying a precision - * in the form %n.m where n is the padding for the whole part and - * m is the number of decimal places to display. n can be excluded - * if desired. n and m may not be more than 9.<p> - * - * If the last parameter (after flattening) is a Throwable it is - * logged specially. - * - * @param level One of DEBUG, INFO, WARN, ERROR, FATAL - * @param message The message to log. - * @param obj1 The first object to match against. - * @param obj2 The second object to match against. - */ - public void logFormatted(int level, String message, - Object obj1, Object obj2) - { - commonLogFormatted(level, message, new Object[] - { - obj1, obj2 - }); - } - - /** - * Logs a formated message. The message itself may contain % - * characters as place holders. This routine will attempt to match - * the placeholder by looking at the type of parameter passed to - * obj1.<p> - * - * If the parameter is an array, it traverses the array first and - * matches parameters sequentially against the array items. - * Otherwise the parameters after <code>message</code> are matched - * in order.<p> - * - * If the place holder matches against a number it is printed as a - * whole number. This can be overridden by specifying a precision - * in the form %n.m where n is the padding for the whole part and - * m is the number of decimal places to display. n can be excluded - * if desired. n and m may not be more than 9.<p> - * - * If the last parameter (after flattening) is a Throwable it is - * logged specially. - * - * @param level One of DEBUG, INFO, WARN, ERROR, FATAL - * @param message The message to log. - * @param obj1 The first object to match against. - * @param obj2 The second object to match against. - * @param obj3 The third object to match against. - */ - public void logFormatted(int level, String message, - Object obj1, Object obj2, - Object obj3) - { - commonLogFormatted(level, message, new Object[] - { - obj1, obj2, obj3 - }); - } - - /** - * Logs a formated message. The message itself may contain % - * characters as place holders. This routine will attempt to match - * the placeholder by looking at the type of parameter passed to - * obj1.<p> - * - * If the parameter is an array, it traverses the array first and - * matches parameters sequentially against the array items. - * Otherwise the parameters after <code>message</code> are matched - * in order.<p> - * - * If the place holder matches against a number it is printed as a - * whole number. This can be overridden by specifying a precision - * in the form %n.m where n is the padding for the whole part and - * m is the number of decimal places to display. n can be excluded - * if desired. n and m may not be more than 9.<p> - * - * If the last parameter (after flattening) is a Throwable it is - * logged specially. - * - * @param level One of DEBUG, INFO, WARN, ERROR, FATAL - * @param message The message to log. - * @param obj1 The first object to match against. - * @param obj2 The second object to match against. - * @param obj3 The third object to match against. - * @param obj4 The forth object to match against. - */ - public void logFormatted(int level, String message, - Object obj1, Object obj2, - Object obj3, Object obj4) - { - commonLogFormatted(level, message, new Object[] - { - obj1, obj2, obj3, obj4 - }); - } - - private void commonLogFormatted(int level, String message, - Object [] unflatParams) - { - - - if (check(level)) - { - Object[] params = flattenArrays(unflatParams); - - if (params[ params.length - 1 ] instanceof Throwable) - { - log(level, StringUtil.format(message, params), - ( Throwable ) params[ params.length - 1 ]); - } - else - { - log(level, StringUtil.format(message, params)); - } + if (params.length > 0 && params[params.length-1] instanceof Throwable) { + log(level, msg, (Throwable)params[params.length-1]); + } else { + log(level, msg); } } /** - * Flattens any contained objects. Only tranverses one level deep. + * Flattens any contained objects. Only traverses one level deep. */ - private Object [] flattenArrays(Object [] objects) - { + private Object[] flattenArrays(Object... unflatParams) { List<Object> results = new ArrayList<Object>(); - - for (int i = 0; i < objects.length; i++) - { - results.addAll(objectToObjectArray(objects[ i ])); + for (Object obj : unflatParams) { + flattenObject(results, obj); } - return results.toArray(new Object[ results.size() ]); + return results.toArray(new Object[results.size()]); } - private List<Object> objectToObjectArray(Object object) - { - List<Object> results = new ArrayList<Object>(); - - if (object instanceof byte []) - { - byte[] array = ( byte [] ) object; - - for (int j = 0; j < array.length; j++) - { - results.add(Byte.valueOf(array[ j ])); + private void flattenObject(List<Object> results, Object object) { + if (object instanceof byte[]) { + for (byte b : (byte[])object) { + results.add(Byte.valueOf(b)); } - } - if (object instanceof char []) - { - char[] array = ( char [] ) object; - - for (int j = 0; j < array.length; j++) - { - results.add(Character.valueOf(array[ j ])); + } else if (object instanceof char[]) { + for (char c : (char[])object) { + results.add(Character.valueOf(c)); } - } - else if (object instanceof short []) - { - short[] array = ( short [] ) object; - - for (int j = 0; j < array.length; j++) - { - results.add(Short.valueOf(array[ j ])); + } else if (object instanceof short[]) { + for (short s : (short[])object) { + results.add(Short.valueOf(s)); } - } - else if (object instanceof int []) - { - int[] array = ( int [] ) object; - - for (int j = 0; j < array.length; j++) - { - results.add(Integer.valueOf(array[ j ])); + } else if (object instanceof int[]) { + for (int i : (int[])object) { + results.add(Integer.valueOf(i)); } - } - else if (object instanceof long []) - { - long[] array = ( long [] ) object; - - for (int j = 0; j < array.length; j++) - { - results.add(Long.valueOf(array[ j ])); + } else if (object instanceof long[]) { + for (long l : (long[])object) { + results.add(Long.valueOf(l)); } - } - else if (object instanceof float []) - { - float[] array = ( float [] ) object; - - for (int j = 0; j < array.length; j++) - { - results.add(new Float(array[ j ])); + } else if (object instanceof float[]) { + for (float f : (float[])object) { + results.add(Float.valueOf(f)); } - } - else if (object instanceof double []) - { - double[] array = ( double [] ) object; - - for (int j = 0; j < array.length; j++) - { - results.add(new Double(array[ j ])); + } else if (object instanceof double[]) { + for (double d : (double[])object) { + results.add(Double.valueOf(d)); } - } - else if (object instanceof Object []) - { - Object[] array = ( Object [] ) object; - - for (int j = 0; j < array.length; j++) - { - results.add(array[ j ]); + } else if (object instanceof Object[]) { + for (Object o : (Object[])object) { + results.add(o); } - } - else - { + } else { results.add(object); } - return results; } } diff --git a/src/java/org/apache/poi/util/StringUtil.java b/src/java/org/apache/poi/util/StringUtil.java index 99880f500e..bd417961ea 100644 --- a/src/java/org/apache/poi/util/StringUtil.java +++ b/src/java/org/apache/poi/util/StringUtil.java @@ -22,6 +22,7 @@ import java.text.FieldPosition; import java.text.NumberFormat; import java.util.HashMap; import java.util.Iterator; +import java.util.Locale; import java.util.Map; import org.apache.poi.hssf.record.RecordInputStream; @@ -310,7 +311,7 @@ public class StringUtil { Number number, String formatting, StringBuffer outputTo) { - NumberFormat numberFormat = NumberFormat.getInstance(); + NumberFormat numberFormat = NumberFormat.getInstance(Locale.US); if ((0 < formatting.length()) && Character.isDigit(formatting.charAt(0))) { numberFormat.setMinimumIntegerDigits( |