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

org.docx4j.utils.CompoundTraversalUtilVisitorCallback 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.

The newest version!
package org.docx4j.utils;

import java.util.HashMap;
import java.util.List;
import java.util.LinkedList;
import java.util.Map;
import java.util.Map.Entry;

/** 
 * Use this if there is more than one object type (eg Tables and Paragraphs)
 * you are interested in doing something with during the traversal.
 * 
 * @author alberto
 */
public class CompoundTraversalUtilVisitorCallback extends
		AbstractTraversalUtilVisitorCallback {

	Map> visitorMap = null;

	public CompoundTraversalUtilVisitorCallback(
			List visitorList) {

		visitorMap = setupTraversalVistorMap(visitorList);
	}

	protected Map> setupTraversalVistorMap(
			List visitorList) {

		Map> ret = new HashMap>();
		Class visitorClass = null;
		List classVisitorList = null;
		for (TraversalUtilVisitor visitor : visitorList) {
			visitorClass = findClassParameter(visitor.getClass());
			if (visitorClass == null) {
				throw new IllegalArgumentException(
						"Can't identify the parameter class of the visitor "
								+ visitor.getClass().getName());
			}
			classVisitorList = ret.get(visitorClass);
			if (classVisitorList == null) {
				classVisitorList = new LinkedList();
				ret.put(visitorClass, classVisitorList);
			}
			classVisitorList.add(visitor);
		}
		return ret;
	}

	@Override
	protected List apply(Object child, Object parent, List siblings) {
		for (final Entry> entrySet : visitorMap.entrySet()) {
			final Class currentClass = entrySet.getKey();
			if (currentClass.isAssignableFrom(child.getClass())) {
				List classVisitorList = entrySet.getValue();
				if (null != classVisitorList) {
					for (TraversalUtilVisitor visitor : classVisitorList) {
						visitor.apply(child, parent, siblings);
					}
				}
			}
		}
		return null;
	}

}