org.docx4j.model.fields.FieldUpdater Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of docx4j Show documentation
Show all versions of docx4j Show documentation
docx4j is a library which helps you to work with the Office Open
XML file format as used in docx
documents, pptx presentations, and xlsx spreadsheets.
package org.docx4j.model.fields;
import java.util.ArrayList;
import java.util.List;
import javax.xml.transform.TransformerException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.docx4j.TraversalUtil;
import org.docx4j.XmlUtils;
import org.docx4j.jaxb.Context;
import org.docx4j.model.fields.docproperty.DocPropertyResolver;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.JaxbXmlPart;
import org.docx4j.openpackaging.parts.relationships.Namespaces;
import org.docx4j.openpackaging.parts.relationships.RelationshipsPart;
import org.docx4j.relationships.Relationship;
import org.docx4j.wml.CTSimpleField;
import org.docx4j.wml.ContentAccessor;
import org.docx4j.wml.P;
import org.docx4j.wml.R;
import org.docx4j.wml.Text;
/**
* Refreshes the values of certain fields in the
* docx (currently DOCPROPERTY only).
*
* Do this whether they are simple or complex.
*
* This updates the docx. If you don't want to do
* that, apply it to a clone instead.
*
* @author jharrop
*
*/
public class FieldUpdater {
private static Logger log = LoggerFactory.getLogger(FieldUpdater.class);
WordprocessingMLPackage wordMLPackage;
DocPropertyResolver docPropertyResolver;
StringBuilder report = null;
public FieldUpdater(WordprocessingMLPackage wordMLPackage) {
this.wordMLPackage = wordMLPackage;
// docPropsCustomPart = wordMLPackage.getDocPropsCustomPart();
docPropertyResolver = new DocPropertyResolver(wordMLPackage);
}
public void update(boolean processHeadersAndFooters) throws Docx4JException {
report = new StringBuilder();
updatePart(wordMLPackage.getMainDocumentPart() );
if (processHeadersAndFooters) {
RelationshipsPart rp = wordMLPackage.getMainDocumentPart().getRelationshipsPart();
for ( Relationship r : rp.getJaxbElement().getRelationship() ) {
if (r.getType().equals(Namespaces.HEADER)
|| r.getType().equals(Namespaces.FOOTER)) {
JaxbXmlPart part = (JaxbXmlPart)rp.getPart(r);
report.append("\n" + part.getPartName() + "\n");
log.debug("\n" + part.getPartName() + "\n");
updatePart(part );
// performOnInstance(
// ((ContentAccessor)part).getContent() );
}
}
}
log.info(report.toString());
}
public void updatePart(JaxbXmlPart part) throws Docx4JException {
updateSimple(part);
updateComplex(part);
}
public void updateSimple(JaxbXmlPart part) throws Docx4JException {
FldSimpleModel fsm = new FldSimpleModel(); //gets reused
List contentList = ((ContentAccessor)part).getContent();
WordprocessingMLPackage wmlPackage = (WordprocessingMLPackage)part.getPackage();
// find fields
SimpleFieldLocator fl = new SimpleFieldLocator();
new TraversalUtil(contentList, fl);
report.append("\n\nSimple Fields in " + part.getPartName() + "\n");
report.append("============= \n");
report.append("Found " + fl.simpleFields.size() + " simple fields \n ");
for( CTSimpleField simpleField : fl.simpleFields ) {
//System.out.println(XmlUtils.marshaltoString(simpleField, true, true));
// System.out.println(simpleField.getInstr());
if ("DOCPROPERTY".equals(FormattingSwitchHelper.getFldSimpleName(simpleField.getInstr()))) {
//only parse those fields that get processed
try {
fsm.build(simpleField.getInstr());
} catch (TransformerException e) {
e.printStackTrace();
}
String key = fsm.getFldParameters().get(0);
String val;
try {
val = (String) docPropertyResolver.getValue(key);
} catch (FieldValueException e) {
report.append( simpleField.getInstr() + "\n");
report.append( key + " -> NOT FOUND! \n");
continue;
}
if (val==null) {
report.append( simpleField.getInstr() + "\n");
report.append( key + " -> NOT FOUND! \n");
} else {
//docPropsCustomPart.getProperty(key);
// System.out.println(val);
val = FormattingSwitchHelper.applyFormattingSwitch(wmlPackage, fsm, val);
// System.out.println("--> " + val);
report.append( simpleField.getInstr() + "\n");
report.append( "--> " + val + "\n");
R r=null;
if (simpleField.getInstr().toUpperCase().contains("MERGEFORMAT")) {
// find the first run and use the formatting of that
r = getFirstRun(simpleField.getContent());
}
if (r==null) {
r = Context.getWmlObjectFactory().createR();
} else {
r.getContent().clear();
}
simpleField.getContent().clear();
simpleField.getContent().add(r);
Text t = Context.getWmlObjectFactory().createText();
t.setValue(val);
// t.setSpace(value) //TODO
r.getContent().add(t);
// System.out.println(XmlUtils.marshaltoString(simpleField, true, true));
}
} else {
report.append("Ignoring " + simpleField.getInstr() + "\n");
}
}
}
private R getFirstRun(List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy