Bug 570803: [Model2Doc][Docx] Document Generation with space in the Eclipse location doesn't work

Change-Id: Ieefbfcd2963a831c3af52776ebedd809517e170d
Signed-off-by: Vincent Lorenzo <vincent.lorenzo@cea.fr>
diff --git a/plugins/core/org.eclipse.papyrus.model2doc.core.builtintypes/src-api/org/eclipse/papyrus/model2doc/core/builtintypes/accessors/IInputFileAccessor.java b/plugins/core/org.eclipse.papyrus.model2doc.core.builtintypes/src-api/org/eclipse/papyrus/model2doc/core/builtintypes/accessors/IInputFileAccessor.java
index c3f5329..c5aef86 100755
--- a/plugins/core/org.eclipse.papyrus.model2doc.core.builtintypes/src-api/org/eclipse/papyrus/model2doc/core/builtintypes/accessors/IInputFileAccessor.java
+++ b/plugins/core/org.eclipse.papyrus.model2doc.core.builtintypes/src-api/org/eclipse/papyrus/model2doc/core/builtintypes/accessors/IInputFileAccessor.java
@@ -1,5 +1,5 @@
 /*****************************************************************************
- * Copyright (c) 2020 CEA LIST and others.
+ * Copyright (c) 2020, 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
@@ -9,8 +9,8 @@
  * SPDX-License-Identifier: EPL-2.0
  *
  * Contributors:
- *  Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation
- *
+ *   Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation
+ *   Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - bug 570803
  *****************************************************************************/
 
 package org.eclipse.papyrus.model2doc.core.builtintypes.accessors;
@@ -37,4 +37,16 @@
 	 *         the platform URI for the file
 	 */
 	public URI createInputFilePlatformURI();
+
+	/**
+	 * This method converts the URL into string, removing the prefix "file:" and decoding the path if required
+	 *
+	 * @param url
+	 *            the url
+	 * @param decode
+	 *            if <code>true</code>, we replace the %20 by the space char
+	 * @return
+	 *         the path string corresponding to the URL
+	 */
+	public String urlToPathString(final URL url, final boolean decode);
 }
diff --git a/plugins/core/org.eclipse.papyrus.model2doc.core.builtintypes/src/org/eclipse/papyrus/model2doc/core/builtintypes/internal/accessors/AbstractFileAccessor.java b/plugins/core/org.eclipse.papyrus.model2doc.core.builtintypes/src/org/eclipse/papyrus/model2doc/core/builtintypes/internal/accessors/AbstractFileAccessor.java
index 9b8edd6..efe59ca 100755
--- a/plugins/core/org.eclipse.papyrus.model2doc.core.builtintypes/src/org/eclipse/papyrus/model2doc/core/builtintypes/internal/accessors/AbstractFileAccessor.java
+++ b/plugins/core/org.eclipse.papyrus.model2doc.core.builtintypes/src/org/eclipse/papyrus/model2doc/core/builtintypes/internal/accessors/AbstractFileAccessor.java
@@ -1,5 +1,5 @@
 /*****************************************************************************
- * Copyright (c) 2020 CEA LIST and others.
+ * Copyright (c) 2020, 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
@@ -10,7 +10,7 @@
  *
  * Contributors:
  *  Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation
- *
+ *  Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - bug 570803
  *****************************************************************************/
 
 package org.eclipse.papyrus.model2doc.core.builtintypes.internal.accessors;
@@ -48,6 +48,11 @@
 	protected static final String ENCODED_SPACE = "%20"; //$NON-NLS-1$
 
 	/**
+	 * the file prefix
+	 */
+	private static final String FILE_PREFIX = "file:/";//$NON-NLS-1$
+
+	/**
 	 * the {@link EObject} referencing a file
 	 */
 	private final T eobject;
@@ -191,4 +196,22 @@
 		}
 		return null;
 	}
+
+	/**
+	 * This method converts the URL into string, removing the prefix "file:" and decoding the path if required
+	 *
+	 * @param url
+	 *            the url
+	 * @param decode
+	 *            if <code>true</code>, we replace the %20 by the space char
+	 * @return
+	 *         the path string corresponding to the URL
+	 */
+	public String urlToPathString(final URL url, final boolean decode) {
+		String path = url.toString().replaceFirst(FILE_PREFIX, ""); //$NON-NLS-1$
+		if (decode) {
+			path = path.replace(ENCODED_SPACE, SPACE);
+		}
+		return path;
+	}
 }
diff --git a/plugins/core/org.eclipse.papyrus.model2doc.core.generatorconfiguration/src-api/org/eclipse/papyrus/model2doc/core/generatorconfiguration/accessors/IOutputFileAccessor.java b/plugins/core/org.eclipse.papyrus.model2doc.core.generatorconfiguration/src-api/org/eclipse/papyrus/model2doc/core/generatorconfiguration/accessors/IOutputFileAccessor.java
index b54349b..b7b6014 100755
--- a/plugins/core/org.eclipse.papyrus.model2doc.core.generatorconfiguration/src-api/org/eclipse/papyrus/model2doc/core/generatorconfiguration/accessors/IOutputFileAccessor.java
+++ b/plugins/core/org.eclipse.papyrus.model2doc.core.generatorconfiguration/src-api/org/eclipse/papyrus/model2doc/core/generatorconfiguration/accessors/IOutputFileAccessor.java
@@ -1,5 +1,5 @@
 /*****************************************************************************
- * Copyright (c) 2020 CEA LIST and others.
+ * Copyright (c) 2020, 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
@@ -10,7 +10,7 @@
  *
  * Contributors:
  *   Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation
- *
+ *   Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - bug 570803
  *****************************************************************************/
 
 package org.eclipse.papyrus.model2doc.core.generatorconfiguration.accessors;
@@ -82,6 +82,18 @@
 	public URL convertToURL(final URI uri);
 
 	/**
+	 * This method converts the URL into string, removing the prefix "file:" and decoding the path if required
+	 *
+	 * @param url
+	 *            the url
+	 * @param decode
+	 *            if <code>true</code>, we replace the %20 by the space char
+	 * @return
+	 *         the path string corresponding to the URL
+	 */
+	public String urlToPathString(final URL url, final boolean decode);
+
+	/**
 	 *
 	 * @return
 	 *         the name of the project impacted by the generation
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 9412bff..e5214fc 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
@@ -14,7 +14,7 @@
  * 	 Pauline DEVILLE (CEA LIST) pauline.deville@cea.fr - Bug 569249
  *   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
  *****************************************************************************/
 package org.eclipse.papyrus.model2doc.docx.internal.transcription;
 
@@ -55,6 +55,7 @@
 import org.eclipse.papyrus.model2doc.core.builtintypes.Row;
 import org.eclipse.papyrus.model2doc.core.builtintypes.TextCell;
 import org.eclipse.papyrus.model2doc.core.builtintypes.TextListItem;
+import org.eclipse.papyrus.model2doc.core.builtintypes.accessors.IInputFileAccessor;
 import org.eclipse.papyrus.model2doc.core.generatorconfiguration.IDocumentGeneratorConfiguration;
 import org.eclipse.papyrus.model2doc.core.generatorconfiguration.accessors.IOutputFileAccessor;
 import org.eclipse.papyrus.model2doc.core.styles.BooleanNamedStyle;
@@ -130,16 +131,17 @@
 		final URL templateURL = this.docxGeneratorConfig.createTemplateFileInputAccessor().createInputFileURL();
 
 		if (templateURL != null) {
-			URL destURL = this.docxGeneratorConfig.createDocumentOutputAccessor().createOutputFileURL(docxGeneratorConfig.getDocumentName(), DOCX_FILE_EXTENTION);
-			String destURI = destURL.getPath().replaceFirst(ECORE_FILE_PREFIX, EMPTY_STRING);
+			final IOutputFileAccessor outputAccessor = this.docxGeneratorConfig.createDocumentOutputAccessor();
+			final URL destURL = outputAccessor.createOutputFileURL(docxGeneratorConfig.getDocumentName(), DOCX_FILE_EXTENTION);
 
+			String destURI = outputAccessor.urlToPathString(destURL, true);
 			// create intermediate folder when they don't exist.
 			// TODO : make common method with image generation and odt
 			// TODO : what to do when dest project doesn't yet exist ?
-			final Path imagePath = new Path(destURI);
+			final Path targetPath = new Path(destURI);
 			// we check all folders tree already exists, and we create them if not
-			if (imagePath.segmentCount() > 1) {
-				final IPath folderPath = imagePath.removeLastSegments(1);
+			if (targetPath.segmentCount() > 1) {
+				final IPath folderPath = targetPath.removeLastSegments(1);
 				final File folder = folderPath.toFile();
 				if (false == folder.exists()) {
 					folder.mkdirs();
@@ -413,8 +415,9 @@
 	@Override
 	public void insertFile(IFileReference fileReference) {
 		try {
-			final URL url = fileReference.getFileAccessor().createInputFileURL();
-			final String path = url.toString().replace(ECORE_FILE_PREFIX, EMPTY_STRING);
+			final IInputFileAccessor accessor = fileReference.getFileAccessor();
+			final URL url = accessor.createInputFileURL();
+			final String path = accessor.urlToPathString(url, true);
 			document.insertFile(path);
 		} catch (Exception e) {
 			Activator.log.warn(NLS.bind("The {0} file can not be inserted", fileReference.getFilePath())); //$NON-NLS-1$
@@ -430,9 +433,10 @@
 	 *            the cell receiving the file
 	 */
 	private void insertFileInTableCell(IFileReference fileReference, XWPFTableCell xwpfCell) {
-		final URL url = fileReference.getFileAccessor().createInputFileURL();
+		final IInputFileAccessor accessor = fileReference.getFileAccessor();
+		final URL url = accessor.createInputFileURL();
 		if (url != null) {
-			final String path = url.toString().replace(ECORE_FILE_PREFIX, EMPTY_STRING);
+			final String path = accessor.urlToPathString(url, true);
 			try {
 				for (int i = 0; i < xwpfCell.getParagraphs().size(); i++) {
 					// when the cell is created, a paragraph is also created, to avoid a useless blank line we remove it
@@ -491,7 +495,7 @@
 		final URI ecoreURI = accessor.createOutputFileURI(docxGeneratorConfig.getDocumentName(), DOCX_FILE_EXTENTION);
 		final URL destURL = accessor.convertToURL(ecoreURI);
 		try {
-			final String destPath = destURL.toString().replace(ECORE_FILE_PREFIX, EMPTY_STRING);
+			String destPath = accessor.urlToPathString(destURL, true);
 			OutputStream outputStream = new FileOutputStream(destPath);
 			document.write(outputStream);
 			outputStream.close();
diff --git a/plugins/emf/org.eclipse.papyrus.model2doc.emf.structure2document/src-api/org/eclipse/papyrus/model2doc/emf/structure2document/generator/transcriber/StructureToTranscriber.java b/plugins/emf/org.eclipse.papyrus.model2doc.emf.structure2document/src-api/org/eclipse/papyrus/model2doc/emf/structure2document/generator/transcriber/StructureToTranscriber.java
index 39eb4e7..5648549 100755
--- a/plugins/emf/org.eclipse.papyrus.model2doc.emf.structure2document/src-api/org/eclipse/papyrus/model2doc/emf/structure2document/generator/transcriber/StructureToTranscriber.java
+++ b/plugins/emf/org.eclipse.papyrus.model2doc.emf.structure2document/src-api/org/eclipse/papyrus/model2doc/emf/structure2document/generator/transcriber/StructureToTranscriber.java
@@ -1,5 +1,5 @@
 /*****************************************************************************
- * Copyright (c) 2019, 2020 CEA LIST.
+ * Copyright (c) 2019 - 2021 CEA LIST.
  *
  * All rights reserved. This program and the accompanying materials
  * are made available under the terms of the Eclipse Public License 2.0
@@ -11,15 +11,17 @@
  * Contributors:
  *    Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation
  *    Pauline DEVILLE (CEA LIST) pauline.deville@cea.fr - bug 568341
- *
+ *    Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - bug 570803 
  *****************************************************************************/
 
 package org.eclipse.papyrus.model2doc.emf.structure2document.generator.transcriber;
 
+import java.net.URL;
 import java.util.Iterator;
 
 import org.eclipse.papyrus.model2doc.core.builtintypes.AbstractList;
 import org.eclipse.papyrus.model2doc.core.builtintypes.AbstractTable;
+import org.eclipse.papyrus.model2doc.core.builtintypes.accessors.IInputFileAccessor;
 import org.eclipse.papyrus.model2doc.core.transcriber.Transcriber;
 import org.eclipse.papyrus.model2doc.core.transcription.Transcription;
 import org.eclipse.papyrus.model2doc.emf.documentstructure.Body;
@@ -191,8 +193,11 @@
 	 *            an image
 	 */
 	private void transcribeImage(final Image image) {
+		final IInputFileAccessor fileAccessor = image.getFileAccessor();
+		final URL url = fileAccessor.createInputFileURL();
+		final String imagePath = fileAccessor.urlToPathString(url, true);
 		// TODO allow to transcription to take an URL instead of a String?!
-		transcription.writeImage(image.getFileAccessor().createInputFileURL().toString(), image.getCaption());
+		transcription.writeImage(imagePath, image.getCaption());
 	}
 
 	/**
diff --git a/plugins/gmf/org.eclipse.papyrus.model2doc.gmf.template2structure/src/org/eclipse/papyrus/model2doc/gmf/template2structure/internal/mapping/GMFDiagramViewMapper.java b/plugins/gmf/org.eclipse.papyrus.model2doc.gmf.template2structure/src/org/eclipse/papyrus/model2doc/gmf/template2structure/internal/mapping/GMFDiagramViewMapper.java
index dc2fb01..859dd4c 100755
--- a/plugins/gmf/org.eclipse.papyrus.model2doc.gmf.template2structure/src/org/eclipse/papyrus/model2doc/gmf/template2structure/internal/mapping/GMFDiagramViewMapper.java
+++ b/plugins/gmf/org.eclipse.papyrus.model2doc.gmf.template2structure/src/org/eclipse/papyrus/model2doc/gmf/template2structure/internal/mapping/GMFDiagramViewMapper.java
@@ -16,6 +16,7 @@
 
 package org.eclipse.papyrus.model2doc.gmf.template2structure.internal.mapping;
 
+import java.net.URL;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Iterator;
@@ -94,8 +95,8 @@
 
 			final IOutputFileAccessor accessor = conf.createImageOutputAccessor();
 			final URI uri = accessor.createOutputFileURI(imageNameBuilder.toString(), GMFDiagramImageUtils.SVG_EXTENSION);
-			String imagePath = accessor.convertToURL(uri).toString();
-			imagePath = imagePath.replaceAll("file:/", ""); //$NON-NLS-1$ //$NON-NLS-2$
+			final URL url = accessor.convertToURL(uri);
+			final String imagePath = accessor.urlToPathString(url, true);
 			GMFDiagramImageUtils.generateImageOfDiagram(current, imagePath, gmfDiagramView.getDiagramImageMargin());
 			image.setFilePath(uri.toString());
 			returnedValue.add(returnedClassType.cast(image));
diff --git a/plugins/integration/org.eclipse.papyrus.model2doc.integration.gmf.template2structure/src/org/eclipse/papyrus/model2doc/integration/gmf/template2structure/internal/mapping/PapyrusGMFDiagramViewMapper.java b/plugins/integration/org.eclipse.papyrus.model2doc.integration.gmf.template2structure/src/org/eclipse/papyrus/model2doc/integration/gmf/template2structure/internal/mapping/PapyrusGMFDiagramViewMapper.java
index d3cdd5a..2d9af39 100755
--- a/plugins/integration/org.eclipse.papyrus.model2doc.integration.gmf.template2structure/src/org/eclipse/papyrus/model2doc/integration/gmf/template2structure/internal/mapping/PapyrusGMFDiagramViewMapper.java
+++ b/plugins/integration/org.eclipse.papyrus.model2doc.integration.gmf.template2structure/src/org/eclipse/papyrus/model2doc/integration/gmf/template2structure/internal/mapping/PapyrusGMFDiagramViewMapper.java
@@ -16,6 +16,7 @@
 
 package org.eclipse.papyrus.model2doc.integration.gmf.template2structure.internal.mapping;
 
+import java.net.URL;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.Iterator;
@@ -103,8 +104,8 @@
 			}
 			final IOutputFileAccessor accessor = conf.createImageOutputAccessor();
 			final URI uri = accessor.createOutputFileURI(imageNameBuilder.toString(), gmfDiagramView.getImageFormat().getLiteral());
-			String imagePath = accessor.convertToURL(uri).toString();
-			imagePath = imagePath.replaceAll("file:/", ""); //$NON-NLS-1$ //$NON-NLS-2$
+			final URL url = accessor.convertToURL(uri);
+			final String imagePath = accessor.urlToPathString(url, true);
 			GMFDiagramImageUtils.generateImageOfDiagram(current, imagePath, gmfDiagramView.getDiagramImageMargin(), gmfDiagramView.getImageFormat());
 			image.setFilePath(uri.toString());
 			returnedValue.add(returnedClassType.cast(image));