001    /**
002     * Copyright (c) 2000-2013 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.servlet.taglib.CustomAttributes;
019    import com.liferay.portal.kernel.util.GetterUtil;
020    import com.liferay.portal.kernel.util.HtmlUtil;
021    import com.liferay.portal.kernel.util.JavaConstants;
022    import com.liferay.portal.kernel.util.StringPool;
023    import com.liferay.portal.kernel.util.Validator;
024    import com.liferay.taglib.aui.base.BaseATag;
025    
026    import java.util.Map;
027    
028    import javax.portlet.PortletResponse;
029    
030    import javax.servlet.http.HttpServletRequest;
031    import javax.servlet.jsp.JspWriter;
032    
033    /**
034     * @author Julio Camarero
035     * @author Jorge Ferrer
036     * @author Brian Wing Shun Chan
037     * @author Shuyang Zhou
038     */
039    public class ATag extends BaseATag {
040    
041            protected boolean isOpensNewWindow() {
042                    String target = getTarget();
043    
044                    if ((target != null) &&
045                            (target.equals("_blank") || target.equals("_new"))) {
046    
047                            return true;
048                    }
049                    else {
050                            return false;
051                    }
052            }
053    
054            @Override
055            protected int processEndTag() throws Exception {
056                    JspWriter jspWriter = pageContext.getOut();
057    
058                    if (Validator.isNotNull(getHref())) {
059                            if (isOpensNewWindow()) {
060                                    jspWriter.write("<span class=\"opens-new-window-accessible\">");
061                                    jspWriter.write(
062                                            LanguageUtil.get(pageContext, "opens-new-window"));
063                                    jspWriter.write("</span>");
064                            }
065    
066                            jspWriter.write("</a>");
067                    }
068                    else {
069                            jspWriter.write("</span>");
070                    }
071    
072                    return EVAL_PAGE;
073            }
074    
075            @Override
076            protected int processStartTag() throws Exception {
077                    JspWriter jspWriter = pageContext.getOut();
078    
079                    String href = getHref();
080                    String cssClass = getCssClass();
081                    String id = getId();
082                    String namespace = _getNamespace();
083                    String lang = getLang();
084                    String onClick = getOnClick();
085                    String target = getTarget();
086                    String title = getTitle();
087                    Map<String, Object> data = getData();
088                    String label = getLabel();
089    
090                    if (Validator.isNotNull(href)) {
091                            jspWriter.write("<a ");
092    
093                            if (Validator.isNotNull(cssClass)) {
094                                    jspWriter.write("class=\"");
095                                    jspWriter.write(cssClass);
096                                    jspWriter.write("\" ");
097                            }
098    
099                            jspWriter.write("href=\"");
100                            jspWriter.write(HtmlUtil.escape(href));
101                            jspWriter.write("\" ");
102    
103                            if (Validator.isNotNull(id)) {
104                                    jspWriter.write("id=\"");
105                                    jspWriter.write(namespace);
106                                    jspWriter.write(id);
107                                    jspWriter.write("\" ");
108                            }
109    
110                            if (Validator.isNotNull(lang)) {
111                                    jspWriter.write("lang=\"");
112                                    jspWriter.write(lang);
113                                    jspWriter.write("\" ");
114                            }
115    
116                            if (Validator.isNotNull(onClick)) {
117                                    jspWriter.write("onClick=\"");
118                                    jspWriter.write(onClick);
119                                    jspWriter.write("\" ");
120                            }
121    
122                            if (Validator.isNotNull(target)) {
123                                    jspWriter.write("target=\"");
124                                    jspWriter.write(target);
125                                    jspWriter.write("\" ");
126                            }
127    
128                            if (Validator.isNotNull(title) || isOpensNewWindow()) {
129                                    jspWriter.write("title=\"");
130    
131                                    if (Validator.isNotNull(title)) {
132                                            jspWriter.write(LanguageUtil.get(pageContext, title));
133                                    }
134    
135                                    if (isOpensNewWindow()) {
136                                            jspWriter.write(
137                                                    LanguageUtil.get(pageContext, "opens-new-window"));
138                                    }
139    
140                                    jspWriter.write("\" ");
141                            }
142    
143                            CustomAttributes customAttributes = getCustomAttributes();
144    
145                            if (customAttributes != null) {
146                                    jspWriter.write(customAttributes.toString());
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                    else {
162                            jspWriter.write("<span ");
163    
164                            if (Validator.isNotNull(cssClass)) {
165                                    jspWriter.write("class=\"");
166                                    jspWriter.write(cssClass);
167                                    jspWriter.write("\" ");
168                            }
169    
170                            if (Validator.isNotNull(id)) {
171                                    jspWriter.write("id=\"");
172                                    jspWriter.write(namespace);
173                                    jspWriter.write(id);
174                                    jspWriter.write("\" ");
175                            }
176    
177                            if (Validator.isNotNull(lang)) {
178                                    jspWriter.write("lang=\"");
179                                    jspWriter.write(lang);
180                                    jspWriter.write("\" ");
181                            }
182    
183                            if (Validator.isNotNull(title) || isOpensNewWindow()) {
184                                    jspWriter.write("title=\"");
185    
186                                    if (Validator.isNotNull(title)) {
187                                            jspWriter.write(LanguageUtil.get(pageContext, title));
188                                    }
189    
190                                    if (isOpensNewWindow()) {
191                                            jspWriter.write(
192                                                    LanguageUtil.get(pageContext, "opens-new-window"));
193                                    }
194    
195                                    jspWriter.write("\" ");
196                            }
197    
198                            CustomAttributes customAttributes = getCustomAttributes();
199    
200                            if (customAttributes != null) {
201                                    jspWriter.write(customAttributes.toString());
202                            }
203    
204                            if (data != null) {
205                                    jspWriter.write(AUIUtil.buildData(data));
206                            }
207    
208                            writeDynamicAttributes(jspWriter);
209    
210                            jspWriter.write(">");
211    
212                            if (Validator.isNotNull(label)) {
213                                    jspWriter.write(LanguageUtil.get(pageContext, label));
214                            }
215                    }
216    
217                    return EVAL_BODY_INCLUDE;
218            }
219    
220            private String _getNamespace() {
221                    HttpServletRequest request =
222                            (HttpServletRequest)pageContext.getRequest();
223    
224                    PortletResponse portletResponse = (PortletResponse)request.getAttribute(
225                            JavaConstants.JAVAX_PORTLET_RESPONSE);
226    
227                    String namespace = StringPool.BLANK;
228    
229                    boolean useNamespace = GetterUtil.getBoolean(
230                            (String)request.getAttribute("aui:form:useNamespace"), true);
231    
232                    if ((portletResponse != null) && useNamespace) {
233                            namespace = portletResponse.getNamespace();
234                    }
235    
236                    return namespace;
237            }
238    
239    }