You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tika.apache.org by ju...@apache.org on 2007/11/26 00:12:06 UTC

svn commit: r598086 - in /incubator/tika/trunk/src: main/java/org/apache/tika/sax/AppendableAdaptor.java test/java/org/apache/tika/sax/ test/java/org/apache/tika/sax/AppendableAdaptorTest.java

Author: jukka
Date: Sun Nov 25 15:12:06 2007
New Revision: 598086

URL: http://svn.apache.org/viewvc?rev=598086&view=rev
Log:
TIKA-102 - Parser implementations loading a large amount of content into a single String could be problematic
    - Forgot to include the new files in Niall Pemberton's patch

Added:
    incubator/tika/trunk/src/main/java/org/apache/tika/sax/AppendableAdaptor.java   (with props)
    incubator/tika/trunk/src/test/java/org/apache/tika/sax/
    incubator/tika/trunk/src/test/java/org/apache/tika/sax/AppendableAdaptorTest.java   (with props)

Added: 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=598086&view=auto
==============================================================================
--- incubator/tika/trunk/src/main/java/org/apache/tika/sax/AppendableAdaptor.java (added)
+++ incubator/tika/trunk/src/main/java/org/apache/tika/sax/AppendableAdaptor.java Sun Nov 25 15:12:06 2007
@@ -0,0 +1,105 @@
+/**
+ * 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.sax;
+
+import java.io.IOException;
+import org.xml.sax.ContentHandler;
+import org.xml.sax.SAXException;
+
+/**
+ * Adaptor which turns a {@link ContentHandler} into an {@link Appendable}.
+ *
+ * @see ContentHandler
+ * @version $Revision$
+ */
+public class AppendableAdaptor implements Appendable {
+
+    /**
+     * Decorated SAX event handler.
+     */
+    private final ContentHandler handler;
+
+    /**
+     * Creates a adaptor for the given SAX event handler.
+     *
+     * @param handler SAX event handler to be decorated, throws
+     * {@link IllegalArgumentException} if null.
+     */
+    public AppendableAdaptor(ContentHandler handler) {
+        if (handler == null) {
+            throw new IllegalArgumentException("Content Handler is missing");
+        }
+        this.handler = handler;
+    }
+
+    /**
+     * Return the content handler.
+     *
+     * @return The content handler
+     */
+    public ContentHandler getHandler() {
+        return handler;
+    }
+
+    /**
+     * Write a single character to the underling content handler.
+     *
+     * @param c The character to write
+     * @return This appendabale instance
+     * @throws IOException if an error occurs writing to the content handler
+     */
+    public Appendable append(char c) throws IOException {
+        return append(Character.toString(c));
+    }
+
+    /**
+     * Write a character sequence to the underling content handler.
+     *
+     * @param charSeq The sequence of characters, ignored if null
+     * @return This appendabale instance
+     * @throws IOException if an error occurs writing to the content handler
+     */
+    public Appendable append(CharSequence charSeq) throws IOException {
+        if (charSeq != null) {
+            append(charSeq, 0, charSeq.length());
+        }
+        return this;
+    }
+
+    /**
+     * Write the specified characters to the underling content handler.
+     *
+     * @param charSeq The sequence of characters, ignored if null
+     * @param start The starting index of the characters to write
+     * @param end The index of the last character +1 to write
+     * @return This appendabale instance
+     * @throws IOException if a {@link SAXException} occurs writing to the
+     *  content handler
+     */
+    public Appendable append(CharSequence charSeq, int start, int end)
+        throws IOException {
+        if (charSeq != null) {
+            try {
+                char[] chars = charSeq.toString().toCharArray();
+                handler.characters(chars, start, (end - start));
+            } catch (SAXException e) {
+                throw new IOException(e.toString());
+            }
+        }
+        return this;
+    }
+}

Propchange: incubator/tika/trunk/src/main/java/org/apache/tika/sax/AppendableAdaptor.java
------------------------------------------------------------------------------
    svn:eol-style = native

Added: incubator/tika/trunk/src/test/java/org/apache/tika/sax/AppendableAdaptorTest.java
URL: http://svn.apache.org/viewvc/incubator/tika/trunk/src/test/java/org/apache/tika/sax/AppendableAdaptorTest.java?rev=598086&view=auto
==============================================================================
--- incubator/tika/trunk/src/test/java/org/apache/tika/sax/AppendableAdaptorTest.java (added)
+++ incubator/tika/trunk/src/test/java/org/apache/tika/sax/AppendableAdaptorTest.java Sun Nov 25 15:12:06 2007
@@ -0,0 +1,129 @@
+/**
+ * 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.sax;
+
+import java.io.IOException;
+import java.io.StringWriter;
+import org.apache.tika.sax.WriteOutContentHandler;
+import junit.framework.TestCase;
+
+/**
+ * Test case for {@link AppendableAdaptor}.
+ *
+ * @version $Revision$
+ */
+public class AppendableAdaptorTest extends TestCase {
+
+    /**
+     * Test {@link AppendableAdaptor#append(char)}.
+     */
+    public void testAppendChar() {
+        StringWriter writer = new StringWriter();
+        WriteOutContentHandler handler = new WriteOutContentHandler(writer);
+        Appendable appendable = new AppendableAdaptor(handler);
+
+        try {
+            appendable.append('F').append('o').append('o');
+        } catch (Throwable t) {
+            fail("Threw: " + t);
+        }
+        assertEquals("Foo", writer.toString());
+    }
+
+    /**
+     * Test {@link AppendableAdaptor#append(String)}.
+     */
+    public void testAppendString() {
+        StringWriter writer = new StringWriter();
+        WriteOutContentHandler handler = new WriteOutContentHandler(writer);
+        Appendable appendable = new AppendableAdaptor(handler);
+
+        try {
+            appendable.append("Foo").append("Bar");
+        } catch (Throwable t) {
+            fail("Threw: " + t);
+        }
+        assertEquals("FooBar", writer.toString());
+    }
+
+    /**
+     * Test {@link AppendableAdaptor#append(String)}.
+     */
+    public void testAppendStringBuilder() {
+        StringWriter writer = new StringWriter();
+        WriteOutContentHandler handler = new WriteOutContentHandler(writer);
+        Appendable appendable = new AppendableAdaptor(handler);
+
+        try {
+            appendable.append(new StringBuilder("Foo"))
+                      .append(new StringBuilder("Bar"));
+        } catch (Throwable t) {
+            fail("Threw: " + t);
+        }
+        assertEquals("FooBar", writer.toString());
+    }
+
+    /**
+     * Test {@link AppendableAdaptor#append(String, int, int)}.
+     */
+    public void testAppendPortion() {
+        StringWriter writer = new StringWriter();
+        WriteOutContentHandler handler = new WriteOutContentHandler(writer);
+        Appendable appendable = new AppendableAdaptor(handler);
+
+        try {
+            appendable.append("12345", 1, 3).append("ABC", 2, 3);
+        } catch (Throwable t) {
+            fail("Threw: " + t);
+        }
+        assertEquals("23C", writer.toString());
+    }
+
+    /**
+     * Test errors
+     */
+    public void testErrors() {
+        try {
+            new AppendableAdaptor(null);
+            fail("Expected IllegalArgumentException");
+        } catch (IllegalArgumentException e) {
+            // expected result
+        }
+
+        WriteOutContentHandler handler = new WriteOutContentHandler(new StringWriter());
+        Appendable appendable = new AppendableAdaptor(handler);
+
+        try {
+            appendable.append("123", 2, 8);
+            fail("End too big, expected IndexOutOfBoundsException");
+        } catch (IOException e) {
+            fail("Threw: " + e);
+        } catch (IndexOutOfBoundsException e) {
+            // expected result
+        }
+
+        try {
+            appendable.append("123", 5, 3);
+            fail("Start too big, expected IndexOutOfBoundsException");
+        } catch (IOException e) {
+            fail("Threw: " + e);
+        } catch (IndexOutOfBoundsException e) {
+            // expected result
+        }
+    }
+
+}

Propchange: incubator/tika/trunk/src/test/java/org/apache/tika/sax/AppendableAdaptorTest.java
------------------------------------------------------------------------------
    svn:eol-style = native