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

org.docx4j.model.fields.ComplexFieldLocator 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: 11.4.11
Show newest version
package org.docx4j.model.fields;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import javax.xml.bind.JAXBElement;

import org.docx4j.TraversalUtil.CallbackImpl;
import org.docx4j.wml.FldChar;
import org.docx4j.wml.P;
import org.docx4j.wml.STFldCharType;
import org.jvnet.jaxb2_commons.ppp.Child;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

	
public class ComplexFieldLocator extends CallbackImpl {
	
	private static Logger log = LoggerFactory.getLogger(ComplexFieldLocator.class);
	
	/**
	 * A list of paragraphs containing field begins.
	 * 
	 * If the paragraph contains 2 fields or nested fields, 
	 * it will still be listed just once
	 */
	List

starts = new ArrayList

(); public List

getStarts() { return starts; } // P currentP; int depth=0; @Override public List apply(Object o) { // System.out.println(o.getClass().getName()); // if (o instanceof P) { // currentP = (P)o; // } if (o instanceof org.docx4j.wml.FldChar) { FldChar fldChar = (FldChar)o; if (fldChar.getFldCharType().equals(STFldCharType.BEGIN) ) { //System.out.println("Found a BEGIN"); depth++; P currentP = pStack.peek(); if (depth==1 && !starts.contains(currentP)) { starts.add(currentP); // System.out.println("Adding " + XmlUtils.marshaltoString(currentP)); } } if (fldChar.getFldCharType().equals(STFldCharType.END) ) { depth--; } } return null; } /* * Need a paragraph stack, to accommodate: * * * */ private LinkedList

pStack = new LinkedList

(); @Override // so we can manage the stack public void walkJAXBElements(Object parent) { if (parent instanceof P) { pStack.push((P)parent); } super.walkJAXBElements(parent); if (parent instanceof P) { pStack.pop(); } } }