Search in sources :

Example 26 with EntityResolver

use of org.xml.sax.EntityResolver in project jbosstools-hibernate by jbosstools.

the class ConfigurationFacadeTest method testGetEntityResolver.

@Test
public void testGetEntityResolver() throws Exception {
    EntityResolver testResolver = new DefaultHandler();
    ConfigurationFacadeImpl facade = (ConfigurationFacadeImpl) configurationFacade;
    Field entityResolverField = ConfigurationFacadeImpl.class.getDeclaredField("entityResolver");
    entityResolverField.setAccessible(true);
    assertNotSame(testResolver, configurationFacade.getEntityResolver());
    entityResolverField.set(facade, testResolver);
    assertSame(testResolver, configurationFacade.getEntityResolver());
}
Also used : Field(java.lang.reflect.Field) EntityResolver(org.xml.sax.EntityResolver) DefaultHandler(org.xml.sax.helpers.DefaultHandler) Test(org.junit.jupiter.api.Test)

Example 27 with EntityResolver

use of org.xml.sax.EntityResolver in project jbosstools-hibernate by jbosstools.

the class ConfigurationFactory method configureStandardConfiguration.

private IConfiguration configureStandardConfiguration(final boolean includeMappings, IConfiguration localCfg, Properties properties) {
    if (properties != null) {
        localCfg = localCfg.setProperties(properties);
    }
    EntityResolver entityResolver = new DTDEntityResolver(service);
    if (StringHelper.isNotEmpty(prefs.getEntityResolverName())) {
        try {
            entityResolver = (EntityResolver) ReflectHelper.classForName(prefs.getEntityResolverName()).newInstance();
        } catch (Exception c) {
            throw new HibernateConsoleRuntimeException(ConsoleMessages.ConsoleConfiguration_could_not_configure_entity_resolver + prefs.getEntityResolverName(), c);
        }
    }
    localCfg.setEntityResolver(entityResolver);
    if (StringHelper.isNotEmpty(prefs.getNamingStrategy())) {
        try {
            INamingStrategy ns = service.newNamingStrategy(prefs.getNamingStrategy());
            localCfg.setNamingStrategy(ns);
        } catch (Exception c) {
            throw new HibernateConsoleRuntimeException(ConsoleMessages.ConsoleConfiguration_could_not_configure_naming_strategy + prefs.getNamingStrategy(), c);
        }
    }
    localCfg = loadConfigurationXML(localCfg, includeMappings, entityResolver);
    changeDatasourceProperties(localCfg);
    localCfg = configureConnectionProfile(localCfg);
    // replace dialect if it is set in preferences
    if (StringHelper.isNotEmpty(prefs.getDialectName())) {
        localCfg.setProperty(environment.getDialect(), prefs.getDialectName());
    }
    if (StringHelper.isEmpty(localCfg.getProperty("javax.persistence.validation.mode"))) {
        // $NON-NLS-1$
        // $NON-NLS-1$//$NON-NLS-2$
        localCfg.setProperty("javax.persistence.validation.mode", "none");
    }
    return localCfg;
}
Also used : INamingStrategy(org.jboss.tools.hibernate.runtime.spi.INamingStrategy) EntityResolver(org.xml.sax.EntityResolver) DTDEntityResolver(org.hibernate.util.xpl.DTDEntityResolver) DTDEntityResolver(org.hibernate.util.xpl.DTDEntityResolver) SQLException(java.sql.SQLException) DocumentException(org.dom4j.DocumentException) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException) SAXParseException(org.xml.sax.SAXParseException) MappingException(org.jboss.tools.hibernate.exception.MappingException) HibernateException(org.jboss.tools.hibernate.runtime.spi.HibernateException)

Example 28 with EntityResolver

use of org.xml.sax.EntityResolver in project jbosstools-hibernate by jbosstools.

the class ConfigurationFacadeTest method testSetEntityResolver.

@Test
public void testSetEntityResolver() throws Exception {
    EntityResolver testResolver = new DefaultHandler();
    ConfigurationFacadeImpl facade = (ConfigurationFacadeImpl) configurationFacade;
    Field entityResolverField = ConfigurationFacadeImpl.class.getDeclaredField("entityResolver");
    entityResolverField.setAccessible(true);
    assertNull(entityResolverField.get(facade));
    configurationFacade.setEntityResolver(testResolver);
    assertSame(testResolver, entityResolverField.get(facade));
}
Also used : Field(java.lang.reflect.Field) EntityResolver(org.xml.sax.EntityResolver) DefaultHandler(org.xml.sax.helpers.DefaultHandler) Test(org.junit.jupiter.api.Test)

Example 29 with EntityResolver

use of org.xml.sax.EntityResolver in project jbosstools-hibernate by jbosstools.

the class ConfigurationFacadeTest method testGetEntityResolver.

@Test
public void testGetEntityResolver() throws Exception {
    EntityResolver testResolver = new DefaultHandler();
    ConfigurationFacadeImpl facade = (ConfigurationFacadeImpl) configurationFacade;
    Field entityResolverField = ConfigurationFacadeImpl.class.getDeclaredField("entityResolver");
    entityResolverField.setAccessible(true);
    assertNotSame(testResolver, configurationFacade.getEntityResolver());
    entityResolverField.set(facade, testResolver);
    assertSame(testResolver, configurationFacade.getEntityResolver());
}
Also used : Field(java.lang.reflect.Field) EntityResolver(org.xml.sax.EntityResolver) DefaultHandler(org.xml.sax.helpers.DefaultHandler) Test(org.junit.jupiter.api.Test)

Example 30 with EntityResolver

use of org.xml.sax.EntityResolver in project datanucleus-core by datanucleus.

the class MetaDataParser method parseMetaDataStream.

/**
 * Method to parse a MetaData file given an InputStream.
 * Closes the input stream when finished.
 * @param in input stream
 * @param filename Name of the file (if applicable)
 * @param handlerName Name of the handler plugin to use when parsing
 * @return The MetaData for this file
 * @throws NucleusException thrown if error occurred
 */
public MetaData parseMetaDataStream(InputStream in, String filename, String handlerName) {
    if (in == null) {
        throw new NullPointerException("input stream is null");
    }
    if (NucleusLogger.METADATA.isDebugEnabled()) {
        NucleusLogger.METADATA.debug(Localiser.msg("044030", filename, handlerName, validate ? "true" : "false"));
    }
    try {
        synchronized (parser) {
            // Generate the required handler to process this metadata
            DefaultHandler handler = null;
            try {
                parser.getXMLReader().setEntityResolver(entityResolver);
                if ("persistence".equalsIgnoreCase(handlerName)) {
                    // "persistence.xml"
                    handler = new org.datanucleus.metadata.xml.PersistenceFileMetaDataHandler(mgr, filename, entityResolver);
                } else {
                    // Fallback to the plugin mechanism for other MetaData handlers
                    Class[] argTypes = new Class[] { ClassConstants.METADATA_MANAGER, ClassConstants.JAVA_LANG_STRING, EntityResolver.class };
                    Object[] argValues = new Object[] { mgr, filename, entityResolver };
                    handler = (DefaultHandler) pluginMgr.createExecutableExtension("org.datanucleus.metadata_handler", "name", handlerName, "class-name", argTypes, argValues);
                    if (handler == null) {
                        // Plugin of this name not found
                        throw new NucleusUserException(Localiser.msg("044028", handlerName)).setFatal();
                    }
                }
            } catch (Exception e) {
                String msg = Localiser.msg("044029", handlerName, e.getMessage());
                throw new NucleusException(msg, e);
            }
            // Set whether to validate
            ((AbstractMetaDataHandler) handler).setValidate(validate);
            // Parse the metadata
            parser.parse(in, handler);
            // Return the FileMetaData that has been parsed
            return ((AbstractMetaDataHandler) handler).getMetaData();
        }
    } catch (NucleusException e) {
        throw e;
    } catch (Exception e) {
        Throwable cause = e;
        if (e instanceof SAXException) {
            cause = ((SAXException) e).getException();
        }
        cause = e.getCause() == null ? cause : e.getCause();
        NucleusLogger.METADATA.error(Localiser.msg("044040", filename, cause));
        if (cause instanceof InvalidMetaDataException) {
            throw (InvalidMetaDataException) cause;
        }
        throw new NucleusException(Localiser.msg("044033", e), cause);
    } finally {
        try {
            in.close();
        } catch (Exception ignore) {
        // Do nothing
        }
    }
}
Also used : InvalidMetaDataException(org.datanucleus.metadata.InvalidMetaDataException) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) EntityResolver(org.xml.sax.EntityResolver) NucleusException(org.datanucleus.exceptions.NucleusException) InvalidMetaDataException(org.datanucleus.metadata.InvalidMetaDataException) SAXException(org.xml.sax.SAXException) NucleusUserException(org.datanucleus.exceptions.NucleusUserException) DefaultHandler(org.xml.sax.helpers.DefaultHandler) SAXException(org.xml.sax.SAXException) NucleusException(org.datanucleus.exceptions.NucleusException)

Aggregations

EntityResolver (org.xml.sax.EntityResolver)85 InputSource (org.xml.sax.InputSource)41 SAXException (org.xml.sax.SAXException)37 IOException (java.io.IOException)32 DefaultHandler (org.xml.sax.helpers.DefaultHandler)25 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)23 DocumentBuilder (javax.xml.parsers.DocumentBuilder)22 Test (org.junit.jupiter.api.Test)22 Document (org.w3c.dom.Document)18 StringReader (java.io.StringReader)15 Field (java.lang.reflect.Field)14 SAXParseException (org.xml.sax.SAXParseException)14 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)11 NodeList (org.w3c.dom.NodeList)11 ByteArrayInputStream (java.io.ByteArrayInputStream)10 InputStream (java.io.InputStream)10 ArrayList (java.util.ArrayList)9 Node (org.w3c.dom.Node)9 ErrorHandler (org.xml.sax.ErrorHandler)9 Element (org.w3c.dom.Element)8