package org.apache.velocity.tools.config;

/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import org.apache.velocity.tools.ToolInfo;
import org.apache.velocity.tools.Utils;

/**
 * 
 *
 * @author Nathan Bubna
 * @version $Id: ToolConfiguration.java 511959 2007-02-26 19:24:39Z nbubna $
 */
public class ToolConfiguration extends Configuration
{
    private String key;
    private String classname;
    private String restrictTo;

    public void setKey(String key)
    {
        this.key = key;
    }

    /**
     * This doesn't take a {@link Class} parameter because
     * this class was not created for all-java configuration.
     */
    public void setClass(String classname)
    {
        this.classname = classname;
    }

    public void setClassname(String classname)
    {
        setClass(classname);
    }

    public void setRestrictTo(String path)
    {
        this.restrictTo = path;
    }

    public String getKey()
    {
        if (this.key != null)
        {
            return this.key;
        }

        DefaultKey defaultKey = 
            (DefaultKey)getToolClass().getAnnotation(DefaultKey.class);
        if (defaultKey != null)
        {
            return defaultKey.value();
        }
        return null;
    }

    public String getClassname()
    {
        return this.classname;
    }

    public Class getToolClass()
    {
        try
        {
            return Utils.getClass(getClassname());
        }
        catch (ClassNotFoundException cnfe)
        {
            throw new ConfigurationException(this, cnfe);
        }
    }

    public String[] getInvalidScopes()
    {
        InvalidScope invalid =
            (InvalidScope)getToolClass().getAnnotation(InvalidScope.class);
        if (invalid != null)
        {
            return invalid.value();
        }
        else
        {
            return new String[] {};
        }
    }

    public String[] getValidScopes()
    {
        ValidScope valid =
            (ValidScope)getToolClass().getAnnotation(ValidScope.class);
        if (valid != null)
        {
            return valid.value();
        }
        else
        {
            return new String[] {};
        }
    }

    public String getRestrictTo()
    {
        return this.restrictTo;
    }

    public ToolInfo createInfo()
    {
        ToolInfo info = new ToolInfo(getKey(), getToolClass());
        info.restrictTo(getRestrictTo());
        // it's ok to use this here, because we know it's the
        // first time properties have been added to this ToolInfo
        info.addProperties(getProperties());
        return info;
    }

    @Override
    public void validate()
    {
        super.validate();
        
        // make sure the key is not null
        if (getKey() == null)
        {
            throw new NullKeyException(this);
        }

        // make sure that we can have an accessible tool class
        if (getClassname() == null)
        {
            throw new ConfigurationException(this, "No tool classname has been set for '"+getKey()+'\'');
        }
        else
        {
            getToolClass();
        }
    }

}

@@ -1420,32 +1420,27 @@
 etClass(
-String classname
+Class clazz
 )%0A    %7B%0A
@@ -1463,30 +1463,36 @@
 ssname = cla
-ssn
+zz.getN
 ame
+()
 ;%0A    %7D%0A%0A   
@@ -1549,25 +1549,33 @@
         
-setClass(
+this.classname = 
 classnam
@@ -1575,17 +1575,16 @@
 lassname
-)
 ;%0A    %7D%0A
@@ -1777,32 +1777,82 @@
 key;%0A        %7D%0A%0A
+        if (getClassname() != null)%0A        %7B%0A    
         DefaultK
@@ -1868,16 +1868,20 @@
 tKey = %0A
+    
         
@@ -1948,24 +1948,28 @@
 s);%0A        
+    
 if (defaultK
@@ -1980,34 +1980,42 @@
 = null)%0A        
+  
-%7B%0A
+  %7B%0A    
             retu
@@ -2029,32 +2029,46 @@
 ultKey.value();%0A
+            %7D%0A
         %7D%0A      

change setClass() to take Class and don't try to look up annotations when the class is null