001/*
002 * Shredzone Commons
003 *
004 * Copyright (C) 2012 Richard "Shred" Körber
005 *   http://commons.shredzone.org
006 *
007 * This program is free software: you can redistribute it and/or modify
008 * it under the terms of the GNU Library General Public License as
009 * published by the Free Software Foundation, either version 3 of the
010 * License, or (at your option) any later version.
011 *
012 * This program is distributed in the hope that it will be useful,
013 * but WITHOUT ANY WARRANTY; without even the implied warranty of
014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
015 * GNU General Public License for more details.
016 *
017 * You should have received a copy of the GNU Library General Public License
018 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
019 */
020package org.shredzone.commons.taglib.processor;
021
022import java.util.Collection;
023import java.util.HashMap;
024import java.util.Map;
025
026import javax.annotation.Nonnull;
027import javax.servlet.jsp.tagext.TryCatchFinally;
028
029/**
030 * This bean stores all information about a tag. Parts of this bean are immutable.
031 *
032 * @author Richard "Shred" Körber
033 */
034public class TagBean implements Comparable<TagBean> {
035
036    private Map<String, AttributeBean> attributes = new HashMap<>();
037
038    private final String name;
039    private final String className;
040    private final String bodycontent;
041    private final String typeClass;
042    private String info;
043    private String proxyClassName;
044    private String beanFactoryReference;
045    private String beanName;
046    private boolean tryCatchFinally;
047
048    /**
049     * Creates and initializes a new {@link TagBean}.
050     *
051     * @param name
052     *            Tag name
053     * @param className
054     *            Name of the implementing class
055     * @param bodycontent
056     *            Body Content flag
057     * @param typeClass
058     *            Tag type class
059     */
060    public TagBean(@Nonnull String name, @Nonnull String className, @Nonnull String bodycontent, @Nonnull String typeClass) {
061        this.name = name;
062        this.className = className;
063        this.bodycontent = bodycontent;
064        this.typeClass = typeClass;
065    }
066
067    public @Nonnull String getName()            { return name; }
068
069    public @Nonnull String getClassName()       { return className; }
070
071    public @Nonnull String getBodycontent()     { return bodycontent; }
072
073    public @Nonnull String getType()            { return typeClass; }
074
075    /**
076     * Information about the tag.
077     */
078    public String getInfo()                     { return info; }
079    public void setInfo(String info)            { this.info = info; }
080
081    /**
082     * Name of the proxy class.
083     */
084    public String getProxyClassName()           { return proxyClassName; }
085    public void setProxyClassName(String proxyClassName) { this.proxyClassName = proxyClassName; }
086
087    /**
088     * A reference to the bean factory.
089     */
090    public String getBeanFactoryReference()     { return beanFactoryReference; }
091    public void setBeanFactoryReference(String beanFactoryReference) { this.beanFactoryReference = beanFactoryReference; }
092
093    /**
094     * Name of the bean.
095     */
096    public String getBeanName()                 { return beanName; }
097    public void setBeanName(String beanName)    { this.beanName = beanName; }
098
099    /**
100     * Does the tag class implement the {@link TryCatchFinally} interface?
101     */
102    public boolean isTryCatchFinally()          { return tryCatchFinally; }
103    public void setTryCatchFinally(boolean tryCatchFinally) { this.tryCatchFinally = tryCatchFinally; }
104
105    /**
106     * Adds a tag attribute to the tag bean.
107     *
108     * @param attribute
109     *            {@link AttributeBean} of the tag attribute
110     */
111    public void addAttribute(@Nonnull AttributeBean attribute) {
112        if (attributes.containsKey(attribute.getName())) {
113            throw new ProcessorException("Tag " + name + ": parameter " + attribute.getName() + " already defined");
114        }
115        attributes.put(attribute.getName(), attribute);
116    }
117
118    /**
119     * Gets all tag attributes.
120     *
121     * @return Collection of {@link AttributeBean} of all tag attributes
122     */
123    public @Nonnull Collection<AttributeBean> getAttributes() {
124        return attributes.values();
125    }
126
127    /**
128     * {@inheritDoc}
129     * <p>
130     * Two {@link TagBean} are considered equal when their names are equal.
131     */
132    @Override
133    public boolean equals(Object obj) {
134        if (obj == null || !(obj instanceof TagBean)) {
135            return false;
136        }
137        return ((TagBean) obj).getName().equals(name);
138    }
139
140    @Override
141    public int hashCode() {
142        return name.hashCode();
143    }
144
145    @Override
146    public int compareTo(TagBean o) {
147        return name.compareTo(o.name);
148    }
149
150}