All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.docx4j.convert.out.common.preprocess.FieldsCombiner Maven / Gradle / Ivy

Go to download

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.

There is a newer version: 6.1.2
Show newest version
/*
   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 pContent) { List pResult = null; boolean haveChanges = false; boolean inField = false; Object item = null; STFldCharType fldCharType = null; int level = 0; int state = STATE_EXPECT_BEGIN; int markIdx = 0; List resultList = new ArrayList(2); StringBuilder instrTextBuffer = new StringBuilder(128); String tmpInstrText = null; if ((pContent != null) && (!pContent.isEmpty())) { pResult = new ArrayList(pContent.size()); for (int i=0; i 0) { state = STATE_EXPECT_BEGIN; //Having empty (eg. XE) fldSimple causes interesting effects to the //layout in word - for the conversion process it probably makes sense, //but if you try to open the resulting document in word it's pure chaos. if ((!resultList.isEmpty()) && (instrTextBuffer.length() > 0)) { pResult.add(createFldSimple(instrTextBuffer.toString(), resultList)); haveChanges = true; markIdx = i + 1; } instrTextBuffer.setLength(0); resultList.clear(); level--; } } } else { switch (state) { case STATE_EXPECT_INSTR: tmpInstrText = getInstrText((R)item); if (tmpInstrText != null) { instrTextBuffer.append(tmpInstrText); } break; case STATE_EXPECT_RESULT: resultList.add(item); } } } else if ((item instanceof JAXBElement) && (((JAXBElement)item).getValue() instanceof CTSimpleField)){ if (markIdx < i) { copyItems(pContent, markIdx, i, pResult); } pResult.add(item); instrTextBuffer.setLength(0); resultList.clear(); markIdx = i + 1; state = STATE_EXPECT_BEGIN; } else if ((item instanceof JAXBElement) && (((JAXBElement)item).getValue() instanceof P.Hyperlink)){ processContent(((P.Hyperlink)((JAXBElement)item).getValue()).getContent()); } } if (haveChanges) { if (markIdx < pContent.size()) { copyItems(pContent, markIdx, pContent.size(), pResult); } pContent.clear(); pContent.addAll(pResult); } } } private String getInstrText(R run) { List rContent = run.getContent(); Object item = null; Text text = null; for (int i=0; i resultList) { CTSimpleField fldSimple = Context.getWmlObjectFactory().createCTSimpleField(); fldSimple.setInstr(instrText); if ((resultList != null) && (!resultList.isEmpty())) { fldSimple.getContent().addAll(resultList); } return fldSimple; } private void copyItems(List source, int startIdx, int endIdx, List destination) { for (int i=startIdx; i rContent = r.getContent(); Object item = null; if ((rContent != null) && (!rContent.isEmpty())) { for (int i=0; i