org.wickedsource.docxstamper.util.walk.CoordinatesWalker Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of docx-stamper Show documentation
Show all versions of docx-stamper Show documentation
Template engine for .docx documents.
The newest version!
package org.wickedsource.docxstamper.util.walk;
import org.docx4j.XmlUtils;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.WordprocessingML.FooterPart;
import org.docx4j.openpackaging.parts.WordprocessingML.HeaderPart;
import org.docx4j.openpackaging.parts.relationships.Namespaces;
import org.docx4j.openpackaging.parts.relationships.RelationshipsPart;
import org.docx4j.relationships.Relationship;
import org.docx4j.wml.*;
import org.wickedsource.docxstamper.api.coordinates.*;
import javax.xml.bind.JAXBElement;
import java.util.ArrayList;
import java.util.List;
public abstract class CoordinatesWalker {
private WordprocessingMLPackage document;
public CoordinatesWalker(WordprocessingMLPackage document) {
this.document = document;
}
public void walk() {
RelationshipsPart relationshipsPart = document.getMainDocumentPart().getRelationshipsPart();
// walk through elements in headers
List headerRelationships = getRelationshipsOfType(document, Namespaces.HEADER);
for (Relationship header : headerRelationships) {
HeaderPart headerPart = (HeaderPart) relationshipsPart.getPart(header.getId());
walkContent(headerPart.getContent());
}
// walk through elements in main document part
walkContent(document.getMainDocumentPart().getContent());
// walk through elements in headers
List footerRelationships = getRelationshipsOfType(document, Namespaces.FOOTER);
for (Relationship footer : footerRelationships) {
FooterPart footerPart = (FooterPart) relationshipsPart.getPart(footer.getId());
walkContent(footerPart.getContent());
}
}
private List getRelationshipsOfType(WordprocessingMLPackage document, String type) {
List allRelationhips = document
.getMainDocumentPart()
.getRelationshipsPart()
.getRelationships()
.getRelationship();
List headerRelationships = new ArrayList<>();
for (Relationship r : allRelationhips) {
if (r.getType().equals(type)) {
headerRelationships.add(r);
}
}
return headerRelationships;
}
private void walkContent(List