Skip to content

FactoryAdapter loadDocument() modifies XMLReader and breaks it #689

@stevedlawrence

Description

@stevedlawrence

We have an XMLReader (via Xerces used for validation) that breaks if setEntityResolver() is called. Instead, the EntityResolver must be set by calling setProperty(..., ...) with the resolver object as the value. If setEntityResolver is called, this XMLReader sees the loaded XML as invalid and fails to parse. Note that in this XMLReader, getEntityResolver() returns null when the entity resolver is set via setProperty`.

Unfortunately, the loadDocument function in the FactoryAdapter sets an entity resolver if getEnitytResolver returns null:

https://github.com/scala/scala-xml/blob/main/shared/src/main/scala/scala/xml/parsing/FactoryAdapter.scala#L120

This causes our XMLReader to fail when used with a FactoryAdapter. It looks like this was added in commit 1cc9ea8 and moved to the FactoryAdapter in subsequent changes, so wasn't an issue prior to v2.2.0 as best I can tell.

Can setting the entity resolver (and maybe other XMLReader changes made in loadDocument) be made configurable so we can disable it if needed?

Or maybe alternatively, change the def reader in XMLLoader.scala so that it makes these changes only to the XMLReader it creates? For example

def reader: XMLReader = {
  val r = parser.getXMLReader
  r.setContentHandler(adapter)
  r.setDTDHandler(adapter)
  r.setEnityResolver(adapter)
  r.setProperty("http://xml.org/sax/properties/lexical-handler", adapter)
  ...
  r
}

This way if we override the reader to provide a custom one we avoid potentially breaking changes, and have full control over the reader, with the understanding that we may need to call some or all of the other functions ourselves to work with a FactoryAdapter.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions