Search in sources :

Example 1 with DOMWriter

use of org.dom4j.io.DOMWriter in project jbosstools-hibernate by jbosstools.

the class ConfigurationFactory method loadConfigurationXML.

@SuppressWarnings("unchecked")
private IConfiguration loadConfigurationXML(IConfiguration localCfg, boolean includeMappings, EntityResolver entityResolver) {
    File configXMLFile = prefs.getConfigXMLFile();
    if (!includeMappings) {
        org.dom4j.Document doc;
        XMLHelper xmlHelper = new XMLHelper();
        InputStream stream = null;
        // $NON-NLS-1$
        String resourceName = "<unknown>";
        if (configXMLFile != null) {
            resourceName = configXMLFile.toString();
            try {
                stream = new FileInputStream(configXMLFile);
            } catch (FileNotFoundException e1) {
                throw new HibernateConsoleRuntimeException(ConsoleMessages.ConsoleConfiguration_could_not_access + configXMLFile, e1);
            }
        } else {
            // $NON-NLS-1$
            resourceName = "/hibernate.cfg.xml";
            if (checkHibernateResoureExistence(resourceName)) {
                // simulate hibernate's
                stream = getResourceAsStream(resourceName);
            // default look up
            } else {
                return localCfg;
            }
        }
        try {
            List<SAXParseException> errors = new ArrayList<SAXParseException>();
            doc = xmlHelper.createSAXReader(resourceName, errors, entityResolver).read(new InputSource(stream));
            if (errors.size() != 0) {
                throw new MappingException(ConsoleMessages.ConsoleConfiguration_invalid_configuration, errors.get(0));
            }
            List<Node> list = doc.getRootElement().element("session-factory").elements(// $NON-NLS-1$ //$NON-NLS-2$
            "mapping");
            for (Node element : list) {
                element.getParent().remove(element);
            }
            DOMWriter dw = new DOMWriter();
            Document document = dw.write(doc);
            return localCfg.configure(document);
        } catch (DocumentException e) {
            throw new HibernateException(ConsoleMessages.ConsoleConfiguration_could_not_parse_configuration + resourceName, e);
        } finally {
            try {
                if (stream != null)
                    stream.close();
            } catch (IOException ioe) {
            // log.warn( "could not close input stream for: " + resourceName, ioe );
            }
        }
    } else {
        if (configXMLFile != null) {
            return localCfg.configure(configXMLFile);
        } else {
            IConfiguration resultCfg = localCfg;
            if (checkHibernateResoureExistence("/hibernate.cfg.xml")) {
                // $NON-NLS-1$
                resultCfg = localCfg.configure();
            }
            return resultCfg;
        }
    }
}
Also used : InputSource(org.xml.sax.InputSource) DOMWriter(org.dom4j.io.DOMWriter) HibernateException(org.jboss.tools.hibernate.runtime.spi.HibernateException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Node(org.dom4j.Node) FileNotFoundException(java.io.FileNotFoundException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Document(org.w3c.dom.Document) FileInputStream(java.io.FileInputStream) MappingException(org.jboss.tools.hibernate.exception.MappingException) SAXParseException(org.xml.sax.SAXParseException) DocumentException(org.dom4j.DocumentException) XMLHelper(org.hibernate.util.xpl.XMLHelper) IConfiguration(org.jboss.tools.hibernate.runtime.spi.IConfiguration) File(java.io.File)

Aggregations

File (java.io.File)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 ArrayList (java.util.ArrayList)1 DocumentException (org.dom4j.DocumentException)1 Node (org.dom4j.Node)1 DOMWriter (org.dom4j.io.DOMWriter)1 XMLHelper (org.hibernate.util.xpl.XMLHelper)1 MappingException (org.jboss.tools.hibernate.exception.MappingException)1 HibernateException (org.jboss.tools.hibernate.runtime.spi.HibernateException)1 IConfiguration (org.jboss.tools.hibernate.runtime.spi.IConfiguration)1 Document (org.w3c.dom.Document)1 InputSource (org.xml.sax.InputSource)1 SAXParseException (org.xml.sax.SAXParseException)1