001    /**
002     * Copyright (c) 2000-2013 Liferay, Inc. All rights reserved.
003     *
004     * The contents of this file are subject to the terms of the Liferay Enterprise
005     * Subscription License ("License"). You may not use this file except in
006     * compliance with the License. You can obtain a copy of the License by
007     * contacting Liferay, Inc. See the License for the specific language governing
008     * permissions and limitations under the License, including but not limited to
009     * distribution rights of the Software.
010     *
011     *
012     *
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 ariaRole = getAriaRole();
082                    String cssClass = getCssClass();
083                    Map<String, Object> data = getData();
084                    String href = getHref();
085                    String id = getId();
086                    String label = getLabel();
087                    String lang = getLang();
088                    Boolean localizeLabel = getLocalizeLabel();
089                    String namespace = _getNamespace();
090                    String onClick = getOnClick();
091                    String target = getTarget();
092                    String title = getTitle();
093    
094                    if (Validator.isNotNull(href)) {
095                            jspWriter.write("<a ");
096    
097                            jspWriter.write("href=\"");
098                            jspWriter.write(HtmlUtil.escapeAttribute(href));
099                            jspWriter.write("\" ");
100    
101                            if (Validator.isNotNull(target)) {
102                                    jspWriter.write("target=\"");
103                                    jspWriter.write(target);
104                                    jspWriter.write("\" ");
105                            }
106                    }
107                    else {
108                            jspWriter.write("<span ");
109                    }
110    
111                    if (Validator.isNotNull(cssClass)) {
112                            jspWriter.write("class=\"");
113                            jspWriter.write(cssClass);
114                            jspWriter.write("\" ");
115                    }
116    
117                    if (Validator.isNotNull(id)) {
118                            jspWriter.write("id=\"");
119                            jspWriter.write(namespace);
120                            jspWriter.write(id);
121                            jspWriter.write("\" ");
122                    }
123    
124                    if (Validator.isNotNull(lang)) {
125                            jspWriter.write("lang=\"");
126                            jspWriter.write(lang);
127                            jspWriter.write("\" ");
128                    }
129    
130                    if (Validator.isNotNull(onClick)) {
131                            jspWriter.write("onClick=\"");
132                            jspWriter.write(onClick);
133                            jspWriter.write("\" ");
134                    }
135    
136                    if (Validator.isNotNull(ariaRole)) {
137                            jspWriter.write("role=\"");
138                            jspWriter.write(ariaRole);
139                            jspWriter.write("\" ");
140                    }
141    
142                    if (Validator.isNotNull(title) || isOpensNewWindow()) {
143                            jspWriter.write("title=\"");
144    
145                            if (Validator.isNotNull(title)) {
146                                    jspWriter.write(LanguageUtil.get(pageContext, title));
147                            }
148    
149                            if (isOpensNewWindow()) {
150                                    jspWriter.write(
151                                            LanguageUtil.get(pageContext, "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                                    jspWriter.write(LanguageUtil.get(pageContext, label));
168                            }
169                            else {
170                                    jspWriter.write(label);
171                            }
172                    }
173    
174                    return EVAL_BODY_INCLUDE;
175            }
176    
177            private String _getNamespace() {
178                    HttpServletRequest request =
179                            (HttpServletRequest)pageContext.getRequest();
180    
181                    PortletResponse portletResponse = (PortletResponse)request.getAttribute(
182                            JavaConstants.JAVAX_PORTLET_RESPONSE);
183    
184                    String namespace = StringPool.BLANK;
185    
186                    boolean useNamespace = GetterUtil.getBoolean(
187                            (String)request.getAttribute("aui:form:useNamespace"), true);
188    
189                    if ((portletResponse != null) && useNamespace) {
190                            namespace = portletResponse.getNamespace();
191                    }
192    
193                    return namespace;
194            }
195    
196            private void _writeDynamicAttributes(JspWriter jspWriter)
197                    throws IOException {
198    
199                    String dynamicAttributesString = InlineUtil.buildDynamicAttributes(
200                            getDynamicAttributes());
201    
202                    if (Validator.isNotNull(dynamicAttributesString)) {
203                            jspWriter.write(dynamicAttributesString);
204                    }
205            }
206    
207    }