Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
68 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
52 changes: 52 additions & 0 deletions
52
docx4j-core/src/main/java/org/docx4j/model/fields/docvariable/DocVariableResolver.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} | ||
|
||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters