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("<span class=\"opens-new-window-accessible\">");
050                                    jspWriter.write(LanguageUtil.get(request, "opens-new-window"));
051                                    jspWriter.write("</span>");
052                            }
053    
054                            jspWriter.write("</a>");
055                    }
056                    else {
057                            jspWriter.write("</span>");
058                    }
059    
060                    return EVAL_PAGE;
061            }
062    
063            @Override
064            protected int processStartTag() throws Exception {
065                    JspWriter jspWriter = pageContext.getOut();
066    
067                    String ariaRole = getAriaRole();
068                    String cssClass = getCssClass();
069                    Map<String, Object> data = getData();
070                    String href = getHref();
071                    String id = getId();
072                    String label = getLabel();
073                    String lang = getLang();
074                    Boolean localizeLabel = getLocalizeLabel();
075                    String namespace = _getNamespace();
076                    String onClick = getOnClick();
077                    String target = getTarget();
078                    String title = getTitle();
079    
080                    if (Validator.isNotNull(href)) {
081                            jspWriter.write("<a ");
082    
083                            jspWriter.write("href=\"");
084                            jspWriter.write(HtmlUtil.escapeAttribute(href));
085                            jspWriter.write("\" ");
086    
087                            if (Validator.isNotNull(target)) {
088                                    jspWriter.write("target=\"");
089                                    jspWriter.write(target);
090                                    jspWriter.write("\" ");
091                            }
092                    }
093                    else {
094                            jspWriter.write("<span ");
095                    }
096    
097                    if (Validator.isNotNull(cssClass)) {
098                            jspWriter.write("class=\"");
099                            jspWriter.write(cssClass);
100                            jspWriter.write("\" ");
101                    }
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(ariaRole)) {
123                            jspWriter.write("role=\"");
124                            jspWriter.write(ariaRole);
125                            jspWriter.write("\" ");
126                    }
127    
128                    if (Validator.isNotNull(title) ||
129                            AUIUtil.isOpensNewWindow(getTarget())) {
130    
131                            jspWriter.write("title=\"");
132    
133                            if (Validator.isNotNull(title)) {
134                                    jspWriter.write(LanguageUtil.get(request, title));
135                            }
136    
137                            if (AUIUtil.isOpensNewWindow(getTarget())) {
138                                    jspWriter.write(LanguageUtil.get(request, "opens-new-window"));
139                            }
140    
141                            jspWriter.write("\" ");
142                    }
143    
144                    if (data != null) {
145                            jspWriter.write(AUIUtil.buildData(data));
146                    }
147    
148                    _writeDynamicAttributes(jspWriter);
149    
150                    jspWriter.write(">");
151    
152                    if (Validator.isNotNull(label)) {
153                            if (localizeLabel) {
154                                    jspWriter.write(LanguageUtil.get(request, label));
155                            }
156                            else {
157                                    jspWriter.write(label);
158                            }
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    }