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
This commit is contained in:
Yegor Kozlov 2010-07-27 05:38:48 +00:00
parent 84811da73d
commit d264f92d24
2 changed files with 21 additions and 1 deletions

View File

@ -34,6 +34,7 @@
<changes>
<release version="3.7-beta2" date="2010-??-??">
<action dev="POI-DEVELOPERS" type="add">49593 - preserve leading and trailing white spaces in XWPFRun</action>
<action dev="POI-DEVELOPERS" type="add">49455 - Insert the content of fldSimple fields into the XWPFWordTextExtractor output</action>
<action dev="POI-DEVELOPERS" type="add">49640 - Fixed parsing formulas containing defined names beginning with an underscore</action>
<action dev="POI-DEVELOPERS" type="add">49538 - Added implementation for POISSON()</action>

View File

@ -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();
}
}
}