//package org.apache.commons.grant;
package org.apache.maven.app;

/*
 * $Header: /home/cvspublic/jakarta-commons-sandbox/grant/src/java/org/apache/commons/grant/GrantProject.java,v 1.1.1.1 2002/06/19 15:42:34 werken Exp $
 * $Revision: 1.1.1.1 $
 * $Date: 2002/06/19 15:42:34 $
 *
 * ====================================================================
 *
 * The Apache Software License, Version 1.1
 *
 * Copyright (c) 1999-2001 The Apache Software Foundation.  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 the
 *        Apache Software Foundation (http://www.apache.org/)."
 *    Alternately, this acknowlegement may appear in the software itself,
 *    if and wherever such third-party acknowlegements normally appear.
 *
 * 4. The names "The Jakarta Project", "Commons", and "Apache Software
 *    Foundation" must not be used to endorse or promote products derived
 *    from this software without prior written permission. For written
 *    permission, please contact apache@apache.org.
 *
 * 5. Products derived from this software may not be called "Apache"
 *    nor may "Apache" appear in their names without prior written
 *    permission of the Apache Group.
 *
 * 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 THE APACHE SOFTWARE FOUNDATION 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 the Apache Software Foundation.  For more
 * information on the Apache Software Foundation, please see
 * <http://www.apache.org/>.
 *
 */

import java.io.File;
import java.util.Hashtable;

import org.apache.tools.ant.Project;
import org.apache.tools.ant.ProjectHelper;
import org.apache.tools.ant.BuildException;


/** A subclass of an ant <code>Project</code> which allows
 *  installation of delegators for particular functions.
 *
 *  <p>
 *  Current delegation points include:
 *
 *  <ul>
 *    <li>Properties</li>
 *  </ul>
 *
 *  @author <a href="mailto:bob@eng.werken.com">bob mcwhirter</a>
 */
public class GrantProject extends Project {

    // ------------------------------------------------------------
    //     Instance members
    // ------------------------------------------------------------

    /** Properties delegate. */
    private PropsHandler propsHandler; 

    // ------------------------------------------------------------
    //     Constructors
    // ------------------------------------------------------------

    /** Construct a new, empty <code>GrantProject</code>.
     *
     *  <p>
     *  Immediately after initialization, a <code>GrantProject</code>
     *  delegates <b>all</b> calls to the normal ant <code>Project</code>
     *  super class.   Only after installing delegators will custom
     *  behavious be acheived.
     *  </p>
     */
    public GrantProject() {
        this.propsHandler = null;
    }

    // ------------------------------------------------------------
    //     Instance methods
    // ------------------------------------------------------------

    /** Install a <code>PropsHandler</code> delegate.
     *
     *  @param propsHandler The <code>PropsHandler</code> to install,
     *         or <code>null</code> to remove any currently installed
     *         <code>PropsHandler</code>.
     */
    public void setPropsHandler(PropsHandler propsHandler) {
        this.propsHandler = propsHandler;
    }

    /** Retrieve the currently installed <code>PropsHandler</code>.
     *
     *  @return The currently installed <code>PropsHandler</code>,
     *          or <code>null</code> if no <code>PropsHandler</code>
     *          had yet to be installed.
     */
    public PropsHandler getPropsHandler() {
        return this.propsHandler;
    }

    // ------------------------------------------------------------
    //     org.apache.tools.ant.Project implementation
    // ------------------------------------------------------------

    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
    //         properties delegators:
    //
    //         If a PropsHandler is not installed, delegate
    //         up the hierarchy to the ant.Project parent
    //         class.  Otherwise, delegate outwards using
    //         the PropsHandler.
    // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 

    public String replaceProperties(String value) throws BuildException {
        return ProjectHelper.replaceProperties(this, value, getProperties() );
    }

    public void setProperty(String key, String value) {
        if (this.propsHandler == null) {
            super.setProperty(key,value);
        }
        else {
            super.setProperty(key,value);
            this.propsHandler.setProperty(key, value);
        }
    }

    public void setUserProperty(String key, String value) {
        if (this.propsHandler == null) {
            super.setUserProperty(key,value);
        }
        else {
            super.setUserProperty(key,value);
            this.propsHandler.setUserProperty(key, value);
        }
    }
    
    public void setNewProperty(String key, String value) {
        if (this.propsHandler == null) {
            super.setNewProperty(key,value);
        }
        else {
            super.setNewProperty(key,value);
            this.propsHandler.setNewProperty(key, value);
        }
    }

    public void setInheritedProperty(String key, String value) {
        if (this.propsHandler == null) {
            super.setInheritedProperty(key,value);
        }
        else {
            super.setInheritedProperty(key,value);
            this.propsHandler.setInheritedProperty(key, value);
        }
    }

    public String getProperty(String key) {
        if (this.propsHandler == null) {
            return super.getProperty(key);
        }

        return this.propsHandler.getProperty(key);
    }

    public String getUserProperty(String key) {
        if (this.propsHandler == null) {
            return super.getUserProperty(key);
        }

        return this.propsHandler.getUserProperty(key);
    }

    public Hashtable getProperties() {
        if (this.propsHandler == null) {
            return super.getProperties();
        }

        return this.propsHandler.getProperties();
    }

    public Hashtable getUserProperties() {
        if (this.propsHandler == null) {
            return super.getUserProperties();
        }

        return this.propsHandler.getUserProperties();
    }

    public void setBaseDir(File baseDir) throws BuildException {
        super.setBaseDir( baseDir );
        setProperty( "basedir" , baseDir.getPath() );
    }
}


