Skip to content

Commit

Permalink
Support updating docx from docvars
Browse files Browse the repository at this point in the history
  • Loading branch information
plutext committed Aug 15, 2020
1 parent 5c0c1b9 commit e7c0be6
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 5 deletions.
Expand Up @@ -4,6 +4,7 @@
import org.docx4j.XmlUtils;
import org.docx4j.jaxb.Context;
import org.docx4j.model.fields.docproperty.DocPropertyResolver;
import org.docx4j.model.fields.docvariable.DocVariableResolver;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.JaxbXmlPart;
Expand Down Expand Up @@ -41,6 +42,7 @@ public class FieldUpdater {

WordprocessingMLPackage wordMLPackage;
DocPropertyResolver docPropertyResolver;
DocVariableResolver docVariableResolver;

StringBuilder report = null;

Expand All @@ -51,6 +53,7 @@ public FieldUpdater(WordprocessingMLPackage wordMLPackage) {
this.wordMLPackage = wordMLPackage;
// docPropsCustomPart = wordMLPackage.getDocPropsCustomPart();
docPropertyResolver = new DocPropertyResolver(wordMLPackage);
docVariableResolver = new DocVariableResolver(wordMLPackage);
}

public void update(boolean processHeadersAndFooters) throws Docx4JException {
Expand Down Expand Up @@ -119,9 +122,13 @@ public void updateSimple(JaxbXmlPart part) throws Docx4JException {

String key = fsm.getFldParameters().get(0);

String val;
String val = null;
try {
val = (String) docPropertyResolver.getValue(key);
if (DOCPROPERTY.equals(fldSimpleName) ) {
val = docPropertyResolver.getValue(key);
} else if (DOCVARIABLE.equals(fldSimpleName) ) {
val = docVariableResolver.getValue(key);
}
} catch (FieldValueException e) {
report.append( simpleField.getInstr() + "\n");
report.append( key + " -> NOT FOUND! \n");
Expand Down Expand Up @@ -160,7 +167,7 @@ public void updateSimple(JaxbXmlPart part) throws Docx4JException {

// System.out.println(XmlUtils.marshaltoString(simpleField, true, true));
}

} else {

report.append("Ignoring " + simpleField.getInstr() + "\n");
Expand Down Expand Up @@ -241,7 +248,11 @@ public void updateComplex(JaxbXmlPart part) throws Docx4JException {
if (key.contains("\"") ) log.debug("(quote char will be disregarded)");
}
key = key.replaceAll("\"", "");
val = (String) docPropertyResolver.getValue(key);
if (DOCPROPERTY.equals(fldName) ) {
val = docPropertyResolver.getValue(key);
} else if (DOCVARIABLE.equals(fldName) ) {
val = docVariableResolver.getValue(key);
}
} else {
log.warn("FldParameters null or empty");
}
Expand Down
@@ -0,0 +1,52 @@
package org.docx4j.model.fields.docvariable;

import java.util.HashMap;

import org.docx4j.model.fields.FieldFormattingException;
import org.docx4j.model.fields.FieldValueException;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.wml.CTDocVar;
import org.docx4j.wml.CTDocVars;
import org.docx4j.wml.CTSettings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* a docVar is stored in the docVars element
* in the DocumentSettings part.
*
* @author jharrop
* @since 8.2.2
*/
public class DocVariableResolver {

private static Logger log = LoggerFactory.getLogger(DocVariableResolver.class);

private HashMap<String, String> docVarMap = new HashMap<String, String>();

public DocVariableResolver(WordprocessingMLPackage wordMLPackage) {

CTSettings settings = wordMLPackage.getMainDocumentPart().getDocumentSettingsPart() == null
? null : wordMLPackage.getMainDocumentPart().getDocumentSettingsPart().getJaxbElement();

if (settings!=null && settings.getDocVars()!=null) {
CTDocVars docvars = settings.getDocVars();
for(CTDocVar docvar: docvars.getDocVar()) {
docVarMap.put(docvar.getName(), docvar.getVal() );
}
}

}

public String getValue(String key) throws FieldFormattingException, FieldValueException {

String value = docVarMap.get(key);
if (value==null) {
throw new FieldValueException("No value found for DOCVARIABLE " + key);
} else {
return value;
}
}


}
Expand Up @@ -13,7 +13,7 @@
* This updates the docx. If you don't want to do
* that, apply it to a clone instead.
*/
public class DocPropertyFieldUpdater {
public class FieldUpdaterExample {

public static void main(String[] args) throws Docx4JException {

Expand Down

0 comments on commit e7c0be6

Please sign in to comment.