org.docx4j.convert.out.common.preprocess.FieldsCombiner 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.
/*
Licensed to Plutext Pty Ltd under one or more contributor license agreements.
* This file is part of docx4j.
docx4j is licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.docx4j.convert.out.common.preprocess;
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.JAXBElement;
import javax.xml.namespace.QName;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.docx4j.TraversalUtil;
import org.docx4j.XmlUtils;
import org.docx4j.jaxb.Context;
import org.docx4j.openpackaging.packages.WordprocessingMLPackage;
import org.docx4j.openpackaging.parts.relationships.Namespaces;
import org.docx4j.utils.TraversalUtilVisitor;
import org.docx4j.wml.CTSimpleField;
import org.docx4j.wml.FldChar;
import org.docx4j.wml.P;
import org.docx4j.wml.R;
import org.docx4j.wml.STFldCharType;
import org.docx4j.wml.Text;
/** This class is something like the opposite to the FieldsPreprocessor. It will
* combine complex fields to simple fields. If there are nested fields, then it
* will try to combine the inner fields without touching the outer ones.
*
*/
public class FieldsCombiner {
private static Logger log = LoggerFactory.getLogger(FieldsCombiner.class);
protected static final CombineVisitor COMBINE_VISITOR = new CombineVisitor();
/** Combine complex fields to w:fldSimple
*
*/
public static void process(WordprocessingMLPackage wmlPackage) {
log.info("starting");
TraversalUtil.visit(wmlPackage, false, COMBINE_VISITOR);
if (log.isDebugEnabled()) {
log.debug(XmlUtils.marshaltoString(wmlPackage.getMainDocumentPart().getJaxbElement(),
true, true));
}
}
protected static class CombineVisitor extends TraversalUtilVisitor {
private final static QName _RInstrText_QNAME =
new QName(Namespaces.NS_WORD12, "instrText");
private static final int STATE_NONE = 0;
private static final int STATE_EXPECT_BEGIN = 1;
private static final int STATE_EXPECT_INSTR = 2;
private static final int STATE_EXPECT_RESULT = 4;
@Override
public void apply(P element) {
processContent(element.getContent());
}
protected void processContent(List