You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@poi.apache.org by ye...@apache.org on 2011/06/25 10:45:49 UTC

svn commit: r1139505 - in /poi/trunk/src: documentation/content/xdocs/status.xml ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFRichTextString.java

Author: yegor
Date: Sat Jun 25 08:45:49 2011
New Revision: 1139505

URL: http://svn.apache.org/viewvc?rev=1139505&view=rev
Log:
Bug 48877 - Fixed XSSFRichTextString to respect leading and trailing line breaks

Modified:
    poi/trunk/src/documentation/content/xdocs/status.xml
    poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java
    poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFRichTextString.java

Modified: poi/trunk/src/documentation/content/xdocs/status.xml
URL: http://svn.apache.org/viewvc/poi/trunk/src/documentation/content/xdocs/status.xml?rev=1139505&r1=1139504&r2=1139505&view=diff
==============================================================================
--- poi/trunk/src/documentation/content/xdocs/status.xml (original)
+++ poi/trunk/src/documentation/content/xdocs/status.xml Sat Jun 25 08:45:49 2011
@@ -34,6 +34,7 @@
 
     <changes>
         <release version="3.8-beta4" date="2011-??-??">
+           <action dev="poi-developers" type="fix">48877 - Fixed XSSFRichTextString to respect leading and trailing line breaks </action>
            <action dev="poi-developers" type="fix">49564 - Fixed default behaviour of XSSFCellStyle.getLocked() </action>
            <action dev="poi-developers" type="fix">48314 - Fixed setting column and row breaks in XSSF</action>
            <action dev="poi-developers" type="add">51424 - Ignore exceptions in ParagraphSprmUncompressor</action>

Modified: poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java?rev=1139505&r1=1139504&r2=1139505&view=diff
==============================================================================
--- poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java (original)
+++ poi/trunk/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java Sat Jun 25 08:45:49 2011
@@ -440,17 +440,21 @@ public class XSSFRichTextString implemen
     }
 
     /**
-     * Add the xml:spaces="preserve" attribute if the string has leading or trailing white spaces
+     * Add the xml:spaces="preserve" attribute if the string has leading or trailing spaces
      *
      * @param xs    the string to check
      */
     protected static void preserveSpaces(STXstring xs) {
         String text = xs.getStringValue();
-        if (text != null && (text.startsWith(" ") || text.endsWith(" "))) {
-            XmlCursor c = xs.newCursor();
-            c.toNextToken();
-            c.insertAttributeWithValue(new QName("http://www.w3.org/XML/1998/namespace", "space"), "preserve");
-            c.dispose();
+        if (text != null && text.length() > 0) {
+            char firstChar = text.charAt(0);
+            char lastChar  = text.charAt(text.length() - 1);
+            if(Character.isWhitespace(firstChar) || Character.isWhitespace(lastChar)) {
+                XmlCursor c = xs.newCursor();
+                c.toNextToken();
+                c.insertAttributeWithValue(new QName("http://www.w3.org/XML/1998/namespace", "space"), "preserve");
+                c.dispose();
+            }
         }
     }
 

Modified: poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFRichTextString.java
URL: http://svn.apache.org/viewvc/poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFRichTextString.java?rev=1139505&r1=1139504&r2=1139505&view=diff
==============================================================================
--- poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFRichTextString.java (original)
+++ poi/trunk/src/ooxml/testcases/org/apache/poi/xssf/usermodel/TestXSSFRichTextString.java Sat Jun 25 08:45:49 2011
@@ -17,10 +17,14 @@
 
 package org.apache.poi.xssf.usermodel;
 
+import java.io.IOException;
+import java.io.StringWriter;
 import java.util.TreeMap;
 
 import junit.framework.TestCase;
 
+import org.apache.poi.POIXMLDocumentPart;
+import org.apache.xmlbeans.XmlOptions;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRPrElt;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTRst;
 import org.openxmlformats.schemas.spreadsheetml.x2006.main.STXstring;
@@ -290,4 +294,64 @@ public final class TestXSSFRichTextStrin
         assertEquals("Apache", str.getCTRst().getRArray(0).getT());
         assertEquals(" Software Foundation", str.getCTRst().getRArray(1).getT());
     }
+
+    public void testLineBreaks_bug48877() throws IOException{
+
+        XSSFFont font = new XSSFFont();
+        font.setBoldweight(XSSFFont.BOLDWEIGHT_BOLD);
+        font.setFontHeightInPoints((short) 14);
+        XSSFRichTextString str;
+        STXstring t1, t2, t3;
+
+        str = new XSSFRichTextString("Incorrect\nLine-Breaking");
+        str.applyFont(0, 8, font);
+        t1 = str.getCTRst().getRList().get(0).xgetT();
+        t2 = str.getCTRst().getRList().get(1).xgetT();
+        assertEquals("<xml-fragment>Incorrec</xml-fragment>", t1.xmlText());
+        assertEquals("<xml-fragment>t\nLine-Breaking</xml-fragment>", t2.xmlText());
+
+        str = new XSSFRichTextString("Incorrect\nLine-Breaking");
+        str.applyFont(0, 9, font);
+        t1 = str.getCTRst().getRList().get(0).xgetT();
+        t2 = str.getCTRst().getRList().get(1).xgetT();
+        assertEquals("<xml-fragment>Incorrect</xml-fragment>", t1.xmlText());
+        assertEquals("<xml-fragment xml:space=\"preserve\">\nLine-Breaking</xml-fragment>", t2.xmlText());
+
+        str = new XSSFRichTextString("Incorrect\n Line-Breaking");
+        str.applyFont(0, 9, font);
+        t1 = str.getCTRst().getRList().get(0).xgetT();
+        t2 = str.getCTRst().getRList().get(1).xgetT();
+        assertEquals("<xml-fragment>Incorrect</xml-fragment>", t1.xmlText());
+        assertEquals("<xml-fragment xml:space=\"preserve\">\n Line-Breaking</xml-fragment>", t2.xmlText());
+
+        str = new XSSFRichTextString("Tab\tseparated\n");
+        t1 = str.getCTRst().xgetT();
+        // trailing \n causes must be preserved
+        assertEquals("<xml-fragment xml:space=\"preserve\">Tab\tseparated\n</xml-fragment>", t1.xmlText());
+
+        str.applyFont(0, 3, font);
+        t1 = str.getCTRst().getRList().get(0).xgetT();
+        t2 = str.getCTRst().getRList().get(1).xgetT();
+        assertEquals("<xml-fragment>Tab</xml-fragment>", t1.xmlText());
+        assertEquals("<xml-fragment xml:space=\"preserve\">\tseparated\n</xml-fragment>", t2.xmlText());
+
+        str = new XSSFRichTextString("Tab\tseparated\n");
+        str.applyFont(0, 4, font);
+        t1 = str.getCTRst().getRList().get(0).xgetT();
+        t2 = str.getCTRst().getRList().get(1).xgetT();
+        // YK: don't know why, but XmlBeans converts leading tab characters to spaces
+        //assertEquals("<xml-fragment>Tab\t</xml-fragment>", t1.xmlText());
+        assertEquals("<xml-fragment xml:space=\"preserve\">separated\n</xml-fragment>", t2.xmlText());
+
+        str = new XSSFRichTextString("\n\n\nNew Line\n\n");
+        str.applyFont(0, 3, font);
+        str.applyFont(11, 13, font);
+        t1 = str.getCTRst().getRList().get(0).xgetT();
+        t2 = str.getCTRst().getRList().get(1).xgetT();
+        t3 = str.getCTRst().getRList().get(2).xgetT();
+        // YK: don't know why, but XmlBeans converts leading tab characters to spaces
+        assertEquals("<xml-fragment xml:space=\"preserve\">\n\n\n</xml-fragment>", t1.xmlText());
+        assertEquals("<xml-fragment>New Line</xml-fragment>", t2.xmlText());
+        assertEquals("<xml-fragment xml:space=\"preserve\">\n\n</xml-fragment>", t3.xmlText());
+    }
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@poi.apache.org
For additional commands, e-mail: commits-help@poi.apache.org