001/*
002 * flattr4j - A Java library for Flattr
003 *
004 * Copyright (C) 2011 Richard "Shred" Körber
005 *   http://flattr4j.shredzone.org
006 *
007 * This program is free software: you can redistribute it and/or modify
008 * it under the terms of the GNU General Public License / GNU Lesser
009 * General Public License as published by the Free Software Foundation,
010 * either version 3 of the License, or (at your option) any later version.
011 *
012 * Licensed under the Apache License, Version 2.0 (the "License");
013 * you may not use this file except in compliance with the License.
014 *
015 * This program is distributed in the hope that it will be useful,
016 * but WITHOUT ANY WARRANTY; without even the implied warranty of
017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
018 */
019package org.shredzone.flattr4j.web.tag;
020
021import javax.servlet.jsp.JspException;
022import javax.servlet.jsp.tagext.BodyTagSupport;
023
024/**
025 * An optional HTML attribute for the parent tag.
026 *
027 * @author Richard "Shred" Körber
028 */
029public class AttributeTag extends BodyTagSupport {
030    private static final long serialVersionUID = -3139247850992098426L;
031
032    private String name;
033    private String value;
034
035    public void setName(String name) {
036        this.name = name;
037    }
038
039    public void setValue(String value) {
040        this.value = value;
041    }
042
043    @Override
044    public int doStartTag() throws JspException {
045        if (value != null) {
046            return SKIP_BODY;
047        } else {
048            return EVAL_BODY_BUFFERED;
049        }
050    }
051
052    @Override
053    public int doEndTag() throws JspException {
054        Attributed parent = TagUtils.findAncestorWithType(this, Attributed.class);
055        if (parent != null) {
056            if (value != null) {
057                parent.setAttribute(name, value);
058            } else {
059                parent.setAttribute(name, getBodyContent().getString());
060            }
061        }
062        return EVAL_PAGE;
063    }
064
065}