Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SDT bind and removal extensions for Footnotes and Endnotes #266

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 3 additions & 11 deletions src/main/java/org/docx4j/Docx4J.java
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}

}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -378,6 +380,10 @@ protected static Set<ContentAccessor> 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));
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
}
}
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/org/docx4j/model/datastorage/OpenDoPEReverter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
}
Expand Down Expand Up @@ -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));
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<CTEndnotes> {
public final class EndnotesPart extends JaxbXmlPartXPathAware<CTEndnotes> implements ContentAccessor {

private ArrayListWml<Object> content;


/* Unfortunately, this class can't easily implement
* ContentAccessor, because to do that,
* both CTFootnotes and CTEndnotes would need
Expand Down Expand Up @@ -70,18 +78,22 @@ public void init() {

}

// /**
// * Convenience method to getJaxbElement().getEndnote()
// * @since 2.8.1
// */
// public List<Object> 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<Object> getContent() {
if (this.getJaxbElement()==null) {
this.setJaxbElement( Context.getWmlObjectFactory().createCTEndnotes() );
}

if (content == null) {
content = new ArrayListWml<Object>(this.getJaxbElement());
}
return this.content;

}


}
Original file line number Diff line number Diff line change
Expand Up @@ -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<CTFootnotes> {
public final class FootnotesPart extends JaxbXmlPartXPathAware<CTFootnotes> implements ContentAccessor {
// implements ContentAccessor {

/* Unfortunately, this class can't easily implement
Expand All @@ -51,6 +56,8 @@ public final class FootnotesPart extends JaxbXmlPartXPathAware<CTFootnotes> {
*/


private ArrayListWml<Object> content;

public FootnotesPart(PartName partName) throws InvalidFormatException {
super(partName);
init();
Expand All @@ -72,6 +79,22 @@ public void init() {

}

/**
* Convenience method to getJaxbElement().getEndnote()
* @since 2.8.1
*/
public List<Object> getContent() {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

By the look of it, this won't work properly. getContent().add(foo) doesn't add foo to the endnote object

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've tried to let FootnotePart and EndnotePart implement the same ContentAccessor as FooterPart and HeaderPart, to handle each part in a similar way. In some previous version, the getContent() method has been un-commented. Is it possible to let FootnotePart and EnddnotePart implement ContentAccessor in a proper way?

If it's not possible to let FootnotePart and EndnotePart implement ContentAccessor, this particullar change can be reverted. The most important part, is the ability to update content controls bindings in these parts.

if (this.getJaxbElement()==null) {
this.setJaxbElement( Context.getWmlObjectFactory().createCTFootnotes() );
}

if (content == null) {
content = new ArrayListWml<Object>(this.getJaxbElement());
}
return this.content;

}

// /**
// * Convenience method to getJaxbElement().getFootnote()
// * @since 2.8.1
Expand Down