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.
Showing posts with label XML. Show all posts
Showing posts with label XML. Show all posts
Feb 15, 2008
Using EMF with XML Catalog
Subscribe to:
Posts (Atom)
