diff --git a/pso-docx-ant/src/main/java/org/pageseeder/docx/ant/ExportTask.java b/pso-docx-ant/src/main/java/org/pageseeder/docx/ant/ExportTask.java index 8ac0b71d..a3ad08e4 100644 --- a/pso-docx-ant/src/main/java/org/pageseeder/docx/ant/ExportTask.java +++ b/pso-docx-ant/src/main/java/org/pageseeder/docx/ant/ExportTask.java @@ -204,11 +204,14 @@ public void execute() throws BuildException { ZipUtils.unzip(this.dotx, prepacked); File document = new File(prepacked, "word/document.xml"); Files.ensureDirectoryExists(document.getParentFile()); + // prefix template media files with a random string to avoid clashes with PSML images + File mediaFolder = new File(prepacked, "word/media"); + String mediaPrefix = "kwo5nu83zotp2-"; + Files.renameFiles(mediaFolder, mediaPrefix); - // 3. (extra) copy everything from the media folder to prepacked folder + // 4. (extra) copy everything from the media folder to prepacked folder if (this.media != null) { log("Copy media files"); - File mediaFolder = new File(prepacked, "word/media"); if (!mediaFolder.exists()) { mediaFolder.mkdirs(); } @@ -220,7 +223,7 @@ public void execute() throws BuildException { } } - // 4. Unnest the files + // 5. Unnest the files log("Unnest"); Templates unnest = XSLT.getTemplatesFromResource("org/pageseeder/docx/xslt/export-unnest.xsl"); File sourceDocument = this.source; @@ -229,8 +232,7 @@ public void execute() throws BuildException { Map noParameters = Collections.emptyMap(); XSLT.transform(sourceDocument, newSourceDocument, unnest, noParameters); - - // 5. Process the files + // 6. Process the files log("Process with XSLT"); // Parse templates @@ -241,6 +243,7 @@ public void execute() throws BuildException { parameters.put("_outputfolder", prepacked.toURI().toString()); parameters.put("_dotxfolder", dotx.toURI().toString()); parameters.put("_docxfilename", this.destination.getName()); + parameters.put("_mediaprefix", mediaPrefix); if (this.config != null) { parameters.put("_configfileurl", this.config.toURI().toString()); } @@ -253,7 +256,7 @@ public void execute() throws BuildException { // Transform XSLT.transform(newSourceDocument, document, templates, parameters); - // 6. Move or Zip the generated content + // 7. Move or Zip the generated content if (parameters.containsKey("expanded") && parameters.get("expanded").equals("true")) { log("Moving"); prepacked.renameTo(this.destination); diff --git a/pso-docx-ant/src/test/export/cases/core-properties/custom.xml b/pso-docx-ant/src/test/export/cases/core-properties/custom.xml index 05f4f33a..d3d9f34d 100644 --- a/pso-docx-ant/src/test/export/cases/core-properties/custom.xml +++ b/pso-docx-ant/src/test/export/cases/core-properties/custom.xml @@ -1 +1 @@ -1.0 \ No newline at end of file +1.0Myvalue \ No newline at end of file diff --git a/pso-docx-ant/src/test/export/cases/core-properties/word-export-template.docx b/pso-docx-ant/src/test/export/cases/core-properties/word-export-template.docx index d4728479..e0b0ffe9 100644 Binary files a/pso-docx-ant/src/test/export/cases/core-properties/word-export-template.docx and b/pso-docx-ant/src/test/export/cases/core-properties/word-export-template.docx differ diff --git a/pso-docx-ant/src/test/export/cases/empty-configuration-images/document.xml b/pso-docx-ant/src/test/export/cases/empty-configuration-images/document.xml index dc09e2b6..6ed3ae04 100644 --- a/pso-docx-ant/src/test/export/cases/empty-configuration-images/document.xml +++ b/pso-docx-ant/src/test/export/cases/empty-configuration-images/document.xml @@ -1 +1 @@ -Test Case 7Basic Word document without configuration.Test of images.Image with caption:Figure 1 CaptionImage without caption: \ No newline at end of file +Test Case 7Basic Word document without configuration.Test of images.Image with caption:Figure 1 CaptionImage without caption: \ No newline at end of file diff --git a/pso-docx-ant/src/test/export/cases/empty-configuration-images/document.xml.rels.xml b/pso-docx-ant/src/test/export/cases/empty-configuration-images/document.xml.rels.xml new file mode 100644 index 00000000..249ba849 --- /dev/null +++ b/pso-docx-ant/src/test/export/cases/empty-configuration-images/document.xml.rels.xml @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/pso-docx-ant/src/test/export/cases/empty-configuration-images/footer3.xml.rels.xml b/pso-docx-ant/src/test/export/cases/empty-configuration-images/footer3.xml.rels.xml new file mode 100644 index 00000000..3b5249f6 --- /dev/null +++ b/pso-docx-ant/src/test/export/cases/empty-configuration-images/footer3.xml.rels.xml @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/pso-docx-ant/src/test/export/cases/empty-configuration-images/header3.xml.rels.xml b/pso-docx-ant/src/test/export/cases/empty-configuration-images/header3.xml.rels.xml new file mode 100644 index 00000000..4347000b --- /dev/null +++ b/pso-docx-ant/src/test/export/cases/empty-configuration-images/header3.xml.rels.xml @@ -0,0 +1 @@ + \ No newline at end of file diff --git a/pso-docx-ant/src/test/export/cases/empty-configuration-images/word-export-template.docx b/pso-docx-ant/src/test/export/cases/empty-configuration-images/word-export-template.docx new file mode 100644 index 00000000..5d0b3094 Binary files /dev/null and b/pso-docx-ant/src/test/export/cases/empty-configuration-images/word-export-template.docx differ diff --git a/pso-docx-ant/src/test/export/cases/empty-configuration-images/word-export-template.dotx b/pso-docx-ant/src/test/export/cases/empty-configuration-images/word-export-template.dotx deleted file mode 100644 index 387b35b4..00000000 Binary files a/pso-docx-ant/src/test/export/cases/empty-configuration-images/word-export-template.dotx and /dev/null differ diff --git a/pso-docx-ant/src/test/java/org/pageseeder/docx/ant/ExportTaskTest.java b/pso-docx-ant/src/test/java/org/pageseeder/docx/ant/ExportTaskTest.java index a67ceac1..a449e091 100644 --- a/pso-docx-ant/src/test/java/org/pageseeder/docx/ant/ExportTaskTest.java +++ b/pso-docx-ant/src/test/java/org/pageseeder/docx/ant/ExportTaskTest.java @@ -175,7 +175,8 @@ public void testEmptyConfigurationHeadings() throws IOException, SAXException { @Test public void testEmptyConfigurationImages() throws IOException, SAXException { - testIndividual("empty-configuration-images"); + testIndividual("empty-configuration-images", + "document,document.xml.rels,footer3.xml.rels,header3.xml.rels"); } @Test @@ -589,6 +590,9 @@ public void testIndividual(File dir, String filenames, boolean saveWorking) thro ("core".equals(name) || "custom".equals(name)) ? "working/prepacked/docProps/" : "item1".equals(name) ? "working/prepacked/customXml/" : "working/prepacked/word/") + name + ".xml"); + if (name.endsWith(".rels")) { + actual = new File(result, "working/prepacked/word/_rels/" + name); + } File expected = new File(dir, name + ".xml"); // Check that the files exist diff --git a/pso-docx-core/src/main/java/org/pageseeder/docx/DOCXProcessor.java b/pso-docx-core/src/main/java/org/pageseeder/docx/DOCXProcessor.java index 6ff11e06..f0100e2f 100644 --- a/pso-docx-core/src/main/java/org/pageseeder/docx/DOCXProcessor.java +++ b/pso-docx-core/src/main/java/org/pageseeder/docx/DOCXProcessor.java @@ -107,18 +107,21 @@ public void process() throws IOException { ZipUtils.unzip(this._builder.dotx(), prepacked); File document = new File(prepacked, "word/document.xml"); Files.ensureDirectoryExists(document.getParentFile()); + // prefix template media files with a random string to avoid clashes with PSML images + File mediaFolder = new File(prepacked, "word/media"); + String mediaPrefix = "kwo5nu83zotp2-"; + Files.renameFiles(mediaFolder, mediaPrefix); - // 3. (extra) copy everything from the media folder to prepacked folder + // 4. (extra) copy everything from the media folder to prepacked folder if (this._builder.media() != null) { log("Copy media files"); - File mediaFolder = new File(prepacked, "word/media"); if (!mediaFolder.exists()) { mediaFolder.mkdirs(); } Files.copyDirectory(this._builder.media(), mediaFolder); } - // 4. Unnest the files + // 5. Unnest the files log("Unnest"); Templates unnest = XSLT.getTemplatesFromResource("org/pageseeder/docx/xslt/export-unnest.xsl"); File sourceDocument = this._builder.source(); @@ -127,7 +130,7 @@ public void process() throws IOException { Map noParameters = Collections.emptyMap(); XSLT.transform(sourceDocument, newSourceDocument, unnest, noParameters); - // 5. Process the files + // 6. Process the files log("Process with XSLT"); // Parse templates Templates templates = XSLT.getTemplatesFromResource("org/pageseeder/docx/xslt/export.xsl"); @@ -137,6 +140,7 @@ public void process() throws IOException { parameters.put("_outputfolder", prepacked.toURI().toString()); parameters.put("_dotxfolder", dotx.toURI().toString()); parameters.put("_docxfilename", this._builder.destination().getName()); + parameters.put("_mediaprefix", mediaPrefix); if (this._builder.config() != null) { parameters.put("_configfileurl", this._builder.config().toURI().toString()); } @@ -147,7 +151,7 @@ public void process() throws IOException { // Transform XSLT.transform(newSourceDocument, document, templates, parameters); - // 6. Move or Zip the generated content + // 7. Move or Zip the generated content if (parameters.containsKey("expanded") && parameters.get("expanded").equals("true")) { log("Moving"); prepacked.renameTo(this._builder.destination()); diff --git a/pso-docx-core/src/main/java/org/pageseeder/docx/util/Files.java b/pso-docx-core/src/main/java/org/pageseeder/docx/util/Files.java index ab47103e..95b4ca18 100644 --- a/pso-docx-core/src/main/java/org/pageseeder/docx/util/Files.java +++ b/pso-docx-core/src/main/java/org/pageseeder/docx/util/Files.java @@ -61,6 +61,23 @@ public static void ensureDirectoryExists(File directory) throws DOCXException { } } + /** + * Rename all the files in the specified directory by adding a prefix to them. + * + * @param dir the directory + * @param prefix the prefix + */ + public static void renameFiles(File dir, String prefix) { + if (dir.exists() && dir.isDirectory()) { + File[] children = dir.listFiles(); + if (children != null) { + for (File child : children) { + child.renameTo(new File(dir, prefix + child.getName())); + } + } + } + } + /** * @param source The source folder * @param target The target folder diff --git a/pso-docx-core/src/main/resources/org/pageseeder/docx/xslt/export.xsl b/pso-docx-core/src/main/resources/org/pageseeder/docx/xslt/export.xsl index de1b1583..0589539a 100644 --- a/pso-docx-core/src/main/resources/org/pageseeder/docx/xslt/export.xsl +++ b/pso-docx-core/src/main/resources/org/pageseeder/docx/xslt/export.xsl @@ -88,6 +88,9 @@ + + + diff --git a/pso-docx-core/src/main/resources/org/pageseeder/docx/xslt/export/word-content_types.xsl b/pso-docx-core/src/main/resources/org/pageseeder/docx/xslt/export/word-content_types.xsl index 05ca8ceb..a26ef2ea 100644 --- a/pso-docx-core/src/main/resources/org/pageseeder/docx/xslt/export/word-content_types.xsl +++ b/pso-docx-core/src/main/resources/org/pageseeder/docx/xslt/export/word-content_types.xsl @@ -21,6 +21,7 @@ xmlns:config="http://pageseeder.org/docx/config" xmlns:vt="http://schemas.openxmlformats.org/officeDocument/2006/docPropsVTypes" xmlns:cust="http://schemas.openxmlformats.org/officeDocument/2006/custom-properties" + xmlns:rel="http://schemas.openxmlformats.org/package/2006/relationships" exclude-result-prefixes="#all"> @@ -185,7 +202,7 @@ - + + + + + + + + + + + + + + + + + + diff --git a/version.txt b/version.txt index 3ab09996..925a2b19 100644 --- a/version.txt +++ b/version.txt @@ -1 +1 @@ -0.8.23 \ No newline at end of file +0.8.24 \ No newline at end of file