How to add custom initialization to the Pull Parser

32 views
Skip to first unread message

Manuel Bernhardt

unread,
May 18, 2012, 11:50:09 AM5/18/12
to scales-xml
Hi,

I'd like to initialize the pull parser so that it doesn't blow up when
undeclared entities come in. Right now I get the following trace:

[error] WstxParsingException: Undeclared general entity "lot"
[error] at [row,col {unknown-source}]: [3,19] (StreamScanner.java:
485)
[error]
com.ctc.wstx.sr.StreamScanner.constructWfcException(StreamScanner.java:
612)
[error]
com.ctc.wstx.sr.StreamScanner.throwParseError(StreamScanner.java:485)


I was thinking of configuring something along the lines of

xmlif.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES,
Boolean.FALSE);
  xmlif.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES,
Boolean.FALSE);
  xmlif.setProperty(XMLInputFactory.IS_COALESCING, Boolean.FALSE);

but how can I get a hold on the reader?

Thanks!

Manuel

Chris

unread,
May 18, 2012, 5:32:16 PM5/18/12
to scales-xml
Hi Manuel,

The parsers used for both pull and sax based parsing are parameters to
the calls allowing you to replace the whole parser factory used.

In the case of pullXml(Resource/Completely) the parserFactoryPool
named parameter supplies the factory with the default being the fun
name DefaultStaxInputFactoryPool.

For example to add those properties define:

  object CustomStaxInputFactoryPool extends
scales.utils.SimpleUnboundedPool[XMLInputFactory] { pool =>

    val cdata = "http://java.sun.com/xml/stream/properties/report-
cdata-event"

    def create = {
      val fac = XMLInputFactory.newInstance()
      if (fac.isPropertySupported(cdata)) {
fac.setProperty(cdata, java.lang.Boolean.TRUE)
      }
      if
(fac.isPropertySupported(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES))
{
fac.setProperty(XMLInputFactory.IS_REPLACING_ENTITY_REFERENCES,
java.lang.Boolean.FALSE)
      }
      if
(fac.isPropertySupported(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES))
{
fac.setProperty(XMLInputFactory.IS_SUPPORTING_EXTERNAL_ENTITIES,
java.lang.Boolean.FALSE)
      }
      if (fac.isPropertySupported(XMLInputFactory.IS_COALESCING)) {
fac.setProperty(XMLInputFactory.IS_COALESCING,
java.lang.Boolean.FALSE)
      }
      fac
    }
  }

Then call pullXml with the pool:

  pullXml( source, parserFactoryPool = CustomStaxInputFactoryPool )

You can also use this approach to customise creation of the factory
itself.

Hope that helps,

Cheers,
Chris

On May 18, 5:50 pm, Manuel Bernhardt <bernhardt.man...@gmail.com>
wrote:

Manuel Bernhardt

unread,
May 19, 2012, 8:07:32 AM5/19/12
to scale...@googlegroups.com
Thanks Chris, this is exactly what I was looking for!

Cheers,

Manuel

Chris Twiner

unread,
May 19, 2012, 9:26:33 AM5/19/12
to scale...@googlegroups.com

Excellent news :)

Reply all
Reply to author
Forward
0 new messages