Browse Source

[bug-64098] XWPFRun: Whitespace in text not preserved if starting with tab character. Thanks to gjmathews

git-svn-id: https://svn.apache.org/repos/asf/poi/trunk@1873188 13f79535-47bb-0310-9956-ffa450edef68
tags/REL_4_1_2
PJ Fanning 4 years ago
parent
commit
92deb8069d

+ 2
- 1
src/ooxml/java/org/apache/poi/xwpf/usermodel/XWPFRun.java View File

@@ -146,7 +146,8 @@ public class XWPFRun implements ISDTContents, IRunElement, CharacterRun {
*/
static void preserveSpaces(XmlString xs) {
String text = xs.getStringValue();
if (text != null && (text.startsWith(" ") || text.endsWith(" "))) {
if (text != null && text.length() >= 1
&& (Character.isWhitespace(text.charAt(0)) || Character.isWhitespace(text.charAt(text.length()-1)))) {
XmlCursor c = xs.newCursor();
c.toNextToken();
c.insertAttributeWithValue(new QName("http://www.w3.org/XML/1998/namespace", "space"), "preserve");

+ 49
- 23
src/ooxml/testcases/org/apache/poi/xwpf/usermodel/TestXWPFRun.java View File

@@ -16,18 +16,6 @@
==================================================================== */
package org.apache.poi.xwpf.usermodel;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Iterator;
import java.util.List;

import org.apache.poi.openxml4j.exceptions.InvalidFormatException;
import org.apache.poi.util.LocaleUtil;
import org.apache.poi.util.Units;
@@ -40,17 +28,16 @@ import org.junit.Test;
import org.openxmlformats.schemas.drawingml.x2006.main.CTBlip;
import org.openxmlformats.schemas.drawingml.x2006.main.CTBlipFillProperties;
import org.openxmlformats.schemas.drawingml.x2006.picture.CTPicture;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTBr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTOnOff;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STBrClear;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STEm;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STHighlightColor;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STOnOff;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STThemeColor;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STUnderline;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.STVerticalAlignRun;
import org.openxmlformats.schemas.wordprocessingml.x2006.main.*;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.math.BigInteger;
import java.util.Iterator;
import java.util.List;

import static org.junit.Assert.*;

/**
* Tests for XWPF Run
@@ -809,4 +796,43 @@ public class TestXWPFRun {
assertEquals(pic.getDepth(), Units.toPoints(32), 0.0);
}
}

@Test
public void testWhitespace() throws IOException {
String[] text = new String[] {
" The quick brown fox",
"\t\tjumped over the lazy dog"
};
ByteArrayOutputStream bos = new ByteArrayOutputStream();
try (XWPFDocument doc = new XWPFDocument();) {
for(String s : text) {
XWPFParagraph p1 = doc.createParagraph();
XWPFRun r1 = p1.createRun();
r1.setText(s);
}

doc.write(bos);
bos.flush();
}

try (
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
XWPFDocument doc = new XWPFDocument(bis)
) {
List<XWPFParagraph> paragraphs = doc.getParagraphs();
assertEquals(2, paragraphs.size());
for (int i = 0; i < text.length; i++) {
XWPFParagraph p1 = paragraphs.get(i);
String expected = text[i];
assertEquals(expected, p1.getText());
CTP ctp = p1.getCTP();
CTR ctr = ctp.getRArray(0);
CTText ctText = ctr.getTArray(0);
// if text has leading whitespace then expect xml-fragment to have xml:space="preserve" set
// <xml-fragment xml:space="preserve" xmlns:w="http://schemas.openxmlformats.org/wordprocessingml/2006/main">
boolean isWhitespace = Character.isWhitespace(expected.charAt(0));
assertEquals(isWhitespace, ctText.isSetSpace());
}
}
}
}

Loading…
Cancel
Save