From f0840d7307f7b9e1ac1210e400d08cdc5fdc35d3 Mon Sep 17 00:00:00 2001 From: Jacob Boje Date: Wed, 4 Oct 2017 14:29:36 +0200 Subject: [PATCH 1/3] Extension to Docx4J sdt binding and removal handlers, with support for FootnotesPart and EndnotesPart. This makes it possible to bind values in these parts as well as remove SDT's from the parts if requested. WML parts for EndnotesPart and FootnotesPart implements ContentAccessor (again..). Docx4J now used RemoveHandler to remove SDT's from supported parts (Main, Footer, Header, Footnotes, Endnotes) --- src/main/java/org/docx4j/Docx4J.java | 14 ++----- .../model/datastorage/BindingHandler.java | 9 ++++- .../model/datastorage/OpenDoPEHandler.java | 6 +++ .../model/datastorage/OpenDoPEIntegrity.java | 6 +++ .../model/datastorage/OpenDoPEReverter.java | 10 +++++ .../model/datastorage/RemovalHandler.java | 6 +++ .../parts/WordprocessingML/EndnotesPart.java | 38 ++++++++++++------- .../parts/WordprocessingML/FootnotesPart.java | 25 +++++++++++- 8 files changed, 88 insertions(+), 26 deletions(-) diff --git a/src/main/java/org/docx4j/Docx4J.java b/src/main/java/org/docx4j/Docx4J.java index a0535f7263..186e92170c 100644 --- a/src/main/java/org/docx4j/Docx4J.java +++ b/src/main/java/org/docx4j/Docx4J.java @@ -497,17 +497,9 @@ protected static String findXPathStorageItemIdInContentControls(WordprocessingML } protected static void removeSDTs(WordprocessingMLPackage wmlPackage)throws Docx4JException { - RemovalHandler removalHandler; - removalHandler = new RemovalHandler(); - removalHandler.removeSDTs(wmlPackage.getMainDocumentPart(), RemovalHandler.Quantifier.ALL, (String[])null); - for (Part part:wmlPackage.getParts().getParts().values()) { - if (part instanceof HeaderPart) { - removalHandler.removeSDTs((HeaderPart)part, RemovalHandler.Quantifier.ALL, (String[])null); - } - else if (part instanceof FooterPart) { - removalHandler.removeSDTs((FooterPart)part, RemovalHandler.Quantifier.ALL, (String[])null); - } - } + RemovalHandler removalHandler; + removalHandler = new RemovalHandler(); + removalHandler.removeSDTs(wmlPackage, RemovalHandler.Quantifier.ALL, (String[])null); } protected static void removeDefinedCustomXmlParts(WordprocessingMLPackage wmlPackage, CustomXmlDataStoragePart customXmlDataStoragePart) { diff --git a/src/main/java/org/docx4j/model/datastorage/BindingHandler.java b/src/main/java/org/docx4j/model/datastorage/BindingHandler.java index bb769ed727..cacb9765a8 100644 --- a/src/main/java/org/docx4j/model/datastorage/BindingHandler.java +++ b/src/main/java/org/docx4j/model/datastorage/BindingHandler.java @@ -32,7 +32,9 @@ import org.docx4j.openpackaging.packages.WordprocessingMLPackage; import org.docx4j.openpackaging.parts.CustomXmlPart; import org.docx4j.openpackaging.parts.JaxbXmlPart; +import org.docx4j.openpackaging.parts.WordprocessingML.EndnotesPart; import org.docx4j.openpackaging.parts.WordprocessingML.FooterPart; +import org.docx4j.openpackaging.parts.WordprocessingML.FootnotesPart; import org.docx4j.openpackaging.parts.WordprocessingML.HeaderPart; import org.docx4j.openpackaging.parts.opendope.XPathsPart; import org.docx4j.openpackaging.parts.relationships.Namespaces; @@ -202,7 +204,12 @@ public void applyBindings() throws Docx4JException { applyBindings((HeaderPart) rp.getPart(r)); } else if (r.getType().equals(Namespaces.FOOTER)) { applyBindings((FooterPart) rp.getPart(r)); - } + } else if (r.getType().equals(Namespaces.FOOTNOTES)) { + applyBindings((FootnotesPart) rp.getPart(r)); + } else if (r.getType().equals(Namespaces.ENDNOTES)) { + applyBindings((EndnotesPart) rp.getPart(r)); + } + } } diff --git a/src/main/java/org/docx4j/model/datastorage/OpenDoPEHandler.java b/src/main/java/org/docx4j/model/datastorage/OpenDoPEHandler.java index 85319b6e99..32b7cd3722 100644 --- a/src/main/java/org/docx4j/model/datastorage/OpenDoPEHandler.java +++ b/src/main/java/org/docx4j/model/datastorage/OpenDoPEHandler.java @@ -50,7 +50,9 @@ import org.docx4j.openpackaging.parts.CustomXmlPart; import org.docx4j.openpackaging.parts.PartName; import org.docx4j.openpackaging.parts.WordprocessingML.AlternativeFormatInputPart; +import org.docx4j.openpackaging.parts.WordprocessingML.EndnotesPart; import org.docx4j.openpackaging.parts.WordprocessingML.FooterPart; +import org.docx4j.openpackaging.parts.WordprocessingML.FootnotesPart; import org.docx4j.openpackaging.parts.WordprocessingML.HeaderPart; import org.docx4j.openpackaging.parts.opendope.ComponentsPart; import org.docx4j.openpackaging.parts.relationships.Namespaces; @@ -378,6 +380,10 @@ protected static Set getParts(WordprocessingMLPackage srcPackag partList.add((HeaderPart) rp.getPart(r)); } else if (r.getType().equals(Namespaces.FOOTER)) { partList.add((FooterPart) rp.getPart(r)); + } else if (r.getType().equals(Namespaces.FOOTNOTES)) { + partList.add((FootnotesPart) rp.getPart(r)); + } else if (r.getType().equals(Namespaces.ENDNOTES)) { + partList.add((EndnotesPart) rp.getPart(r)); } } diff --git a/src/main/java/org/docx4j/model/datastorage/OpenDoPEIntegrity.java b/src/main/java/org/docx4j/model/datastorage/OpenDoPEIntegrity.java index bc4686931c..f96d11de7e 100644 --- a/src/main/java/org/docx4j/model/datastorage/OpenDoPEIntegrity.java +++ b/src/main/java/org/docx4j/model/datastorage/OpenDoPEIntegrity.java @@ -37,7 +37,9 @@ import org.docx4j.openpackaging.exceptions.Docx4JException; import org.docx4j.openpackaging.packages.WordprocessingMLPackage; import org.docx4j.openpackaging.parts.JaxbXmlPart; +import org.docx4j.openpackaging.parts.WordprocessingML.EndnotesPart; import org.docx4j.openpackaging.parts.WordprocessingML.FooterPart; +import org.docx4j.openpackaging.parts.WordprocessingML.FootnotesPart; import org.docx4j.openpackaging.parts.WordprocessingML.HeaderPart; import org.docx4j.openpackaging.parts.relationships.Namespaces; import org.docx4j.openpackaging.parts.relationships.RelationshipsPart; @@ -124,6 +126,10 @@ public void process(WordprocessingMLPackage wordMLPackage) throws Docx4JExceptio process((HeaderPart) rp.getPart(r)); } else if (r.getType().equals(Namespaces.FOOTER)) { process((FooterPart) rp.getPart(r)); + } else if (r.getType().equals(Namespaces.FOOTNOTES)) { + process((FootnotesPart) rp.getPart(r)); + } else if (r.getType().equals(Namespaces.ENDNOTES)) { + process((EndnotesPart) rp.getPart(r)); } } } diff --git a/src/main/java/org/docx4j/model/datastorage/OpenDoPEReverter.java b/src/main/java/org/docx4j/model/datastorage/OpenDoPEReverter.java index fcd9b5bc54..263994f709 100644 --- a/src/main/java/org/docx4j/model/datastorage/OpenDoPEReverter.java +++ b/src/main/java/org/docx4j/model/datastorage/OpenDoPEReverter.java @@ -34,7 +34,9 @@ import org.docx4j.model.sdt.QueryString; import org.docx4j.openpackaging.exceptions.Docx4JException; import org.docx4j.openpackaging.packages.WordprocessingMLPackage; +import org.docx4j.openpackaging.parts.WordprocessingML.EndnotesPart; import org.docx4j.openpackaging.parts.WordprocessingML.FooterPart; +import org.docx4j.openpackaging.parts.WordprocessingML.FootnotesPart; import org.docx4j.openpackaging.parts.WordprocessingML.HeaderPart; import org.docx4j.openpackaging.parts.opendope.XPathsPart; import org.docx4j.openpackaging.parts.relationships.Namespaces; @@ -175,6 +177,10 @@ private void findSdtsInTemplate(WordprocessingMLPackage pkg, TopLevelSdtTemplate findSdtsInTemplatePart((HeaderPart) rp.getPart(r), sdtPrFinder); } else if (r.getType().equals(Namespaces.FOOTER)) { findSdtsInTemplatePart((FooterPart) rp.getPart(r), sdtPrFinder); + } else if (r.getType().equals(Namespaces.FOOTNOTES)) { + findSdtsInTemplatePart((FootnotesPart) rp.getPart(r), sdtPrFinder); + } else if (r.getType().equals(Namespaces.ENDNOTES)) { + findSdtsInTemplatePart((EndnotesPart) rp.getPart(r), sdtPrFinder); } } } @@ -281,6 +287,10 @@ private void handleSdtsInInstance() throws Docx4JException { handleSdtsInInstancePart((HeaderPart) rp.getPart(r)); } else if (r.getType().equals(Namespaces.FOOTER)) { handleSdtsInInstancePart((FooterPart) rp.getPart(r)); + } else if (r.getType().equals(Namespaces.FOOTNOTES)) { + handleSdtsInInstancePart((FootnotesPart) rp.getPart(r)); + } else if (r.getType().equals(Namespaces.ENDNOTES)) { + handleSdtsInInstancePart((EndnotesPart) rp.getPart(r)); } } } diff --git a/src/main/java/org/docx4j/model/datastorage/RemovalHandler.java b/src/main/java/org/docx4j/model/datastorage/RemovalHandler.java index edeb74e61a..210755c40e 100644 --- a/src/main/java/org/docx4j/model/datastorage/RemovalHandler.java +++ b/src/main/java/org/docx4j/model/datastorage/RemovalHandler.java @@ -38,7 +38,9 @@ import org.docx4j.openpackaging.exceptions.Docx4JException; import org.docx4j.openpackaging.packages.WordprocessingMLPackage; import org.docx4j.openpackaging.parts.JaxbXmlPart; +import org.docx4j.openpackaging.parts.WordprocessingML.EndnotesPart; import org.docx4j.openpackaging.parts.WordprocessingML.FooterPart; +import org.docx4j.openpackaging.parts.WordprocessingML.FootnotesPart; import org.docx4j.openpackaging.parts.WordprocessingML.HeaderPart; import org.docx4j.openpackaging.parts.relationships.Namespaces; import org.docx4j.openpackaging.parts.relationships.RelationshipsPart; @@ -136,6 +138,10 @@ public void removeSDTs(WordprocessingMLPackage wordMLPackage, removeSDTs((HeaderPart) rp.getPart(r), quantifier, keys); } else if (r.getType().equals(Namespaces.FOOTER)) { removeSDTs((FooterPart) rp.getPart(r), quantifier, keys); + } else if (r.getType().equals(Namespaces.FOOTNOTES)) { + removeSDTs((FootnotesPart) rp.getPart(r), quantifier, keys); + } else if (r.getType().equals(Namespaces.ENDNOTES)) { + removeSDTs((EndnotesPart) rp.getPart(r), quantifier, keys); } } } diff --git a/src/main/java/org/docx4j/openpackaging/parts/WordprocessingML/EndnotesPart.java b/src/main/java/org/docx4j/openpackaging/parts/WordprocessingML/EndnotesPart.java index 8d79fb384b..ee25c1b54c 100644 --- a/src/main/java/org/docx4j/openpackaging/parts/WordprocessingML/EndnotesPart.java +++ b/src/main/java/org/docx4j/openpackaging/parts/WordprocessingML/EndnotesPart.java @@ -21,15 +21,23 @@ package org.docx4j.openpackaging.parts.WordprocessingML; +import java.util.List; + +import org.docx4j.jaxb.Context; import org.docx4j.openpackaging.exceptions.InvalidFormatException; import org.docx4j.openpackaging.parts.JaxbXmlPartXPathAware; import org.docx4j.openpackaging.parts.PartName; import org.docx4j.openpackaging.parts.relationships.Namespaces; +import org.docx4j.wml.ArrayListWml; import org.docx4j.wml.CTEndnotes; +import org.docx4j.wml.ContentAccessor; -public final class EndnotesPart extends JaxbXmlPartXPathAware { +public final class EndnotesPart extends JaxbXmlPartXPathAware implements ContentAccessor { + private ArrayListWml content; + + /* Unfortunately, this class can't easily implement * ContentAccessor, because to do that, * both CTFootnotes and CTEndnotes would need @@ -70,18 +78,22 @@ public void init() { } -// /** -// * Convenience method to getJaxbElement().getEndnote() -// * @since 2.8.1 -// */ -// public List getContent() { -// -// if (this.getJaxbElement()==null) { -// this.setJaxbElement( Context.getWmlObjectFactory().createCTEndnotes() ); -// } -// -// return this.getJaxbElement().getEndnote(); -// } + + /** + * Convenience method to getJaxbElement().getEndnote() + * @since 2.8.1 + */ + public List getContent() { + if (this.getJaxbElement()==null) { + this.setJaxbElement( Context.getWmlObjectFactory().createCTEndnotes() ); + } + + if (content == null) { + content = new ArrayListWml(this.getJaxbElement()); + } + return this.content; + + } } diff --git a/src/main/java/org/docx4j/openpackaging/parts/WordprocessingML/FootnotesPart.java b/src/main/java/org/docx4j/openpackaging/parts/WordprocessingML/FootnotesPart.java index c11f52b990..01b60db68a 100644 --- a/src/main/java/org/docx4j/openpackaging/parts/WordprocessingML/FootnotesPart.java +++ b/src/main/java/org/docx4j/openpackaging/parts/WordprocessingML/FootnotesPart.java @@ -21,14 +21,19 @@ package org.docx4j.openpackaging.parts.WordprocessingML; +import java.util.List; + +import org.docx4j.jaxb.Context; import org.docx4j.openpackaging.exceptions.InvalidFormatException; import org.docx4j.openpackaging.parts.JaxbXmlPartXPathAware; import org.docx4j.openpackaging.parts.PartName; import org.docx4j.openpackaging.parts.relationships.Namespaces; +import org.docx4j.wml.ArrayListWml; import org.docx4j.wml.CTFootnotes; +import org.docx4j.wml.ContentAccessor; -public final class FootnotesPart extends JaxbXmlPartXPathAware { +public final class FootnotesPart extends JaxbXmlPartXPathAware implements ContentAccessor { // implements ContentAccessor { /* Unfortunately, this class can't easily implement @@ -51,6 +56,8 @@ public final class FootnotesPart extends JaxbXmlPartXPathAware { */ + private ArrayListWml content; + public FootnotesPart(PartName partName) throws InvalidFormatException { super(partName); init(); @@ -72,6 +79,22 @@ public void init() { } + /** + * Convenience method to getJaxbElement().getEndnote() + * @since 2.8.1 + */ + public List getContent() { + if (this.getJaxbElement()==null) { + this.setJaxbElement( Context.getWmlObjectFactory().createCTFootnotes() ); + } + + if (content == null) { + content = new ArrayListWml(this.getJaxbElement()); + } + return this.content; + + } + // /** // * Convenience method to getJaxbElement().getFootnote() // * @since 2.8.1 From f0158c08df2c7674fd4429e920e9373db28641bf Mon Sep 17 00:00:00 2001 From: Jacob Boje Date: Wed, 4 Oct 2017 14:29:36 +0200 Subject: [PATCH 2/3] Extension to Docx4J sdt binding and removal handlers, with support for FootnotesPart and EndnotesPart. This makes it possible to bind values in these parts as well as remove SDT's from the parts if requested. WML parts for EndnotesPart and FootnotesPart implements ContentAccessor (again..). Docx4J now used RemoveHandler to remove SDT's from supported parts (Main, Footer, Header, Footnotes, Endnotes) --- src/main/java/org/docx4j/Docx4J.java | 14 ++----- .../model/datastorage/BindingHandler.java | 9 ++++- .../model/datastorage/OpenDoPEHandler.java | 6 +++ .../model/datastorage/OpenDoPEIntegrity.java | 6 +++ .../model/datastorage/OpenDoPEReverter.java | 10 +++++ .../model/datastorage/RemovalHandler.java | 6 +++ .../parts/WordprocessingML/EndnotesPart.java | 38 ++++++++++++------- .../parts/WordprocessingML/FootnotesPart.java | 25 +++++++++++- 8 files changed, 88 insertions(+), 26 deletions(-) diff --git a/src/main/java/org/docx4j/Docx4J.java b/src/main/java/org/docx4j/Docx4J.java index a0535f7263..186e92170c 100644 --- a/src/main/java/org/docx4j/Docx4J.java +++ b/src/main/java/org/docx4j/Docx4J.java @@ -497,17 +497,9 @@ protected static String findXPathStorageItemIdInContentControls(WordprocessingML } protected static void removeSDTs(WordprocessingMLPackage wmlPackage)throws Docx4JException { - RemovalHandler removalHandler; - removalHandler = new RemovalHandler(); - removalHandler.removeSDTs(wmlPackage.getMainDocumentPart(), RemovalHandler.Quantifier.ALL, (String[])null); - for (Part part:wmlPackage.getParts().getParts().values()) { - if (part instanceof HeaderPart) { - removalHandler.removeSDTs((HeaderPart)part, RemovalHandler.Quantifier.ALL, (String[])null); - } - else if (part instanceof FooterPart) { - removalHandler.removeSDTs((FooterPart)part, RemovalHandler.Quantifier.ALL, (String[])null); - } - } + RemovalHandler removalHandler; + removalHandler = new RemovalHandler(); + removalHandler.removeSDTs(wmlPackage, RemovalHandler.Quantifier.ALL, (String[])null); } protected static void removeDefinedCustomXmlParts(WordprocessingMLPackage wmlPackage, CustomXmlDataStoragePart customXmlDataStoragePart) { diff --git a/src/main/java/org/docx4j/model/datastorage/BindingHandler.java b/src/main/java/org/docx4j/model/datastorage/BindingHandler.java index bb769ed727..cacb9765a8 100644 --- a/src/main/java/org/docx4j/model/datastorage/BindingHandler.java +++ b/src/main/java/org/docx4j/model/datastorage/BindingHandler.java @@ -32,7 +32,9 @@ import org.docx4j.openpackaging.packages.WordprocessingMLPackage; import org.docx4j.openpackaging.parts.CustomXmlPart; import org.docx4j.openpackaging.parts.JaxbXmlPart; +import org.docx4j.openpackaging.parts.WordprocessingML.EndnotesPart; import org.docx4j.openpackaging.parts.WordprocessingML.FooterPart; +import org.docx4j.openpackaging.parts.WordprocessingML.FootnotesPart; import org.docx4j.openpackaging.parts.WordprocessingML.HeaderPart; import org.docx4j.openpackaging.parts.opendope.XPathsPart; import org.docx4j.openpackaging.parts.relationships.Namespaces; @@ -202,7 +204,12 @@ public void applyBindings() throws Docx4JException { applyBindings((HeaderPart) rp.getPart(r)); } else if (r.getType().equals(Namespaces.FOOTER)) { applyBindings((FooterPart) rp.getPart(r)); - } + } else if (r.getType().equals(Namespaces.FOOTNOTES)) { + applyBindings((FootnotesPart) rp.getPart(r)); + } else if (r.getType().equals(Namespaces.ENDNOTES)) { + applyBindings((EndnotesPart) rp.getPart(r)); + } + } } diff --git a/src/main/java/org/docx4j/model/datastorage/OpenDoPEHandler.java b/src/main/java/org/docx4j/model/datastorage/OpenDoPEHandler.java index 85319b6e99..32b7cd3722 100644 --- a/src/main/java/org/docx4j/model/datastorage/OpenDoPEHandler.java +++ b/src/main/java/org/docx4j/model/datastorage/OpenDoPEHandler.java @@ -50,7 +50,9 @@ import org.docx4j.openpackaging.parts.CustomXmlPart; import org.docx4j.openpackaging.parts.PartName; import org.docx4j.openpackaging.parts.WordprocessingML.AlternativeFormatInputPart; +import org.docx4j.openpackaging.parts.WordprocessingML.EndnotesPart; import org.docx4j.openpackaging.parts.WordprocessingML.FooterPart; +import org.docx4j.openpackaging.parts.WordprocessingML.FootnotesPart; import org.docx4j.openpackaging.parts.WordprocessingML.HeaderPart; import org.docx4j.openpackaging.parts.opendope.ComponentsPart; import org.docx4j.openpackaging.parts.relationships.Namespaces; @@ -378,6 +380,10 @@ protected static Set getParts(WordprocessingMLPackage srcPackag partList.add((HeaderPart) rp.getPart(r)); } else if (r.getType().equals(Namespaces.FOOTER)) { partList.add((FooterPart) rp.getPart(r)); + } else if (r.getType().equals(Namespaces.FOOTNOTES)) { + partList.add((FootnotesPart) rp.getPart(r)); + } else if (r.getType().equals(Namespaces.ENDNOTES)) { + partList.add((EndnotesPart) rp.getPart(r)); } } diff --git a/src/main/java/org/docx4j/model/datastorage/OpenDoPEIntegrity.java b/src/main/java/org/docx4j/model/datastorage/OpenDoPEIntegrity.java index bc4686931c..f96d11de7e 100644 --- a/src/main/java/org/docx4j/model/datastorage/OpenDoPEIntegrity.java +++ b/src/main/java/org/docx4j/model/datastorage/OpenDoPEIntegrity.java @@ -37,7 +37,9 @@ import org.docx4j.openpackaging.exceptions.Docx4JException; import org.docx4j.openpackaging.packages.WordprocessingMLPackage; import org.docx4j.openpackaging.parts.JaxbXmlPart; +import org.docx4j.openpackaging.parts.WordprocessingML.EndnotesPart; import org.docx4j.openpackaging.parts.WordprocessingML.FooterPart; +import org.docx4j.openpackaging.parts.WordprocessingML.FootnotesPart; import org.docx4j.openpackaging.parts.WordprocessingML.HeaderPart; import org.docx4j.openpackaging.parts.relationships.Namespaces; import org.docx4j.openpackaging.parts.relationships.RelationshipsPart; @@ -124,6 +126,10 @@ public void process(WordprocessingMLPackage wordMLPackage) throws Docx4JExceptio process((HeaderPart) rp.getPart(r)); } else if (r.getType().equals(Namespaces.FOOTER)) { process((FooterPart) rp.getPart(r)); + } else if (r.getType().equals(Namespaces.FOOTNOTES)) { + process((FootnotesPart) rp.getPart(r)); + } else if (r.getType().equals(Namespaces.ENDNOTES)) { + process((EndnotesPart) rp.getPart(r)); } } } diff --git a/src/main/java/org/docx4j/model/datastorage/OpenDoPEReverter.java b/src/main/java/org/docx4j/model/datastorage/OpenDoPEReverter.java index fcd9b5bc54..263994f709 100644 --- a/src/main/java/org/docx4j/model/datastorage/OpenDoPEReverter.java +++ b/src/main/java/org/docx4j/model/datastorage/OpenDoPEReverter.java @@ -34,7 +34,9 @@ import org.docx4j.model.sdt.QueryString; import org.docx4j.openpackaging.exceptions.Docx4JException; import org.docx4j.openpackaging.packages.WordprocessingMLPackage; +import org.docx4j.openpackaging.parts.WordprocessingML.EndnotesPart; import org.docx4j.openpackaging.parts.WordprocessingML.FooterPart; +import org.docx4j.openpackaging.parts.WordprocessingML.FootnotesPart; import org.docx4j.openpackaging.parts.WordprocessingML.HeaderPart; import org.docx4j.openpackaging.parts.opendope.XPathsPart; import org.docx4j.openpackaging.parts.relationships.Namespaces; @@ -175,6 +177,10 @@ private void findSdtsInTemplate(WordprocessingMLPackage pkg, TopLevelSdtTemplate findSdtsInTemplatePart((HeaderPart) rp.getPart(r), sdtPrFinder); } else if (r.getType().equals(Namespaces.FOOTER)) { findSdtsInTemplatePart((FooterPart) rp.getPart(r), sdtPrFinder); + } else if (r.getType().equals(Namespaces.FOOTNOTES)) { + findSdtsInTemplatePart((FootnotesPart) rp.getPart(r), sdtPrFinder); + } else if (r.getType().equals(Namespaces.ENDNOTES)) { + findSdtsInTemplatePart((EndnotesPart) rp.getPart(r), sdtPrFinder); } } } @@ -281,6 +287,10 @@ private void handleSdtsInInstance() throws Docx4JException { handleSdtsInInstancePart((HeaderPart) rp.getPart(r)); } else if (r.getType().equals(Namespaces.FOOTER)) { handleSdtsInInstancePart((FooterPart) rp.getPart(r)); + } else if (r.getType().equals(Namespaces.FOOTNOTES)) { + handleSdtsInInstancePart((FootnotesPart) rp.getPart(r)); + } else if (r.getType().equals(Namespaces.ENDNOTES)) { + handleSdtsInInstancePart((EndnotesPart) rp.getPart(r)); } } } diff --git a/src/main/java/org/docx4j/model/datastorage/RemovalHandler.java b/src/main/java/org/docx4j/model/datastorage/RemovalHandler.java index edeb74e61a..210755c40e 100644 --- a/src/main/java/org/docx4j/model/datastorage/RemovalHandler.java +++ b/src/main/java/org/docx4j/model/datastorage/RemovalHandler.java @@ -38,7 +38,9 @@ import org.docx4j.openpackaging.exceptions.Docx4JException; import org.docx4j.openpackaging.packages.WordprocessingMLPackage; import org.docx4j.openpackaging.parts.JaxbXmlPart; +import org.docx4j.openpackaging.parts.WordprocessingML.EndnotesPart; import org.docx4j.openpackaging.parts.WordprocessingML.FooterPart; +import org.docx4j.openpackaging.parts.WordprocessingML.FootnotesPart; import org.docx4j.openpackaging.parts.WordprocessingML.HeaderPart; import org.docx4j.openpackaging.parts.relationships.Namespaces; import org.docx4j.openpackaging.parts.relationships.RelationshipsPart; @@ -136,6 +138,10 @@ public void removeSDTs(WordprocessingMLPackage wordMLPackage, removeSDTs((HeaderPart) rp.getPart(r), quantifier, keys); } else if (r.getType().equals(Namespaces.FOOTER)) { removeSDTs((FooterPart) rp.getPart(r), quantifier, keys); + } else if (r.getType().equals(Namespaces.FOOTNOTES)) { + removeSDTs((FootnotesPart) rp.getPart(r), quantifier, keys); + } else if (r.getType().equals(Namespaces.ENDNOTES)) { + removeSDTs((EndnotesPart) rp.getPart(r), quantifier, keys); } } } diff --git a/src/main/java/org/docx4j/openpackaging/parts/WordprocessingML/EndnotesPart.java b/src/main/java/org/docx4j/openpackaging/parts/WordprocessingML/EndnotesPart.java index 8d79fb384b..ee25c1b54c 100644 --- a/src/main/java/org/docx4j/openpackaging/parts/WordprocessingML/EndnotesPart.java +++ b/src/main/java/org/docx4j/openpackaging/parts/WordprocessingML/EndnotesPart.java @@ -21,15 +21,23 @@ package org.docx4j.openpackaging.parts.WordprocessingML; +import java.util.List; + +import org.docx4j.jaxb.Context; import org.docx4j.openpackaging.exceptions.InvalidFormatException; import org.docx4j.openpackaging.parts.JaxbXmlPartXPathAware; import org.docx4j.openpackaging.parts.PartName; import org.docx4j.openpackaging.parts.relationships.Namespaces; +import org.docx4j.wml.ArrayListWml; import org.docx4j.wml.CTEndnotes; +import org.docx4j.wml.ContentAccessor; -public final class EndnotesPart extends JaxbXmlPartXPathAware { +public final class EndnotesPart extends JaxbXmlPartXPathAware implements ContentAccessor { + private ArrayListWml content; + + /* Unfortunately, this class can't easily implement * ContentAccessor, because to do that, * both CTFootnotes and CTEndnotes would need @@ -70,18 +78,22 @@ public void init() { } -// /** -// * Convenience method to getJaxbElement().getEndnote() -// * @since 2.8.1 -// */ -// public List getContent() { -// -// if (this.getJaxbElement()==null) { -// this.setJaxbElement( Context.getWmlObjectFactory().createCTEndnotes() ); -// } -// -// return this.getJaxbElement().getEndnote(); -// } + + /** + * Convenience method to getJaxbElement().getEndnote() + * @since 2.8.1 + */ + public List getContent() { + if (this.getJaxbElement()==null) { + this.setJaxbElement( Context.getWmlObjectFactory().createCTEndnotes() ); + } + + if (content == null) { + content = new ArrayListWml(this.getJaxbElement()); + } + return this.content; + + } } diff --git a/src/main/java/org/docx4j/openpackaging/parts/WordprocessingML/FootnotesPart.java b/src/main/java/org/docx4j/openpackaging/parts/WordprocessingML/FootnotesPart.java index c11f52b990..01b60db68a 100644 --- a/src/main/java/org/docx4j/openpackaging/parts/WordprocessingML/FootnotesPart.java +++ b/src/main/java/org/docx4j/openpackaging/parts/WordprocessingML/FootnotesPart.java @@ -21,14 +21,19 @@ package org.docx4j.openpackaging.parts.WordprocessingML; +import java.util.List; + +import org.docx4j.jaxb.Context; import org.docx4j.openpackaging.exceptions.InvalidFormatException; import org.docx4j.openpackaging.parts.JaxbXmlPartXPathAware; import org.docx4j.openpackaging.parts.PartName; import org.docx4j.openpackaging.parts.relationships.Namespaces; +import org.docx4j.wml.ArrayListWml; import org.docx4j.wml.CTFootnotes; +import org.docx4j.wml.ContentAccessor; -public final class FootnotesPart extends JaxbXmlPartXPathAware { +public final class FootnotesPart extends JaxbXmlPartXPathAware implements ContentAccessor { // implements ContentAccessor { /* Unfortunately, this class can't easily implement @@ -51,6 +56,8 @@ public final class FootnotesPart extends JaxbXmlPartXPathAware { */ + private ArrayListWml content; + public FootnotesPart(PartName partName) throws InvalidFormatException { super(partName); init(); @@ -72,6 +79,22 @@ public void init() { } + /** + * Convenience method to getJaxbElement().getEndnote() + * @since 2.8.1 + */ + public List getContent() { + if (this.getJaxbElement()==null) { + this.setJaxbElement( Context.getWmlObjectFactory().createCTFootnotes() ); + } + + if (content == null) { + content = new ArrayListWml(this.getJaxbElement()); + } + return this.content; + + } + // /** // * Convenience method to getJaxbElement().getFootnote() // * @since 2.8.1 From 5adfea6a819e67c445f1b05815d42d605549c1e4 Mon Sep 17 00:00:00 2001 From: Jacob Boje Date: Fri, 25 Jan 2019 15:18:42 +0100 Subject: [PATCH 3/3] ContentAccessor changed to use generic type. EndnotesPart, FootnotesPart now implements ContentAccessor in a proper way. All this to support OpenDope processing of SDTs in Footnotes and Endnotes part --- .../parts/WordprocessingML/EndnotesPart.java | 32 ++++++++------- .../parts/WordprocessingML/FootnotesPart.java | 41 +++++++++++-------- src/main/java/org/docx4j/wml/CTFtnEdn.java | 1 - .../java/org/docx4j/wml/ContentAccessor.java | 5 +-- src/main/java/org/docx4j/wml/Ftr.java | 2 +- src/main/java/org/docx4j/wml/P.java | 4 +- 6 files changed, 48 insertions(+), 37 deletions(-) diff --git a/src/main/java/org/docx4j/openpackaging/parts/WordprocessingML/EndnotesPart.java b/src/main/java/org/docx4j/openpackaging/parts/WordprocessingML/EndnotesPart.java index ee25c1b54c..9cc620428e 100644 --- a/src/main/java/org/docx4j/openpackaging/parts/WordprocessingML/EndnotesPart.java +++ b/src/main/java/org/docx4j/openpackaging/parts/WordprocessingML/EndnotesPart.java @@ -24,19 +24,19 @@ import java.util.List; import org.docx4j.jaxb.Context; +import org.docx4j.openpackaging.exceptions.Docx4JException; import org.docx4j.openpackaging.exceptions.InvalidFormatException; import org.docx4j.openpackaging.parts.JaxbXmlPartXPathAware; import org.docx4j.openpackaging.parts.PartName; import org.docx4j.openpackaging.parts.relationships.Namespaces; -import org.docx4j.wml.ArrayListWml; import org.docx4j.wml.CTEndnotes; +import org.docx4j.wml.CTFtnEdn; import org.docx4j.wml.ContentAccessor; -public final class EndnotesPart extends JaxbXmlPartXPathAware implements ContentAccessor { +public final class EndnotesPart extends JaxbXmlPartXPathAware + implements ContentAccessor { - private ArrayListWml content; - /* Unfortunately, this class can't easily implement * ContentAccessor, because to do that, @@ -83,16 +83,20 @@ public void init() { * Convenience method to getJaxbElement().getEndnote() * @since 2.8.1 */ - public List getContent() { - if (this.getJaxbElement()==null) { - this.setJaxbElement( Context.getWmlObjectFactory().createCTEndnotes() ); - } - - if (content == null) { - content = new ArrayListWml(this.getJaxbElement()); - } - return this.content; - + public List getContent() { + try { + CTEndnotes contents = this.getContents(); + if (contents==null) { + contents = Context.getWmlObjectFactory().createCTEndnotes(); + this.setContents(contents); + } + + return contents.getEndnote(); + } catch (Docx4JException e) { + // Similar exception handling as in deprectad JaxbXmlPart.getJaxbElement(). + log.error(e.getMessage(), e); + return null; + } } diff --git a/src/main/java/org/docx4j/openpackaging/parts/WordprocessingML/FootnotesPart.java b/src/main/java/org/docx4j/openpackaging/parts/WordprocessingML/FootnotesPart.java index 01b60db68a..7f07a76796 100644 --- a/src/main/java/org/docx4j/openpackaging/parts/WordprocessingML/FootnotesPart.java +++ b/src/main/java/org/docx4j/openpackaging/parts/WordprocessingML/FootnotesPart.java @@ -24,17 +24,18 @@ import java.util.List; import org.docx4j.jaxb.Context; +import org.docx4j.openpackaging.exceptions.Docx4JException; import org.docx4j.openpackaging.exceptions.InvalidFormatException; import org.docx4j.openpackaging.parts.JaxbXmlPartXPathAware; import org.docx4j.openpackaging.parts.PartName; import org.docx4j.openpackaging.parts.relationships.Namespaces; -import org.docx4j.wml.ArrayListWml; import org.docx4j.wml.CTFootnotes; +import org.docx4j.wml.CTFtnEdn; import org.docx4j.wml.ContentAccessor; -public final class FootnotesPart extends JaxbXmlPartXPathAware implements ContentAccessor { - // implements ContentAccessor { +public final class FootnotesPart extends JaxbXmlPartXPathAware + implements ContentAccessor { /* Unfortunately, this class can't easily implement * ContentAccessor, because to do that, @@ -53,11 +54,14 @@ public final class FootnotesPart extends JaxbXmlPartXPathAware impl * the first thing to do would be to change wml.xsd, so we generate * distinct classes CTFtn and CTEdn. But that would be backwards incompatible, * unless we also retained CTFtnEdn in some fashion? + * + * JABOJ78: Is this really that hard to accomplished. By removing generic from + * ContentAccessor.getList return and letting FootnotesPart return list of CTFootnotes seems + * to do the trick! + * */ - private ArrayListWml content; - public FootnotesPart(PartName partName) throws InvalidFormatException { super(partName); init(); @@ -79,20 +83,25 @@ public void init() { } - /** - * Convenience method to getJaxbElement().getEndnote() + /** + * Convenience method to getJaxbElement().getFootnote() * @since 2.8.1 */ - public List getContent() { - if (this.getJaxbElement()==null) { - this.setJaxbElement( Context.getWmlObjectFactory().createCTFootnotes() ); - } - - if (content == null) { - content = new ArrayListWml(this.getJaxbElement()); - } - return this.content; + public List getContent() { + try { + CTFootnotes contents = this.getContents(); + if (contents==null) { + contents = Context.getWmlObjectFactory().createCTFootnotes(); + this.setContents(contents); + } + return contents.getFootnote(); + } catch (Docx4JException e) { + // Similar exception handling as in deprectad JaxbXmlPart.getJaxbElement(). + log.error(e.getMessage(), e); + return null; + } + } // /** diff --git a/src/main/java/org/docx4j/wml/CTFtnEdn.java b/src/main/java/org/docx4j/wml/CTFtnEdn.java index 0086f372e9..917aedaf1d 100644 --- a/src/main/java/org/docx4j/wml/CTFtnEdn.java +++ b/src/main/java/org/docx4j/wml/CTFtnEdn.java @@ -22,7 +22,6 @@ package org.docx4j.wml; import java.math.BigInteger; -import java.util.ArrayList; import java.util.List; import javax.xml.bind.JAXBElement; diff --git a/src/main/java/org/docx4j/wml/ContentAccessor.java b/src/main/java/org/docx4j/wml/ContentAccessor.java index ca325df75d..91ba95772e 100644 --- a/src/main/java/org/docx4j/wml/ContentAccessor.java +++ b/src/main/java/org/docx4j/wml/ContentAccessor.java @@ -5,8 +5,7 @@ /** * @since 2.7 */ -public interface ContentAccessor { - - public List getContent(); +public interface ContentAccessor { + public List getContent(); } diff --git a/src/main/java/org/docx4j/wml/Ftr.java b/src/main/java/org/docx4j/wml/Ftr.java index aa3308acc3..9a0ba148b7 100644 --- a/src/main/java/org/docx4j/wml/Ftr.java +++ b/src/main/java/org/docx4j/wml/Ftr.java @@ -64,7 +64,7 @@ }) @XmlRootElement(name = "ftr") public class Ftr - implements Child, ContentAccessor + implements Child, ContentAccessor { @XmlElementRefs({ diff --git a/src/main/java/org/docx4j/wml/P.java b/src/main/java/org/docx4j/wml/P.java index 2c85331e98..1d3fa44565 100644 --- a/src/main/java/org/docx4j/wml/P.java +++ b/src/main/java/org/docx4j/wml/P.java @@ -75,7 +75,7 @@ "content" }) @XmlRootElement(name = "p") -public class P implements Child, ContentAccessor +public class P implements Child, ContentAccessor { private static Logger log = LoggerFactory.getLogger(P.class); @@ -469,7 +469,7 @@ public void afterUnmarshal(Unmarshaller unmarshaller, Object parent) { }) @XmlRootElement(name = "hyperlink") public static class Hyperlink - implements Child, ContentAccessor + implements Child, ContentAccessor { @XmlElementRefs({