org.docx4j.model.datastorage.migration.FromVariableReplacement 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.datastorage.migration;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import org.docx4j.TraversalUtil;
import org.docx4j.TraversalUtil.CallbackImpl;
import org.docx4j.XmlUtils;
import org.docx4j.customXmlProperties.DatastoreItem;
import org.docx4j.customXmlProperties.SchemaRefs;
import org.docx4j.customXmlProperties.SchemaRefs.SchemaRef;
import org.docx4j.jaxb.Context;
import org.docx4j.openpackaging.exceptions.InvalidFormatException;
import org.docx4j.openpackaging.io.SaveToZipFile;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.CustomXmlDataStoragePropertiesPart;
import org.docx4j.openpackaging.parts.opendope.JaxbCustomXmlDataStoragePart;
import org.docx4j.openpackaging.parts.relationships.RelationshipsPart.AddPartBehaviour;
import org.docx4j.wml.P;
import org.docx4j.wml.R;
import org.docx4j.wml.Text;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* This class will help you to migrate
* from a string replacement approach,
* to use of content control data bindings.
*
* After migrating, you'll be able to
* use the OpenDoPE authoring tool to
* add repeats, conditionals, and other
* OpenDoPE features, if you need them.
*
* It assumes your magic strings take the
* form ${--}. If they don't, you'll
* need to modify this class, or use Word
* to search/replace to change them so they
* take that form.
*
* We'd be happy to accept a patch which
* specifies a regex the magic string
* must match.
*
* Limitations: this first version
* operates only on the main document part
* (ie it won't process variables in
* headers/footers, footnotes/endnotes,
* or comments)
*
* @author jharrop
* @since 3.0.0
*/
public class FromVariableReplacement extends AbstractMigrator {
private static Logger log = LoggerFactory.getLogger(FromVariableReplacement.class);
public WordprocessingMLPackage migrate(WordprocessingMLPackage pkgIn) throws Exception {
// TODO - test that OpenDoPE parts aren't already present
// or if they are, that this docx is using our answer format
// (since only that format is supported here)
// Clone it
WordprocessingMLPackage pkgOut = (WordprocessingMLPackage)pkgIn.clone();
// Run the cleaner first
VariablePrepare.prepare(pkgOut);
// Create the CustomXML parts
createParts(pkgOut);
// Operate at the p level
PFinder pFinder = new PFinder();
new TraversalUtil(pkgOut.getMainDocumentPart().getContent(), pFinder);
for ( P p : pFinder.pList) {
List