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 all 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.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.CTEndnotes;
import org.docx4j.wml.CTFtnEdn;
import org.docx4j.wml.ContentAccessor;


public final class EndnotesPart extends JaxbXmlPartXPathAware<CTEndnotes> {
public final class EndnotesPart extends JaxbXmlPartXPathAware<CTEndnotes>
implements ContentAccessor<CTFtnEdn> {


/* 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,26 @@ 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<CTFtnEdn> 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;
}
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,21 @@
package org.docx4j.openpackaging.parts.WordprocessingML;


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.CTFootnotes;
import org.docx4j.wml.CTFtnEdn;
import org.docx4j.wml.ContentAccessor;


public final class FootnotesPart extends JaxbXmlPartXPathAware<CTFootnotes> {
// implements ContentAccessor {
public final class FootnotesPart extends JaxbXmlPartXPathAware<CTFootnotes>
implements ContentAccessor<CTFtnEdn> {

/* Unfortunately, this class can't easily implement
* ContentAccessor, because to do that,
Expand All @@ -48,6 +54,11 @@ public final class FootnotesPart extends JaxbXmlPartXPathAware<CTFootnotes> {
* 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 <Object> from
* ContentAccessor.getList return and letting FootnotesPart return list of CTFootnotes seems
* to do the trick!
*
*/


Expand All @@ -72,6 +83,27 @@ public void init() {

}

/**
* Convenience method to getJaxbElement().getFootnote()
* @since 2.8.1
*/
public List<CTFtnEdn> 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;
}

}

// /**
// * Convenience method to getJaxbElement().getFootnote()
// * @since 2.8.1
Expand Down
1 change: 0 additions & 1 deletion src/main/java/org/docx4j/wml/CTFtnEdn.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 2 additions & 3 deletions src/main/java/org/docx4j/wml/ContentAccessor.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
/**
* @since 2.7
*/
public interface ContentAccessor {

public List<Object> getContent();
public interface ContentAccessor<E> {
public List<E> getContent();

}
2 changes: 1 addition & 1 deletion src/main/java/org/docx4j/wml/Ftr.java
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@
})
@XmlRootElement(name = "ftr")
public class Ftr
implements Child, ContentAccessor
implements Child, ContentAccessor<Object>
{

@XmlElementRefs({
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/org/docx4j/wml/P.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
"content"
})
@XmlRootElement(name = "p")
public class P implements Child, ContentAccessor
public class P implements Child, ContentAccessor<Object>
{
private static Logger log = LoggerFactory.getLogger(P.class);

Expand Down Expand Up @@ -469,7 +469,7 @@ public void afterUnmarshal(Unmarshaller unmarshaller, Object parent) {
})
@XmlRootElement(name = "hyperlink")
public static class Hyperlink
implements Child, ContentAccessor
implements Child, ContentAccessor<Object>
{

@XmlElementRefs({
Expand Down