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

org.verapdf.tools.StaticResources Maven / Gradle / Ivy

There is a newer version: 1.26.1
Show newest version
package org.verapdf.tools;

import org.verapdf.cos.COSKey;
import org.verapdf.pd.font.FontProgram;
import org.verapdf.pd.font.cmap.CMap;
import org.verapdf.pd.structure.PDStructureNameSpace;

import java.util.HashMap;
import java.util.Map;

/**
 * Class handles static resources that need to be reset with each parsing of
 * document.
 *
 * @author Sergey Shemyakov
 */
public class StaticResources {

    public static Map cMapCache = new HashMap<>();
    public static Map structureNameSpaceCache = new HashMap<>();
    public static Map cachedFonts = new HashMap<>();

    /**
     * Caches CMap object.
     *
     * @param name is string key for cached CMap.
     * @param cMap is CMap object for caching.
     */
    public static void cacheCMap(String name, CMap cMap) {
        cMapCache.put(name, cMap);
    }

    /**
     * Gets CMap for this string key.
     *
     * @param name is key for CMap.
     * @return cached CMap with this name or null if no CMap available.
     */
    public static CMap getCMap(String name) {
        return cMapCache.get(name);
    }

    /**
     * Caches structure name space. Key is chosen to be indirect reference key
     * of this namespace dictionary.
     *
     * @param nameSpace is PD structure name space to cache.
     */
    public static void cacheStructureNameSpace(PDStructureNameSpace nameSpace) {
        COSKey key = nameSpace.getObject().getObjectKey();
        structureNameSpaceCache.put(key, nameSpace);
    }

    /**
     * Gets cached pd structure name space.
     * @param key is COSKey of namespace to get.
     * @return cached namespace with this COSKey or null if no namespace
     * available.
     */
    public static PDStructureNameSpace getStructureNameSpace(COSKey key) {
        return structureNameSpaceCache.get(key);
    }

    private StaticResources() {
    }

    public static void cacheFontProgram(String key, FontProgram font) {
        if (key != null) {
            cachedFonts.put(key, font);
        }
    }

    public static FontProgram getCachedFont(String key) {
        if (key == null) {
            return null;
        }
        return cachedFonts.get(key);
    }

    /**
     * Clears all cached static resources.
     */
    public static void clear() {
        cMapCache.clear();
        structureNameSpaceCache.clear();
        cachedFonts.clear();
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy