Author: jukka Date: Fri Dec 21 01:01:07 2007 New Revision: 606139 URL: http://svn.apache.org/viewvc?rev=606139&view=rev Log: TIKA-104 - Add utility methods to throw IOException with the caused intialized - Added an IOException subclass instead - Adapted code from Niall Pemberton's patch
Added: incubator/tika/trunk/src/main/java/org/apache/tika/exception/CauseIOException.java incubator/tika/trunk/src/test/java/org/apache/tika/exception/ incubator/tika/trunk/src/test/java/org/apache/tika/exception/CauseIOExceptionTest.java Modified: incubator/tika/trunk/CHANGES.txt incubator/tika/trunk/src/main/java/org/apache/tika/parser/pdf/PDF2XHTML.java incubator/tika/trunk/src/main/java/org/apache/tika/sax/AppendableAdaptor.java Modified: incubator/tika/trunk/CHANGES.txt URL: http://svn.apache.org/viewvc/incubator/tika/trunk/CHANGES.txt?rev=606139&r1=606138&r2=606139&view=diff ============================================================================== --- incubator/tika/trunk/CHANGES.txt (original) +++ incubator/tika/trunk/CHANGES.txt Fri Dec 21 01:01:07 2007 @@ -133,5 +133,7 @@ 60. TIKA-102 - Parser implementations loading a large amount of content into a single String could be problematic (Niall Pemberton) - 61. TIKA-107 - Remove use of assertions for argument checking (Niall Pemberton) - \ No newline at end of file +61. TIKA-107 - Remove use of assertions for argument checking (Niall Pemberton) + +62. TIKA-104 - Add utility methods to throw IOException with the caused + intialized (jukka & Niall Pemberton) Added: incubator/tika/trunk/src/main/java/org/apache/tika/exception/CauseIOException.java URL: http://svn.apache.org/viewvc/incubator/tika/trunk/src/main/java/org/apache/tika/exception/CauseIOException.java?rev=606139&view=auto ============================================================================== --- incubator/tika/trunk/src/main/java/org/apache/tika/exception/CauseIOException.java (added) +++ incubator/tika/trunk/src/main/java/org/apache/tika/exception/CauseIOException.java Fri Dec 21 01:01:07 2007 @@ -0,0 +1,51 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tika.exception; + +/** + * IOException subclass with the [EMAIL PROTECTED] Throwable} constructor that's + * missing before Java 6. + */ +public class CauseIOException extends java.io.IOException { + + /** + * Creates an IOException with the given message. + * + * @param message exception message + */ + public CauseIOException(String message) { + super(message); + } + + /** + * Creates an IOException with the given message and root cause. + * <p> + * This constructor was not added in the underlying + * [EMAIL PROTECTED] java.io.IOException) class until Java 6. + * This is a convenience method which uses the + * [EMAIL PROTECTED] #IOException(String)} constructor and calls the + * [EMAIL PROTECTED] #initCause(Throwable)} method to set the root cause. + * + * @param message exception message + * @param cause root cause + */ + public CauseIOException(String message, Throwable cause) { + this(message); + initCause(cause); + } + +} Modified: incubator/tika/trunk/src/main/java/org/apache/tika/parser/pdf/PDF2XHTML.java URL: http://svn.apache.org/viewvc/incubator/tika/trunk/src/main/java/org/apache/tika/parser/pdf/PDF2XHTML.java?rev=606139&r1=606138&r2=606139&view=diff ============================================================================== --- incubator/tika/trunk/src/main/java/org/apache/tika/parser/pdf/PDF2XHTML.java (original) +++ incubator/tika/trunk/src/main/java/org/apache/tika/parser/pdf/PDF2XHTML.java Fri Dec 21 01:01:07 2007 @@ -18,6 +18,7 @@ import java.io.IOException; +import org.apache.tika.exception.CauseIOException; import org.apache.tika.exception.TikaException; import org.apache.tika.metadata.Metadata; import org.apache.tika.sax.XHTMLContentHandler; @@ -70,7 +71,7 @@ try { handler.startDocument(); } catch (SAXException e) { - throw new IOException("Unable to start a document: reason: "+e.getMessage()); + throw new CauseIOException("Unable to start a document", e); } } @@ -78,7 +79,7 @@ try { handler.endDocument(); } catch (SAXException e) { - throw new IOException("Unable to end a document: reason: "+e.getMessage()); + throw new CauseIOException("Unable to end a document", e); } } @@ -86,7 +87,7 @@ try { handler.startElement("div"); } catch (SAXException e) { - throw new IOException("Unable to start a page: reason: "+e.getMessage()); + throw new CauseIOException("Unable to start a page", e); } } @@ -94,7 +95,7 @@ try { handler.endElement("div"); } catch (SAXException e) { - throw new IOException("Unable to end a page: reason: "+e.getMessage()); + throw new CauseIOException("Unable to end a page", e); } } @@ -102,7 +103,7 @@ try { handler.startElement("p"); } catch (SAXException e) { - throw new IOException("Unable to start a paragraph: reason: "+e.getMessage()); + throw new CauseIOException("Unable to start a paragraph", e); } } @@ -110,7 +111,7 @@ try { handler.endElement("p"); } catch (SAXException e) { - throw new IOException("Unable to end a paragraph: reason: "+e.getMessage()); + throw new CauseIOException("Unable to end a paragraph", e); } } @@ -118,7 +119,8 @@ try { handler.characters(text.getCharacter()); } catch (SAXException e) { - throw new IOException("Unable to write a newline: reason: "+e.getMessage()); + throw new CauseIOException( + "Unable to write a character: " + text.getCharacter(), e); } } @@ -126,7 +128,7 @@ try { handler.characters("\n"); } catch (SAXException e) { - throw new IOException("Unable to write a newline: reason: "+e.getMessage()); + throw new CauseIOException("Unable to write a newline", e); } } @@ -135,7 +137,7 @@ try { handler.characters(" "); } catch (SAXException e) { - throw new IOException("Unable to write a space: reason: "+e.getMessage()); + throw new CauseIOException("Unable to write a space", e); } } Modified: incubator/tika/trunk/src/main/java/org/apache/tika/sax/AppendableAdaptor.java URL: http://svn.apache.org/viewvc/incubator/tika/trunk/src/main/java/org/apache/tika/sax/AppendableAdaptor.java?rev=606139&r1=606138&r2=606139&view=diff ============================================================================== --- incubator/tika/trunk/src/main/java/org/apache/tika/sax/AppendableAdaptor.java (original) +++ incubator/tika/trunk/src/main/java/org/apache/tika/sax/AppendableAdaptor.java Fri Dec 21 01:01:07 2007 @@ -17,6 +17,8 @@ package org.apache.tika.sax; import java.io.IOException; + +import org.apache.tika.exception.CauseIOException; import org.xml.sax.ContentHandler; import org.xml.sax.SAXException; @@ -97,7 +99,9 @@ char[] chars = charSeq.toString().toCharArray(); handler.characters(chars, start, (end - start)); } catch (SAXException e) { - throw new IOException(e.toString()); + throw new CauseIOException( + "Error processing character content: " + + charSeq.subSequence(start, end), e); } } return this; Added: incubator/tika/trunk/src/test/java/org/apache/tika/exception/CauseIOExceptionTest.java URL: http://svn.apache.org/viewvc/incubator/tika/trunk/src/test/java/org/apache/tika/exception/CauseIOExceptionTest.java?rev=606139&view=auto ============================================================================== --- incubator/tika/trunk/src/test/java/org/apache/tika/exception/CauseIOExceptionTest.java (added) +++ incubator/tika/trunk/src/test/java/org/apache/tika/exception/CauseIOExceptionTest.java Fri Dec 21 01:01:07 2007 @@ -0,0 +1,46 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.tika.exception; + +import junit.framework.TestCase; + +/** + * Unit tests for the [EMAIL PROTECTED] CauseIOException} class. + */ +public class CauseIOExceptionTest extends TestCase { + + /** + * Test for the [EMAIL PROTECTED] CauseIOException#IOException(String)} constructor. + */ + public void testIOExceptionString() { + CauseIOException exception = new CauseIOException("message"); + assertEquals("message", exception.getMessage()); + assertNull(exception.getCause()); + } + + /** + * Test for the [EMAIL PROTECTED] IOException#IOException(String, Throwable))} + * constructor. + */ + public void testIOExceptionStringThrowable() { + Throwable cause = new IllegalArgumentException("cause"); + CauseIOException exception = new CauseIOException("message", cause); + assertEquals("message", exception.getMessage()); + assertEquals(cause, exception.getCause()); + } + +}