001    /**
002     * Copyright (c) 2000-2012 Liferay, Inc. All rights reserved.
003     *
004     * This library is free software; you can redistribute it and/or modify it under
005     * the terms of the GNU Lesser General Public License as published by the Free
006     * Software Foundation; either version 2.1 of the License, or (at your option)
007     * any later version.
008     *
009     * This library is distributed in the hope that it will be useful, but WITHOUT
010     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
011     * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
012     * details.
013     */
014    
015    package com.liferay.taglib.aui;
016    
017    import com.liferay.portal.kernel.language.LanguageUtil;
018    import com.liferay.portal.kernel.util.GetterUtil;
019    import com.liferay.portal.kernel.util.HtmlUtil;
020    import com.liferay.portal.kernel.util.JavaConstants;
021    import com.liferay.portal.kernel.util.StringPool;
022    import com.liferay.portal.kernel.util.Validator;
023    import com.liferay.taglib.aui.base.BaseATag;
024    import com.liferay.taglib.util.InlineUtil;
025    
026    import java.io.IOException;
027    
028    import java.util.Map;
029    
030    import javax.portlet.PortletResponse;
031    
032    import javax.servlet.http.HttpServletRequest;
033    import javax.servlet.jsp.JspWriter;
034    
035    /**
036     * @author Julio Camarero
037     * @author Jorge Ferrer
038     * @author Brian Wing Shun Chan
039     * @author Shuyang Zhou
040     */
041    public class ATag extends BaseATag {
042    
043            protected boolean isOpensNewWindow() {
044                    String target = getTarget();
045    
046                    if ((target != null) &&
047                            (target.equals("_blank") || target.equals("_new"))) {
048    
049                            return true;
050                    }
051                    else {
052                            return false;
053                    }
054            }
055    
056            @Override
057            protected int processEndTag() throws Exception {
058                    JspWriter jspWriter = pageContext.getOut();
059    
060                    if (Validator.isNotNull(getHref())) {
061                            if (isOpensNewWindow()) {
062                                    jspWriter.write("<span class=\"opens-new-window-accessible\">");
063                                    jspWriter.write(
064                                            LanguageUtil.get(pageContext, "opens-new-window"));
065                                    jspWriter.write("</span>");
066                            }
067    
068                            jspWriter.write("</a>");
069                    }
070                    else {
071                            jspWriter.write("</span>");
072                    }
073    
074                    return EVAL_PAGE;
075            }
076    
077            @Override
078            protected int processStartTag() throws Exception {
079                    JspWriter jspWriter = pageContext.getOut();
080    
081                    String href = getHref();
082                    String cssClass = getCssClass();
083                    String id = getId();
084                    String namespace = _getNamespace();
085                    String lang = getLang();
086                    String onClick = getOnClick();
087                    String target = getTarget();
088                    String title = getTitle();
089                    Map<String, Object> data = getData();
090                    String label = getLabel();
091    
092                    if (Validator.isNotNull(href)) {
093                            jspWriter.write("<a ");
094    
095                            jspWriter.write("href=\"");
096                            jspWriter.write(HtmlUtil.escape(href));
097                            jspWriter.write("\" ");
098    
099                            if (Validator.isNotNull(target)) {
100                                    jspWriter.write("target=\"");
101                                    jspWriter.write(target);
102                                    jspWriter.write("\" ");
103                            }
104                    }
105                    else {
106                            jspWriter.write("<span ");
107                    }
108    
109                    if (Validator.isNotNull(cssClass)) {
110                            jspWriter.write("class=\"");
111                            jspWriter.write(cssClass);
112                            jspWriter.write("\" ");
113                    }
114    
115                    if (Validator.isNotNull(id)) {
116                            jspWriter.write("id=\"");
117                            jspWriter.write(namespace);
118                            jspWriter.write(id);
119                            jspWriter.write("\" ");
120                    }
121    
122                    if (Validator.isNotNull(lang)) {
123                            jspWriter.write("lang=\"");
124                            jspWriter.write(lang);
125                            jspWriter.write("\" ");
126                    }
127    
128                    if (Validator.isNotNull(onClick)) {
129                            jspWriter.write("onClick=\"");
130                            jspWriter.write(onClick);
131                            jspWriter.write("\" ");
132                    }
133    
134                    if (Validator.isNotNull(title) || isOpensNewWindow()) {
135                            jspWriter.write("title=\"");
136    
137                            if (Validator.isNotNull(title)) {
138                                    jspWriter.write(LanguageUtil.get(pageContext, title));
139                            }
140    
141                            if (isOpensNewWindow()) {
142                                    jspWriter.write(
143                                            LanguageUtil.get(pageContext, "opens-new-window"));
144                            }
145    
146                            jspWriter.write("\" ");
147                    }
148    
149                    if (data != null) {
150                            jspWriter.write(AUIUtil.buildData(data));
151                    }
152    
153                    _writeDynamicAttributes(jspWriter);
154    
155                    jspWriter.write(">");
156    
157                    if (Validator.isNotNull(label)) {
158                            jspWriter.write(LanguageUtil.get(pageContext, label));
159                    }
160    
161                    return EVAL_BODY_INCLUDE;
162            }
163    
164            private String _getNamespace() {
165                    HttpServletRequest request =
166                            (HttpServletRequest)pageContext.getRequest();
167    
168                    PortletResponse portletResponse = (PortletResponse)request.getAttribute(
169                            JavaConstants.JAVAX_PORTLET_RESPONSE);
170    
171                    String namespace = StringPool.BLANK;
172    
173                    boolean useNamespace = GetterUtil.getBoolean(
174                            (String)request.getAttribute("aui:form:useNamespace"), true);
175    
176                    if ((portletResponse != null) && useNamespace) {
177                            namespace = portletResponse.getNamespace();
178                    }
179    
180                    return namespace;
181            }
182    
183            private void _writeDynamicAttributes(JspWriter jspWriter)
184                    throws IOException {
185    
186                    String dynamicAttributesString = InlineUtil.buildDynamicAttributes(
187                            getDynamicAttributes());
188    
189                    if (Validator.isNotNull(dynamicAttributesString)) {
190                            jspWriter.write(dynamicAttributesString);
191                    }
192            }
193    
194    }