<commit_before>package org.lightmare.jpa.hibernate.internal;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.persistence.PersistenceException;
import javax.persistence.spi.PersistenceUnitTransactionType;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.jpa.AvailableSettings;
import org.hibernate.jpa.boot.archive.internal.ArchiveHelper;
import org.hibernate.jpa.boot.internal.ParsedPersistenceXmlDescriptor;
import org.hibernate.jpa.boot.internal.PersistenceXmlParser;
import org.hibernate.jpa.internal.EntityManagerMessageLogger;
import org.hibernate.jpa.internal.util.ConfigurationHelper;
import org.hibernate.metamodel.source.XsdException;
import org.jboss.logging.Logger;
import org.lightmare.jpa.hibernate.HibernatePersistenceProviderImpl;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class PersistenceXmlParserImpl extends PersistenceXmlParser {

    private HibernatePersistenceProviderImpl.MetaConfig metaConfig;

    private static final EntityManagerMessageLogger LOG = Logger
	    .getMessageLogger(EntityManagerMessageLogger.class,
		    PersistenceXmlParser.class.getName());

    private final ClassLoaderService classLoaderService;
    private final PersistenceUnitTransactionType defaultTransactionType;

    @SuppressWarnings({ "deprecation", "rawtypes" })
    public static List<ParsedPersistenceXmlDescriptor> locatePersistenceUnits(
	    Map integration,
	    HibernatePersistenceProviderImpl.MetaConfig metaConfig) {
	final PersistenceXmlParserImpl parser = new PersistenceXmlParserImpl(
		ClassLoaderServiceImpl.fromConfigSettings(integration),
		PersistenceUnitTransactionType.RESOURCE_LOCAL);
	parser.metaConfig = metaConfig;

	return parser.doResolve(integration);
    }

    public PersistenceXmlParserImpl(ClassLoaderService classLoaderService,
	    PersistenceUnitTransactionType defaultTransactionType) {
	super(classLoaderService, defaultTransactionType);
	this.classLoaderService = classLoaderService;
	this.defaultTransactionType = defaultTransactionType;
    }

    @SuppressWarnings("rawtypes")
    public List<ParsedPersistenceXmlDescriptor> doResolve(Map integration) {
	final List<ParsedPersistenceXmlDescriptor> persistenceUnits = new ArrayList<ParsedPersistenceXmlDescriptor>();

	final List<URL> xmlUrls;

	if (metaConfig.xmls == null || metaConfig.xmls.isEmpty()) {
	    xmlUrls = classLoaderService
		    .locateResources("META-INF/persistence.xml");
	} else {
	    xmlUrls = metaConfig.xmls;
	}

	if (xmlUrls.isEmpty()) {
	    LOG.unableToFindPersistenceXmlInClasspath();
	} else {
	    for (URL xmlUrl : xmlUrls) {
		persistenceUnits
			.addAll(parsePersistenceXml(xmlUrl, integration));
	    }
	}

	return persistenceUnits;
    }

    @SuppressWarnings("rawtypes")
    private List<ParsedPersistenceXmlDescriptor> parsePersistenceXml(
	    URL xmlUrl, Map integration) {
	LOG.tracef("Attempting to parse persistence.xml file : %s",
		xmlUrl.toExternalForm());

	final Document doc = loadUrl(xmlUrl);
	final Element top = doc.getDocumentElement();

	final List<ParsedPersistenceXmlDescriptor> persistenceUnits = new ArrayList<ParsedPersistenceXmlDescriptor>();

	final NodeList children = top.getChildNodes();
	for (int i = 0; i < children.getLength(); i++) {
	    if (children.item(i).getNodeType() == Node.ELEMENT_NODE) {
		final Element element = (Element) children.item(i);
		final String tag = element.getTagName();
		if (tag.equals("persistence-unit")) {
		    final URL puRootUrl = ArchiveHelper.getJarURLFromURLEntry(
			    xmlUrl, "/META-INF/persistence.xml");
		    ParsedPersistenceXmlDescriptor persistenceUnit = new ParsedPersistenceXmlDescriptor(
			    puRootUrl);
		    bindPersistenceUnit(persistenceUnit, element);

		    // per JPA spec, any settings passed in to
		    // PersistenceProvider bootstrap methods should override
		    // values found in persistence.xml
		    if (integration.containsKey(AvailableSettings.PROVIDER)) {
			persistenceUnit
				.setProviderClassName((String) integration
					.get(AvailableSettings.PROVIDER));
		    }
		    if (integration
			    .containsKey(AvailableSettings.TRANSACTION_TYPE)) {
			String transactionType = (String) integration
				.get(AvailableSettings.TRANSACTION_TYPE);
			persistenceUnit
				.setTransactionType(parseTransactionType(transactionType));
		    }
		    if (integration
			    .containsKey(AvailableSettings.JTA_DATASOURCE)) {
			persistenceUnit.setJtaDataSource(integration
				.get(AvailableSettings.JTA_DATASOURCE));
		    }
		    if (integration
			    .containsKey(AvailableSettings.NON_JTA_DATASOURCE)) {
			persistenceUnit.setNonJtaDataSource(integration
				.get(AvailableSettings.NON_JTA_DATASOURCE));
		    }

		    decodeTransactionType(persistenceUnit);

		    Properties properties = persistenceUnit.getProperties();
		    ConfigurationHelper.overrideProperties(properties,
			    integration);<commit_msg>imporved "hibernate" utilities<commit_after>package org.lightmare.jpa.hibernate.internal;

import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
import java.net.URLConnection;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Properties;

import javax.persistence.PersistenceException;
import javax.persistence.spi.PersistenceUnitTransactionType;
import javax.xml.XMLConstants;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamSource;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import javax.xml.validation.Validator;

import org.hibernate.boot.registry.classloading.internal.ClassLoaderServiceImpl;
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
import org.hibernate.internal.util.StringHelper;
import org.hibernate.jpa.AvailableSettings;
import org.hibernate.jpa.boot.archive.internal.ArchiveHelper;
import org.hibernate.jpa.boot.internal.ParsedPersistenceXmlDescriptor;
import org.hibernate.jpa.boot.internal.PersistenceXmlParser;
import org.hibernate.jpa.internal.EntityManagerMessageLogger;
import org.hibernate.jpa.internal.util.ConfigurationHelper;
import org.hibernate.metamodel.source.XsdException;
import org.jboss.logging.Logger;
import org.lightmare.jpa.hibernate.HibernatePersistenceProviderImpl;
import org.lightmare.utils.ObjectUtils;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.ErrorHandler;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.SAXParseException;

public class PersistenceXmlParserImpl extends PersistenceXmlParser {

    private HibernatePersistenceProviderImpl.MetaConfig metaConfig;

    private static final EntityManagerMessageLogger LOG = Logger
	    .getMessageLogger(EntityManagerMessageLogger.class,
		    PersistenceXmlParser.class.getName());

    private final ClassLoaderService classLoaderService;
    private final PersistenceUnitTransactionType defaultTransactionType;

    @SuppressWarnings({ "deprecation", "rawtypes" })
    public static List<ParsedPersistenceXmlDescriptor> locatePersistenceUnits(
	    Map integration,
	    HibernatePersistenceProviderImpl.MetaConfig metaConfig) {
	final PersistenceXmlParserImpl parser = new PersistenceXmlParserImpl(
		ClassLoaderServiceImpl.fromConfigSettings(integration),
		PersistenceUnitTransactionType.RESOURCE_LOCAL);
	parser.metaConfig = metaConfig;

	return parser.doResolve(integration);
    }

    public PersistenceXmlParserImpl(ClassLoaderService classLoaderService,
	    PersistenceUnitTransactionType defaultTransactionType) {
	super(classLoaderService, defaultTransactionType);
	this.classLoaderService = classLoaderService;
	this.defaultTransactionType = defaultTransactionType;
    }

    @SuppressWarnings("rawtypes")
    public List<ParsedPersistenceXmlDescriptor> doResolve(Map integration) {
	final List<ParsedPersistenceXmlDescriptor> persistenceUnits = new ArrayList<ParsedPersistenceXmlDescriptor>();

	final List<URL> xmlUrls;

	if (metaConfig.xmls == null || metaConfig.xmls.isEmpty()) {
	    xmlUrls = classLoaderService
		    .locateResources("META-INF/persistence.xml");
	} else {
	    xmlUrls = metaConfig.xmls;
	}

	if (xmlUrls.isEmpty()) {
	    LOG.unableToFindPersistenceXmlInClasspath();
	} else {
	    for (URL xmlUrl : xmlUrls) {
		persistenceUnits
			.addAll(parsePersistenceXml(xmlUrl, integration));
	    }
	}

	return persistenceUnits;
    }

    private void resolveTransactionType(
	    ParsedPersistenceXmlDescriptor persistenceUnit) {

	if (ObjectUtils.notNull(metaConfig) && metaConfig.swapDataSource) {
	    persistenceUnit
		    .setTransactionType(PersistenceUnitTransactionType.RESOURCE_LOCAL);
	}
    }

    private void resolveDataSource(
	    ParsedPersistenceXmlDescriptor persistenceUnit) {

	Object dataSource = persistenceUnit.getJtaDataSource();
	if (ObjectUtils.notNull(metaConfig) && metaConfig.swapDataSource) {
	    persistenceUnit.setNonJtaDataSource(dataSource);
	}
    }

    @SuppressWarnings("rawtypes")
    private List<ParsedPersistenceXmlDescriptor> parsePersistenceXml(
	    URL xmlUrl, Map integration) {
	LOG.tracef("Attempting to parse persistence.xml file : %s",
		xmlUrl.toExternalForm());

	final Document doc = loadUrl(xmlUrl);
	final Element top = doc.getDocumentElement();

	final List<ParsedPersistenceXmlDescriptor> persistenceUnits = new ArrayList<ParsedPersistenceXmlDescriptor>();

	final NodeList children = top.getChildNodes();
	for (int i = 0; i < children.getLength(); i++) {
	    if (children.item(i).getNodeType() == Node.ELEMENT_NODE) {
		final Element element = (Element) children.item(i);
		final String tag = element.getTagName();
		if (tag.equals("persistence-unit")) {
		    final URL puRootUrl = ArchiveHelper.getJarURLFromURLEntry(
			    xmlUrl, "/META-INF/persistence.xml");
		    ParsedPersistenceXmlDescriptor persistenceUnit = new ParsedPersistenceXmlDescriptor(
			    puRootUrl);
		    bindPersistenceUnit(persistenceUnit, element);

		    // per JPA spec, any settings passed in to
		    // PersistenceProvider bootstrap methods should override
		    // values found in persistence.xml
		    if (integration.containsKey(AvailableSettings.PROVIDER)) {
			persistenceUnit
				.setProviderClassName((String) integration
					.get(AvailableSettings.PROVIDER));
		    }
		    if (integration
			    .containsKey(AvailableSettings.TRANSACTION_TYPE)) {
			String transactionType = (String) integration
				.get(AvailableSettings.TRANSACTION_TYPE);
			persistenceUnit
				.setTransactionType(parseTransactionType(transactionType));
		    }

		    resolveTransactionType(persistenceUnit);

		    if (integration
			    .containsKey(AvailableSettings.JTA_DATASOURCE)) {
			persistenceUnit.setJtaDataSource(integration
				.get(AvailableSettings.JTA_DATASOURCE));
		    }
		    if (integration
			    .containsKey(AvailableSettings.NON_JTA_DATASOURCE)) {
			persistenceUnit.setNonJtaDataSource(integration
				.get(AvailableSettings.NON_JTA_DATASOURCE));
		    }

		    resolveDataSource(persistenceUnit);

		    decodeTransactionType(persistenceUnit);

		    Properties properties = persistenceUnit.getProperties();
		    ConfigurationHelper.overrideProperties(properties,
			    integration);
{'fraction_non_alphanumeric': 0.08897202342225113, 'fraction_numerical': 0.0008132726089785296, 'mean_word_length': 5.322365038560411, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 62}, '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': '3526520', 'n_tokens_mistral': 3694, 'n_tokens_neox': 3368, 'n_words': 603}