Search in sources :

Example 1 with XMLHelper

use of org.hibernate.util.xpl.XMLHelper in project jbosstools-hibernate by jbosstools.

the class OpenMappingUtils method getDocument.

/**
 * Trying to find hibernate console config mapping file,
 * which is corresponding to provided element.
 *
 * @param configXMLFile
 * @param entityResolver
 * @return
 */
public static Document getDocument(java.io.File configXMLFile, EntityResolver entityResolver) {
    Document doc = null;
    if (configXMLFile == null) {
        return doc;
    }
    InputStream stream = null;
    try {
        stream = new FileInputStream(configXMLFile);
    } catch (FileNotFoundException e) {
        // $NON-NLS-1$
        HibernateConsolePlugin.getDefault().logErrorMessage("Configuration file not found", e);
    }
    try {
        List<SAXParseException> errors = new ArrayList<SAXParseException>();
        XMLHelper helper = new XMLHelper();
        SAXReader saxReader = helper.createSAXReader(configXMLFile.getPath(), errors, entityResolver);
        saxReader.setValidation(false);
        doc = saxReader.read(new InputSource(stream));
        if (errors.size() != 0) {
            // $NON-NLS-1$
            HibernateConsolePlugin.getDefault().logErrorMessage("invalid configuration", (Throwable[]) errors.toArray(new Throwable[0]));
        }
    } catch (DocumentException e) {
        // $NON-NLS-1$
        HibernateConsolePlugin.getDefault().logErrorMessage("Could not parse configuration", e);
    } finally {
        try {
            if (stream != null)
                stream.close();
        } catch (IOException ioe) {
            // $NON-NLS-1$
            HibernateConsolePlugin.getDefault().logErrorMessage("could not close input stream for", ioe);
        }
    }
    return doc;
}
Also used : InputSource(org.xml.sax.InputSource) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) SAXReader(org.dom4j.io.SAXReader) FileNotFoundException(java.io.FileNotFoundException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Document(org.dom4j.Document) IDocument(org.eclipse.jface.text.IDocument) FileInputStream(java.io.FileInputStream) SAXParseException(org.xml.sax.SAXParseException) DocumentException(org.dom4j.DocumentException) XMLHelper(org.hibernate.util.xpl.XMLHelper)

Example 2 with XMLHelper

use of org.hibernate.util.xpl.XMLHelper 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

FileInputStream (java.io.FileInputStream)2 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 ArrayList (java.util.ArrayList)2 DocumentException (org.dom4j.DocumentException)2 XMLHelper (org.hibernate.util.xpl.XMLHelper)2 InputSource (org.xml.sax.InputSource)2 SAXParseException (org.xml.sax.SAXParseException)2 File (java.io.File)1 Document (org.dom4j.Document)1 Node (org.dom4j.Node)1 DOMWriter (org.dom4j.io.DOMWriter)1 SAXReader (org.dom4j.io.SAXReader)1 IDocument (org.eclipse.jface.text.IDocument)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