<commit_before>package org.anodyneos.commons.xml;

import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;

import javax.xml.transform.Source;
import javax.xml.transform.TransformerException;
import javax.xml.transform.URIResolver;
import javax.xml.transform.stream.StreamSource;

import org.anodyneos.commons.net.URI;
import org.anodyneos.commons.net.URIHandler;
import org.anodyneos.commons.net.URIHelper;
import org.apache.xml.resolver.Catalog;
import org.apache.xml.resolver.CatalogManager;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
 * @TODO: More configuration for XMLCatalogs. Allow custom properties file,
 * protocolHandler vs. catalog preference?
 */
public class UnifiedResolver extends URIHelper implements EntityResolver, URIResolver  {

    private boolean defaultLookupEnabled;
    private HashMap protocolHandlers = new HashMap();
    private CatalogManager catalogManager = null;
    private Catalog catalog = null;

    // constructors
    public UnifiedResolver() {
        this(true);
    }
    public UnifiedResolver(boolean defaultLookupEnabled) {
        setDefaultLookupEnabled(defaultLookupEnabled);
    }

    // catalog
    public synchronized void setXMLCatalogEnabled(boolean enableXMLCatalog) {
        if (enableXMLCatalog && catalog == null) {
            catalogManager = new CatalogManager();
            catalog = catalogManager.getCatalog();
        } else if (! enableXMLCatalog) {
            catalogManager = null;
            catalog = null;
        }
    }

    // default lookup
    public void setDefaultLookupEnabled(boolean defaultLookupEnabled) {
        this.defaultLookupEnabled = defaultLookupEnabled;
    }
    public boolean isDefaultLookupEnabled() {
        return defaultLookupEnabled;
    }

    // SAX EntityResolver
    public InputSource resolveEntity(String publicId, String systemId)
    throws org.xml.sax.SAXException, java.net.MalformedURLException {

        InputSource inputSource = null;

        URI systemURI;
        try {
            systemURI = new URI(systemId);
        } catch (URI.MalformedURIException e) {
            throw new MalformedURLException(systemId);
        }

        try {
            URLConnection conn = openConnection(publicId, systemURI);
            if (null == conn) {
                if (isDefaultLookupEnabled()) {
                    // Let default resolver handle all other URLs
                    return null;
                } else {
                    // Only internal resolvers are allowed
                    throw new SAXException("URI not supported for: '" + systemId + "'.");
                }
            }
            InputStream inputStream = conn.getInputStream();
            inputSource = new InputSource(inputStream);
            inputSource.setPublicId(publicId);
            inputSource.setSystemId(systemId);
        } catch (java.io.FileNotFoundException e) {
            throw new SAXException("Resource: '" + systemId + "'" + " not found.");
        } catch (java.io.IOException e) {
            throw new SAXException("IOException opening resource: '" + systemId + "'"
                    + " not found.", e);
        }

        return inputSource;
    }

    // trax URIResolver
    public Source resolve(String href, String base)
    throws javax.xml.transform.TransformerException {
        URI uri;
        try {<commit_msg>Instead of throwing a simple SAXException, throw a a SAXException containing a FileNotFoundException<commit_after>package org.anodyneos.commons.xml;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.HashMap;

import javax.xml.transform.Source;
import javax.xml.transform.TransformerException;
import javax.xml.transform.URIResolver;
import javax.xml.transform.stream.StreamSource;

import org.anodyneos.commons.net.URI;
import org.anodyneos.commons.net.URIHandler;
import org.anodyneos.commons.net.URIHelper;
import org.apache.xml.resolver.Catalog;
import org.apache.xml.resolver.CatalogManager;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

/**
 * @TODO: More configuration for XMLCatalogs. Allow custom properties file,
 * protocolHandler vs. catalog preference?
 */
public class UnifiedResolver extends URIHelper implements EntityResolver, URIResolver  {

    private boolean defaultLookupEnabled;
    private HashMap protocolHandlers = new HashMap();
    private CatalogManager catalogManager = null;
    private Catalog catalog = null;

    // constructors
    public UnifiedResolver() {
        this(true);
    }
    public UnifiedResolver(boolean defaultLookupEnabled) {
        setDefaultLookupEnabled(defaultLookupEnabled);
    }

    // catalog
    public synchronized void setXMLCatalogEnabled(boolean enableXMLCatalog) {
        if (enableXMLCatalog && catalog == null) {
            catalogManager = new CatalogManager();
            catalog = catalogManager.getCatalog();
        } else if (! enableXMLCatalog) {
            catalogManager = null;
            catalog = null;
        }
    }

    // default lookup
    public void setDefaultLookupEnabled(boolean defaultLookupEnabled) {
        this.defaultLookupEnabled = defaultLookupEnabled;
    }
    public boolean isDefaultLookupEnabled() {
        return defaultLookupEnabled;
    }

    // SAX EntityResolver
    public InputSource resolveEntity(String publicId, String systemId)
    throws org.xml.sax.SAXException, java.net.MalformedURLException {

        InputSource inputSource = null;

        URI systemURI;
        try {
            systemURI = new URI(systemId);
        } catch (URI.MalformedURIException e) {
            throw new MalformedURLException(systemId);
        }

        try {
            URLConnection conn = openConnection(publicId, systemURI);
            if (null == conn) {
                if (isDefaultLookupEnabled()) {
                    // Let default resolver handle all other URLs
                    return null;
                } else {
                    // Only internal resolvers are allowed, and none of them were able to find what you're looking for
                    throw new FileNotFoundException("File not found '" + systemId + "'.");
                }
            }
            InputStream inputStream = conn.getInputStream();
            inputSource = new InputSource(inputStream);
            inputSource.setPublicId(publicId);
            inputSource.setSystemId(systemId);
        } catch (java.io.IOException e) {
            throw new SAXException("IOException opening resource: '" + systemId + "'"
                    + " not found.", e);
        }

        return inputSource;
    }

    // trax URIResolver
    public Source resolve(String href, String base)
    throws javax.xml.transform.TransformerException {
        URI uri;
        try {
{'fraction_non_alphanumeric': 0.08420016868147315, 'fraction_numerical': 0.0, 'mean_word_length': 2.6227087576374744, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 24}, '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': '133237', 'n_tokens_mistral': 1817, 'n_tokens_neox': 1782, 'n_words': 456}