tags:

views:

92

answers:

1

In Java SAX processing, I dot not manage to obtain entity reference names from the org.xml.sax.ext.EntityResolver2 resolveEntity method. The name argument is always null even if the documentation states that "'name' is never null when invoked by a SAX2 parser".

Any idea ?

To demonstrate the issue, I have created a Jython 2.5 script. It prints (on my box):

Entity Resolver 2 enabled: True
Entity name: None

Jython 2.5 script:

# Jython
from org.python.core.util import StringUtil
from jarray import array

# Java Standard Edition
from org.xml.sax import *
from org.xml.sax.ext import *
from org.xml.sax.helpers import *
from java.io import ByteArrayInputStream

xml = """\
<!DOCTYPE doc
[<!ENTITY entity SYSTEM "entity-file">
]>
<doc>&entity;</doc>
"""

def empty_source():
    _source = InputSource()
    byte_stream = ByteArrayInputStream(array([], "b"))
    _source.setByteStream(byte_stream)
    return _source

class Handler(EntityResolver2):
    def getExternalSubset(self, name, baseURI):
        return None
    def resolveEntity(self, name, publicId, baseURI, systemId):
        print "Entity name:", name 
        return empty_source()

reader = XMLReaderFactory.createXMLReader()
enabled = reader.getFeature("http://xml.org/sax/features/use-entity-resolver2")
print "Entity Resolver 2 enabled:", enabled 

handler = Handler()
reader.setEntityResolver(handler) 
bytes = StringUtil.toBytes(xml)
byte_stream = ByteArrayInputStream(bytes)
source = InputSource(byte_stream)
reader.parse(source)
A: 

Use a more recent version of Xerces (2.9.1 for example), instead of the "old" one bundled with Java.

boisgera