From 247f441b31c0b04ba6ec4151cbeaec6acc84349b Mon Sep 17 00:00:00 2001 From: Yegor Kozlov Date: Tue, 27 Jul 2010 05:38:48 +0000 Subject: [PATCH] preserve leading and trailing white spaces in XWPFRun, see bug #49593 git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@979540 13f79535-47bb-0310-9956-ffa450edef68 --- src/documentation/content/xdocs/status.xml | 1 + .../apache/poi/xwpf/usermodel/XWPFRun.java | 21 ++++++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/documentation/content/xdocs/status.xml b/src/documentation/content/xdocs/status.xml index e2a5d1dae9..3d631d457a 100644 --- a/src/documentation/content/xdocs/status.xml +++ b/src/documentation/content/xdocs/status.xml @@ -34,6 +34,7 @@ + 49593 - preserve leading and trailing white spaces in XWPFRun 49455 - Insert the content of fldSimple fields into the XWPFWordTextExtractor output 49640 - Fixed parsing formulas containing defined names beginning with an underscore 49538 - Added implementation for POISSON() diff --git a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java index 6dcaf5c72a..dccd02bfbb 100644 --- a/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java +++ b/src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java @@ -19,6 +19,8 @@ package org.apache.poi.xwpf.usermodel; import java.math.BigInteger; import org.apache.poi.util.Internal; +import org.apache.xmlbeans.XmlString; +import org.apache.xmlbeans.XmlCursor; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBr; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTFonts; import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTHpsMeasure; @@ -35,6 +37,8 @@ import org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STUnderline; import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalAlignRun; +import javax.xml.namespace.QName; + /** * XWPFRun object defines a region of text with a common set of properties * @@ -140,6 +144,7 @@ public class XWPFRun { if(pos > run.sizeOfTArray()) throw new ArrayIndexOutOfBoundsException("Value too large for the parameter position in XWPFRun.setText(String value,int pos)"); CTText t = (pos < run.sizeOfTArray() && pos >= 0) ? run.getTArray(pos) : run.addNewT(); t.setStringValue(value); + preserveSpaces(t); } @@ -472,5 +477,19 @@ public class XWPFRun { //TODO } - + /** + * Add the xml:spaces="preserve" attribute if the string has leading or trailing white spaces + * + * @param xs the string to check + */ + static void preserveSpaces(XmlString 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(); + } + } + } -- 2.39.5