[digester3] Namespaces, prefixes with digester

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

[digester3] Namespaces, prefixes with digester

Christopher Schultz-2
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

All,

I'm having an odd problem which I'm not sure I've seen before. I
*think* I'm doing everything correctly, but I'm not getting the
behavior I expect.

I'm trying to parse an XML document using digester 3.2. My code looks
like this:

        Digester digester = new Digester();

        try {

digester.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);

digester.setFeature("http://xml.org/sax/features/external-general-entiti
es",
false);

digester.setFeature("http://xml.org/sax/features/external-parameter-enti
ties",
false);

digester.setFeature("http://apache.org/xml/features/disallow-doctype-dec
l",
true);
        } catch (ParserConfigurationException pce) {
            throw new SAXException("Cannot enable parser feature", pce);
        }

        digester.setRuleNamespaceURI("my-namespace-URI");
        digester.setNamespaceAware(true);
        digester.setValidating(false);

        MyTopLevelBean bean = new MyTopLevelBean();
        digester.push(bean);

        digester.add[Whatever]("path/without/namespace/prefix", ...);
        [... other rule additions ...]

        digester.parse(xmlInput);

When I run this with an XML document with no namespace prefix but with
a namespace matching the one I have in the code:

  <document xmlns="my-namespace-URI"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:namespaceLocation="my-namespace-URI http://my/namespace/schema.xsd">
    <sample-value>foo</sample-value>
  </document>

All works as expected. But if I modify the document to include a
namespace prefix:

  <foo:document xmlns:foo="my-namespace-URI"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:namespaceLocation="my-namespace-URI http://my/namespace/schema.xsd">
    <foo:sample-value>foo</foo:sample-value>
  </foo:document>

I don't get any errors, but none of my rules fire and I get no data
coming out of the document. The code is identical.

Am I overlooking something very obvious?

- -chris
-----BEGIN PGP SIGNATURE-----
Comment: Using GnuPG with Thunderbird - https://www.enigmail.net/

iQIzBAEBCAAdFiEEMmKgYcQvxMe7tcJcHPApP6U8pFgFAl5G0yUACgkQHPApP6U8
pFirZBAAj4FZ2NLJWczJDCAMTc+usKlUw0ThrxNt45LAO0TU2geMqnXi8EhwLNgS
Bx9wJC4PGlgVhrUOktsIVnHlyCTi7UJcr4flakSWpJQSlpX5psV484f0eUgi7A3D
9IUsmRgfsKCtEZJjIrUiLJqIK9HrsxJx18FYGwpvFiYVtr+O/D4Uhw2aHexpShmG
c9xktRj/xi2ek9GgBbm/jVQIyMSWaVr+gn+XUxlKxhr0CTN58/Lyc2LtdJ/5xWg6
C7Lr2CxA1YuRbUswPIA2/xfcSqB4b0MGvSd9NeWFaMporwM5EXXqSvXUJXUQ9Co0
PKISLyWCr2BaCPXKkmHyL6biBU8TfdCpsWI2tb/fx9IBmkV3+kV9hCdLS8XhS3nZ
SBumzhCXVGMVYE5cAr8EvN4S81GtMertiLMI6oH8X7Zk0HK/ufQhRIXe3UY7kYQ0
2JkasShdBHji29sP6irnAY7C3m+kShFeGcOfQ8WwcIb02SUdk3zb/0JlDQhZBXBs
oTCIFNRHwkETjgcQM5Cq6wjljGvDhA+aL+V7AWLGGqrkwV5Jc2QoarK14GXC9fQt
zn67fTr/4Qk5zFoUJuyPmm/Dyld1xRO+M3nuN9N0s11ncIeTvyDqpwLbaAf9Iyiz
SLe//fGrP1M3w/nH1EBSQHmkU/6vZM5hx03gSsPZgpSEHyOUPhc=
=UaQv
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: [digester3] Namespaces, prefixes with digester

Christopher Schultz-2
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA256

All,

I've built a pair of unit tests which I think should pass. The
"namespacePrefix" one fails.

Any help with this would be appreciated. It's entirelyentierly
possible that I'm missing some obvious concept or setting of the digeste
r.

The "config" method probably does things that are unnecessary -- such
as setting the various XML parsing features. But I wanted this to
mirror as closely as possible the situation in my application.

Thanks,
- -chris

=== CUT ===

    @Test
    public void testXMLParsing() throws Exception {
        testXMLParsing("<list xmlns=\"http://apache.org/test\">"
                + "<element>foo</element>"
                + "</list>");
    }

    @Test
    public void testNamespacePrefixedXMLParsing() throws Exception {
        testXMLParsing("<ns:list xmlns:ns=\"http://apache.org/test\">"
                + "<ns:element>foo</ns:element>"
                + "</ns:list>");
    }

    private void testXMLParsing(String xml) throws Exception {
        Digester digester = config(new Digester());

        ArrayList<String> list = new ArrayList<String>();
        digester.push(list);

        digester.addCallMethod("list/element", "add", 1, new Class[] {
Object.class });
        digester.addCallParam("list/element", 0);

        digester.parse(new StringReader(xml));

        Assert.assertEquals(1,  list.size());
        Assert.assertEquals("foo", list.get(0));
    }

    private Digester config(Digester digester) throws SAXException {
        try {

digester.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);

digester.setFeature("http://xml.org/sax/features/external-general-entiti
es",
false);

digester.setFeature("http://xml.org/sax/features/external-parameter-enti
ties",
false);

digester.setFeature("http://apache.org/xml/features/disallow-doctype-dec
l",
true);
        } catch (ParserConfigurationException pce) {
            throw new SAXException("Cannot enable parser feature", pce);
        }

        digester.setRuleNamespaceURI("http://apache.org/test");
        digester.setNamespaceAware(true);
        digester.setValidating(false);

        return digester;
    }


=== CUT ===

On 2/14/20 12:04, Christopher Schultz wrote:

> All,
>
> I'm having an odd problem which I'm not sure I've seen before. I
> *think* I'm doing everything correctly, but I'm not getting the
> behavior I expect.
>
> I'm trying to parse an XML document using digester 3.2. My code
> looks like this:
>
> Digester digester = new Digester();
>
> try {
>
> digester.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
>
> digester.setFeature("http://xml.org/sax/features/external-general-enti
ti
>
>
es",
> false);
>
> digester.setFeature("http://xml.org/sax/features/external-parameter-en
ti
>
>
ties",
> false);
>
> digester.setFeature("http://apache.org/xml/features/disallow-doctype-d
ec
>
>
l",

> true); } catch (ParserConfigurationException pce) { throw new
> SAXException("Cannot enable parser feature", pce); }
>
> digester.setRuleNamespaceURI("my-namespace-URI");
> digester.setNamespaceAware(true); digester.setValidating(false);
>
> MyTopLevelBean bean = new MyTopLevelBean(); digester.push(bean);
>
> digester.add[Whatever]("path/without/namespace/prefix", ...); [...
> other rule additions ...]
>
> digester.parse(xmlInput);
>
> When I run this with an XML document with no namespace prefix but
> with a namespace matching the one I have in the code:
>
> <document xmlns="my-namespace-URI"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xsi:namespaceLocation="my-namespace-URI
> http://my/namespace/schema.xsd"> <sample-value>foo</sample-value>
> </document>
>
> All works as expected. But if I modify the document to include a
> namespace prefix:
>
> <foo:document xmlns:foo="my-namespace-URI"
> xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
> xsi:namespaceLocation="my-namespace-URI
> http://my/namespace/schema.xsd">
> <foo:sample-value>foo</foo:sample-value> </foo:document>
>
> I don't get any errors, but none of my rules fire and I get no
> data coming out of the document. The code is identical.
>
> Am I overlooking something very obvious?
>
> -chris
>
-----BEGIN PGP SIGNATURE-----
Comment: Using GnuPG with Thunderbird - https://www.enigmail.net/

iQIzBAEBCAAdFiEEMmKgYcQvxMe7tcJcHPApP6U8pFgFAl5IP4wACgkQHPApP6U8
pFgSDRAAxYfJqPJ3YKiISLbPX0JgtIyD3o3NeFdimXQVMdduHif6aO+W8oHzOfWN
/OejhyGpwcdgVXbyot7kvyNGFED63+fa/wYW3qoJWfMCtxwjQgXFywEml+dVgU8L
9fVQRLf39d1bvXCGYovofEHRBeVxZkBQ1Jo0JHoGDOwNCNdyBQjXBYd1HFUtEOmE
vp540GsoXsXXNC4j/rFfhY87zJ8wI6PvBOEztu9rk0+cM0JPkIADpSgLT1njQWa9
FoTqlYdktp2y2Q0aZXY6lnUhSsJDDkLRtaySwULfXyYsKz6H4PRe4gZe3TFuJMgw
mNpnVSP8bxbD2JNugm4AiontICnauMJR6NW/H0qCxyD8oJzvEcpt1umO6dUF+ESb
5+5FJe+ngBThJ4RX4R7371So8lKCuHyHQ5L4QiAFq060V8af8HpDdTTj6vxiKxVc
Co/sHT8vbk4zc1bADsOKqUcvWxJbtav7cyl8nY5ejTIXQYF5RKhk8Qmh/lJ9U8yF
v8vRDvpnc4eE1/1Z765uXDOajELip/dKFMBU2iVvW7JuXjsLyLH7hMeDHSjBvynA
m/GEGitI0gZk/OddhQXv/BrljgpGHJQTJosmK8q2xDfSRHEk2pXzTwbXokhuNKYd
JXLM4RgJW0dyQpDxGHK/EUPekDWyGX7c3lCenbXrtckSgKW566c=
=IfD6
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]