Bug 569059 - [Model2Doc] the generated docx file can't be opened in Word Load the template and save it in the new file directly Signed-off-by: Pauline DEVILLE <pauline.deville@cea.fr> Change-Id: I48ccf8a8f6071dcd772411c05f9b7f1d9494d2e8 Signed-off-by: Vincent Lorenzo <vincent.lorenzo@cea.fr>
diff --git a/plugins/core/org.eclipse.papyrus.model2doc.core.generatorconfiguration/META-INF/MANIFEST.MF b/plugins/core/org.eclipse.papyrus.model2doc.core.generatorconfiguration/META-INF/MANIFEST.MF index 6df2aa5..3f1520b 100755 --- a/plugins/core/org.eclipse.papyrus.model2doc.core.generatorconfiguration/META-INF/MANIFEST.MF +++ b/plugins/core/org.eclipse.papyrus.model2doc.core.generatorconfiguration/META-INF/MANIFEST.MF
@@ -18,3 +18,4 @@ org.eclipse.papyrus.model2doc.core.generatorconfiguration.util Automatic-Module-Name: org.eclipse.papyrus.model2doc.core.generatorconfiguration Bundle-ActivationPolicy: lazy +Bundle-Activator: org.eclipse.papyrus.model2doc.core.generatorconfiguration.internal.Activator
diff --git a/plugins/core/org.eclipse.papyrus.model2doc.core.generatorconfiguration/src/org/eclipse/papyrus/model2doc/core/generatorconfiguration/operations/GeneratorConfigurationOperations.java b/plugins/core/org.eclipse.papyrus.model2doc.core.generatorconfiguration/src/org/eclipse/papyrus/model2doc/core/generatorconfiguration/operations/GeneratorConfigurationOperations.java index be59c22..fb74d99 100755 --- a/plugins/core/org.eclipse.papyrus.model2doc.core.generatorconfiguration/src/org/eclipse/papyrus/model2doc/core/generatorconfiguration/operations/GeneratorConfigurationOperations.java +++ b/plugins/core/org.eclipse.papyrus.model2doc.core.generatorconfiguration/src/org/eclipse/papyrus/model2doc/core/generatorconfiguration/operations/GeneratorConfigurationOperations.java
@@ -303,7 +303,10 @@ return URI.createFileURI(filePath).toString();// it add the file:/ before the path } String res = convertToLocalPath(eobject, filePath); - return URI.createFileURI(res).toString(); + if (res != null) { + return URI.createFileURI(res).toString(); + } + return null; } /**
diff --git a/plugins/docx/org.eclipse.papyrus.model2doc.docx/src/org/eclipse/papyrus/model2doc/docx/internal/services/StyleServiceImpl.java b/plugins/docx/org.eclipse.papyrus.model2doc.docx/src/org/eclipse/papyrus/model2doc/docx/internal/services/StyleServiceImpl.java index cfaf24e..92b6024 100755 --- a/plugins/docx/org.eclipse.papyrus.model2doc.docx/src/org/eclipse/papyrus/model2doc/docx/internal/services/StyleServiceImpl.java +++ b/plugins/docx/org.eclipse.papyrus.model2doc.docx/src/org/eclipse/papyrus/model2doc/docx/internal/services/StyleServiceImpl.java
@@ -93,7 +93,7 @@ styleName = StyleConstants.NO_HEADER_STYLE_VALUE; } - if (document.getStyles().styleExist(styleName)) { + if (document.getStyles() != null && document.getStyles().styleExist(styleName)) { xwpfTable.setStyleID(styleName); return true; }
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 5030964..cd2d1a2 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
@@ -27,14 +27,12 @@ import java.util.List; import org.apache.poi.openxml4j.exceptions.InvalidFormatException; +import org.apache.poi.openxml4j.opc.OPCPackage; import org.apache.poi.xwpf.usermodel.Document; import org.apache.poi.xwpf.usermodel.TableWidthType; -import org.apache.poi.xwpf.usermodel.XWPFDocument; import org.apache.poi.xwpf.usermodel.XWPFParagraph; import org.apache.poi.xwpf.usermodel.XWPFRun; -import org.apache.poi.xwpf.usermodel.XWPFStyles; import org.apache.poi.xwpf.usermodel.XWPFTable; -import org.apache.xmlbeans.XmlException; import org.eclipse.core.runtime.Assert; import org.eclipse.osgi.util.NLS; import org.eclipse.papyrus.model2doc.core.author.IAuthor; @@ -64,6 +62,10 @@ public class DocxTranscription implements Transcription { + private static final String DOTX_SCHEMA = "application/vnd.openxmlformats-officedocument.wordprocessingml.template.main+xml";//$NON-NLS-1$ + + private static final String DOCX_SCHEMA = "application/vnd.openxmlformats-officedocument.wordprocessingml.document.main+xml";//$NON-NLS-1$ + private IDocumentGeneratorConfiguration docxGeneratorConfig; private CustomXWPFDocument document; @@ -85,28 +87,49 @@ public DocxTranscription(IDocumentGeneratorConfiguration dgc) { this.docxGeneratorConfig = dgc; this.styleService = new StyleServiceImpl(); - this.document = new CustomXWPFDocument(); try { - InputStream template = getTemplateInputStream(); - if (template != null) { - // bug 569059 - see https://stackoverrun.com/fr/q/4419808 for solution - XWPFDocument templateDocument = new XWPFDocument(template); - final XWPFStyles newStyles = this.document.createStyles(); - newStyles.setStyles(templateDocument.getStyle()); - templateDocument.close(); + final InputStream templateInputStream = getFileWithTemplateLoaded(); + if (templateInputStream != null) { + // bug 569059 - see https://stackoverflow.com/questions/16264247/create-document-from-dotx-template-with-apache-poi-hwpf for solution + this.document = new CustomXWPFDocument(templateInputStream); + templateInputStream.close(); } } catch (IOException e) { Activator.log.warn("Cannot apply the template file"); //$NON-NLS-1$ - } catch (XmlException e) { - Activator.log.warn("Cannot apply the template file"); //$NON-NLS-1$ + } catch (InvalidFormatException e1) { + Activator.log.error("Cannot apply the template file", e1);//$NON-NLS-1$ + } + if (this.document == null) { + this.document = new CustomXWPFDocument(); } } - private InputStream getTemplateInputStream() throws IOException { - String stringUri = GeneratorConfigurationOperations.getTemplateFilePathInLocalPath(docxGeneratorConfig); - stringUri = stringUri.replaceFirst(ECORE_FILE_PREFIX, EMPTY_STRING); - InputStream stream = new FileInputStream(stringUri); - return stream; + /** + * This method open the destination file and replace it by the the template + * + * @return an inputStream on the destination file + * @throws IOException + * @throws InvalidFormatException + */ + private InputStream getFileWithTemplateLoaded() throws IOException, InvalidFormatException { + String templateURI = GeneratorConfigurationOperations.getTemplateFilePathInLocalPath(docxGeneratorConfig); + if (templateURI != null) { + templateURI = templateURI.replaceFirst(ECORE_FILE_PREFIX, EMPTY_STRING); + String destURI = GeneratorConfigurationOperations.getDocumentFileLocalPath(docxGeneratorConfig, DOCX_FILE_EXTENTION); + destURI = destURI.replaceFirst(ECORE_FILE_PREFIX, EMPTY_STRING); + FileInputStream templateIS = new FileInputStream(templateURI); + FileOutputStream destIS = new FileOutputStream(destURI); + + OPCPackage pkg = OPCPackage.open(templateIS); + pkg.replaceContentType(DOTX_SCHEMA, DOCX_SCHEMA); + pkg.save(destIS); + + templateIS.close(); + destIS.close(); + + return new FileInputStream(destURI); + } + return null; } @Override