When you try to load an XML document using EMF, you might have noticed that the XML Catalog is not respected and might throw parse exceptions. First I thought I had made a mistake in extending org.eclipse.wst.xml.core.catalogContributions and was debugging that. Only after a while I realized that the XML Catalog is contributed by WTP and EMF is not depending on that. So there is no way for EMF to know the existence of such catalog. I found a quick solution using some internal classes. I'm not sure whether its the best, but it works (at least for my requirement)
The idea is when EMF fails to resolve when loading an XML we should get the WTP Catalog and try to resolve with that:
loadOptions.put(XMLResource.OPTION_USE_PARSER_POOL, new XMLParserPoolImpl() {
@Override
public synchronized XMLDefaultHandler getDefaultHandler(XMLResource resource, XMLLoad xmlLoad, XMLHelper helper, Map options) {
return
new SAXXMLHandler(resource, helper, options)
{
@Override
public InputSource resolveEntity(String publicId, String systemId) throws SAXException {
InputSource result;
try {
result = super.resolveEntity(publicId, systemId);
}catch(SAXException e) {
try {
ICatalog catalog = XMLCorePlugin.getDefault().getDefaultXMLCatalog();
String resolvePublic = catalog.resolvePublic(publicId, systemId).substring(5);
InputStream inputStream = getURIConverter().createInputStream(URI.createFileURI(resolvePublic));
result = new InputSource(inputStream);
result.setPublicId(publicId);
result.setSystemId(systemId);
return result;
} catch (Exception exception) {
throw new SAXException(exception);
}
}
return result;
}
};
}
});
There are many cases where this code will fail (like if you had added the catalog entry by uri/systemId) and there might be a much better way of doing that without using any internal classes. Till I find that, I'm going to live with this hack.
Feb 15, 2008
Using EMF with XML Catalog
Subscribe to:
Post Comments (Atom)

4 comments:
Keep in mind that EMF has very few internal classes, i.e., almost none. Your approach here is not using internal classes.
A more flexible way to support what you are doing is to specialize the URIConverterImpl's (or preferably the ExtensibleURIConverterImpl's for EMF 2.4) normalize method so that normalizing the URI takes the XML catalog into account.
Thanks Ed. Will try that way also.
BTW, welcome back from your vacation. Lots of queries are awaiting for you in the newsgroup :-)
hey,
can u help me on loading an xmi(or xml its pretty much the same i think) using EMF.the goal of this operation is to be able to count some metrics like number of classes etc.
hey,
can u help me on loading an xmi(or xml its pretty much the same i think) using EMF.the goal of this operation is to be able to count some metrics like number of classes etc.
Post a Comment