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 iconCssClass = getIconCssClass();
073                    String label = getLabel();
074                    String lang = getLang();
075                    Boolean localizeLabel = getLocalizeLabel();
076                    String namespace = _getNamespace();
077                    String onClick = getOnClick();
078                    String target = getTarget();
079                    String title = getTitle();
080    
081                    if (Validator.isNotNull(href)) {
082                            jspWriter.write("<a ");
083    
084                            jspWriter.write("href=\"");
085                            jspWriter.write(HtmlUtil.escapeAttribute(href));
086                            jspWriter.write("\" ");
087    
088                            if (Validator.isNotNull(target)) {
089                                    jspWriter.write("target=\"");
090                                    jspWriter.write(target);
091                                    jspWriter.write("\" ");
092                            }
093                    }
094                    else {
095                            jspWriter.write("<span ");
096                    }
097    
098                    if (Validator.isNotNull(cssClass)) {
099                            jspWriter.write("class=\"");
100                            jspWriter.write(cssClass);
101                            jspWriter.write("\" ");
102                    }
103    
104                    if (Validator.isNotNull(id)) {
105                            jspWriter.write("id=\"");
106                            jspWriter.write(namespace);
107                            jspWriter.write(id);
108                            jspWriter.write("\" ");
109                    }
110    
111                    if (Validator.isNotNull(lang)) {
112                            jspWriter.write("lang=\"");
113                            jspWriter.write(lang);
114                            jspWriter.write("\" ");
115                    }
116    
117                    if (Validator.isNotNull(onClick)) {
118                            jspWriter.write("onClick=\"");
119                            jspWriter.write(onClick);
120                            jspWriter.write("\" ");
121                    }
122    
123                    if (Validator.isNotNull(ariaRole)) {
124                            jspWriter.write("role=\"");
125                            jspWriter.write(ariaRole);
126                            jspWriter.write("\" ");
127                    }
128    
129                    if (Validator.isNotNull(title) ||
130                            AUIUtil.isOpensNewWindow(getTarget())) {
131    
132                            jspWriter.write("title=\"");
133    
134                            if (Validator.isNotNull(title)) {
135                                    jspWriter.write(LanguageUtil.get(request, title));
136                            }
137    
138                            if (AUIUtil.isOpensNewWindow(getTarget())) {
139                                    jspWriter.write(LanguageUtil.get(request, "opens-new-window"));
140                            }
141    
142                            jspWriter.write("\" ");
143                    }
144    
145                    if (data != null) {
146                            jspWriter.write(AUIUtil.buildData(data));
147                    }
148    
149                    _writeDynamicAttributes(jspWriter);
150    
151                    jspWriter.write(">");
152    
153                    if (Validator.isNotNull(label)) {
154                            if (localizeLabel) {
155                                    jspWriter.write(LanguageUtil.get(request, label));
156                            }
157                            else {
158                                    jspWriter.write(label);
159                            }
160                    }
161    
162                    if (Validator.isNotNull(iconCssClass)) {
163                            jspWriter.write("<span class=\"icon-monospaced ");
164                            jspWriter.write(iconCssClass);
165                            jspWriter.write("\"></span>");
166                    }
167    
168                    return EVAL_BODY_INCLUDE;
169            }
170    
171            private String _getNamespace() {
172                    HttpServletRequest request =
173                            (HttpServletRequest)pageContext.getRequest();
174    
175                    PortletResponse portletResponse = (PortletResponse)request.getAttribute(
176                            JavaConstants.JAVAX_PORTLET_RESPONSE);
177    
178                    String namespace = StringPool.BLANK;
179    
180                    boolean useNamespace = GetterUtil.getBoolean(
181                            (String)request.getAttribute("aui:form:useNamespace"), true);
182    
183                    if ((portletResponse != null) && useNamespace) {
184                            namespace = portletResponse.getNamespace();
185                    }
186    
187                    return namespace;
188            }
189    
190            private void _writeDynamicAttributes(JspWriter jspWriter)
191                    throws IOException {
192    
193                    String dynamicAttributesString = InlineUtil.buildDynamicAttributes(
194                            getDynamicAttributes());
195    
196                    if (Validator.isNotNull(dynamicAttributesString)) {
197                            jspWriter.write(dynamicAttributesString);
198                    }
199            }
200    
201    }