Bug 571508 - [Model2Doc][Docx] Line breaks in the model ...

(ex: body of requirements) are not keep in the final document

- Replace line drop and tab by docx compliant chars
- Remove useless carriage return

Change-Id: I7c262f6ff16130153bb01851d64f6b22e94bc96f
Signed-off-by: Yoann FARRE <yoann.farre@cil4sys.com>
diff --git a/plugins/docx/org.eclipse.papyrus.model2doc.docx/src/org/eclipse/papyrus/model2doc/docx/internal/transcription/DocxTranscription.java b/plugins/docx/org.eclipse.papyrus.model2doc.docx/src/org/eclipse/papyrus/model2doc/docx/internal/transcription/DocxTranscription.java
index e5214fc..1927ced 100755
--- a/plugins/docx/org.eclipse.papyrus.model2doc.docx/src/org/eclipse/papyrus/model2doc/docx/internal/transcription/DocxTranscription.java
+++ b/plugins/docx/org.eclipse.papyrus.model2doc.docx/src/org/eclipse/papyrus/model2doc/docx/internal/transcription/DocxTranscription.java
@@ -15,6 +15,7 @@
  *   Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - bug 569817
  *   Pauline DEVILLE (CEA LIST) pauline.deville@cea.fr - Bug 570290
  *   Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - bug 570803
+ *   Yoann Farre (CIL4Sys Engineering) yoann.farre@cil4sys.com - Bug 571508
  *****************************************************************************/
 package org.eclipse.papyrus.model2doc.docx.internal.transcription;
 
@@ -70,6 +71,7 @@
 import org.eclipse.papyrus.model2doc.docx.internal.poi.CustomXWPFTable;
 import org.eclipse.papyrus.model2doc.docx.internal.services.StyleServiceImpl;
 import org.eclipse.papyrus.model2doc.docx.internal.util.ImageUtils;
+import org.eclipse.papyrus.model2doc.docx.internal.util.TextUtils;
 import org.eclipse.papyrus.model2doc.docx.services.StyleService;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTR;
 import org.openxmlformats.schemas.wordprocessingml.x2006.main.CTRPr;
@@ -226,8 +228,7 @@
 	@Override
 	public void writeParagraph(String paragraph, boolean processRichText) {
 		XWPFParagraph p = document.createParagraph();
-		XWPFRun run = p.createRun();
-		run.setText(paragraph);
+		TextUtils.fillParagraph(p, paragraph);
 	}
 
 	@Override
@@ -301,7 +302,10 @@
 				Cell cell = cellIter.next();
 				if (cell instanceof TextCell) {
 					TextCell textCell = (TextCell) cell;
-					xwpfTable.getRow(rowIndex).getCell(cellIndex).setText(textCell.getText());
+					XWPFParagraph paragraph = xwpfTable.getRow(rowIndex).getCell(cellIndex).getParagraphArray(0);
+					if (null != paragraph) {
+						TextUtils.fillParagraph(paragraph, textCell.getText());
+					}
 				} else if (cell instanceof FileReferenceCell) {
 					XWPFTableCell xwpfCell = xwpfTable.getRow(rowIndex).getCell(cellIndex);
 					insertFileInTableCell(((FileReferenceCell) cell).getFileReference(), xwpfCell);
diff --git a/plugins/docx/org.eclipse.papyrus.model2doc.docx/src/org/eclipse/papyrus/model2doc/docx/internal/util/TextUtils.java b/plugins/docx/org.eclipse.papyrus.model2doc.docx/src/org/eclipse/papyrus/model2doc/docx/internal/util/TextUtils.java
new file mode 100644
index 0000000..9e751d2
--- /dev/null
+++ b/plugins/docx/org.eclipse.papyrus.model2doc.docx/src/org/eclipse/papyrus/model2doc/docx/internal/util/TextUtils.java
@@ -0,0 +1,85 @@
+/*****************************************************************************
+ * Copyright (c) 2021 CEA LIST and others.
+ *
+ * All rights reserved. This program and the accompanying materials
+ * are made available under the terms of the Eclipse Public License 2.0
+ * which accompanies this distribution, and is available at
+ * http://www.eclipse.org/legal/epl-2.0/
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ *
+ * Contributors:
+ *   Yoann Farre (CIL4Sys Engineering) - Initial API and implementation
+ *
+ *****************************************************************************/
+
+package org.eclipse.papyrus.model2doc.docx.internal.util;
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.apache.poi.xwpf.usermodel.XWPFParagraph;
+import org.apache.poi.xwpf.usermodel.XWPFRun;
+
+/**
+ * The purpose of this class is to manage white characters as tab and line break (Bug 571508)
+ */
+public class TextUtils {
+
+	private static final String DROP_LINE = "\n"; //$NON-NLS-1$
+
+	private static final String TAB = "\t"; //$NON-NLS-1$
+
+	private static final List<String> CHARS_TO_CONVERT = Arrays.asList(DROP_LINE, TAB);
+
+	/**
+	 * Fill the paragraph with the given text and convert special chars to docx compliant chars.
+	 *
+	 * @param p
+	 * @param text
+	 */
+	public static void fillParagraph(XWPFParagraph p, String text) {
+		if (text != null) {
+			String subString = text;
+
+			while (!subString.isEmpty()) {
+				int min = -1;
+				String splitDelimiter = ""; //$NON-NLS-1$
+				for (String s : CHARS_TO_CONVERT) {
+					String[] replacement = subString.split(s, 2);
+					if (min == -1) {
+						min = replacement[0].length();
+						splitDelimiter = s;
+					} else if (min != -1 && replacement[0].length() < min) {
+						min = replacement[0].length();
+						splitDelimiter = s;
+					}
+				}
+
+				String[] splittedString = subString.split(splitDelimiter, 2);
+				String firstPart = splittedString[0];
+				XWPFRun newRun = p.createRun();
+				newRun.setText(firstPart);
+
+				if (splittedString.length > 1) {
+					switch (splitDelimiter) {
+					case DROP_LINE:
+						newRun.addBreak();
+						break;
+					case TAB:
+						newRun.addTab();
+						break;
+					default:
+						break;
+					}
+				}
+
+				if (splittedString.length > 1) {
+					subString = splittedString[1];
+				} else {
+					subString = ""; //$NON-NLS-1$
+				}
+			}
+		}
+	}
+}