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)) {
143                            jspWriter.write("title=\"");
144    
145                            if (Validator.isNotNull(title)) {
146                                    jspWriter.write(LanguageUtil.get(pageContext, title));
147                            }
148    
149                            jspWriter.write("\" ");
150                    }
151    
152                    if (data != null) {
153                            jspWriter.write(AUIUtil.buildData(data));
154                    }
155    
156                    _writeDynamicAttributes(jspWriter);
157    
158                    jspWriter.write(">");
159    
160                    if (Validator.isNotNull(label)) {
161                            if (localizeLabel) {
162                                    jspWriter.write(LanguageUtil.get(pageContext, label));
163                            }
164                            else {
165                                    jspWriter.write(label);
166                            }
167                    }
168    
169                    return EVAL_BODY_INCLUDE;
170            }
171    
172            private String _getNamespace() {
173                    HttpServletRequest request =
174                            (HttpServletRequest)pageContext.getRequest();
175    
176                    PortletResponse portletResponse = (PortletResponse)request.getAttribute(
177                            JavaConstants.JAVAX_PORTLET_RESPONSE);
178    
179                    String namespace = StringPool.BLANK;
180    
181                    boolean useNamespace = GetterUtil.getBoolean(
182                            (String)request.getAttribute("aui:form:useNamespace"), true);
183    
184                    if ((portletResponse != null) && useNamespace) {
185                            namespace = portletResponse.getNamespace();
186                    }
187    
188                    return namespace;
189            }
190    
191            private void _writeDynamicAttributes(JspWriter jspWriter)
192                    throws IOException {
193    
194                    String dynamicAttributesString = InlineUtil.buildDynamicAttributes(
195                            getDynamicAttributes());
196    
197                    if (Validator.isNotNull(dynamicAttributesString)) {
198                            jspWriter.write(dynamicAttributesString);
199                    }
200            }
201    
202    }