// in the first section
hfTemplates = new HashMap<CTRel, JaxbXmlPart>();
SectionWrapper sw = input.getDocumentModel().getSections().get(0);
SectPr sectPr = sw.getSectPr();
List<CTRel> hdrFtrRefs = sectPr.getEGHdrFtrReferences();
titlePage = sectPr.getTitlePg();
for (CTRel rel : hdrFtrRefs) {
String relId = rel.getId();
log.debug("for h|f relId: " + relId);
JaxbXmlPart part = (JaxbXmlPart)input.getMainDocumentPart().getRelationshipsPart().getPart(relId);
FieldsPreprocessor.complexifyFields(part );
log.debug("complexified: " + XmlUtils.marshaltoString(part.getJaxbElement(), true));
hfTemplates.put(rel, part);
}
}
// Create WordprocessingMLPackage target, by cloning
OpcPackage result = null;
ByteArrayOutputStream baos = new ByteArrayOutputStream();
SaveToZipFile saver = new SaveToZipFile(input);
saver.save(baos);
byte[] template = baos.toByteArray();
WordprocessingMLPackage target = WordprocessingMLPackage.load(
new ByteArrayInputStream(template));
// populate main document part
SectPr documentSeparator = getDocumentSeparator(target);
if (processHeadersAndFooters) {
if (titlePage!=null
&& titlePage.isVal()) {
documentSeparator.setTitlePg(titlePage);
}
documentSeparator.getEGHdrFtrReferences().clear();
}
target.getMainDocumentPart().getContent().clear();
/*
* What we're doing, effectively, is doing the
* main content in a single hit (ie for all
* instances), and then, for each instance,
* doing the headers/footers.
*
* It is this way because that is how the code
* has evolved.
*
* Since we have to do the headers/footers
* instance by instance, it would probably
* be neater to do the main content at
* the same time (ie instead of using
* performOverList at the start of this method).
*/
int i = 0;
for (List<Object> content : mdpResults) {
// now inject the content
target.getMainDocumentPart().getContent().addAll(content);
log.debug(XmlUtils.marshaltoString(target.getMainDocumentPart().getJaxbElement(), true, true) );
// add sectPr to final paragraph
Object last = content.get( content.size()-1);
P lastP = null;
if (last instanceof P) {
lastP = (P)last;
} else {
lastP = Context.getWmlObjectFactory().createP();
target.getMainDocumentPart().getContent().add(lastP);
}
if (lastP.getPPr()==null) {
lastP.setPPr(Context.getWmlObjectFactory().createPPr());
}
SectPr thisSection = XmlUtils.deepCopy(documentSeparator);
lastP.getPPr().setSectPr(thisSection);
if (processHeadersAndFooters) {
for( CTRel ctRel : hfTemplates.keySet()) {
// Create a suitable part
JaxbXmlPart part = hfTemplates.get(ctRel);
JaxbXmlPart clonedPart = null;
if (part instanceof HeaderPart) {
clonedPart = new HeaderPart();
clonedPart.setJaxbElement(Context.getWmlObjectFactory().createHdr());
} else if (part instanceof FooterPart) {
clonedPart = new FooterPart();
clonedPart.setJaxbElement(Context.getWmlObjectFactory().createFtr());
}
// Populate it
List<Object> newContent = performOnInstance(input,
((ContentAccessor)part).getContent(),
data.get(i), formTextFieldNames );
((ContentAccessor)clonedPart).getContent().addAll(newContent);
// Add it
Relationship rel = target.getMainDocumentPart().addTargetPart(clonedPart, AddPartBehaviour.RENAME_IF_NAME_EXISTS);
// Now add CTRel!
CTRel newHfRef = XmlUtils.deepCopy(ctRel);
newHfRef.setId(rel.getId());
thisSection.getEGHdrFtrReferences().add(newHfRef);
}
}
i++;
}