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

org.docx4j.utils.AltChunkFinder 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
/**
 *  Copyright 2012, Plutext Pty Ltd.
 *   
 *  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.utils;

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

import org.docx4j.XmlUtils;
import org.docx4j.TraversalUtil.CallbackImpl;
import org.docx4j.wml.CTAltChunk;

/**
 * @author jharrop
 * @since 2.8
 */
public class AltChunkFinder extends CallbackImpl {
	
	private List altChunks = new ArrayList(); 
	

	@Override
	public List apply(Object o) {
		// Not used
		return null;
	}
	
	public List apply(Object o, List contentList, int index) {
		
		if (o instanceof CTAltChunk) {				
			getAltChunks().add(
					new LocatedChunk((CTAltChunk)o, contentList, index));
		}			
		return null;
	}
	
	@Override
	public void walkJAXBElements(Object parent) {
		
		
		List children = getChildren(parent);
		if (children != null) {

			int index = 0;
			for (Object o : children) {

				o = XmlUtils.unwrap(o);
				
				this.apply(o, children, index);

				if (this.shouldTraverse(o)) {
					walkJAXBElements(o);
				}
				index++;

			}
		}
	}
	
	
	public List getAltChunks() {
		return altChunks;
	}


	/**
	 * Track the parent of altChunk.  This is a workaround
	 * for the problem getting the parent of something
	 * wrapped in a JAXBElement (which is sometimes the
	 * case).
	 * 
	 * @author jharrop
	 *
	 */
	public static class LocatedChunk {
		
		LocatedChunk(CTAltChunk altChunk, List contentList, int index) {
			this.setAltChunk(altChunk);
			this.contentList = contentList;
			this.index=index;
		}
		
		public CTAltChunk getAltChunk() {
			return altChunk;
		}
		public void setAltChunk(CTAltChunk altChunk) {
			this.altChunk = altChunk;
		}

		private CTAltChunk altChunk;		
		List contentList;
		public List getContentList() {
			return contentList;
		}
		int index;
		public int getIndex() {
			return index;			
		}
	}
}