/*
 *  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: XMLReaderPool.java 4403 2006-09-27 12:27:37Z wolfgang_m $
 */
package org.exist.util;

import org.apache.commons.pool.PoolableObjectFactory;
import org.apache.commons.pool.impl.StackObjectPool;
import org.exist.storage.BrokerPool;
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 {

    private final static DefaultHandler2 DUMMY_HANDLER = new DefaultHandler2();
    
    /**
     * 
     * 
     * @param factory 
     * @param maxIdle 
     * @param initIdleCapacity 
     */
    public XMLReaderPool(PoolableObjectFactory factory, int maxIdle, int initIdleCapacity) {
        super(factory, maxIdle, initIdleCapacity);
    }

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

    public synchronized void returnXMLReader(XMLReader reader) {
        if (reader == null) {
            return;
        }
        try {
            reader.setContentHandler(DUMMY_HANDLER);
            reader.setErrorHandler(DUMMY_HANDLER);
            reader.setProperty("http://xml.org/sax/properties/lexical-handler", DUMMY_HANDLER);
            returnObject(reader);
        } catch (Exception e) {
            throw new IllegalStateException("error while returning XMLReader: " + e.getMessage());
        }
    }
}
