Package org.exist.util

Source Code of org.exist.util.XMLReaderPool

/*
*  eXist Open Source Native XML Database
*  Copyright (C) 2000-04,  Wolfgang M. Meier (wolfgang@exist-db.org)
*
*  This library is free software; you can redistribute it and/or
*  modify it under the terms of the GNU Library General Public License
*  as published by the Free Software Foundation; either version 2
*  of the License, or (at your option) any later version.
*
*  This library is distributed in the hope that it will be useful,
*  but WITHOUT ANY WARRANTY; without even the implied warranty of
*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
*  GNU Library General Public License for more details.
*
*  You should have received a copy of the GNU General Public License
*  along with this program; if not, write to the Free Software
*  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
*
*  $Id$
*/
package org.exist.util;

import org.apache.commons.pool.PoolableObjectFactory;
import org.apache.commons.pool.impl.StackObjectPool;
import org.apache.log4j.Logger;
import org.exist.Namespaces;
import org.exist.storage.BrokerPool;
import org.exist.validation.GrammarPool;
import org.xml.sax.SAXNotRecognizedException;
import org.xml.sax.SAXNotSupportedException;
import org.xml.sax.XMLReader;
import org.xml.sax.ext.DefaultHandler2;

/**
* Maintains a pool of XMLReader objects. The pool is available through
* {@link BrokerPool#getParserPool()}.
*
* @author wolf
*/
public class XMLReaderPool extends StackObjectPool<XMLReader> {

    private final static Logger LOG = Logger.getLogger(XMLReaderPool.class);

    private final static DefaultHandler2 DUMMY_HANDLER = new DefaultHandler2();

  private final Configuration config;

    /**
     *
     *
     * @param factory
     * @param maxIdle
     * @param initIdleCapacity
     */
    public XMLReaderPool(Configuration config, PoolableObjectFactory<XMLReader> factory, int maxIdle, int initIdleCapacity) {
        super(factory, maxIdle, initIdleCapacity);
        this.config = config;
    }

    public synchronized XMLReader borrowXMLReader() {
        try {
            return super.borrowObject();
        } catch (final Exception e) {
            throw new IllegalStateException("error while returning XMLReader: " + e.getMessage(), e );
        }
    }

    @Override
    public synchronized XMLReader borrowObject() throws Exception {
        return borrowXMLReader();
    }
   
  
    public synchronized void returnXMLReader(XMLReader reader) {
        if (reader == null) {
            return;
        }
       
        try {           
            reader.setContentHandler(DUMMY_HANDLER);
            reader.setErrorHandler(DUMMY_HANDLER);
            reader.setProperty(Namespaces.SAX_LEXICAL_HANDLER, DUMMY_HANDLER);
           
            // DIZZZ; workaround Xerces bug. Cached DTDs cause for problems during validation parsing.
            final GrammarPool grammarPool =
               (GrammarPool) getReaderProperty(reader,
                                    XMLReaderObjectFactory.APACHE_PROPERTIES_INTERNAL_GRAMMARPOOL);
            if(grammarPool!=null){
                grammarPool.clearDTDs();
            }
           
            super.returnObject(reader);
           
        } catch (final Exception e) {
            throw new IllegalStateException("error while returning XMLReader: " + e.getMessage(), e);
        }
    }

    @Override
    public synchronized void returnObject(XMLReader obj) throws Exception {
        returnXMLReader(obj);
    }  

    private Object getReaderProperty(XMLReader xmlReader, String propertyName){

        Object object = null;
        try {
            object=xmlReader.getProperty(propertyName);

        } catch (final SAXNotRecognizedException ex) {
            LOG.error("SAXNotRecognizedException: " + ex.getMessage());

        } catch (final SAXNotSupportedException ex) {
            LOG.error("SAXNotSupportedException:" + ex.getMessage());
        }
        return object;
    }

   
}
TOP

Related Classes of org.exist.util.XMLReaderPool

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.