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