Bug 568255 - [Model2Doc][Docx] Add developer tools to extract files from Docx
Signed-off-by: Pauline DEVILLE <pauline.deville@cea.fr>
Change-Id: I832381015990c2ed5d51ab9315227d770423eec7
diff --git a/plugins/dev/org.eclipse.papyrus.model2doc.dev.tools/META-INF/MANIFEST.MF b/plugins/dev/org.eclipse.papyrus.model2doc.dev.tools/META-INF/MANIFEST.MF
index 8b0cbdb..7e9d214 100755
--- a/plugins/dev/org.eclipse.papyrus.model2doc.dev.tools/META-INF/MANIFEST.MF
+++ b/plugins/dev/org.eclipse.papyrus.model2doc.dev.tools/META-INF/MANIFEST.MF
@@ -10,10 +10,7 @@
org.eclipse.emf.ecore;bundle-version="[2.17.0,3.0.0)",
org.eclipse.core.resources;bundle-version="[3.13.0,4.0.0)",
org.eclipse.core.runtime;bundle-version="[3.15.0,4.0.0)",
- javax.xml;bundle-version="[1.3.4,2.0.0)",
org.eclipse.papyrus.model2doc.odt.lib;bundle-version="[0.7.0,1.0.0)",
- org.apache.xerces;bundle-version="[2.9.0,3.0.0)",
- org.eclipse.wst.xml.core;bundle-version="[1.2.0,2.0.0)",
org.eclipse.papyrus.infra.core.log;bundle-version="[1.2.0,2.0.0)",
org.eclipse.papyrus.model2doc.emf.documentstructuretemplate;bundle-version="[0.7.0,1.0.0)"
Bundle-Activator: org.eclipse.papyrus.model2doc.dev.tools.Activator
diff --git a/plugins/dev/org.eclipse.papyrus.model2doc.dev.tools/plugin.xml b/plugins/dev/org.eclipse.papyrus.model2doc.dev.tools/plugin.xml
index 33d84fc..109ae70 100755
--- a/plugins/dev/org.eclipse.papyrus.model2doc.dev.tools/plugin.xml
+++ b/plugins/dev/org.eclipse.papyrus.model2doc.dev.tools/plugin.xml
@@ -55,6 +55,21 @@
checkEnabled="true">
</visibleWhen>
</command>
+ <command
+ commandId="org.eclipse.papyrus.model2doc.dev.tools.extractFullDocxContents"
+ style="push">
+ <visibleWhen
+ checkEnabled="true">
+ </visibleWhen>
+ </command>
+ <command
+ commandId="org.eclipse.papyrus.model2doc.dev.tools.extractDocxXMLDocument"
+ label="Extract Docx Document XML File"
+ style="push">
+ <visibleWhen
+ checkEnabled="true">
+ </visibleWhen>
+ </command>
<separator
name="org.eclipse.papyrus.model2doc.dev.tools.ui.menu.separator.after"
visible="true">
@@ -79,6 +94,16 @@
id="org.eclipse.papyrus.model2doc.dev.tools.extractFullODTContents"
name="Extract Full ODT file contents">
</command>
+ <command
+ description="This command allows to extract all the contents of an docx file"
+ id="org.eclipse.papyrus.model2doc.dev.tools.extractFullDocxContents"
+ name="Extract Full Docx file contents">
+ </command>
+ <command
+ description="This command allows to extract the file document.xml from an docx file"
+ id="org.eclipse.papyrus.model2doc.dev.tools.extractDocxXMLDocument"
+ name="Extract document.xml file">
+ </command>
</extension>
<extension
point="org.eclipse.ui.handlers">
@@ -138,6 +163,58 @@
</with>
</activeWhen>
</handler>
+ <handler
+ class="org.eclipse.papyrus.model2doc.dev.tools.handlers.UnzipDocxFileHandler"
+ commandId="org.eclipse.papyrus.model2doc.dev.tools.extractFullDocxContents">
+ <activeWhen>
+ <with
+ variable="selection">
+ <iterate
+ ifEmpty="false"
+ operator="and">
+ <adapt
+ type="org.eclipse.core.resources.IFile">
+ <or>
+ <test
+ property="org.eclipse.core.resources.name"
+ value="*.docx">
+ </test>
+ <test
+ property="org.eclipse.core.resources.name"
+ value="*.dotx">
+ </test>
+ </or>
+ </adapt>
+ </iterate>
+ </with>
+ </activeWhen>
+ </handler>
+ <handler
+ class="org.eclipse.papyrus.model2doc.dev.tools.handlers.ExtractDocxContentsXmlFileHandler"
+ commandId="org.eclipse.papyrus.model2doc.dev.tools.extractDocxXMLDocument">
+ <activeWhen>
+ <with
+ variable="selection">
+ <iterate
+ ifEmpty="false"
+ operator="and">
+ <adapt
+ type="org.eclipse.core.resources.IFile">
+ <or>
+ <test
+ property="org.eclipse.core.resources.name"
+ value="*.docx">
+ </test>
+ <test
+ property="org.eclipse.core.resources.name"
+ value="*.dotx">
+ </test>
+ </or>
+ </adapt>
+ </iterate>
+ </with>
+ </activeWhen>
+ </handler>
</extension>
</plugin>
diff --git a/plugins/dev/org.eclipse.papyrus.model2doc.dev.tools/src/org/eclipse/papyrus/model2doc/dev/tools/handlers/AbstractODTFileHandler.java b/plugins/dev/org.eclipse.papyrus.model2doc.dev.tools/src/org/eclipse/papyrus/model2doc/dev/tools/handlers/AbstractZipFileHandler.java
similarity index 70%
rename from plugins/dev/org.eclipse.papyrus.model2doc.dev.tools/src/org/eclipse/papyrus/model2doc/dev/tools/handlers/AbstractODTFileHandler.java
rename to plugins/dev/org.eclipse.papyrus.model2doc.dev.tools/src/org/eclipse/papyrus/model2doc/dev/tools/handlers/AbstractZipFileHandler.java
index 8449544..c431d69 100755
--- a/plugins/dev/org.eclipse.papyrus.model2doc.dev.tools/src/org/eclipse/papyrus/model2doc/dev/tools/handlers/AbstractODTFileHandler.java
+++ b/plugins/dev/org.eclipse.papyrus.model2doc.dev.tools/src/org/eclipse/papyrus/model2doc/dev/tools/handlers/AbstractZipFileHandler.java
@@ -1,5 +1,5 @@
/*****************************************************************************
- * Copyright (c) 2019 CEA LIST and others.
+ * Copyright (c) 2019, 2020 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,13 +9,13 @@
* SPDX-License-Identifier: EPL-2.0
*
* Contributors:
- * CEA LIST - 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 568483
+ * Pauline DEVILLE - Bug 568255 [Model2Doc][Docx] Add developer tools to extract files from Docx
*****************************************************************************/
package org.eclipse.papyrus.model2doc.dev.tools.handlers;
-import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@@ -30,31 +30,15 @@
import org.eclipse.core.runtime.Path;
import org.eclipse.emf.common.util.URI;
import org.eclipse.jface.viewers.IStructuredSelection;
-import org.eclipse.osgi.util.NLS;
import org.eclipse.papyrus.model2doc.dev.tools.Activator;
-import org.eclipse.wst.xml.core.internal.formatter.XMLFormatterFormatProcessor;
/**
- * TODO move me into an odt dev plugin
- *
- * Abstract handler used to manipulate ODT file
+ * Abstract handler used to manipulate zip file
*/
-public abstract class AbstractODTFileHandler extends AbstractHandler {
+public abstract class AbstractZipFileHandler extends AbstractHandler {
protected static final String XML_EXTENSION = "xml"; //$NON-NLS-1$
- protected static final String ODT_EXTENSION = "odt"; //$NON-NLS-1$
-
- protected static final String OTT_EXTENSION = "ott"; //$NON-NLS-1$
-
- protected static final String CONTENT_XML_FILE = "content.xml"; //$NON-NLS-1$
-
- /**
- * the format processor used to format XML files
- */
- protected static final XMLFormatterFormatProcessor processor = new XMLFormatterFormatProcessor();
-
-
// TODO move me in a util class
protected static final String getOSPathFromURI(final URI uri) {
String workspaceLocation = null;
@@ -69,7 +53,7 @@
* @return
* the URI of the uml selected files
*/
- protected final List<URI> getSelectedOdtFileURI() {
+ protected final List<URI> getSelectedFileURI() {
final Iterator<?> iter = getCurrentStructuredSelection().iterator();
final List<URI> uris = new ArrayList<>();
// 1. we iterate on the selection
@@ -82,7 +66,7 @@
pathName = f.getFullPath().toPortableString();
}
final URI uri = URI.createPlatformResourceURI(pathName, true);
- if (ODT_EXTENSION.equals(uri.fileExtension()) || OTT_EXTENSION.equals(uri.fileExtension())) {
+ if (getFileExtension().equals(uri.fileExtension()) || getTemplateExtension().equals(uri.fileExtension())) {
uris.add(uri);
}
}
@@ -90,6 +74,16 @@
}
/**
+ * @return
+ */
+ protected abstract String getTemplateExtension();
+
+ /**
+ * @return
+ */
+ protected abstract String getFileExtension();
+
+ /**
*
* @see org.eclipse.core.commands.AbstractHandler#setEnabled(java.lang.Object)
*
@@ -97,7 +91,7 @@
*/
@Override
public void setEnabled(Object evaluationContext) {
- super.setBaseEnabled(getSelectedOdtFileURI().size() > 0);
+ super.setBaseEnabled(getSelectedFileURI().size() > 0);
}
/**
@@ -130,16 +124,6 @@
protected static final void formatXMLFile(final String filePath) {
// formatting file now
final IFile file = ResourcesPlugin.getWorkspace().getRoot().getFileForLocation(new Path(filePath));
- // file.exists()
- if (null != file) {
- try {
- processor.formatFile(file);
- Activator.log.info(NLS.bind("XML file {0} is now formatted", filePath)); //$NON-NLS-1$
- } catch (IOException ex) {
- Activator.log.error(NLS.bind("IOException formating the XML file from {0}", filePath), ex); //$NON-NLS-1$
- } catch (CoreException ex) {
- Activator.log.error(NLS.bind("Exception during the formatting of {0}", filePath), ex); //$NON-NLS-1$
- }
- }
+ org.eclipse.papyrus.model2doc.dev.tools.utils.XMLFormatter.format(file);
}
}
diff --git a/plugins/dev/org.eclipse.papyrus.model2doc.dev.tools/src/org/eclipse/papyrus/model2doc/dev/tools/handlers/ExtractContentsXmlFileHandler.java b/plugins/dev/org.eclipse.papyrus.model2doc.dev.tools/src/org/eclipse/papyrus/model2doc/dev/tools/handlers/ExtractContentsXmlFileHandler.java
index c46e5fc..59d57ac 100755
--- a/plugins/dev/org.eclipse.papyrus.model2doc.dev.tools/src/org/eclipse/papyrus/model2doc/dev/tools/handlers/ExtractContentsXmlFileHandler.java
+++ b/plugins/dev/org.eclipse.papyrus.model2doc.dev.tools/src/org/eclipse/papyrus/model2doc/dev/tools/handlers/ExtractContentsXmlFileHandler.java
@@ -1,5 +1,5 @@
/*****************************************************************************
- * Copyright (c) 2019 CEA LIST.
+ * Copyright (c) 2019, 2020 CEA LIST.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
@@ -10,6 +10,7 @@
*
* Contributors:
* Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation
+ * Pauline DEVILLE - Bug 568255 [Model2Doc][Docx] Add developer tools to extract files from Docx
*****************************************************************************/
package org.eclipse.papyrus.model2doc.dev.tools.handlers;
@@ -33,8 +34,7 @@
* This class has been created to extract easily the file content.xml from an odt file
*
*/
-public class ExtractContentsXmlFileHandler extends AbstractODTFileHandler {
-
+public class ExtractContentsXmlFileHandler extends AbstractZipFileHandler {
/**
*
@@ -46,7 +46,7 @@
*/
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
- for (final URI uri : getSelectedOdtFileURI()) {
+ for (final URI uri : getSelectedFileURI()) {
// convert the current odt file platform/resource path into an xml path according to the OS (id for windows : C:\... or D:\...)
final String contentXMLLocation = getOSPathFromURI(uri.trimFileExtension().appendFileExtension(XML_EXTENSION));
@@ -61,7 +61,7 @@
final FileOutputStream outputStream = new FileOutputStream(contentXMLLocation);
while (entries.hasMoreElements()) {
final ZipEntry current = entries.nextElement();
- if (CONTENT_XML_FILE.equals(current.getName())) {
+ if (IOdtConstants.CONTENT_XML_FILE.equals(current.getName())) {
final InputStream in = odtZipFile.getInputStream(current);
while (0 < in.available()) {
final int read = in.read();
@@ -93,4 +93,24 @@
return null;
}
+ /**
+ * @see org.eclipse.papyrus.model2doc.dev.tools.handlers.AbstractZipFileHandler#getTemplateExtension()
+ *
+ * @return
+ */
+ @Override
+ protected String getTemplateExtension() {
+ return IOdtConstants.OTT_EXTENSION;
+ }
+
+ /**
+ * @see org.eclipse.papyrus.model2doc.dev.tools.handlers.AbstractZipFileHandler#getFileExtension()
+ *
+ * @return
+ */
+ @Override
+ protected String getFileExtension() {
+ return IOdtConstants.ODT_EXTENSION;
+ }
+
}
diff --git a/plugins/dev/org.eclipse.papyrus.model2doc.dev.tools/src/org/eclipse/papyrus/model2doc/dev/tools/handlers/ExtractDocxContentsXmlFileHandler.java b/plugins/dev/org.eclipse.papyrus.model2doc.dev.tools/src/org/eclipse/papyrus/model2doc/dev/tools/handlers/ExtractDocxContentsXmlFileHandler.java
new file mode 100755
index 0000000..6be8d02
--- /dev/null
+++ b/plugins/dev/org.eclipse.papyrus.model2doc.dev.tools/src/org/eclipse/papyrus/model2doc/dev/tools/handlers/ExtractDocxContentsXmlFileHandler.java
@@ -0,0 +1,115 @@
+/*****************************************************************************
+ * Copyright (c) 2020 CEA LIST.
+ *
+ * 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
+ * https://www.eclipse.org/legal/epl-2.0/
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ *
+ * Contributors:
+ * Pauline DEVILLE (CEA LIST) pauline.deville@cea.fr - Initial API and implementation
+ *****************************************************************************/
+package org.eclipse.papyrus.model2doc.dev.tools.handlers;
+
+import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Enumeration;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
+
+import org.eclipse.core.commands.ExecutionEvent;
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.emf.common.util.URI;
+import org.eclipse.osgi.util.NLS;
+import org.eclipse.papyrus.model2doc.dev.tools.Activator;
+
+/**
+ *
+ * This class has been created to extract easily the file document.xml from an docx file
+ *
+ */
+public class ExtractDocxContentsXmlFileHandler extends AbstractZipFileHandler {
+
+ /**
+ *
+ * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
+ *
+ * @param event
+ * @return
+ * @throws ExecutionException
+ */
+ @Override
+ public Object execute(ExecutionEvent event) throws ExecutionException {
+ for (final URI uri : getSelectedFileURI()) {
+
+ // convert the current file platform/resource path into an xml path according to the OS (id for windows : C:\... or D:\...)
+ final String contentXMLLocation = getOSPathFromURI(uri.trimFileExtension().appendFileExtension(XML_EXTENSION));
+
+ // same thing for the selected docx file
+ final String docxFileLocation = getOSPathFromURI(uri);
+
+ final File docxFile = new File(docxFileLocation);
+ try {
+ final ZipFile docxZipFile = new ZipFile(docxFile);
+ final Enumeration<? extends ZipEntry> entries = docxZipFile.entries();
+ final FileOutputStream outputStream = new FileOutputStream(contentXMLLocation);
+ while (entries.hasMoreElements()) {
+ final ZipEntry current = entries.nextElement();
+ if (IDocxConstant.DOCX_CONTENT_FILE.equals(current.getName())) {
+ final InputStream in = docxZipFile.getInputStream(current);
+ while (0 < in.available()) {
+ final int read = in.read();
+ outputStream.write(read);
+ }
+
+ in.close();
+ break;// we can go out, we manage the wanted file
+ }
+ }
+
+ docxZipFile.close();
+ outputStream.close();
+ Activator.log.info(NLS.bind("XML file extracted from {0}", uri.toString())); //$NON-NLS-1$
+ } catch (FileNotFoundException e) {
+ Activator.log.error(NLS.bind("XML file not found in {0}", uri.toString()), e); //$NON-NLS-1$
+ } catch (IOException e) {
+ Activator.log.error(NLS.bind("IOException extracting XML file from {0}", uri.toString()), e); //$NON-NLS-1$
+ }
+
+
+ // we refresh the workspace
+ refreshWorkspace();
+
+ formatXMLFile(contentXMLLocation);
+
+ }
+
+ return null;
+ }
+
+ /**
+ * @see org.eclipse.papyrus.model2doc.dev.tools.handlers.AbstractZipFileHandler#getTemplateExtension()
+ *
+ * @return
+ */
+ @Override
+ protected String getTemplateExtension() {
+ return IDocxConstant.DOTX_EXTENSION;
+ }
+
+ /**
+ * @see org.eclipse.papyrus.model2doc.dev.tools.handlers.AbstractZipFileHandler#getFileExtension()
+ *
+ * @return
+ */
+ @Override
+ protected String getFileExtension() {
+ return IDocxConstant.DOCX_EXTENSION;
+ }
+
+}
diff --git a/plugins/dev/org.eclipse.papyrus.model2doc.dev.tools/src/org/eclipse/papyrus/model2doc/dev/tools/handlers/IDocxConstant.java b/plugins/dev/org.eclipse.papyrus.model2doc.dev.tools/src/org/eclipse/papyrus/model2doc/dev/tools/handlers/IDocxConstant.java
new file mode 100755
index 0000000..66565a0
--- /dev/null
+++ b/plugins/dev/org.eclipse.papyrus.model2doc.dev.tools/src/org/eclipse/papyrus/model2doc/dev/tools/handlers/IDocxConstant.java
@@ -0,0 +1,28 @@
+/*****************************************************************************
+ * Copyright (c) 2020 CEA LIST.
+ *
+ * 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
+ * https://www.eclipse.org/legal/epl-2.0/
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ *
+ * Contributors:
+ * Pauline DEVILLE (CEA LIST) pauline.deville@cea.fr - Initial API and implementation
+ *****************************************************************************/
+
+package org.eclipse.papyrus.model2doc.dev.tools.handlers;
+
+/**
+ *
+ */
+public interface IDocxConstant {
+
+ public static final String DOCX_EXTENSION = "docx"; //$NON-NLS-1$
+
+ public static final String DOTX_EXTENSION = "dotx"; //$NON-NLS-1$
+
+ public static final String DOCX_CONTENT_FILE = "word/document.xml"; //$NON-NLS-1$
+
+}
diff --git a/plugins/dev/org.eclipse.papyrus.model2doc.dev.tools/src/org/eclipse/papyrus/model2doc/dev/tools/handlers/IOdtConstants.java b/plugins/dev/org.eclipse.papyrus.model2doc.dev.tools/src/org/eclipse/papyrus/model2doc/dev/tools/handlers/IOdtConstants.java
new file mode 100755
index 0000000..6d98b67
--- /dev/null
+++ b/plugins/dev/org.eclipse.papyrus.model2doc.dev.tools/src/org/eclipse/papyrus/model2doc/dev/tools/handlers/IOdtConstants.java
@@ -0,0 +1,28 @@
+/*****************************************************************************
+ * Copyright (c) 2020 CEA LIST.
+ *
+ * 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
+ * https://www.eclipse.org/legal/epl-2.0/
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ *
+ * Contributors:
+ * Pauline DEVILLE (CEA LIST) pauline.deville@cea.fr - Initial API and implementation
+ *****************************************************************************/
+
+package org.eclipse.papyrus.model2doc.dev.tools.handlers;
+
+/**
+ *
+ */
+public interface IOdtConstants {
+
+ public static final String ODT_EXTENSION = "odt"; //$NON-NLS-1$
+
+ public static final String OTT_EXTENSION = "ott"; //$NON-NLS-1$
+
+ public static final String CONTENT_XML_FILE = "content.xml"; //$NON-NLS-1$
+
+}
diff --git a/plugins/dev/org.eclipse.papyrus.model2doc.dev.tools/src/org/eclipse/papyrus/model2doc/dev/tools/handlers/UnzipDocxFileHandler.java b/plugins/dev/org.eclipse.papyrus.model2doc.dev.tools/src/org/eclipse/papyrus/model2doc/dev/tools/handlers/UnzipDocxFileHandler.java
new file mode 100755
index 0000000..54ca08a
--- /dev/null
+++ b/plugins/dev/org.eclipse.papyrus.model2doc.dev.tools/src/org/eclipse/papyrus/model2doc/dev/tools/handlers/UnzipDocxFileHandler.java
@@ -0,0 +1,140 @@
+/*****************************************************************************
+ * Copyright (c) 2020 CEA LIST.
+ *
+ * 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
+ * https://www.eclipse.org/legal/epl-2.0/
+ *
+ * SPDX-License-Identifier: EPL-2.0
+ *
+ * Contributors:
+ * Pauline DEVILLE (CEA LIST) pauline.deville@cea.fr - Initial API and implementation
+ *****************************************************************************/
+package org.eclipse.papyrus.model2doc.dev.tools.handlers;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Iterator;
+import java.util.List;
+import java.util.zip.ZipInputStream;
+
+import org.eclipse.core.commands.ExecutionEvent;
+import org.eclipse.core.commands.ExecutionException;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.resources.ResourcesPlugin;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.emf.common.util.URI;
+import org.eclipse.papyrus.model2doc.dev.tools.Activator;
+import org.eclipse.papyrus.model2doc.dev.tools.utils.ZipUtils;
+
+/**
+ *
+ * This class has been created to extract easily all files owned by an docx file and format the xml files.
+ *
+ */
+public class UnzipDocxFileHandler extends AbstractZipFileHandler {
+
+ /**
+ *
+ * @see org.eclipse.core.commands.IHandler#execute(org.eclipse.core.commands.ExecutionEvent)
+ *
+ * @param event
+ * @return
+ * @throws ExecutionException
+ */
+ @Override
+ public Object execute(ExecutionEvent event) throws ExecutionException {
+ for (final URI uri : getSelectedFileURI()) {
+
+ // same thing for the selected docx file
+ final String docxFileLocation = getOSPathFromURI(uri);
+
+ try {
+ ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(docxFileLocation));
+ final String outputFolder = getOSPathFromURI(uri.trimFileExtension());
+ File f = new File(outputFolder);
+
+ ZipUtils.extract(zipInputStream, f);
+ // we refresh the workspace
+ refreshWorkspace();
+
+
+ // format XML files
+ formatAllXMLFiles(f);
+ } catch (IOException e) {
+ Activator.log.error(e);
+ }
+ }
+
+
+ // we refresh the workspace
+ try {
+ Activator.log.info("Refresh workspace"); //$NON-NLS-1$
+ ResourcesPlugin.getWorkspace().getRoot().refreshLocal(IResource.DEPTH_INFINITE, new NullProgressMonitor());
+ } catch (CoreException e) {
+ Activator.log.error("Exception during workspace refresh", e); //$NON-NLS-1$
+ }
+
+ return null;
+ }
+
+ /**
+ * This method format all xml files owned by the folder (recursively)
+ *
+ * @param folder
+ * a folder
+ */
+ private static final void formatAllXMLFiles(final File folder) {
+ final Iterator<File> iter = getAllOwnedFiles(folder).iterator();
+ while (iter.hasNext()) {
+ final File current = iter.next();
+ final String path = current.getAbsolutePath();
+ formatXMLFile(path);
+ }
+ }
+
+ /**
+ *
+ * @param folder
+ * a folder
+ * @return
+ * all the owned files recursively
+ */
+ private static final List<File> getAllOwnedFiles(final File folder) {
+ final List<File> files = new ArrayList<>();
+ final List<File> tmp = Arrays.asList(folder.listFiles());
+ files.addAll(tmp);
+ for (File current : tmp) {
+ if (current.isDirectory()) {
+ files.addAll(getAllOwnedFiles(current));
+ }
+ }
+
+ return files;
+ }
+
+ /**
+ * @see org.eclipse.papyrus.model2doc.dev.tools.handlers.AbstractZipFileHandler#getTemplateExtension()
+ *
+ * @return
+ */
+ @Override
+ protected String getTemplateExtension() {
+ return IDocxConstant.DOTX_EXTENSION;
+ }
+
+ /**
+ * @see org.eclipse.papyrus.model2doc.dev.tools.handlers.AbstractZipFileHandler#getFileExtension()
+ *
+ * @return
+ */
+ @Override
+ protected String getFileExtension() {
+ return IDocxConstant.DOCX_EXTENSION;
+ }
+}
diff --git a/plugins/dev/org.eclipse.papyrus.model2doc.dev.tools/src/org/eclipse/papyrus/model2doc/dev/tools/handlers/UnzipOdtFileHandler.java b/plugins/dev/org.eclipse.papyrus.model2doc.dev.tools/src/org/eclipse/papyrus/model2doc/dev/tools/handlers/UnzipOdtFileHandler.java
index 8a0cb09..f2137f1 100755
--- a/plugins/dev/org.eclipse.papyrus.model2doc.dev.tools/src/org/eclipse/papyrus/model2doc/dev/tools/handlers/UnzipOdtFileHandler.java
+++ b/plugins/dev/org.eclipse.papyrus.model2doc.dev.tools/src/org/eclipse/papyrus/model2doc/dev/tools/handlers/UnzipOdtFileHandler.java
@@ -1,5 +1,5 @@
/*****************************************************************************
- * Copyright (c) 2019 CEA LIST.
+ * Copyright (c) 2019, 2020 CEA LIST.
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License 2.0
@@ -10,6 +10,7 @@
*
* Contributors:
* Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation
+ * Pauline DEVILLE - Bug 568255 [Model2Doc][Docx] Add developer tools to extract files from Docx
*****************************************************************************/
package org.eclipse.papyrus.model2doc.dev.tools.handlers;
@@ -37,7 +38,7 @@
* This class has been created to extract easily all files owned by an odt file and format the xml files.
*
*/
-public class UnzipOdtFileHandler extends AbstractODTFileHandler {
+public class UnzipOdtFileHandler extends AbstractZipFileHandler {
/**
*
@@ -49,7 +50,7 @@
*/
@Override
public Object execute(ExecutionEvent event) throws ExecutionException {
- for (final URI uri : getSelectedOdtFileURI()) {
+ for (final URI uri : getSelectedFileURI()) {
// same thing for the selected odt file
final String odtFileLocation = getOSPathFromURI(uri);
@@ -117,4 +118,24 @@
return files;
}
+
+ /**
+ * @see org.eclipse.papyrus.model2doc.dev.tools.handlers.AbstractZipFileHandler#getTemplateExtension()
+ *
+ * @return
+ */
+ @Override
+ protected String getTemplateExtension() {
+ return IOdtConstants.OTT_EXTENSION;
+ }
+
+ /**
+ * @see org.eclipse.papyrus.model2doc.dev.tools.handlers.AbstractZipFileHandler#getFileExtension()
+ *
+ * @return
+ */
+ @Override
+ protected String getFileExtension() {
+ return IOdtConstants.ODT_EXTENSION;
+ }
}
diff --git a/plugins/dev/org.eclipse.papyrus.model2doc.dev.tools/src/org/eclipse/papyrus/model2doc/dev/tools/utils/XMLFormatter.java b/plugins/dev/org.eclipse.papyrus.model2doc.dev.tools/src/org/eclipse/papyrus/model2doc/dev/tools/utils/XMLFormatter.java
new file mode 100755
index 0000000..bdecb4b
--- /dev/null
+++ b/plugins/dev/org.eclipse.papyrus.model2doc.dev.tools/src/org/eclipse/papyrus/model2doc/dev/tools/utils/XMLFormatter.java
@@ -0,0 +1,81 @@
+/*****************************************************************************
+ * Copyright (c) 2020 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:
+ * Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation
+ *
+ *****************************************************************************/
+package org.eclipse.papyrus.model2doc.dev.tools.utils;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.io.StringWriter;
+
+import javax.xml.transform.OutputKeys;
+import javax.xml.transform.Source;
+import javax.xml.transform.Transformer;
+import javax.xml.transform.TransformerConfigurationException;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.TransformerFactory;
+import javax.xml.transform.stream.StreamResult;
+import javax.xml.transform.stream.StreamSource;
+
+import org.eclipse.core.resources.IFile;
+import org.eclipse.core.resources.IResource;
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.NullProgressMonitor;
+import org.eclipse.osgi.util.NLS;
+import org.eclipse.papyrus.model2doc.dev.tools.Activator;
+
+/**
+ * TODO : move me into papyrus ? class SyncManifestAndPOMVersions.java in papyrus has also an XML formatter
+ */
+public class XMLFormatter {
+
+ private XMLFormatter() {
+ // to prevent instanciation
+ }
+
+ // previsouly it was this formatter (used my the Eclipse XML Editor
+ // public static final void format(final IFile file) {
+ // final org.eclipse.wst.xml.core.internal.formatter.XMLFormatterFormatProcessor processor = new XMLFormatterFormatProcessor();
+ // if (null != file) {
+ // try {
+ // processor.formatFile(file);
+ // Activator.log.info(NLS.bind("XML file {0} is now formatted", file.getFullPath())); //$NON-NLS-1$
+ // } catch (IOException ex) {
+ // Activator.log.error(NLS.bind("IOException formating the XML file from {0}", file.getFullPath()), ex); //$NON-NLS-1$
+ // } catch (CoreException ex) {
+ // Activator.log.error(NLS.bind("Exception during the formatting of {0}", file.getFullPath()), ex); //$NON-NLS-1$
+ // }
+ // }
+ // }
+ public static final void format(final IFile xmlFile) {
+ try {
+ InputStream input = xmlFile.getContents();
+ Source xmlInput = new StreamSource(input);
+ StringWriter stringWriter = new StringWriter();
+ StreamResult xmlOutput = new StreamResult(stringWriter);
+ TransformerFactory transformerFactory = TransformerFactory.newInstance();
+ transformerFactory.setAttribute("indent-number", 4); //$NON-NLS-1$
+ Transformer transformer = transformerFactory.newTransformer();
+ transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
+ transformer.transform(xmlInput, xmlOutput);
+ InputStream in = new ByteArrayInputStream(stringWriter.toString().getBytes());
+ xmlFile.setContents(in, IResource.FORCE, new NullProgressMonitor());
+ } catch (CoreException ex) {
+ Activator.log.error(NLS.bind("Exception during the formatting of {0}", xmlFile.getFullPath()), ex); //$NON-NLS-1$
+ } catch (TransformerConfigurationException e) {
+ Activator.log.error(NLS.bind("Exception during the formatting of {0}", xmlFile.getFullPath()), e); //$NON-NLS-1$
+ } catch (TransformerException e) {
+ Activator.log.error(NLS.bind("Exception during the formatting of {0}", xmlFile.getFullPath()), e); //$NON-NLS-1$
+ }
+ }
+}