[jdom-interest] JDOM b7 SAXBuilder bug

Paolo Lioy jdom at yaoo.org
Thu Sep 6 01:43:55 PDT 2001


hi All,

Using this release I experienced a bug in the class SAXBuilder which make any custom filter useless (try the demos)

I have patched the SAXBuilder and fix this bug ,using the faster way. 
( theres many way to fix it and they can also involve the design structure) 

the problem :

 an error in the method "configureParser" or a wrong position of the code which concatenate the chain of the custom filters
  anyway you can see that each time the "build" method is called a call to configureParser is made before to parse the file
  this method setup the chain of custom filter in order to put the default parser as the main parent of the chain 
  [ root.setParent(parser); ]
  and the custom filter  as the top of the chain
 [ parser = saxXMLFilter; ]
 this is ok but doesn't make any sense since the variable parser is a local variable of the method and it will never be used after the configuration

-----------------------------------------------------------------------------------------------------------------------------------------
    public Document build(InputSource in) throws JDOMException {
        SAXHandler contentHandler = null;

        try {
            // Create and configure the content handler.
            contentHandler = createContentHandler();
            configureContentHandler(contentHandler);

            // Create and configure the parser.
            XMLReader parser = createParser();
            configureParser(parser, contentHandler);

            // Parse the document.
            parser.parse(in);

        .......ecc
-----------------------------------------------------------------------------------------------------------------------------------------
    protected void configureParser(XMLReader parser, SAXHandler contentHandler)  throws Exception {

        // Install optional filter
        if (saxXMLFilter != null) {
            // Connect filter chain to parser
            XMLFilter root = saxXMLFilter;
            while (root.getParent() instanceof XMLFilter) {
                root = (XMLFilter)root.getParent();
            }
            root.setParent(parser);

            // Read from filter
            parser = saxXMLFilter;
        }

        // Setup SAX handlers.
        .......ecc
-----------------------------------------------------------------------------------------------------------------------------------------

one solution :

make the method "configureParser" to return a value so when the method "build" call the top filter it will be the correct one
-----------------------------------------------------------------------------------------------------------------------------------------
    public Document build(InputSource in) throws JDOMException {
        SAXHandler contentHandler = null;

        try {
            // Create and configure the content handler.
            contentHandler = createContentHandler();
            configureContentHandler(contentHandler);

            // Create and configure the parser.
            XMLReader parser = createParser();
            parser=configureParser(parser, contentHandler);

            // Parse the document.
            parser.parse(in);

            ...................... ecc
-----------------------------------------------------------------------------------------------------------------------------------------
    protected XMLReader configureParser(XMLReader parser, SAXHandler contentHandler)  throws Exception {

        // Install optional filter
        if (saxXMLFilter != null) {
            // Connect filter chain to parser
            XMLFilter root = saxXMLFilter;
            while (root.getParent() instanceof XMLFilter) {
                root = (XMLFilter)root.getParent();
            }
            root.setParent(parser);

            // Read from filter
            parser = saxXMLFilter;
        }
        ...................... ecc
        return parser; 
-----------------------------------------------------------------------------------------------------------------------------------------

regards,
Paolo Lioy

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://jdom.org/pipermail/jdom-interest/attachments/20010906/0ac12e68/attachment.htm


More information about the jdom-interest mailing list