Skip to content

Commit

Permalink
Map<QName, CustomXmlPart> answerDomDocs, instead of DOM document, so …
Browse files Browse the repository at this point in the history
…we can use the same itemid.
  • Loading branch information
plutext committed Aug 11, 2018
1 parent 07f809e commit 8b4082a
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 48 deletions.
17 changes: 12 additions & 5 deletions src/main/java/org/docx4j/Docx4J.java
Expand Up @@ -49,6 +49,7 @@
import org.docx4j.model.datastorage.DocxFetcher;
import org.docx4j.model.datastorage.DomToXPathMap;
import org.docx4j.model.datastorage.OpenDoPEHandler;
import org.docx4j.model.datastorage.OpenDoPEHandlerComponents;
import org.docx4j.model.datastorage.OpenDoPEIntegrity;
import org.docx4j.model.datastorage.OpenDoPEIntegrityAfterBinding;
import org.docx4j.model.datastorage.RemovalHandler;
Expand Down Expand Up @@ -471,14 +472,20 @@ public static void bind(WordprocessingMLPackage wmlPackage, Document xmlDocument

StartEvent startEvent = new StartEvent( WellKnownJobTypes.BIND, wmlPackage, WellKnownProcessSteps.BIND_BIND_XML_OpenDoPEHandler );
startEvent.publish();

// since 6.1, component processing happens first,
// and as a discrete step
OpenDoPEHandlerComponents componentsHandler =
new OpenDoPEHandlerComponents(wmlPackage);
if (docxFetcher!=null) {
componentsHandler.setDocxFetcher(docxFetcher);
}
WordprocessingMLPackage tmpMergeResult = componentsHandler.fetchComponents();

// since 3.2.2, OpenDoPEHandler also handles w15 repeatingSection,
// and does that whether or not we have an XPaths part
openDoPEHandler = new OpenDoPEHandler(wmlPackage);
if (docxFetcher!=null) {
openDoPEHandler.setDocxFetcher(docxFetcher);
}
WordprocessingMLPackage tmpMergeResult = openDoPEHandler.preprocess();
openDoPEHandler = new OpenDoPEHandler(tmpMergeResult);
tmpMergeResult = openDoPEHandler.preprocess();

DomToXPathMap domToXPathMap = openDoPEHandler.getDomToXPathMap();

Expand Down
Expand Up @@ -226,15 +226,13 @@ public WordprocessingMLPackage fetchComponents()
// process altChunk
try {
// Use reflection, so docx4j can be built
// by users who don't have the MergeDocx utility
// by users who don't have the Enterprise MergeDocx utility
Class<?> documentBuilder = Class
.forName("com.plutext.merge.altchunk.ProcessAltChunk");
// Method method = documentBuilder.getMethod("merge",
// wmlPkgList.getClass());
Method[] methods = documentBuilder.getMethods();
Method processMethod = null;
for (int j = 0; j < methods.length; j++) {
log.debug(methods[j].getName());
// log.debug(methods[j].getName());
if (methods[j].getName().equals("process")
&& methods[j].getParameterCount()==5) {
processMethod = methods[j];
Expand Down Expand Up @@ -500,14 +498,11 @@ private List<Object> processBindingRoleIfAny(

log.debug("Encountered Conditional: " + tag.getVal());

// TODO later; perhaps take context from condition eg
// xpath="local-name(/yaml/paths[1]/*[1]/*[1])='get'"
// later perhaps, could take context from condition eg
// xpath="local-name(/yaml/paths[1]/*[1]/*[1])='get'"

// // this only handles simple conditions
// Condition c = conditionsMap.get(conditionId);
// if (c == null) {
// log.error("Missing condition " + conditionId);
// }
// but ability to explicity set context on a component
// is effective, so no need for this.

List<Object> newContent = new ArrayList<Object>();
newContent.add(sdt);
Expand Down Expand Up @@ -667,32 +662,6 @@ private List<Object> processOpenDopeComponent(WordprocessingMLPackage srcPackage

}


private List<Object> processOpenDopeRepeat(Object sdt,
Map<String, CustomXmlPart> customXmlDataStorageParts) {

Tag tag = OpenDoPEHandler.getSdtPr(sdt).getTag();

HashMap<String, String> map = QueryString.parseQueryString(
tag.getVal(), true);

String repeatId = map.get(OpenDoPEHandler.BINDING_ROLE_REPEAT);

// Check, whether we are in an old repeat case. These can be removed.
if (StringUtils.isEmpty(repeatId))
return new ArrayList<Object>();

// org.opendope.xpaths.Xpaths.Xpath xpathObj = XPathsPart.getXPathById(
// xPaths, repeatId);
org.opendope.xpaths.Xpaths.Xpath xpathObj = xpathsMap.get(repeatId);

String storeItemId = xpathObj.getDataBinding().getStoreItemID();
String xpath = xpathObj.getDataBinding().getXpath();
String prefixMappings = xpathObj.getDataBinding().getPrefixMappings();

// TODO: set context from xpath
return null;
}
}

}
Expand Up @@ -33,6 +33,7 @@
import org.docx4j.model.datastorage.BindingHandler;
import org.docx4j.model.datastorage.DocxFetcher;
import org.docx4j.model.datastorage.OpenDoPEHandler;
import org.docx4j.model.datastorage.OpenDoPEHandlerComponents;
import org.docx4j.model.datastorage.OpenDoPEIntegrity;
import org.docx4j.model.datastorage.OpenDoPEReverter;
import org.docx4j.model.datastorage.RemovalHandler;
Expand All @@ -42,6 +43,7 @@
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;



/**
* This sample demonstrates populating content controls
* from a custom xml part (based on the xpaths given
Expand Down Expand Up @@ -81,15 +83,37 @@ public static void main(String[] args) throws Exception {

StringBuilder timingSummary = new StringBuilder();

// Process components

// Docx4j 6.1: simplified component process model:
// 1. components don't have to be at the top paragraph level of the content tree,
// BUT:
// 2. component processing is now done before condition/repeat processing
// 3. component processing is not recursive anymore
// 4. components typically use the "main" answer file
long startTime = System.currentTimeMillis();
OpenDoPEHandlerComponents componentsHandler =
new OpenDoPEHandlerComponents(wordMLPackage);
componentsHandler.setDocxFetcher(new MyDocxFetcher() );
wordMLPackage = componentsHandler.fetchComponents();
long endTime = System.currentTimeMillis();
timingSummary.append("Component processing: " + (endTime-startTime));

System.out.println(
XmlUtils.marshaltoString(wordMLPackage.getMainDocumentPart().getJaxbElement(), true, true)
);

SaveToZipFile saver = new SaveToZipFile(wordMLPackage);
saver.save(filepathprefix + "_components_done.docx");
System.out.println("Saved: " + filepathprefix + "_components_done.docx");

// Process conditionals and repeats
long startTime = System.currentTimeMillis();
startTime = System.currentTimeMillis();
OpenDoPEHandler odh = new OpenDoPEHandler(wordMLPackage);
odh.setDocxFetcher(new MyDocxFetcher() );
wordMLPackage = odh.preprocess();
long endTime = System.currentTimeMillis();
timingSummary.append("OpenDoPEHandler: " + (endTime-startTime));

endTime = System.currentTimeMillis();
timingSummary.append("\nOpenDoPEHandler: " + (endTime-startTime));
System.out.println(
XmlUtils.marshaltoString(wordMLPackage.getMainDocumentPart().getJaxbElement(), true, true)
);
Expand All @@ -103,7 +127,7 @@ public static void main(String[] args) throws Exception {
System.out.println(
XmlUtils.marshaltoString(wordMLPackage.getMainDocumentPart().getJaxbElement(), true, true)
);
SaveToZipFile saver = new SaveToZipFile(wordMLPackage);
saver = new SaveToZipFile(wordMLPackage);
saver.save(filepathprefix + "_preprocessed.docx");
System.out.println("Saved: " + filepathprefix + "_preprocessed.docx");

Expand Down

0 comments on commit 8b4082a

Please sign in to comment.