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

org.docx4j.finders.RangeFinder 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.5.0
Show newest version
package org.docx4j.finders;

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

import javax.xml.bind.JAXBElement;

import org.docx4j.XmlUtils;
import org.docx4j.TraversalUtil.CallbackImpl;
import org.docx4j.wml.CTBookmark;
import org.docx4j.wml.CTMarkupRange;
import org.docx4j.wml.Text;
import org.jvnet.jaxb2_commons.ppp.Child;

/**
 * This is a simplified version of 
 * org.docx4j.model.datastorage.BookmarkRenumber.RangeTraverser
 */
public class RangeFinder extends CallbackImpl {
	
	List starts = new ArrayList();
	/**
	 * @return the starts
	 */
	public List getStarts() {
		return starts;
	}

	List ends   = new ArrayList();
	/**
	 * @return the ends
	 */
	public List getEnds() {
		return ends;
	}

	List refs   = new ArrayList();
	
	/**
	 * @return the refs
	 */
	public List getRefs() {
		return refs;
	}

	String startElement; 
	String endElement; 


	public RangeFinder(String startElement, String endElement) {
		
		this.startElement = "org.docx4j.wml." + startElement;
		this.endElement   = "org.docx4j.wml." + endElement;
	}

	@Override
	public List apply(Object o) {
		
		if (o.getClass().getName().equals(startElement)) {
			if (o instanceof CTBookmark) { 
				CTBookmark bookmark = (CTBookmark)o;
					starts.add(bookmark);
			} 
		}
		
		if (o.getClass().getName().equals(endElement)) {
			if (o instanceof CTMarkupRange) { 
				CTMarkupRange bookmark = (CTMarkupRange)o;
					ends.add(bookmark);
			} 
		}

		if (startElement.equals("org.docx4j.wml.CTBookmark") 
				&& o instanceof javax.xml.bind.JAXBElement
				&& ((JAXBElement)o).getName().getLocalPart().equals("instrText")) {
			refs.add( (Text)XmlUtils.unwrap(o) );
		}
		
		return null;
	}
	
}