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

org.docx4j.model.images.AbstractConversionImageHandler 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
/*
   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.model.images;

import java.io.UnsupportedEncodingException;
import java.util.Map;
import java.util.TreeMap;
import java.util.UUID;

import org.apache.commons.codec.binary.Base64;
import org.docx4j.openpackaging.exceptions.Docx4JException;
import org.docx4j.openpackaging.parts.WordprocessingML.BinaryPart;
import org.docx4j.relationships.Relationship;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public abstract class AbstractConversionImageHandler implements ConversionImageHandler {
	
	protected static Logger log = LoggerFactory.getLogger(AbstractConversionImageHandler.class);
	protected String uuid = UUID.randomUUID().toString();
	protected Map handledImagesMap = new TreeMap();
	protected String imageDirPath = null;  // TODO FIXME should not be here; move to FileConversionImageHandler
	protected boolean includeUUID = true;
	
	/** Creates an AbstractConversionImageHandler
	 * @param imageDirPath, the path, where the images will be stored
	 * @param includeUUID, should the image names be prefixed with an UUID to differentiate different runs 
	 */
	protected AbstractConversionImageHandler(String imageDirPath, boolean includeUUID) {
		this.imageDirPath = (imageDirPath == null ? 
					System.getProperty("java.io.tmpdir") : 
					imageDirPath);
		this.includeUUID = includeUUID;
	}

	@Override
	public String handleImage(AbstractWordXmlPicture picture, Relationship relationship, BinaryPart part) throws Docx4JException {
	String key = createKey(relationship, part);
	String uri = null;
		if (handledImagesMap.containsKey(key)) {
			uri = handledImagesMap.get(key);
		}
		else {
			uri = doHandleImage(picture, relationship, part);
			handledImagesMap.put(key, uri);
		}
		return uri;
	}

	protected String createKey(Relationship relationship, BinaryPart part) {
		return relationship.getTarget();
	}

	protected String doHandleImage(AbstractWordXmlPicture picture, Relationship relationship, BinaryPart part) throws Docx4JException {
	String uri = null;
		if (isInternalImage(picture, relationship, part)) {
			uri = handleInternalImage(picture, relationship, part);
		} else { // External
			uri = handleExternalImage(picture, relationship, part);
		}
		return uri;
	}

	protected boolean isInternalImage(AbstractWordXmlPicture picture, Relationship relationship, BinaryPart part) throws Docx4JException {
		//treat external images, that are loaded, as internal images
		return (part != null) &&
			   ((part.getExternalTarget() == null) || (part.getBuffer() != null)); 	
	}

	/**
	 * @param picture
	 * @param relationship
	 * @param binaryPart
	 * @return uri for the image we've saved, or null
	 */
	protected String handleInternalImage(AbstractWordXmlPicture picture, Relationship relationship, BinaryPart binaryPart) throws Docx4JException {
	byte[] bytes = getImageData(binaryPart);
	String uri = null;
		if (imageDirPath.equals("")) {
			// TODO: this isn't going to work for XSL FO!
			// So for XSL FO, you always need an imageDirPath! 

			//  -1 ? partname.substring(p + 1) : partname);
	}
    
	/** Get the image data of the buffer
	 * 
	 * @param binaryPart
	 * @return
	 */
	protected byte[] getImageData(BinaryPart binaryPart) {
		return binaryPart.getBytes();
	}

	/**
	 * @param picture
	 * @param relationship
	 * @param part (is always null)
	 * @return uri for the image we've saved, or null
	 */
	protected String handleExternalImage(AbstractWordXmlPicture picture, Relationship relationship, BinaryPart part) {
		return relationship.getTarget();
	}
	

	/** If the instance is reused, it should be cleared first
	 */
	public void clear() {
		uuid = UUID.randomUUID().toString();
		handledImagesMap.clear();		
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy