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;
}
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;
}
}
}
Aggregations