Kawai Wiki

Rev

Rev 206 | Rev 228 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

package org.kawai.tag.kcode;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;

import org.kawai.AppManager;
import org.kawai.action.PageAction;
import org.kawai.model.Page;
import org.mentawai.core.Props;
import org.mentawai.util.HttpUtils;

/**
 * KCode class PageLink.
 *
 * @author Sergio Oliveira
 *
 */

public class PageLink extends AbstractKCode {
       
        private static final String PRETTY_URL_OPTION = "use_pretty_urls";

    /**
     * {@inheritDoc}
     */

    @Override
    public boolean hasBody() {
        return false;
    }

    /**
     * {@inheritDoc}
     */

    @Override
    public String getTag() {
        return "pageLink";
    }

    /**
     * {@inheritDoc}
     */

    @Override
    public String process(PageContext pageContext, Map<String, String> attrs, String html) throws JspException {

        HttpServletRequest req = getRequest(pageContext);

        String name = attrs.get("name");
        if (name == null) throw new JspException("Name attribute is mandatory for tag pageLink!");

        String text = attrs.get("text");
        if (text == null) throw new JspException("Text attribute is mandatory for tag pageLink!");

        String pretty = attrs.get("pretty");
        boolean isPretty;
       
        if (pretty != null) {
                isPretty = Boolean.parseBoolean(pretty);
        } else {
                Props props = AppManager.getInstance().getProps();
            isPretty = props.has(PRETTY_URL_OPTION) && props.getBoolean(PRETTY_URL_OPTION);
        }

        String linkClass = attrs.get("linkClass");
        String clickedClass = attrs.get("clickedClass");

        String sizeClass = attrs.get("class");
        if (sizeClass != null) {
            linkClass = sizeClass;
            clickedClass = sizeClass + "Clicked";
        }

        // check if we are displaying that page...
        Page page = (Page) pageContext.findAttribute(PageAction.BODY_COLUMN_KEY);
        if (page != null && page.getName().equals(name)) {
            return "<span" + (clickedClass != null ? " class=\"" + clickedClass + "\"" : "") + ">" + text + "</span>";
        } else {
                if (isPretty) {
                        String prettyURL = HttpUtils.getPrettyURL("Page", null, name);
                        return "<a href=\"" + req.getContextPath() + prettyURL + "\"" + (linkClass != null ? " class=\"" + linkClass + "\"" : "") + ">" + text + "</a>";
                } else {
                        return "<a href=\"" + req.getContextPath() + "/Page." + AppManager.EXTENSION + "?name=" + name + "\"" + (linkClass != null ? " class=\"" + linkClass + "\"" : "") + ">" + text + "</a>";
                }
        }
    }

}