aboutsummaryrefslogtreecommitdiffstats
path: root/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java
diff options
context:
space:
mode:
authorYegor Kozlov <yegor@apache.org>2009-10-28 10:31:34 +0000
committerYegor Kozlov <yegor@apache.org>2009-10-28 10:31:34 +0000
commit8e2028896e95fe0371bd4bfe2b82e8198bf3e2e1 (patch)
tree7abc5b1da5b3b6bee3213282f39e41112423b731 /src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java
parent9743058a89b26423979ae653706be4d29f6b2723 (diff)
downloadpoi-8e2028896e95fe0371bd4bfe2b82e8198bf3e2e1.tar.gz
poi-8e2028896e95fe0371bd4bfe2b82e8198bf3e2e1.zip
preserve leading and trailing white spaces in XSSFRichTextString, see Bugzilla 48070
git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@830492 13f79535-47bb-0310-9956-ffa450edef68
Diffstat (limited to 'src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java')
-rw-r--r--src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java24
1 files changed, 22 insertions, 2 deletions
diff --git a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java
index aa4e87f78e..fdf23ab9a6 100644
--- a/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java
+++ b/src/ooxml/java/org/apache/poi/xssf/usermodel/XSSFRichTextString.java
@@ -20,8 +20,11 @@ package org.apache.poi.xssf.usermodel;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.RichTextString;
import org.apache.poi.xssf.model.StylesTable;
+import org.apache.xmlbeans.XmlObject;
+import org.apache.xmlbeans.XmlCursor;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.*;
+import javax.xml.namespace.QName;
import java.util.ArrayList;
@@ -70,15 +73,16 @@ public class XSSFRichTextString implements RichTextString {
private StylesTable styles;
/**
- * Create a rich text string and initialize it with empty string
+ * Create a rich text string
*/
public XSSFRichTextString(String str) {
st = CTRst.Factory.newInstance();
st.setT(str);
+ preserveSpaces(st.xgetT());
}
/**
- * Create empty rich text string
+ * Create empty rich text string and initialize it with empty string
*/
public XSSFRichTextString() {
st = CTRst.Factory.newInstance();
@@ -342,6 +346,7 @@ public class XSSFRichTextString implements RichTextString {
public void setString(String s){
clearFormatting();
st.setT(s);
+ preserveSpaces(st.xgetT());
}
/**
@@ -461,4 +466,19 @@ public class XSSFRichTextString implements RichTextString {
return ctFont;
}
+
+ /**
+ * Add the xml:spaces="preserve" attribute if the string has leading or trailing white 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();
+ }
+ }
}