<commit_before>package net.sf.jooreports.templates.xmlfilters;

import net.sf.jooreports.opendocument.OpenDocumentNamespaces;
import net.sf.jooreports.templates.TemplateFreemarkerNamespace;
import nu.xom.Attribute;
import nu.xom.Document;
import nu.xom.Element;
import nu.xom.Nodes;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Processes content.xml for dynamic images<p>
 * Only images enclosed in a draw:frame with a name starting with {@link #IMAGE_NAME_PREFIX} and ending with {@link #IMAGE_NAME_SUFFIX} will be processed
 */
public class DynamicImageFilter extends XmlEntryFilter {

	/**
	 * Only images enclosed in a draw:frame with a name starting with
	 * this prefix will be processed 
	 */
	public static final String IMAGE_NAME_PREFIX = TemplateFreemarkerNamespace.NAME + ".image(";

	/**
	 * Only images enclosed in a draw:frame with a name ending with
	 * this suffix will be processed 
	 */
	public static final String IMAGE_NAME_SUFFIX = ")";

	private static final String IMAGE_WIDTH_PREFIX	= TemplateFreemarkerNamespace.NAME + ".imageWidth(";
	private static final String IMAGE_HEIGHT_PREFIX = TemplateFreemarkerNamespace.NAME + ".imageHeight(";

	private static final Log log = LogFactory.getLog(DynamicImageFilter.class);

	public void doFilter(Document document) {
		Nodes nodes = document.query("//draw:image", OpenDocumentNamespaces.XPATH_CONTEXT);
		for (int nodeIndex = 0; nodeIndex < nodes.size(); nodeIndex++) {
			Element imageElement = (Element) nodes.get(nodeIndex);
			Element frameElement = (Element) imageElement.getParent();
			if (!"draw:frame".equals(frameElement.getQualifiedName())) {
				log.warn("draw:image not inside a draw:frame? skipping");
				continue;
			}
			String frameName = frameElement.getAttributeValue("name", OpenDocumentNamespaces.URI_DRAW);
			if (frameName != null && 
					frameName.toLowerCase().startsWith(IMAGE_NAME_PREFIX) && 
					frameName.endsWith(IMAGE_NAME_SUFFIX)) {
				Attribute hrefAttribute = imageElement.getAttribute("href", OpenDocumentNamespaces.URI_XLINK);
				String defaultImageName = hrefAttribute.getValue();
				if (frameName.contains(",")) {
					Attribute widthAttribute = frameElement.getAttribute("width", OpenDocumentNamespaces.URI_SVG);
					Attribute heightAttribute = frameElement.getAttribute("height", OpenDocumentNamespaces.URI_SVG);
					String maxSize = ",'" + widthAttribute.getValue().trim() + "','" + 
											heightAttribute.getValue().trim() + "',";
					String sizeParameters = frameName.replaceFirst(",", maxSize);
					widthAttribute.setValue("${" + sizeParameters.replace(IMAGE_NAME_PREFIX, 
							IMAGE_WIDTH_PREFIX+"'"+defaultImageName+"',") + "}");
					heightAttribute.setValue("${" + sizeParameters.replace(IMAGE_NAME_PREFIX, 
							IMAGE_HEIGHT_PREFIX+"'"+defaultImageName+"',") + "}");
					System.out.println("widthAttribute:"+widthAttribute.getValue());
					System.out.println("heightAttribute:"+heightAttribute.getValue());
					frameName = frameName.split(",")[0] + IMAGE_NAME_SUFFIX;
				}
				hrefAttribute.setValue("${" + frameName.replace(IMAGE_NAME_PREFIX, 
						IMAGE_NAME_PREFIX+"'"+defaultImageName+"',") + "}");
			}
		}
	}
}<commit_msg>fixed resize problem with dynamic images while using element as fileName<commit_after>package net.sf.jooreports.templates.xmlfilters;

import net.sf.jooreports.opendocument.OpenDocumentNamespaces;
import net.sf.jooreports.templates.TemplateFreemarkerNamespace;
import nu.xom.Attribute;
import nu.xom.Document;
import nu.xom.Element;
import nu.xom.Nodes;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

/**
 * Processes content.xml for dynamic images<p>
 * Only images enclosed in a draw:frame with a name starting with {@link #IMAGE_NAME_PREFIX} and ending with {@link #IMAGE_NAME_SUFFIX} will be processed
 */
public class DynamicImageFilter extends XmlEntryFilter {

	/**
	 * Only images enclosed in a draw:frame with a name starting with
	 * this prefix will be processed 
	 */
	public static final String IMAGE_NAME_PREFIX = TemplateFreemarkerNamespace.NAME + ".image(";

	/**
	 * Only images enclosed in a draw:frame with a name ending with
	 * this suffix will be processed 
	 */
	public static final String IMAGE_NAME_SUFFIX = ")";

	private static final String IMAGE_WIDTH_PREFIX	= TemplateFreemarkerNamespace.NAME + ".imageWidth(";
	private static final String IMAGE_HEIGHT_PREFIX = TemplateFreemarkerNamespace.NAME + ".imageHeight(";

	private static final Log log = LogFactory.getLog(DynamicImageFilter.class);

	public void doFilter(Document document) {
		Nodes nodes = document.query("//draw:image", OpenDocumentNamespaces.XPATH_CONTEXT);
		for (int nodeIndex = 0; nodeIndex < nodes.size(); nodeIndex++) {
			Element imageElement = (Element) nodes.get(nodeIndex);
			Element frameElement = (Element) imageElement.getParent();
			if (!"draw:frame".equals(frameElement.getQualifiedName())) {
				log.warn("draw:image not inside a draw:frame? skipping");
				continue;
			}
			String frameName = frameElement.getAttributeValue("name", OpenDocumentNamespaces.URI_DRAW);
			if (frameName != null && 
					frameName.toLowerCase().startsWith(IMAGE_NAME_PREFIX) && 
					frameName.endsWith(IMAGE_NAME_SUFFIX)) {
				Attribute hrefAttribute = imageElement.getAttribute("href", OpenDocumentNamespaces.URI_XLINK);
				String defaultImageName = hrefAttribute.getValue();
				if (frameName.contains(",")) {
					Attribute widthAttribute = frameElement.getAttribute("width", OpenDocumentNamespaces.URI_SVG);
					Attribute heightAttribute = frameElement.getAttribute("height", OpenDocumentNamespaces.URI_SVG);
					String maxSize = ",'" + widthAttribute.getValue().trim() + "','" + 
											heightAttribute.getValue().trim() + "',";
					String sizeParameters = frameName.replaceFirst(",", maxSize);
					widthAttribute.setValue("${" + sizeParameters.replace(IMAGE_NAME_PREFIX, 
							IMAGE_WIDTH_PREFIX+"'"+defaultImageName+"',") + "}");
					heightAttribute.setValue("${" + sizeParameters.replace(IMAGE_NAME_PREFIX, 
							IMAGE_HEIGHT_PREFIX+"'"+defaultImageName+"',") + "}");
					frameName = frameName.split(",")[0] + IMAGE_NAME_SUFFIX;
				}
				hrefAttribute.setValue("${" + frameName.replace(IMAGE_NAME_PREFIX, 
						IMAGE_NAME_PREFIX+"'"+defaultImageName+"',") + "}");
			}
		}
	}
}
{'fraction_non_alphanumeric': 0.12372107665669763, 'fraction_numerical': 0.0006296237997796317, 'mean_word_length': 5.115495668912415, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 4}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4743114', 'n_tokens_mistral': 2048, 'n_tokens_neox': 1820, 'n_words': 394}