head	1.1;
access;
symbols;
locks; strict;
comment	@# @;


1.1
date	2001.07.17.00.29.10;	author dlr;	state Exp;
branches;
next	;


desc
@@


1.1
log
@Removed HelmHeaders Joist dependency by moving some files which I had previously overlooked from Joist into Helm.  Provided better JavaDoc for the NoodleFilter class.
@
text
@/* ================================================================
 * Copyright (c) 2000 Collab.Net.  All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are
 * met:
 * 
 * 1. Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the distribution.
 * 
 * 3. The end-user documentation included with the redistribution, if
 * any, must include the following acknowlegement: "This product includes
 * software developed by Collab.Net <http://www.Collab.Net/>."
 * Alternately, this acknowlegement may appear in the software itself, if
 * and wherever such third-party acknowlegements normally appear.
 * 
 * 4. The hosted project names must not be used to endorse or promote
 * products derived from this software without prior written
 * permission. For written permission, please contact info@@collab.net.
 * 
 * 5. Products derived from this software may not use the "Tigris" or 
 * "Scarab" names nor may "Tigris" or "Scarab" appear in their names without 
 * prior written permission of Collab.Net.
 * 
 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
 * IN NO EVENT SHALL COLLAB.NET OR ITS CONTRIBUTORS BE LIABLE FOR ANY
 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * ====================================================================
 * 
 * This software consists of voluntary contributions made by many
 * individuals on behalf of Collab.Net.
 */ 
package org.tigris.helm.publish;

import java.util.*;

import org.apache.turbine.util.Log;

/**
 * This class is responsible for loading all the NoodleFilters
 * as well as executing them.
 *
 * @@author <a href="mailto:jon@@collab.net">Jon S. Stevens</a>
 */
public final class NoodleFilterLoader
{
    /** The single instance of this class. */
    private static NoodleFilterLoader instance = null;
    /** this contains the filters */
    private static ResourceBundle rb = null;
    /** a cache of the list of filters */
    private static String[] preFilters = null;
    /** a cache of the list of filters */
    private static String[] postFilters = null;
    /** a cache of instances of pre NoodleFilters keyed by classname */
    private static Hashtable preFilterCache = new Hashtable();
    /** a cache of instances of post NoodleFilters keyed by classname */
    private static Hashtable postFilterCache = new Hashtable();

    /**
     * This constructor is private to force clients to use getInstance()
     * to access this class.
     */
    private NoodleFilterLoader()
    {
    }
    
    /**
        This method executes all the prefilters in the order
        in which they were defined in the properties files.
    */
    public final void doExecutePreFilters(NoodleData noodleData)
        throws Exception
    {
        executeFilters(preFilters, preFilterCache, noodleData);
    }

    /**
        This method executes all the postfilters in the order
        in which they were defined in the properties files.
    */
    public final void doExecutePostFilters(NoodleData noodleData)
        throws Exception
    {
        executeFilters(postFilters, postFilterCache, noodleData);
    }

    /**
        execute the filters in such a way that it doesn't matter if they are 
        post or pre. the public doExecute*Filters methods will determine that.
    */
    private final void executeFilters(String[] filters, Hashtable filterCache, NoodleData noodleData)
        throws Exception
    {
        for (int i=0;i<filters.length ;i++ )
        {
            NoodleFilter nf = null;

            if (filters[i] == null)
		throw new Exception ("A filter entry was null." +
                    "Please validate that the entries in " +
                    "NoodleResources.properties are correctly defined.");

            // check the cache
            if (filterCache.containsKey(filters[i]))
            {
                // get the object from the cache if it exists
                nf = (NoodleFilter) filterCache.get(filters[i]);                
            }
            else
            {
                // not in cache. create it and add it to the cache
                Class servClass = Class.forName( filters[i] );                
                nf = ( NoodleFilter ) servClass.newInstance();
                filterCache.put (filters[i], nf );
            }
            nf.doProcess(noodleData);
        }        
    }
    
    /**
     * The method through which this class is accessed.
     *
     * @@return The single instance of this class.
     */
    public static final NoodleFilterLoader getInstance()
    {
        if (instance == null)
        {
            synchronized (NoodleFilterLoader.class)
            {
                if (instance == null)
                {
                    instance = new NoodleFilterLoader();
                    rb = NoodleResources.getInstance().getBundle();
                    // needs to execute _after_ above line
                    preFilters = getOrderedValues("prefilter");
                    postFilters = getOrderedValues("postfilter");
                }
            }
        }
        return instance;
    }
    
    /**
        not the most efficient, but it works and is only executed during
        initialization.
    */
    private static final String[] getOrderedValues(String prefix)
    {
        Hashtable rawData = new Hashtable();
        // get a list of keys from the resource bundle
        for (Enumeration e = rb.getKeys(); e.hasMoreElements();)
        {
            String filter = (String) e.nextElement();
            // if it starts with the prefix, add it to the hashtable keyed
            // off of the order number
            if (filter.startsWith(prefix))
            {
                rawData.put (getNumber(filter), rb.getString(filter));
            }
        }
        String[] items = new String[rawData.size()];
        // put things into the String[] in order
        for (int i=0;i < rawData.size() ;i++ )
        {
            items[i] = (String) rawData.get(new Integer(i).toString());
        }
        return items;
    }
    
    /**
        INPUT: prefilter.1
        <p>
        OUTPUT: 1
    */
    private static final String getNumber(String name)
    {
        return (name.substring(name.lastIndexOf(".")+1, name.length()));
    }
}    
@
