package org.apache.maven.xdoc;

/* ====================================================================
 *   Copyright 2001-2004 The Apache Software Foundation.
 *
 *   Licensed 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.commons.jelly.JellyTagException;
import org.apache.commons.jelly.MissingAttributeException;
import org.apache.commons.jelly.TagSupport;
import org.apache.commons.jelly.XMLOutput;

import com.werken.forehead.ForeheadClassLoader;

/**
 * Return to the old classloader
 * 
 * @see GrabClassLoaderTag
 * 
 * @author <a href="mailto:vincent.siveton@gmail.com">Vincent Siveton </a>
 * @version 1.0
 */
public class UnGrabClassLoaderTag extends TagSupport {
    /* 
     * @see org.apache.commons.jelly.Tag#doTag(org.apache.commons.jelly.XMLOutput)
     */
    public void doTag(XMLOutput arg0) throws MissingAttributeException, JellyTagException {
        if (getContext() == null) {
            throw new JellyTagException("The current MavenContext is null!");
        }
        
        ForeheadClassLoader currentClassLoader = null;
        try {
            currentClassLoader = (ForeheadClassLoader) getContext().getClassLoader();
        } catch (ClassCastException e) {
            throw new JellyTagException("The current classloader in the MavenContext is not an instance of ForeheadClassLoader");
        }
        
        if (currentClassLoader == null) {
            throw new JellyTagException("No classloader found in the current MavenContext");
        }
        
        ClassLoader oldClassLoader = currentClassLoader.getParent();
        getContext().setClassLoader(oldClassLoader);
    }
}
