<commit_before>/*
 * $Id$
 * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
 * For details on use and redistribution please refer to the
 * LICENSE file included with these sources.
 */

package org.apache.fop.image;

// Java
import java.io.IOException;
import java.io.InputStream;
import java.io.BufferedInputStream;
import java.io.File;
import java.net.URL;
import java.net.MalformedURLException;
import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.WeakHashMap;
import java.util.Map;
import java.util.HashSet;
import java.util.Set;
import java.util.Collections;
import java.util.Iterator;

// FOP
import org.apache.fop.image.analyser.ImageReaderFactory;
import org.apache.fop.image.analyser.ImageReader;
import org.apache.fop.fo.FOUserAgent;

// Avalon
import org.apache.avalon.framework.logger.Logger;

/**
 * create FopImage objects (with a configuration file - not yet implemented).
 * @author <NAME>
 */
public class ImageFactory {
    private static ImageFactory factory = new ImageFactory();
    ImageCache cache = new ContextImageCache(true);

    private ImageFactory() {}

    /**
     * Get static image factory instance.
     *
     * @return the image factory instance
     */
    public static ImageFactory getInstance() {
        return factory;
    }

    /**
     * Get the url string from a wrapped url.
     *
     * @href the input wrapped url
     * @return the raw url
     */
    public static String getURL(String href) {
        /*
         * According to section 5.11 a <uri-specification> is:
         * "url(" + URI + ")"
         * according to 7.28.7 a <uri-specification> is:
         * URI
         * So handle both.
         */
        // Get the absolute URL
        URL absoluteURL = null;
        href = href.trim();
        if (href.startsWith("url(") && (href.indexOf(")") != -1)) {
            href = href.substring(4, href.indexOf(")")).trim();
            if (href.startsWith("'") && href.endsWith("'")) {
                href = href.substring(1, href.length() - 1);
            } else if (href.startsWith("\"") && href.endsWith("\"")) {
                href = href.substring(1, href.length() - 1);
            }
        } else {
            // warn
        }
        return href;
    }

    /**
     * Get the image from the cache or load.
     * If this returns null then the image could not be loaded
     * due to an error. Messages should be logged.
     * Before calling this the getURL(url) must be used.
     *
     * @param url the url for the image
     * @param context the user agent context
     * @return the fop image instance
     */
    public FopImage getImage(String url, FOUserAgent context) {
        return cache.getImage(url, context);
    }

    /**
     * Release an image from the cache.
     * This can be used if the renderer has its own cache of
     * the image.
     * The image should then be put into the weak cache.
     *
     * @param url the url for the image
     * @param context the user agent context
     */
    public void releaseImage(String url, FOUserAgent context) {
        cache.releaseImage(url, context);
    }

    /**
     * create an FopImage objects.
     * @param href image URL as a String
     * @return a new FopImage object
     *
     * @param href the url for the image
     * @param baseURL the base url
     * @param ua the user agent context
     * @return the fop image instance
     */
    protected static FopImage loadImage(String href, String baseURL,
                                        FOUserAgent ua) {
        Logger log = ua.getLogger();

        InputStream imgIS = openStream(href, baseURL, ua);
<commit_msg>add method to remove context<commit_after>/*
 * $Id$
 * Copyright (C) 2001 The Apache Software Foundation. All rights reserved.
 * For details on use and redistribution please refer to the
 * LICENSE file included with these sources.
 */

package org.apache.fop.image;

// Java
import java.io.IOException;
import java.io.InputStream;
import java.io.BufferedInputStream;
import java.io.File;
import java.net.URL;
import java.net.MalformedURLException;
import java.lang.reflect.Constructor;
import java.util.HashMap;
import java.util.WeakHashMap;
import java.util.Map;
import java.util.HashSet;
import java.util.Set;
import java.util.Collections;
import java.util.Iterator;

// FOP
import org.apache.fop.image.analyser.ImageReaderFactory;
import org.apache.fop.image.analyser.ImageReader;
import org.apache.fop.fo.FOUserAgent;

// Avalon
import org.apache.avalon.framework.logger.Logger;

/**
 * create FopImage objects (with a configuration file - not yet implemented).
 * @author <NAME>
 */
public class ImageFactory {
    private static ImageFactory factory = new ImageFactory();
    ImageCache cache = new ContextImageCache(true);

    private ImageFactory() {}

    /**
     * Get static image factory instance.
     *
     * @return the image factory instance
     */
    public static ImageFactory getInstance() {
        return factory;
    }

    /**
     * Get the url string from a wrapped url.
     *
     * @href the input wrapped url
     * @return the raw url
     */
    public static String getURL(String href) {
        /*
         * According to section 5.11 a <uri-specification> is:
         * "url(" + URI + ")"
         * according to 7.28.7 a <uri-specification> is:
         * URI
         * So handle both.
         */
        // Get the absolute URL
        URL absoluteURL = null;
        href = href.trim();
        if (href.startsWith("url(") && (href.indexOf(")") != -1)) {
            href = href.substring(4, href.indexOf(")")).trim();
            if (href.startsWith("'") && href.endsWith("'")) {
                href = href.substring(1, href.length() - 1);
            } else if (href.startsWith("\"") && href.endsWith("\"")) {
                href = href.substring(1, href.length() - 1);
            }
        } else {
            // warn
        }
        return href;
    }

    /**
     * Get the image from the cache or load.
     * If this returns null then the image could not be loaded
     * due to an error. Messages should be logged.
     * Before calling this the getURL(url) must be used.
     *
     * @param url the url for the image
     * @param context the user agent context
     * @return the fop image instance
     */
    public FopImage getImage(String url, FOUserAgent context) {
        return cache.getImage(url, context);
    }

    /**
     * Release an image from the cache.
     * This can be used if the renderer has its own cache of
     * the image.
     * The image should then be put into the weak cache.
     *
     * @param url the url for the image
     * @param context the user agent context
     */
    public void releaseImage(String url, FOUserAgent context) {
        cache.releaseImage(url, context);
    }

    /**
     * Release the context and all images in the context.
     *
     * @param context the context to remove
     */
    public void removeContext(FOUserAgent context) {
        cache.removeContext(context);
    }

    /**
     * create an FopImage objects.
     * @param href image URL as a String
     * @return a new FopImage object
     *
     * @param href the url for the image
     * @param baseURL the base url
     * @param ua the user agent context
     * @return the fop image instance
     */
    protected static FopImage loadImage(String href, String baseURL,
                                        FOUserAgent ua) {
        Logger log = ua.getLogger();

        InputStream imgIS = openStream(href, baseURL, ua);

{'fraction_non_alphanumeric': 0.10383806519453208, 'fraction_numerical': 0.004468980021030494, 'mean_word_length': 2.258672376873662, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6260211', 'n_tokens_mistral': 2169, 'n_tokens_neox': 2146, 'n_words': 738}