001    /**
002     * Copyright (c) 2000-present 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            @Override
044            protected int processEndTag() throws Exception {
045                    JspWriter jspWriter = pageContext.getOut();
046    
047                    if (Validator.isNotNull(getHref())) {
048                            if (AUIUtil.isOpensNewWindow(getTarget())) {
049                                    jspWriter.write(StringPool.SPACE);
050                                    jspWriter.write("<span class=\"icon-external-link\"></span>");
051                                    jspWriter.write("<span class=\"sr-only\">");
052                                    jspWriter.write(LanguageUtil.get(request, "opens-new-window"));
053                                    jspWriter.write("</span>");
054                            }
055    
056                            jspWriter.write("</a>");
057                    }
058                    else {
059                            jspWriter.write("</span>");
060                    }
061    
062                    return EVAL_PAGE;
063            }
064    
065            @Override
066            protected int processStartTag() throws Exception {
067                    JspWriter jspWriter = pageContext.getOut();
068    
069                    String ariaRole = getAriaRole();
070                    String cssClass = getCssClass();
071                    Map<String, Object> data = getData();
072                    String href = getHref();
073                    String id = getId();
074                    String iconCssClass = getIconCssClass();
075                    String label = getLabel();
076                    String lang = getLang();
077                    Boolean localizeLabel = getLocalizeLabel();
078                    String namespace = _getNamespace();
079                    String onClick = getOnClick();
080                    String target = getTarget();
081                    String title = getTitle();
082    
083                    if (Validator.isNotNull(href)) {
084                            jspWriter.write("<a ");
085    
086                            jspWriter.write("href=\"");
087                            jspWriter.write(HtmlUtil.escapeAttribute(href));
088                            jspWriter.write("\" ");
089    
090                            if (Validator.isNotNull(target)) {
091                                    jspWriter.write("target=\"");
092                                    jspWriter.write(target);
093                                    jspWriter.write("\" ");
094                            }
095                    }
096                    else {
097                            jspWriter.write("<span ");
098                    }
099    
100                    if (Validator.isNotNull(cssClass)) {
101                            jspWriter.write("class=\"");
102                            jspWriter.write(cssClass);
103                            jspWriter.write("\" ");
104                    }
105    
106                    if (Validator.isNotNull(id)) {
107                            jspWriter.write("id=\"");
108                            jspWriter.write(namespace);
109                            jspWriter.write(id);
110                            jspWriter.write("\" ");
111                    }
112    
113                    if (Validator.isNotNull(lang)) {
114                            jspWriter.write("lang=\"");
115                            jspWriter.write(lang);
116                            jspWriter.write("\" ");
117                    }
118    
119                    if (Validator.isNotNull(onClick)) {
120                            jspWriter.write("onClick=\"");
121                            jspWriter.write(onClick);
122                            jspWriter.write("\" ");
123                    }
124    
125                    if (Validator.isNotNull(ariaRole)) {
126                            jspWriter.write("role=\"");
127                            jspWriter.write(ariaRole);
128                            jspWriter.write("\" ");
129                    }
130    
131                    if (Validator.isNotNull(title) ||
132                            AUIUtil.isOpensNewWindow(getTarget())) {
133    
134                            jspWriter.write("title=\"");
135    
136                            if (Validator.isNotNull(title)) {
137                                    jspWriter.write(LanguageUtil.get(request, title));
138                            }
139    
140                            if (AUIUtil.isOpensNewWindow(getTarget())) {
141                                    jspWriter.write(LanguageUtil.get(request, "opens-new-window"));
142                            }
143    
144                            jspWriter.write("\" ");
145                    }
146    
147                    if (data != null) {
148                            jspWriter.write(AUIUtil.buildData(data));
149                    }
150    
151                    _writeDynamicAttributes(jspWriter);
152    
153                    jspWriter.write(">");
154    
155                    if (Validator.isNotNull(label)) {
156                            if (localizeLabel) {
157                                    jspWriter.write(LanguageUtil.get(request, label));
158                            }
159                            else {
160                                    jspWriter.write(label);
161                            }
162                    }
163    
164                    if (Validator.isNotNull(iconCssClass)) {
165                            jspWriter.write("<span class=\"icon-monospaced ");
166                            jspWriter.write(iconCssClass);
167                            jspWriter.write("\"></span>");
168                    }
169    
170                    return EVAL_BODY_INCLUDE;
171            }
172    
173            private String _getNamespace() {
174                    HttpServletRequest request =
175                            (HttpServletRequest)pageContext.getRequest();
176    
177                    PortletResponse portletResponse = (PortletResponse)request.getAttribute(
178                            JavaConstants.JAVAX_PORTLET_RESPONSE);
179    
180                    String namespace = StringPool.BLANK;
181    
182                    boolean useNamespace = GetterUtil.getBoolean(
183                            (String)request.getAttribute("aui:form:useNamespace"), true);
184    
185                    if ((portletResponse != null) && useNamespace) {
186                            namespace = portletResponse.getNamespace();
187                    }
188    
189                    return namespace;
190            }
191    
192            private void _writeDynamicAttributes(JspWriter jspWriter)
193                    throws IOException {
194    
195                    String dynamicAttributesString = InlineUtil.buildDynamicAttributes(
196                            getDynamicAttributes());
197    
198                    if (Validator.isNotNull(dynamicAttributesString)) {
199                            jspWriter.write(dynamicAttributesString);
200                    }
201            }
202    
203    }