001    /**
002     * Copyright (c) 2000-2012 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            protected boolean isOpensNewWindow() {
044                    String target = getTarget();
045    
046                    if ((target != null) &&
047                            (target.equals("_blank") || target.equals("_new"))) {
048    
049                            return true;
050                    }
051                    else {
052                            return false;
053                    }
054            }
055    
056            @Override
057            protected int processEndTag() throws Exception {
058                    JspWriter jspWriter = pageContext.getOut();
059    
060                    if (Validator.isNotNull(getHref())) {
061                            if (isOpensNewWindow()) {
062                                    jspWriter.write("<span class=\"opens-new-window-accessible\">");
063                                    jspWriter.write(
064                                            LanguageUtil.get(pageContext, "opens-new-window"));
065                                    jspWriter.write("</span>");
066                            }
067    
068                            jspWriter.write("</a>");
069                    }
070                    else {
071                            jspWriter.write("</span>");
072                    }
073    
074                    return EVAL_PAGE;
075            }
076    
077            @Override
078            protected int processStartTag() throws Exception {
079                    JspWriter jspWriter = pageContext.getOut();
080    
081                    String href = getHref();
082                    String cssClass = getCssClass();
083                    String id = getId();
084                    String namespace = _getNamespace();
085                    String lang = getLang();
086                    String onClick = getOnClick();
087                    String target = getTarget();
088                    String title = getTitle();
089                    Map<String, Object> data = getData();
090                    String label = getLabel();
091    
092                    if (Validator.isNotNull(href)) {
093                            jspWriter.write("<a ");
094    
095                            if (Validator.isNotNull(cssClass)) {
096                                    jspWriter.write("class=\"");
097                                    jspWriter.write(cssClass);
098                                    jspWriter.write("\" ");
099                            }
100    
101                            jspWriter.write("href=\"");
102                            jspWriter.write(HtmlUtil.escape(href));
103                            jspWriter.write("\" ");
104    
105                            if (Validator.isNotNull(id)) {
106                                    jspWriter.write("id=\"");
107                                    jspWriter.write(namespace);
108                                    jspWriter.write(id);
109                                    jspWriter.write("\" ");
110                            }
111    
112                            if (Validator.isNotNull(lang)) {
113                                    jspWriter.write("lang=\"");
114                                    jspWriter.write(lang);
115                                    jspWriter.write("\" ");
116                            }
117    
118                            if (Validator.isNotNull(onClick)) {
119                                    jspWriter.write("onClick=\"");
120                                    jspWriter.write(onClick);
121                                    jspWriter.write("\" ");
122                            }
123    
124                            if (Validator.isNotNull(target)) {
125                                    jspWriter.write("target=\"");
126                                    jspWriter.write(target);
127                                    jspWriter.write("\" ");
128                            }
129    
130                            if (Validator.isNotNull(title) || isOpensNewWindow()) {
131                                    jspWriter.write("title=\"");
132    
133                                    if (Validator.isNotNull(title)) {
134                                            jspWriter.write(LanguageUtil.get(pageContext, title));
135                                    }
136    
137                                    if (isOpensNewWindow()) {
138                                            jspWriter.write(
139                                                    LanguageUtil.get(pageContext, "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                                    jspWriter.write(LanguageUtil.get(pageContext, label));
155                            }
156                    }
157                    else {
158                            jspWriter.write("<span ");
159    
160                            if (Validator.isNotNull(cssClass)) {
161                                    jspWriter.write("class=\"");
162                                    jspWriter.write(cssClass);
163                                    jspWriter.write("\" ");
164                            }
165    
166                            if (Validator.isNotNull(id)) {
167                                    jspWriter.write("id=\"");
168                                    jspWriter.write(namespace);
169                                    jspWriter.write(id);
170                                    jspWriter.write("\" ");
171                            }
172    
173                            if (Validator.isNotNull(lang)) {
174                                    jspWriter.write("lang=\"");
175                                    jspWriter.write(lang);
176                                    jspWriter.write("\" ");
177                            }
178    
179                            if (Validator.isNotNull(title) || isOpensNewWindow()) {
180                                    jspWriter.write("title=\"");
181    
182                                    if (Validator.isNotNull(title)) {
183                                            jspWriter.write(LanguageUtil.get(pageContext, title));
184                                    }
185    
186                                    if (isOpensNewWindow()) {
187                                            jspWriter.write(
188                                                    LanguageUtil.get(pageContext, "opens-new-window"));
189                                    }
190    
191                                    jspWriter.write("\" ");
192                            }
193    
194                            if (data != null) {
195                                    jspWriter.write(AUIUtil.buildData(data));
196                            }
197    
198                            _writeDynamicAttributes(jspWriter);
199    
200                            jspWriter.write(">");
201    
202                            if (Validator.isNotNull(label)) {
203                                    jspWriter.write(LanguageUtil.get(pageContext, label));
204                            }
205                    }
206    
207                    return EVAL_BODY_INCLUDE;
208            }
209    
210            private String _getNamespace() {
211                    HttpServletRequest request =
212                            (HttpServletRequest)pageContext.getRequest();
213    
214                    PortletResponse portletResponse = (PortletResponse)request.getAttribute(
215                            JavaConstants.JAVAX_PORTLET_RESPONSE);
216    
217                    String namespace = StringPool.BLANK;
218    
219                    boolean useNamespace = GetterUtil.getBoolean(
220                            (String)request.getAttribute("aui:form:useNamespace"), true);
221    
222                    if ((portletResponse != null) && useNamespace) {
223                            namespace = portletResponse.getNamespace();
224                    }
225    
226                    return namespace;
227            }
228    
229            private void _writeDynamicAttributes(JspWriter jspWriter)
230                    throws IOException {
231    
232                    String dynamicAttributesString = InlineUtil.buildDynamicAttributes(
233                            getDynamicAttributes());
234    
235                    if (Validator.isNotNull(dynamicAttributesString)) {
236                            jspWriter.write(dynamicAttributesString);
237                    }
238            }
239    
240    }