<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>
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;
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
*
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);
}
//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();
+ }
+ }
+
}