001/*
002 * Copyright (c) 2004-2010, Kohsuke Kawaguchi
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification, are permitted provided
006 * that the following conditions are met:
007 *
008 *     * Redistributions of source code must retain the above copyright notice, this list of
009 *       conditions and the following disclaimer.
010 *     * Redistributions in binary form must reproduce the above copyright notice, this list of
011 *       conditions and the following disclaimer in the documentation and/or other materials
012 *       provided with the distribution.
013 *
014 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
015 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
016 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
017 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
018 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
019 * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
020 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
021 * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
022 */
023
024package org.kohsuke.stapler.jelly;
025
026import org.apache.commons.jelly.JellyContext;
027import org.apache.commons.jelly.JellyException;
028import org.apache.commons.jelly.JellyTagException;
029import org.apache.commons.jelly.Script;
030import org.apache.commons.jelly.Tag;
031import org.apache.commons.jelly.TagLibrary;
032import org.apache.commons.jelly.expression.Expression;
033import org.apache.commons.jelly.expression.ExpressionSupport;
034import org.apache.commons.jelly.impl.TagScript;
035import org.kohsuke.stapler.MetaClass;
036import org.kohsuke.stapler.WebApp;
037import org.kohsuke.stapler.lang.Klass;
038import org.xml.sax.Attributes;
039
040/**
041 * Loads Jelly views associated with "it" as if it were taglibs.
042 *
043 * @author Kohsuke Kawaguchi
044 */
045public class ThisTagLibrary extends TagLibrary {
046    private final Expression expr;
047
048    /**
049     * @param expr
050     *      Expression that evaluates to {@link Class} to resolve scripts from.
051     */
052    public ThisTagLibrary(Expression expr) {
053        this.expr = expr;
054    }
055
056    /**
057     * IIUC, this method will never be invoked.
058     */
059    @Override
060    public Tag createTag(final String name, Attributes attributes) throws JellyException {
061        return null;
062    }
063
064    @Override
065    public TagScript createTagScript(final String tagName, Attributes atts) throws JellyException {
066        return new CallTagLibScript() {
067            @Override
068            protected Script resolveDefinition(JellyContext context) throws JellyTagException {
069                Object it = expr.evaluate(context);
070                if (it==null)
071                    throw new JellyTagException("'"+ expr.getExpressionText() +"' evaluated to null");
072                try {
073                    WebApp webApp = WebApp.getCurrent();
074                    MetaClass c = webApp.getMetaClass(it instanceof Class ? Klass.java((Class)it):  webApp.getKlass(it));
075                    // prefer 'foo.jellytag' to avoid tags from showing up as views,
076                    // but for backward compatibility, support the plain .jelly extension as well.
077                    Script tag = c.loadTearOff(JellyClassTearOff.class).findScript(tagName+".jellytag");
078                    if (tag==null)
079                        tag = c.loadTearOff(JellyClassTearOff.class).findScript(tagName+".jelly");
080                    if (tag ==null)
081                        throw new JellyTagException("No such tag file "+tagName+".jellytag in "+c);
082                    return tag;
083                } catch (JellyException e) {
084                    throw new JellyTagException("Failed to load "+tagName+".jellytag from "+it,e);
085                }
086            }
087        };
088    }
089
090    public static final ThisTagLibrary INSTANCE = new ThisTagLibrary(new ExpressionSupport() {
091        public String getExpressionText() {
092            return "it";
093        }
094
095        public Object evaluate(JellyContext context) {
096            return context.getVariable("it");
097        }
098    });
099}