incubator-odf-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From fh...@apache.org
Subject svn commit: r1546302 - in /incubator/odf/trunk: CHANGES.txt simple/src/main/java/org/odftoolkit/simple/text/Paragraph.java simple/src/test/java/org/odftoolkit/simple/text/ParagraphTest.java simple/src/test/resources/Bell.odt
Date Thu, 28 Nov 2013 07:30:53 GMT
Author: fhopf
Date: Thu Nov 28 07:30:52 2013
New Revision: 1546302

URL: http://svn.apache.org/r1546302
Log:
ODFTOOLKIT-379: Preventing NullPointerException in Paragraph.getTextContent(). Patch by Marc
Schreiber

Added:
    incubator/odf/trunk/simple/src/test/resources/Bell.odt   (with props)
Modified:
    incubator/odf/trunk/CHANGES.txt
    incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/text/Paragraph.java
    incubator/odf/trunk/simple/src/test/java/org/odftoolkit/simple/text/ParagraphTest.java

Modified: incubator/odf/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/incubator/odf/trunk/CHANGES.txt?rev=1546302&r1=1546301&r2=1546302&view=diff
==============================================================================
--- incubator/odf/trunk/CHANGES.txt (original)
+++ incubator/odf/trunk/CHANGES.txt Thu Nov 28 07:30:52 2013
@@ -40,6 +40,7 @@ Bug Fixes
 * ODFTOOLKIT-355 NullPointerException when removing rows from a new Spreadsheet table (Florian
Hopf)
 * ODFTOOLKIT-362 TextSelection.replaceWith() ends in an infinite loop (Florian Hopf)
 * ODFTOOLKIT-363 Table#appendRow() method creates a row without any cell (Florian Hopf)
+* ODFTOOLKIT-379 NullPointerException in Paragraph.getTextContent() (Florian Hopf, Patch
by Marc Schreiber)
 
 Other
 -----

Modified: incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/text/Paragraph.java
URL: http://svn.apache.org/viewvc/incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/text/Paragraph.java?rev=1546302&r1=1546301&r2=1546302&view=diff
==============================================================================
--- incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/text/Paragraph.java (original)
+++ incubator/odf/trunk/simple/src/main/java/org/odftoolkit/simple/text/Paragraph.java Thu
Nov 28 07:30:52 2013
@@ -226,7 +226,7 @@ public class Paragraph extends Component
 	}
 
 	static String getTextContent(OdfElement ownerEle) {
-		StringBuffer buffer = new StringBuffer();
+		StringBuilder buffer = new StringBuilder();
 		NodeList nodeList = ownerEle.getChildNodes();
 		int i;
 		for (i = 0; i < nodeList.getLength(); i++) {
@@ -235,9 +235,12 @@ public class Paragraph extends Component
 			if (node.getNodeType() == Node.TEXT_NODE)
 				buffer.append(node.getNodeValue());
 			else if (node.getNodeType() == Node.ELEMENT_NODE) {
-				if (node.getNodeName().equals("text:s")) {
-					int count = ((TextSElement) node).getTextCAttribute();
-					for (int j = 0; j < count; j++)
+				if (node instanceof TextSpanElement) {
+					buffer.append(((TextSpanElement) node).getTextContent());
+				}
+				else if (node.getNodeName().equals("text:s")) {
+					Integer count = ((TextSElement) node).getTextCAttribute();
+					for (int j = 0; j < (count != null ? count : 0); j++)
 						buffer.append(' ');
 				} else if (node.getNodeName().equals("text:tab"))
 					buffer.append('\t');

Modified: incubator/odf/trunk/simple/src/test/java/org/odftoolkit/simple/text/ParagraphTest.java
URL: http://svn.apache.org/viewvc/incubator/odf/trunk/simple/src/test/java/org/odftoolkit/simple/text/ParagraphTest.java?rev=1546302&r1=1546301&r2=1546302&view=diff
==============================================================================
--- incubator/odf/trunk/simple/src/test/java/org/odftoolkit/simple/text/ParagraphTest.java
(original)
+++ incubator/odf/trunk/simple/src/test/java/org/odftoolkit/simple/text/ParagraphTest.java
Thu Nov 28 07:30:52 2013
@@ -19,6 +19,7 @@ under the License.
 
 package org.odftoolkit.simple.text;
 
+import java.io.FileNotFoundException;
 import java.net.URI;
 import java.util.Iterator;
 import java.util.logging.Level;
@@ -310,4 +311,24 @@ public class ParagraphTest {
 			Assert.fail();
 		}
 	}
+        
+	@Test
+	public void testGetTextContentWithLibreOfficeFile() throws FileNotFoundException, Exception
{
+		TextDocument document = TextDocument.loadDocument(ResourceUtilities.getAbsolutePath("Bell.odt"));
+		Paragraph paragraph = document.getParagraphByIndex(0, true);
+
+		Assert.assertEquals(
+			"Bell, based in Los Angeles, makes and distributes electronic articles, "
+			+ "computer and building products.", 
+			paragraph.getTextContent());
+	}
+        
+	@Test
+	public void shouldReturnMultipleLines() throws Exception {
+		final String expected = "Hello\tWorld!\twho \t ever read this, is stupid.\n\n\nReally!";
+		TextDocument document = TextDocument.newTextDocument();
+		Paragraph p = document.addParagraph(expected);
+		String textContent = p.getTextContent();
+		Assert.assertEquals(expected, textContent.replace("\r", ""));
+	}
 }

Added: incubator/odf/trunk/simple/src/test/resources/Bell.odt
URL: http://svn.apache.org/viewvc/incubator/odf/trunk/simple/src/test/resources/Bell.odt?rev=1546302&view=auto
==============================================================================
Binary file - no diff available.

Propchange: incubator/odf/trunk/simple/src/test/resources/Bell.odt
------------------------------------------------------------------------------
    svn:mime-type = application/octet-stream



Mime
View raw message