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

org.daisy.dotify.common.xml.EntityResolverCache Maven / Gradle / Ivy

There is a newer version: 1.0.7
Show newest version
package org.daisy.dotify.common.xml;

import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;

/**
 * Provides an entity resolver with cache.
 *
 * @author Joel Håkansson
 */
public class EntityResolverCache implements EntityResolver {

    private final URLCache cache;

    /**
     * Creates a new entity resolver with cache.
     */
    public EntityResolverCache() {
        cache = new URLCache();
    }

    @Override
    public InputSource resolveEntity(String publicId, String systemId)
            throws SAXException, IOException {
        try {
            URL url = new URI(systemId).toURL();
            InputSource is = new InputSource(cache.openStream(url));
            is.setPublicId(publicId);
            is.setSystemId(systemId);
            return is;
        } catch (URISyntaxException e) {
            return null;
        } catch (IOException e) {
            return null;
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy